2
0

i_skey.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * IDEA low level APIs are deprecated for public use, but still ok for internal
  11. * use where we're using them to implement the higher level EVP interface, as is
  12. * the case here.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <openssl/idea.h>
  16. #include "idea_local.h"
  17. static IDEA_INT inverse(unsigned int xin);
  18. void IDEA_set_encrypt_key(const unsigned char *key, IDEA_KEY_SCHEDULE *ks)
  19. {
  20. int i;
  21. register IDEA_INT *kt, *kf, r0, r1, r2;
  22. kt = &(ks->data[0][0]);
  23. n2s(key, kt[0]);
  24. n2s(key, kt[1]);
  25. n2s(key, kt[2]);
  26. n2s(key, kt[3]);
  27. n2s(key, kt[4]);
  28. n2s(key, kt[5]);
  29. n2s(key, kt[6]);
  30. n2s(key, kt[7]);
  31. kf = kt;
  32. kt += 8;
  33. for (i = 0; i < 6; i++) {
  34. r2 = kf[1];
  35. r1 = kf[2];
  36. *(kt++) = ((r2 << 9) | (r1 >> 7)) & 0xffff;
  37. r0 = kf[3];
  38. *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
  39. r1 = kf[4];
  40. *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
  41. r0 = kf[5];
  42. *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
  43. r1 = kf[6];
  44. *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
  45. r0 = kf[7];
  46. *(kt++) = ((r1 << 9) | (r0 >> 7)) & 0xffff;
  47. r1 = kf[0];
  48. if (i >= 5)
  49. break;
  50. *(kt++) = ((r0 << 9) | (r1 >> 7)) & 0xffff;
  51. *(kt++) = ((r1 << 9) | (r2 >> 7)) & 0xffff;
  52. kf += 8;
  53. }
  54. }
  55. void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)
  56. {
  57. int r;
  58. register IDEA_INT *fp, *tp, t;
  59. tp = &(dk->data[0][0]);
  60. fp = &(ek->data[8][0]);
  61. for (r = 0; r < 9; r++) {
  62. *(tp++) = inverse(fp[0]);
  63. *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);
  64. *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);
  65. *(tp++) = inverse(fp[3]);
  66. if (r == 8)
  67. break;
  68. fp -= 6;
  69. *(tp++) = fp[4];
  70. *(tp++) = fp[5];
  71. }
  72. tp = &(dk->data[0][0]);
  73. t = tp[1];
  74. tp[1] = tp[2];
  75. tp[2] = t;
  76. t = tp[49];
  77. tp[49] = tp[50];
  78. tp[50] = t;
  79. }
  80. /* taken directly from the 'paper' I'll have a look at it later */
  81. static IDEA_INT inverse(unsigned int xin)
  82. {
  83. long n1, n2, q, r, b1, b2, t;
  84. if (xin == 0)
  85. b2 = 0;
  86. else {
  87. n1 = 0x10001;
  88. n2 = xin;
  89. b2 = 1;
  90. b1 = 0;
  91. do {
  92. r = (n1 % n2);
  93. q = (n1 - r) / n2;
  94. if (r == 0) {
  95. if (b2 < 0)
  96. b2 = 0x10001 + b2;
  97. } else {
  98. n1 = n2;
  99. n2 = r;
  100. t = b2;
  101. b2 = b1 - q * b2;
  102. b1 = t;
  103. }
  104. } while (r != 0);
  105. }
  106. return (IDEA_INT)b2;
  107. }