yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 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 | #include <openssl/opensslv.h> |
| 13 | |
| 14 | const char *RC4_options(void) |
| 15 | { |
| 16 | if (sizeof(RC4_INT) == 1) |
| 17 | return "rc4(char)"; |
| 18 | else |
| 19 | return "rc4(int)"; |
| 20 | } |
| 21 | |
| 22 | /*- |
| 23 | * RC4 as implemented from a posting from |
| 24 | * Newsgroups: sci.crypt |
| 25 | * Subject: RC4 Algorithm revealed. |
| 26 | * Message-ID: <sternCvKL4B.Hyy@netcom.com> |
| 27 | * Date: Wed, 14 Sep 1994 06:35:31 GMT |
| 28 | */ |
| 29 | |
| 30 | void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) |
| 31 | { |
| 32 | register RC4_INT tmp; |
| 33 | register int id1, id2; |
| 34 | register RC4_INT *d; |
| 35 | unsigned int i; |
| 36 | |
| 37 | d = &(key->data[0]); |
| 38 | key->x = 0; |
| 39 | key->y = 0; |
| 40 | id1 = id2 = 0; |
| 41 | |
| 42 | #define SK_LOOP(d,n) { \ |
| 43 | tmp=d[(n)]; \ |
| 44 | id2 = (data[id1] + tmp + id2) & 0xff; \ |
| 45 | if (++id1 == len) id1=0; \ |
| 46 | d[(n)]=d[id2]; \ |
| 47 | d[id2]=tmp; } |
| 48 | |
| 49 | for (i = 0; i < 256; i++) |
| 50 | d[i] = i; |
| 51 | for (i = 0; i < 256; i += 4) { |
| 52 | SK_LOOP(d, i + 0); |
| 53 | SK_LOOP(d, i + 1); |
| 54 | SK_LOOP(d, i + 2); |
| 55 | SK_LOOP(d, i + 3); |
| 56 | } |
| 57 | } |