xref: /openssl/crypto/rc4/rc4_skey.c (revision 33388b44)
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * RC4 low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/rc4.h>
17 #include "rc4_local.h"
18 #include <openssl/opensslv.h>
19 
RC4_options(void)20 const char *RC4_options(void)
21 {
22     if (sizeof(RC4_INT) == 1)
23         return "rc4(char)";
24     else
25         return "rc4(int)";
26 }
27 
28 /*-
29  * RC4 as implemented from a posting from
30  * Newsgroups: sci.crypt
31  * Subject: RC4 Algorithm revealed.
32  * Message-ID: <sternCvKL4B.Hyy@netcom.com>
33  * Date: Wed, 14 Sep 1994 06:35:31 GMT
34  */
35 
RC4_set_key(RC4_KEY * key,int len,const unsigned char * data)36 void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
37 {
38     register RC4_INT tmp;
39     register int id1, id2;
40     register RC4_INT *d;
41     unsigned int i;
42 
43     d = &(key->data[0]);
44     key->x = 0;
45     key->y = 0;
46     id1 = id2 = 0;
47 
48 #define SK_LOOP(d,n) { \
49                 tmp=d[(n)]; \
50                 id2 = (data[id1] + tmp + id2) & 0xff; \
51                 if (++id1 == len) id1=0; \
52                 d[(n)]=d[id2]; \
53                 d[id2]=tmp; }
54 
55     for (i = 0; i < 256; i++)
56         d[i] = i;
57     for (i = 0; i < 256; i += 4) {
58         SK_LOOP(d, i + 0);
59         SK_LOOP(d, i + 1);
60         SK_LOOP(d, i + 2);
61         SK_LOOP(d, i + 3);
62     }
63 }
64