summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2024-03-28 01:49:29 +0000
committerjsing <>2024-03-28 01:49:29 +0000
commitefdc2fdd7e31b41f518a70445b050722a765ff02 (patch)
treeca5f083b55057a8157b330fc459a97936af57f81 /src
parent852ce4f4fa6a26ebd4801907061f85fb0c1f4e99 (diff)
downloadopenbsd-efdc2fdd7e31b41f518a70445b050722a765ff02.tar.gz
openbsd-efdc2fdd7e31b41f518a70445b050722a765ff02.tar.bz2
openbsd-efdc2fdd7e31b41f518a70445b050722a765ff02.zip
Use C functions for RC4 public API.
Rather than having public API switch between C and assembly, always use C functions as entry points, which then call an assembly implementation (if available). This makes it significantly easier to deal with symbol aliasing/namespaces and it also means we benefit from vulnerability prevention provided by the C compiler. Rename the assembly generated functions from RC4() to rc4_internal() and RC4_set_key() to rc4_set_key_internal(). Always include rc4.c and change it to use defines that are similar to those used in BN. ok beck@ joshua@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/arch/amd64/Makefile.inc5
-rw-r--r--src/lib/libcrypto/arch/i386/Makefile.inc5
-rw-r--r--src/lib/libcrypto/rc4/asm/rc4-586.pl13
-rwxr-xr-xsrc/lib/libcrypto/rc4/asm/rc4-x86_64.pl16
-rw-r--r--src/lib/libcrypto/rc4/rc4.c34
5 files changed, 52 insertions, 21 deletions
diff --git a/src/lib/libcrypto/arch/amd64/Makefile.inc b/src/lib/libcrypto/arch/amd64/Makefile.inc
index b04923b596..617b9feb9a 100644
--- a/src/lib/libcrypto/arch/amd64/Makefile.inc
+++ b/src/lib/libcrypto/arch/amd64/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.16 2024/03/28 01:41:16 jsing Exp $ 1# $OpenBSD: Makefile.inc,v 1.17 2024/03/28 01:49:29 jsing Exp $
2 2
3# amd64-specific libcrypto build rules 3# amd64-specific libcrypto build rules
4 4
@@ -46,7 +46,10 @@ SSLASM+= md5 md5-x86_64
46CFLAGS+= -DGHASH_ASM 46CFLAGS+= -DGHASH_ASM
47SSLASM+= modes ghash-x86_64 47SSLASM+= modes ghash-x86_64
48# rc4 48# rc4
49CFLAGS+= -DHAVE_RC4_INTERNAL
50CFLAGS+= -DHAVE_RC4_SET_KEY_INTERNAL
49SSLASM+= rc4 rc4-x86_64 51SSLASM+= rc4 rc4-x86_64
52SRCS+= rc4.c
50# ripemd 53# ripemd
51# sha 54# sha
52CFLAGS+= -DSHA1_ASM 55CFLAGS+= -DSHA1_ASM
diff --git a/src/lib/libcrypto/arch/i386/Makefile.inc b/src/lib/libcrypto/arch/i386/Makefile.inc
index c1a52e786a..911f02073b 100644
--- a/src/lib/libcrypto/arch/i386/Makefile.inc
+++ b/src/lib/libcrypto/arch/i386/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.13 2024/03/28 01:41:16 jsing Exp $ 1# $OpenBSD: Makefile.inc,v 1.14 2024/03/28 01:49:29 jsing Exp $
2 2
3# i386-specific libcrypto build rules 3# i386-specific libcrypto build rules
4 4
@@ -26,7 +26,10 @@ SSLASM+= md5 md5-586
26CFLAGS+= -DGHASH_ASM 26CFLAGS+= -DGHASH_ASM
27SSLASM+= modes ghash-x86 27SSLASM+= modes ghash-x86
28# rc4 28# rc4
29CFLAGS+= -DHAVE_RC4_INTERNAL
30CFLAGS+= -DHAVE_RC4_SET_KEY_INTERNAL
29SSLASM+= rc4 rc4-586 31SSLASM+= rc4 rc4-586
32SRCS+= rc4.c
30# sha 33# sha
31CFLAGS+= -DSHA1_ASM 34CFLAGS+= -DSHA1_ASM
32SSLASM+= sha sha1-586 35SSLASM+= sha sha1-586
diff --git a/src/lib/libcrypto/rc4/asm/rc4-586.pl b/src/lib/libcrypto/rc4/asm/rc4-586.pl
index bb30f9b88f..8fffe91e74 100644
--- a/src/lib/libcrypto/rc4/asm/rc4-586.pl
+++ b/src/lib/libcrypto/rc4/asm/rc4-586.pl
@@ -152,8 +152,9 @@ if ($alt=0) {
152 152
153&external_label("OPENSSL_ia32cap_P"); 153&external_label("OPENSSL_ia32cap_P");
154 154
155# void RC4(RC4_KEY *key,size_t len,const unsigned char *inp,unsigned char *out); 155# void rc4_internal(RC4_KEY *key, size_t len, const unsigned char *inp,
156&function_begin("RC4"); 156# unsigned char *out);
157&function_begin("rc4_internal");
157 &mov ($dat,&wparam(0)); # load key schedule pointer 158 &mov ($dat,&wparam(0)); # load key schedule pointer
158 &mov ($ty, &wparam(1)); # load len 159 &mov ($ty, &wparam(1)); # load len
159 &mov ($inp,&wparam(2)); # load inp 160 &mov ($inp,&wparam(2)); # load inp
@@ -291,7 +292,7 @@ if ($alt=0) {
291 &mov (&DWP(-4,$dat),$yy); # save key->y 292 &mov (&DWP(-4,$dat),$yy); # save key->y
292 &mov (&BP(-8,$dat),&LB($xx)); # save key->x 293 &mov (&BP(-8,$dat),&LB($xx)); # save key->x
293&set_label("abort"); 294&set_label("abort");
294&function_end("RC4"); 295&function_end("rc4_internal");
295 296
296######################################################################## 297########################################################################
297 298
@@ -301,8 +302,8 @@ $idi="ebp";
301$ido="ecx"; 302$ido="ecx";
302$idx="edx"; 303$idx="edx";
303 304
304# void RC4_set_key(RC4_KEY *key,int len,const unsigned char *data); 305# void rc4_set_key_internal(RC4_KEY *key,int len,const unsigned char *data);
305&function_begin("RC4_set_key"); 306&function_begin("rc4_set_key_internal");
306 &mov ($out,&wparam(0)); # load key 307 &mov ($out,&wparam(0)); # load key
307 &mov ($idi,&wparam(1)); # load len 308 &mov ($idi,&wparam(1)); # load len
308 &mov ($inp,&wparam(2)); # load data 309 &mov ($inp,&wparam(2)); # load data
@@ -382,6 +383,6 @@ $idx="edx";
382 &xor ("eax","eax"); 383 &xor ("eax","eax");
383 &mov (&DWP(-8,$out),"eax"); # key->x=0; 384 &mov (&DWP(-8,$out),"eax"); # key->x=0;
384 &mov (&DWP(-4,$out),"eax"); # key->y=0; 385 &mov (&DWP(-4,$out),"eax"); # key->y=0;
385&function_end("RC4_set_key"); 386&function_end("rc4_set_key_internal");
386 387
387&asm_finish(); 388&asm_finish();
diff --git a/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl b/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
index a9cf9d1bdf..4dfce6a9ad 100755
--- a/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
+++ b/src/lib/libcrypto/rc4/asm/rc4-x86_64.pl
@@ -124,10 +124,10 @@ $code=<<___;
124.extern OPENSSL_ia32cap_P 124.extern OPENSSL_ia32cap_P
125.hidden OPENSSL_ia32cap_P 125.hidden OPENSSL_ia32cap_P
126 126
127.globl RC4 127.globl rc4_internal
128.type RC4,\@function,4 128.type rc4_internal,\@function,4
129.align 16 129.align 16
130RC4: 130rc4_internal:
131 _CET_ENDBR 131 _CET_ENDBR
132 or $len,$len 132 or $len,$len
133 jne .Lentry 133 jne .Lentry
@@ -423,7 +423,7 @@ $code.=<<___;
423 add \$24,%rsp 423 add \$24,%rsp
424.Lepilogue: 424.Lepilogue:
425 ret 425 ret
426.size RC4,.-RC4 426.size rc4_internal,.-rc4_internal
427___ 427___
428} 428}
429 429
@@ -431,10 +431,10 @@ $idx="%r8";
431$ido="%r9"; 431$ido="%r9";
432 432
433$code.=<<___; 433$code.=<<___;
434.globl RC4_set_key 434.globl rc4_set_key_internal
435.type RC4_set_key,\@function,3 435.type rc4_set_key_internal,\@function,3
436.align 16 436.align 16
437RC4_set_key: 437rc4_set_key_internal:
438 _CET_ENDBR 438 _CET_ENDBR
439 lea 8($dat),$dat 439 lea 8($dat),$dat
440 lea ($inp,$len),$inp 440 lea ($inp,$len),$inp
@@ -502,7 +502,7 @@ RC4_set_key:
502 mov %eax,-8($dat) 502 mov %eax,-8($dat)
503 mov %eax,-4($dat) 503 mov %eax,-4($dat)
504 ret 504 ret
505.size RC4_set_key,.-RC4_set_key 505.size rc4_set_key_internal,.-rc4_set_key_internal
506___ 506___
507 507
508sub reg_part { 508sub reg_part {
diff --git a/src/lib/libcrypto/rc4/rc4.c b/src/lib/libcrypto/rc4/rc4.c
index bbf7c3ae4e..8ff8191a51 100644
--- a/src/lib/libcrypto/rc4/rc4.c
+++ b/src/lib/libcrypto/rc4/rc4.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rc4.c,v 1.8 2024/03/27 12:54:42 jsing Exp $ */ 1/* $OpenBSD: rc4.c,v 1.9 2024/03/28 01:49:29 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -68,8 +68,13 @@
68 * Date: Wed, 14 Sep 1994 06:35:31 GMT 68 * Date: Wed, 14 Sep 1994 06:35:31 GMT
69 */ 69 */
70 70
71void 71#ifdef HAVE_RC4_INTERNAL
72RC4(RC4_KEY *key, size_t len, const unsigned char *indata, 72void rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
73 unsigned char *outdata);
74
75#else
76static void
77rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
73 unsigned char *outdata) 78 unsigned char *outdata)
74{ 79{
75 RC4_INT *d; 80 RC4_INT *d;
@@ -251,9 +256,14 @@ RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
251 key->x = x; 256 key->x = x;
252 key->y = y; 257 key->y = y;
253} 258}
259#endif
254 260
255void 261#ifdef HAVE_RC4_SET_KEY_INTERNAL
256RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) 262void rc4_set_key_internal(RC4_KEY *key, int len, const unsigned char *data);
263
264#else
265static void
266rc4_set_key_internal(RC4_KEY *key, int len, const unsigned char *data)
257{ 267{
258 RC4_INT tmp; 268 RC4_INT tmp;
259 int id1, id2; 269 int id1, id2;
@@ -281,3 +291,17 @@ RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
281 SK_LOOP(d, i + 3); 291 SK_LOOP(d, i + 3);
282 } 292 }
283} 293}
294#endif
295
296void
297RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
298 unsigned char *outdata)
299{
300 rc4_internal(key, len, indata, outdata);
301}
302
303void
304RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
305{
306 rc4_set_key_internal(key, len, data);
307}