/src/boringssl/crypto/rc4/rc4.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/rc4.h> |
16 | | |
17 | | |
18 | 0 | void RC4(RC4_KEY *key, size_t len, const uint8_t *in, uint8_t *out) { |
19 | 0 | uint32_t x = key->x; |
20 | 0 | uint32_t y = key->y; |
21 | 0 | uint32_t *d = key->data; |
22 | |
|
23 | 0 | for (size_t i = 0; i < len; i++) { |
24 | 0 | x = (x + 1) & 0xff; |
25 | 0 | uint32_t tx = d[x]; |
26 | 0 | y = (tx + y) & 0xff; |
27 | 0 | uint32_t ty = d[y]; |
28 | 0 | d[x] = ty; |
29 | 0 | d[y] = tx; |
30 | 0 | out[i] = d[(tx + ty) & 0xff] ^ in[i]; |
31 | 0 | } |
32 | |
|
33 | 0 | key->x = x; |
34 | 0 | key->y = y; |
35 | 0 | } |
36 | | |
37 | 0 | void RC4_set_key(RC4_KEY *rc4key, unsigned len, const uint8_t *key) { |
38 | 0 | uint32_t *d = &rc4key->data[0]; |
39 | 0 | rc4key->x = 0; |
40 | 0 | rc4key->y = 0; |
41 | |
|
42 | 0 | for (unsigned i = 0; i < 256; i++) { |
43 | 0 | d[i] = i; |
44 | 0 | } |
45 | |
|
46 | 0 | unsigned id1 = 0, id2 = 0; |
47 | 0 | for (unsigned i = 0; i < 256; i++) { |
48 | 0 | uint32_t tmp = d[i]; |
49 | 0 | id2 = (key[id1] + tmp + id2) & 0xff; |
50 | 0 | if (++id1 == len) { |
51 | 0 | id1 = 0; |
52 | 0 | } |
53 | 0 | d[i] = d[id2]; |
54 | 0 | d[id2] = tmp; |
55 | 0 | } |
56 | 0 | } |