1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/rc4.h>
11 #include "rc4_local.h"
12
13 /*-
14 * RC4 as implemented from a posting from
15 * Newsgroups: sci.crypt
16 * Subject: RC4 Algorithm revealed.
17 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
18 * Date: Wed, 14 Sep 1994 06:35:31 GMT
19 */
20
RC4(RC4_KEY * key,size_t len,const unsigned char * indata,unsigned char * outdata)21 void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
22 unsigned char *outdata)
23 {
24 register RC4_INT *d;
25 register RC4_INT x, y, tx, ty;
26 size_t i;
27
28 x = key->x;
29 y = key->y;
30 d = key->data;
31
32 #define LOOP(in,out) \
33 x=((x+1)&0xff); \
34 tx=d[x]; \
35 y=(tx+y)&0xff; \
36 d[x]=ty=d[y]; \
37 d[y]=tx; \
38 (out) = d[(tx+ty)&0xff]^ (in);
39
40 i = len >> 3;
41 if (i) {
42 for (;;) {
43 LOOP(indata[0], outdata[0]);
44 LOOP(indata[1], outdata[1]);
45 LOOP(indata[2], outdata[2]);
46 LOOP(indata[3], outdata[3]);
47 LOOP(indata[4], outdata[4]);
48 LOOP(indata[5], outdata[5]);
49 LOOP(indata[6], outdata[6]);
50 LOOP(indata[7], outdata[7]);
51 indata += 8;
52 outdata += 8;
53 if (--i == 0)
54 break;
55 }
56 }
57 i = len & 0x07;
58 if (i) {
59 for (;;) {
60 LOOP(indata[0], outdata[0]);
61 if (--i == 0)
62 break;
63 LOOP(indata[1], outdata[1]);
64 if (--i == 0)
65 break;
66 LOOP(indata[2], outdata[2]);
67 if (--i == 0)
68 break;
69 LOOP(indata[3], outdata[3]);
70 if (--i == 0)
71 break;
72 LOOP(indata[4], outdata[4]);
73 if (--i == 0)
74 break;
75 LOOP(indata[5], outdata[5]);
76 if (--i == 0)
77 break;
78 LOOP(indata[6], outdata[6]);
79 if (--i == 0)
80 break;
81 }
82 }
83 key->x = x;
84 key->y = y;
85 }
86