| 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 |
{ |
| 22 |
RC5_32_INT L[64], l, ll, A, B, *S, k; |
| 23 |
int i, j, m, c, t, ii, jj; |
| 24 |
|
| 25 |
if (len > 255) |
| 26 |
return 0; |
| 27 |
|
| 28 |
if ((rounds != RC5_16_ROUNDS) && (rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS)) |
| 29 |
rounds = RC5_16_ROUNDS; |
| 30 |
|
| 31 |
key->rounds = rounds; |
| 32 |
S = &(key->data[0]); |
| 33 |
j = 0; |
| 34 |
for (i = 0; i <= (len - 8); i += 8) { |
| 35 |
c2l(data, l); |
| 36 |
L[j++] = l; |
| 37 |
c2l(data, l); |
| 38 |
L[j++] = l; |
| 39 |
} |
| 40 |
ii = len - i; |
| 41 |
if (ii) { |
| 42 |
k = len & 0x07; |
| 43 |
c2ln(data, l, ll, k); |
| 44 |
L[j + 0] = l; |
| 45 |
L[j + 1] = ll; |
| 46 |
} |
| 47 |
|
| 48 |
c = (len + 3) / 4; |
| 49 |
t = (rounds + 1) * 2; |
| 50 |
S[0] = RC5_32_P; |
| 51 |
for (i = 1; i < t; i++) |
| 52 |
S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK; |
| 53 |
|
| 54 |
j = (t > c) ? t : c; |
| 55 |
j *= 3; |
| 56 |
ii = jj = 0; |
| 57 |
A = B = 0; |
| 58 |
for (i = 0; i < j; i++) { |
| 59 |
k = (S[ii] + A + B) & RC5_32_MASK; |
| 60 |
A = S[ii] = ROTATE_l32(k, 3); |
| 61 |
m = (int)(A + B); |
| 62 |
k = (L[jj] + A + B) & RC5_32_MASK; |
| 63 |
B = L[jj] = ROTATE_l32(k, m); |
| 64 |
if (++ii >= t) |
| 65 |
ii = 0; |
| 66 |
if (++jj >= c) |
| 67 |
jj = 0; |
| 68 |
} |
| 69 |
|
| 70 |
return 1; |
| 71 |
} |