/src/openssl/crypto/rc5/rc5_skey.c
Line | Count | Source (jump to first uncovered line) |
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 | | * RC5 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/rc5.h> |
17 | | #include "rc5_local.h" |
18 | | |
19 | | int RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data, |
20 | | int rounds) |
21 | 0 | { |
22 | 0 | RC5_32_INT L[64], l, ll, A, B, *S, k; |
23 | 0 | int i, j, m, c, t, ii, jj; |
24 | |
|
25 | 0 | if (len > 255) |
26 | 0 | return 0; |
27 | | |
28 | 0 | if ((rounds != RC5_16_ROUNDS) && |
29 | 0 | (rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS)) |
30 | 0 | rounds = RC5_16_ROUNDS; |
31 | |
|
32 | 0 | key->rounds = rounds; |
33 | 0 | S = &(key->data[0]); |
34 | 0 | j = 0; |
35 | 0 | for (i = 0; i <= (len - 8); i += 8) { |
36 | 0 | c2l(data, l); |
37 | 0 | L[j++] = l; |
38 | 0 | c2l(data, l); |
39 | 0 | L[j++] = l; |
40 | 0 | } |
41 | 0 | ii = len - i; |
42 | 0 | if (ii) { |
43 | 0 | k = len & 0x07; |
44 | 0 | c2ln(data, l, ll, k); |
45 | 0 | L[j + 0] = l; |
46 | 0 | L[j + 1] = ll; |
47 | 0 | } |
48 | |
|
49 | 0 | c = (len + 3) / 4; |
50 | 0 | t = (rounds + 1) * 2; |
51 | 0 | S[0] = RC5_32_P; |
52 | 0 | for (i = 1; i < t; i++) |
53 | 0 | S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK; |
54 | |
|
55 | 0 | j = (t > c) ? t : c; |
56 | 0 | j *= 3; |
57 | 0 | ii = jj = 0; |
58 | 0 | A = B = 0; |
59 | 0 | for (i = 0; i < j; i++) { |
60 | 0 | k = (S[ii] + A + B) & RC5_32_MASK; |
61 | 0 | A = S[ii] = ROTATE_l32(k, 3); |
62 | 0 | m = (int)(A + B); |
63 | 0 | k = (L[jj] + A + B) & RC5_32_MASK; |
64 | 0 | B = L[jj] = ROTATE_l32(k, m); |
65 | 0 | if (++ii >= t) |
66 | 0 | ii = 0; |
67 | 0 | if (++jj >= c) |
68 | 0 | jj = 0; |
69 | 0 | } |
70 | |
|
71 | 0 | return 1; |
72 | 0 | } |