2016-05-17 18:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:56:39 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:56:39 +00:00
|
|
|
*/
|
|
|
|
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/rc5.h>
|
2019-09-27 21:58:06 +00:00
|
|
|
#include "rc5_local.h"
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2000-06-03 14:13:58 +00:00
|
|
|
void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
|
2015-01-22 03:40:55 +00:00
|
|
|
int rounds)
|
|
|
|
{
|
|
|
|
RC5_32_INT L[64], l, ll, A, B, *S, k;
|
|
|
|
int i, j, m, c, t, ii, jj;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((rounds != RC5_16_ROUNDS) &&
|
|
|
|
(rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS))
|
|
|
|
rounds = RC5_16_ROUNDS;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
key->rounds = rounds;
|
|
|
|
S = &(key->data[0]);
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i <= (len - 8); i += 8) {
|
|
|
|
c2l(data, l);
|
|
|
|
L[j++] = l;
|
|
|
|
c2l(data, l);
|
|
|
|
L[j++] = l;
|
|
|
|
}
|
|
|
|
ii = len - i;
|
|
|
|
if (ii) {
|
|
|
|
k = len & 0x07;
|
|
|
|
c2ln(data, l, ll, k);
|
|
|
|
L[j + 0] = l;
|
|
|
|
L[j + 1] = ll;
|
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
c = (len + 3) / 4;
|
|
|
|
t = (rounds + 1) * 2;
|
|
|
|
S[0] = RC5_32_P;
|
|
|
|
for (i = 1; i < t; i++)
|
|
|
|
S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
j = (t > c) ? t : c;
|
|
|
|
j *= 3;
|
|
|
|
ii = jj = 0;
|
|
|
|
A = B = 0;
|
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
k = (S[ii] + A + B) & RC5_32_MASK;
|
|
|
|
A = S[ii] = ROTATE_l32(k, 3);
|
|
|
|
m = (int)(A + B);
|
|
|
|
k = (L[jj] + A + B) & RC5_32_MASK;
|
|
|
|
B = L[jj] = ROTATE_l32(k, m);
|
|
|
|
if (++ii >= t)
|
|
|
|
ii = 0;
|
|
|
|
if (++jj >= c)
|
|
|
|
jj = 0;
|
|
|
|
}
|
|
|
|
}
|