rc5_skey.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright 1995-2016 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. #include <openssl/rc5.h>
  10. #include "rc5_locl.h"
  11. void RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
  12. int rounds)
  13. {
  14. RC5_32_INT L[64], l, ll, A, B, *S, k;
  15. int i, j, m, c, t, ii, jj;
  16. if ((rounds != RC5_16_ROUNDS) &&
  17. (rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS))
  18. rounds = RC5_16_ROUNDS;
  19. key->rounds = rounds;
  20. S = &(key->data[0]);
  21. j = 0;
  22. for (i = 0; i <= (len - 8); i += 8) {
  23. c2l(data, l);
  24. L[j++] = l;
  25. c2l(data, l);
  26. L[j++] = l;
  27. }
  28. ii = len - i;
  29. if (ii) {
  30. k = len & 0x07;
  31. c2ln(data, l, ll, k);
  32. L[j + 0] = l;
  33. L[j + 1] = ll;
  34. }
  35. c = (len + 3) / 4;
  36. t = (rounds + 1) * 2;
  37. S[0] = RC5_32_P;
  38. for (i = 1; i < t; i++)
  39. S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK;
  40. j = (t > c) ? t : c;
  41. j *= 3;
  42. ii = jj = 0;
  43. A = B = 0;
  44. for (i = 0; i < j; i++) {
  45. k = (S[ii] + A + B) & RC5_32_MASK;
  46. A = S[ii] = ROTATE_l32(k, 3);
  47. m = (int)(A + B);
  48. k = (L[jj] + A + B) & RC5_32_MASK;
  49. B = L[jj] = ROTATE_l32(k, m);
  50. if (++ii >= t)
  51. ii = 0;
  52. if (++jj >= c)
  53. jj = 0;
  54. }
  55. }