summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2025-08-14 14:55:43 +0000
committerjsing <>2025-08-14 14:55:43 +0000
commit3d3d845569e98c5b0f11899e4958cca728c8b99a (patch)
treea341bde2bee149d6abd69978567dd1198a091e3d
parenta3eb15e012fb4bb74bd6789fdb9318e71bb32d97 (diff)
downloadopenbsd-3d3d845569e98c5b0f11899e4958cca728c8b99a.tar.gz
openbsd-3d3d845569e98c5b0f11899e4958cca728c8b99a.tar.bz2
openbsd-3d3d845569e98c5b0f11899e4958cca728c8b99a.zip
Clean up parts of rc4.
Provide a static inline rc4_step() function that replaces the near identical RC4_STEP and RC4_LOOP macros. Simplify the processing loop and use for loops with small constants, which the compiler can unroll if it wants to do so. Inline the SK_LOOP macro in rc4_set_key_internal(), also using a small loop that the compiler will most likely unroll. ok tb@
-rw-r--r--src/lib/libcrypto/rc4/rc4.c119
1 files changed, 40 insertions, 79 deletions
diff --git a/src/lib/libcrypto/rc4/rc4.c b/src/lib/libcrypto/rc4/rc4.c
index 56ed43cba7..9c0a61162d 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.13 2025/01/27 14:02:32 jsing Exp $ */ 1/* $OpenBSD: rc4.c,v 1.14 2025/08/14 14:55:43 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 *
@@ -75,12 +75,25 @@ void rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
75 unsigned char *outdata); 75 unsigned char *outdata);
76 76
77#else 77#else
78static inline RC4_INT
79rc4_step(RC4_INT *d, RC4_INT *x, RC4_INT *y)
80{
81 RC4_INT tx, ty;
82
83 *x = (*x + 1) & 0xff;
84 tx = d[*x];
85 *y = (tx + *y) & 0xff;
86 d[*x] = ty = d[*y];
87 d[*y] = tx;
88
89 return d[(tx + ty) & 0xff];
90}
91
78static void 92static void
79rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata, 93rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
80 unsigned char *outdata) 94 unsigned char *outdata)
81{ 95{
82 RC4_INT *d; 96 RC4_INT *d, x, y;
83 RC4_INT x, y,tx, ty;
84 size_t i; 97 size_t i;
85 98
86 x = key->x; 99 x = key->x;
@@ -119,15 +132,7 @@ rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
119 * <appro@fy.chalmers.se> 132 * <appro@fy.chalmers.se>
120 */ 133 */
121 134
122# define RC4_STEP ( \ 135# define RC4_STEP ((RC4_CHUNK)rc4_step(d, &x, &y))
123 x=(x+1) &0xff, \
124 tx=d[x], \
125 y=(tx+y)&0xff, \
126 ty=d[y], \
127 d[y]=tx, \
128 d[x]=ty, \
129 (RC4_CHUNK)d[(tx+ty)&0xff]\
130 )
131 136
132 if ((((size_t)indata & (sizeof(RC4_CHUNK) - 1)) | 137 if ((((size_t)indata & (sizeof(RC4_CHUNK) - 1)) |
133 ((size_t)outdata & (sizeof(RC4_CHUNK) - 1))) == 0 ) { 138 ((size_t)outdata & (sizeof(RC4_CHUNK) - 1))) == 0 ) {
@@ -196,59 +201,18 @@ rc4_internal(RC4_KEY *key, size_t len, const unsigned char *indata,
196#endif 201#endif
197 } 202 }
198#endif 203#endif
199#define RC4_LOOP(in,out) \
200 x=((x+1)&0xff); \
201 tx=d[x]; \
202 y=(tx+y)&0xff; \
203 d[x]=ty=d[y]; \
204 d[y]=tx; \
205 (out) = d[(tx+ty)&0xff]^ (in);
206
207 i = len >> 3;
208 if (i) {
209 for (;;) {
210 RC4_LOOP(indata[0], outdata[0]);
211 RC4_LOOP(indata[1], outdata[1]);
212 RC4_LOOP(indata[2], outdata[2]);
213 RC4_LOOP(indata[3], outdata[3]);
214 RC4_LOOP(indata[4], outdata[4]);
215 RC4_LOOP(indata[5], outdata[5]);
216 RC4_LOOP(indata[6], outdata[6]);
217 RC4_LOOP(indata[7], outdata[7]);
218 204
219 indata += 8; 205 while (len >= 8) {
220 outdata += 8; 206 for (i = 0; i < 8; i++)
207 outdata[i] = rc4_step(d, &x, &y) ^ indata[i];
221 208
222 if (--i == 0) 209 indata += 8;
223 break; 210 outdata += 8;
224 } 211 len -= 8;
225 }
226 i = len&0x07;
227 if (i) {
228 for (;;) {
229 RC4_LOOP(indata[0], outdata[0]);
230 if (--i == 0)
231 break;
232 RC4_LOOP(indata[1], outdata[1]);
233 if (--i == 0)
234 break;
235 RC4_LOOP(indata[2], outdata[2]);
236 if (--i == 0)
237 break;
238 RC4_LOOP(indata[3], outdata[3]);
239 if (--i == 0)
240 break;
241 RC4_LOOP(indata[4], outdata[4]);
242 if (--i == 0)
243 break;
244 RC4_LOOP(indata[5], outdata[5]);
245 if (--i == 0)
246 break;
247 RC4_LOOP(indata[6], outdata[6]);
248 if (--i == 0)
249 break;
250 }
251 } 212 }
213 for (i = 0; i < len; i++)
214 outdata[i] = rc4_step(d, &x, &y) ^ indata[i];
215
252 key->x = x; 216 key->x = x;
253 key->y = y; 217 key->y = y;
254} 218}
@@ -261,30 +225,27 @@ void rc4_set_key_internal(RC4_KEY *key, int len, const unsigned char *data);
261static inline void 225static inline void
262rc4_set_key_internal(RC4_KEY *key, int len, const unsigned char *data) 226rc4_set_key_internal(RC4_KEY *key, int len, const unsigned char *data)
263{ 227{
264 RC4_INT tmp; 228 RC4_INT *d, tmp;
265 int id1, id2; 229 int idx1, idx2;
266 RC4_INT *d; 230 int i, j;
267 unsigned int i;
268 231
269 d = &(key->data[0]); 232 d = key->data;
270 key->x = 0; 233 key->x = 0;
271 key->y = 0; 234 key->y = 0;
272 id1 = id2 = 0; 235 idx1 = idx2 = 0;
273
274#define SK_LOOP(d,n) { \
275 tmp=d[(n)]; \
276 id2 = (data[id1] + tmp + id2) & 0xff; \
277 if (++id1 == len) id1=0; \
278 d[(n)]=d[id2]; \
279 d[id2]=tmp; }
280 236
281 for (i = 0; i < 256; i++) 237 for (i = 0; i < 256; i++)
282 d[i] = i; 238 d[i] = i;
283 for (i = 0; i < 256; i += 4) { 239 for (i = 0; i < 256; i += 4) {
284 SK_LOOP(d, i + 0); 240 for (j = 0; j < 4; j++) {
285 SK_LOOP(d, i + 1); 241 tmp = d[i + j];
286 SK_LOOP(d, i + 2); 242 idx2 = (data[idx1] + tmp + idx2) & 0xff;
287 SK_LOOP(d, i + 3); 243 d[i + j] = d[idx2];
244 d[idx2] = tmp;
245
246 if (++idx1 == len)
247 idx1 = 0;
248 }
288 } 249 }
289} 250}
290#endif 251#endif