2
0

rc5_skey.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * RC5 low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/rc5.h>
  15. #include "rc5_local.h"
  16. int RC5_32_set_key(RC5_32_KEY *key, int len, const unsigned char *data,
  17. int rounds)
  18. {
  19. RC5_32_INT L[64], l, ll, A, B, *S, k;
  20. int i, j, m, c, t, ii, jj;
  21. if (len > 255)
  22. return 0;
  23. if ((rounds != RC5_16_ROUNDS) &&
  24. (rounds != RC5_12_ROUNDS) && (rounds != RC5_8_ROUNDS))
  25. rounds = RC5_16_ROUNDS;
  26. key->rounds = rounds;
  27. S = &(key->data[0]);
  28. j = 0;
  29. for (i = 0; i <= (len - 8); i += 8) {
  30. c2l(data, l);
  31. L[j++] = l;
  32. c2l(data, l);
  33. L[j++] = l;
  34. }
  35. ii = len - i;
  36. if (ii) {
  37. k = len & 0x07;
  38. c2ln(data, l, ll, k);
  39. L[j + 0] = l;
  40. L[j + 1] = ll;
  41. }
  42. c = (len + 3) / 4;
  43. t = (rounds + 1) * 2;
  44. S[0] = RC5_32_P;
  45. for (i = 1; i < t; i++)
  46. S[i] = (S[i - 1] + RC5_32_Q) & RC5_32_MASK;
  47. j = (t > c) ? t : c;
  48. j *= 3;
  49. ii = jj = 0;
  50. A = B = 0;
  51. for (i = 0; i < j; i++) {
  52. k = (S[ii] + A + B) & RC5_32_MASK;
  53. A = S[ii] = ROTATE_l32(k, 3);
  54. m = (int)(A + B);
  55. k = (L[jj] + A + B) & RC5_32_MASK;
  56. B = L[jj] = ROTATE_l32(k, m);
  57. if (++ii >= t)
  58. ii = 0;
  59. if (++jj >= c)
  60. jj = 0;
  61. }
  62. return 1;
  63. }