| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/rc4.h> |
| 5 |
|
| 6 |
const char *RC4_options(void) { |
| 7 |
return "rc4(ptr,int)"; |
| 8 |
} |
| 9 |
|
| 10 |
void RC4(RC4_KEY *key, size_t len, const uint8_t *in, uint8_t *out) { |
| 11 |
uint32_t x = key->x; |
| 12 |
uint32_t y = key->y; |
| 13 |
uint32_t *d = key->data; |
| 14 |
|
| 15 |
for (size_t i = 0; i < len; i++) { |
| 16 |
x = (x + 1) & 0xff; |
| 17 |
uint32_t tx = d[x]; |
| 18 |
y = (tx + y) & 0xff; |
| 19 |
uint32_t ty = d[y]; |
| 20 |
d[x] = ty; |
| 21 |
d[y] = tx; |
| 22 |
out[i] = d[(tx + ty) & 0xff] ^ in[i]; |
| 23 |
} |
| 24 |
|
| 25 |
key->x = x; |
| 26 |
key->y = y; |
| 27 |
} |
| 28 |
|
| 29 |
void RC4_set_key(RC4_KEY *rc4key, unsigned len, const uint8_t *key) { |
| 30 |
uint32_t *d = &rc4key->data[0]; |
| 31 |
rc4key->x = 0; |
| 32 |
rc4key->y = 0; |
| 33 |
|
| 34 |
for (unsigned i = 0; i < 256; i++) { |
| 35 |
d[i] = i; |
| 36 |
} |
| 37 |
|
| 38 |
unsigned id1 = 0, id2 = 0; |
| 39 |
for (unsigned i = 0; i < 256; i++) { |
| 40 |
uint32_t tmp = d[i]; |
| 41 |
id2 = (key[id1] + tmp + id2) & 0xff; |
| 42 |
if (++id1 == len) { |
| 43 |
id1 = 0; |
| 44 |
} |
| 45 |
d[i] = d[id2]; |
| 46 |
d[id2] = tmp; |
| 47 |
} |
| 48 |
} |