OLD | NEW |
1 /* crypto/rc4/rc4_enc.c */ | 1 /* crypto/rc4/rc4_enc.c */ |
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 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 #include "rc4_locl.h" | 60 #include "rc4_locl.h" |
61 | 61 |
62 /* RC4 as implemented from a posting from | 62 /* RC4 as implemented from a posting from |
63 * Newsgroups: sci.crypt | 63 * Newsgroups: sci.crypt |
64 * From: sterndark@netcom.com (David Sterndark) | 64 * From: sterndark@netcom.com (David Sterndark) |
65 * Subject: RC4 Algorithm revealed. | 65 * Subject: RC4 Algorithm revealed. |
66 * Message-ID: <sternCvKL4B.Hyy@netcom.com> | 66 * Message-ID: <sternCvKL4B.Hyy@netcom.com> |
67 * Date: Wed, 14 Sep 1994 06:35:31 GMT | 67 * Date: Wed, 14 Sep 1994 06:35:31 GMT |
68 */ | 68 */ |
69 | 69 |
70 void RC4(RC4_KEY *key, unsigned long len, const unsigned char *indata, | 70 void RC4(RC4_KEY *key, size_t len, const unsigned char *indata, |
71 unsigned char *outdata) | 71 unsigned char *outdata) |
72 { | 72 { |
73 register RC4_INT *d; | 73 register RC4_INT *d; |
74 register RC4_INT x,y,tx,ty; | 74 register RC4_INT x,y,tx,ty; |
75 » int i; | 75 » size_t i; |
76 | 76 |
77 x=key->x; | 77 x=key->x; |
78 y=key->y; | 78 y=key->y; |
79 d=key->data; | 79 d=key->data; |
80 | 80 |
81 #if defined(RC4_CHUNK) | 81 #if defined(RC4_CHUNK) |
82 /* | 82 /* |
83 * The original reason for implementing this(*) was the fact that | 83 * The original reason for implementing this(*) was the fact that |
84 * pre-21164a Alpha CPUs don't have byte load/store instructions | 84 * pre-21164a Alpha CPUs don't have byte load/store instructions |
85 * and e.g. a byte store has to be done with 64-bit load, shift, | 85 * and e.g. a byte store has to be done with 64-bit load, shift, |
(...skipping 27 matching lines...) Expand all Loading... |
113 # define RC4_STEP ( \ | 113 # define RC4_STEP ( \ |
114 x=(x+1) &0xff, \ | 114 x=(x+1) &0xff, \ |
115 tx=d[x], \ | 115 tx=d[x], \ |
116 y=(tx+y)&0xff, \ | 116 y=(tx+y)&0xff, \ |
117 ty=d[y], \ | 117 ty=d[y], \ |
118 d[y]=tx, \ | 118 d[y]=tx, \ |
119 d[x]=ty, \ | 119 d[x]=ty, \ |
120 (RC4_CHUNK)d[(tx+ty)&0xff]\ | 120 (RC4_CHUNK)d[(tx+ty)&0xff]\ |
121 ) | 121 ) |
122 | 122 |
123 » if ( ( ((unsigned long)indata & (sizeof(RC4_CHUNK)-1)) | | 123 » if ( ( ((size_t)indata & (sizeof(RC4_CHUNK)-1)) | |
124 » ((unsigned long)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 ) | 124 » ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 ) |
125 { | 125 { |
126 RC4_CHUNK ichunk,otp; | 126 RC4_CHUNK ichunk,otp; |
127 const union { long one; char little; } is_endian = {1}; | 127 const union { long one; char little; } is_endian = {1}; |
128 | 128 |
129 /* | 129 /* |
130 * I reckon we can afford to implement both endian | 130 * I reckon we can afford to implement both endian |
131 * cases and to decide which way to take at run-time | 131 * cases and to decide which way to take at run-time |
132 * because the machine code appears to be very compact | 132 * because the machine code appears to be very compact |
133 * and redundant 1-2KB is perfectly tolerable (i.e. | 133 * and redundant 1-2KB is perfectly tolerable (i.e. |
134 * in case the compiler fails to eliminate it:-). By | 134 * in case the compiler fails to eliminate it:-). By |
(...skipping 15 matching lines...) Expand all Loading... |
150 * before); | 150 * before); |
151 * - in case you wonder "&(sizeof(RC4_CHUNK)*8-1)" in | 151 * - in case you wonder "&(sizeof(RC4_CHUNK)*8-1)" in |
152 * [LB]ESHFT guards against "shift is out of range" | 152 * [LB]ESHFT guards against "shift is out of range" |
153 * warnings when sizeof(RC4_CHUNK)!=8 | 153 * warnings when sizeof(RC4_CHUNK)!=8 |
154 * | 154 * |
155 * <appro@fy.chalmers.se> | 155 * <appro@fy.chalmers.se> |
156 */ | 156 */ |
157 if (!is_endian.little) | 157 if (!is_endian.little) |
158 { /* BIG-ENDIAN CASE */ | 158 { /* BIG-ENDIAN CASE */ |
159 # define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1)) | 159 # define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1)) |
160 » » » for (;len&~(sizeof(RC4_CHUNK)-1);len-=sizeof(RC4_CHUNK)) | 160 » » » for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK)) |
161 { | 161 { |
162 ichunk = *(RC4_CHUNK *)indata; | 162 ichunk = *(RC4_CHUNK *)indata; |
163 otp = RC4_STEP<<BESHFT(0); | 163 otp = RC4_STEP<<BESHFT(0); |
164 otp |= RC4_STEP<<BESHFT(1); | 164 otp |= RC4_STEP<<BESHFT(1); |
165 otp |= RC4_STEP<<BESHFT(2); | 165 otp |= RC4_STEP<<BESHFT(2); |
166 otp |= RC4_STEP<<BESHFT(3); | 166 otp |= RC4_STEP<<BESHFT(3); |
167 if (sizeof(RC4_CHUNK)==8) | 167 if (sizeof(RC4_CHUNK)==8) |
168 { | 168 { |
169 otp |= RC4_STEP<<BESHFT(4); | 169 otp |= RC4_STEP<<BESHFT(4); |
170 otp |= RC4_STEP<<BESHFT(5); | 170 otp |= RC4_STEP<<BESHFT(5); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 ochunk |= (otp^ichunk) & mask; | 203 ochunk |= (otp^ichunk) & mask; |
204 *(RC4_CHUNK *)outdata = ochunk; | 204 *(RC4_CHUNK *)outdata = ochunk; |
205 } | 205 } |
206 key->x=x; | 206 key->x=x; |
207 key->y=y; | 207 key->y=y; |
208 return; | 208 return; |
209 } | 209 } |
210 else | 210 else |
211 { /* LITTLE-ENDIAN CASE */ | 211 { /* LITTLE-ENDIAN CASE */ |
212 # define LESHFT(c) (((c)*8)&(sizeof(RC4_CHUNK)*8-1)) | 212 # define LESHFT(c) (((c)*8)&(sizeof(RC4_CHUNK)*8-1)) |
213 » » » for (;len&~(sizeof(RC4_CHUNK)-1);len-=sizeof(RC4_CHUNK)) | 213 » » » for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK)) |
214 { | 214 { |
215 ichunk = *(RC4_CHUNK *)indata; | 215 ichunk = *(RC4_CHUNK *)indata; |
216 otp = RC4_STEP; | 216 otp = RC4_STEP; |
217 otp |= RC4_STEP<<8; | 217 otp |= RC4_STEP<<8; |
218 otp |= RC4_STEP<<16; | 218 otp |= RC4_STEP<<16; |
219 otp |= RC4_STEP<<24; | 219 otp |= RC4_STEP<<24; |
220 if (sizeof(RC4_CHUNK)==8) | 220 if (sizeof(RC4_CHUNK)==8) |
221 { | 221 { |
222 otp |= RC4_STEP<<LESHFT(4); | 222 otp |= RC4_STEP<<LESHFT(4); |
223 otp |= RC4_STEP<<LESHFT(5); | 223 otp |= RC4_STEP<<LESHFT(5); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 d[x]=ty=d[y]; \ | 269 d[x]=ty=d[y]; \ |
270 d[y]=tx; \ | 270 d[y]=tx; \ |
271 (out) = d[(tx+ty)&0xff]^ (in); | 271 (out) = d[(tx+ty)&0xff]^ (in); |
272 | 272 |
273 #ifndef RC4_INDEX | 273 #ifndef RC4_INDEX |
274 #define RC4_LOOP(a,b,i) LOOP(*((a)++),*((b)++)) | 274 #define RC4_LOOP(a,b,i) LOOP(*((a)++),*((b)++)) |
275 #else | 275 #else |
276 #define RC4_LOOP(a,b,i) LOOP(a[i],b[i]) | 276 #define RC4_LOOP(a,b,i) LOOP(a[i],b[i]) |
277 #endif | 277 #endif |
278 | 278 |
279 » i=(int)(len>>3L); | 279 » i=len>>3; |
280 if (i) | 280 if (i) |
281 { | 281 { |
282 for (;;) | 282 for (;;) |
283 { | 283 { |
284 RC4_LOOP(indata,outdata,0); | 284 RC4_LOOP(indata,outdata,0); |
285 RC4_LOOP(indata,outdata,1); | 285 RC4_LOOP(indata,outdata,1); |
286 RC4_LOOP(indata,outdata,2); | 286 RC4_LOOP(indata,outdata,2); |
287 RC4_LOOP(indata,outdata,3); | 287 RC4_LOOP(indata,outdata,3); |
288 RC4_LOOP(indata,outdata,4); | 288 RC4_LOOP(indata,outdata,4); |
289 RC4_LOOP(indata,outdata,5); | 289 RC4_LOOP(indata,outdata,5); |
290 RC4_LOOP(indata,outdata,6); | 290 RC4_LOOP(indata,outdata,6); |
291 RC4_LOOP(indata,outdata,7); | 291 RC4_LOOP(indata,outdata,7); |
292 #ifdef RC4_INDEX | 292 #ifdef RC4_INDEX |
293 indata+=8; | 293 indata+=8; |
294 outdata+=8; | 294 outdata+=8; |
295 #endif | 295 #endif |
296 if (--i == 0) break; | 296 if (--i == 0) break; |
297 } | 297 } |
298 } | 298 } |
299 » i=(int)len&0x07; | 299 » i=len&0x07; |
300 if (i) | 300 if (i) |
301 { | 301 { |
302 for (;;) | 302 for (;;) |
303 { | 303 { |
304 RC4_LOOP(indata,outdata,0); if (--i == 0) break; | 304 RC4_LOOP(indata,outdata,0); if (--i == 0) break; |
305 RC4_LOOP(indata,outdata,1); if (--i == 0) break; | 305 RC4_LOOP(indata,outdata,1); if (--i == 0) break; |
306 RC4_LOOP(indata,outdata,2); if (--i == 0) break; | 306 RC4_LOOP(indata,outdata,2); if (--i == 0) break; |
307 RC4_LOOP(indata,outdata,3); if (--i == 0) break; | 307 RC4_LOOP(indata,outdata,3); if (--i == 0) break; |
308 RC4_LOOP(indata,outdata,4); if (--i == 0) break; | 308 RC4_LOOP(indata,outdata,4); if (--i == 0) break; |
309 RC4_LOOP(indata,outdata,5); if (--i == 0) break; | 309 RC4_LOOP(indata,outdata,5); if (--i == 0) break; |
310 RC4_LOOP(indata,outdata,6); if (--i == 0) break; | 310 RC4_LOOP(indata,outdata,6); if (--i == 0) break; |
311 } | 311 } |
312 } | 312 } |
313 key->x=x; | 313 key->x=x; |
314 key->y=y; | 314 key->y=y; |
315 } | 315 } |
OLD | NEW |