idea.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* idea.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef HAVE_IDEA
  26. #include <wolfssl/wolfcrypt/idea.h>
  27. #include <wolfssl/wolfcrypt/error-crypt.h>
  28. #include <wolfssl/wolfcrypt/logging.h>
  29. #ifdef NO_INLINE
  30. #include <wolfssl/wolfcrypt/misc.h>
  31. #else
  32. #define WOLFSSL_MISC_INCLUDED
  33. #include <wolfcrypt/src/misc.c>
  34. #endif
  35. /* multiplication of x and y modulo 2^16+1
  36. * IDEA specify a special case when an entry value is 0 ( x or y)
  37. * then it must be replaced by 2^16
  38. */
  39. static WC_INLINE word16 idea_mult(word16 x, word16 y)
  40. {
  41. long mul, res;
  42. mul = (long)x * (long)y;
  43. if (mul) {
  44. res = (mul & IDEA_MASK) - ((word32)mul >> 16);
  45. if (res <= 0)
  46. res += IDEA_MODULO;
  47. return (word16) (res & IDEA_MASK);
  48. }
  49. if (!x)
  50. return ((IDEA_MODULO - y) & IDEA_MASK);
  51. /* !y */
  52. return ((IDEA_MODULO - x) & IDEA_MASK);
  53. }
  54. /* compute 1/a modulo 2^16+1 using Extended euclidean algorithm
  55. * adapted from fp_invmod */
  56. static WC_INLINE word16 idea_invmod(word16 x)
  57. {
  58. int u, v, b, d;
  59. if (x <= 1)
  60. return x;
  61. u = IDEA_MODULO;
  62. v = x;
  63. d = 1;
  64. b = 0;
  65. do {
  66. while (!(u & 1)) {
  67. u >>= 1;
  68. if (b & 1)
  69. b -= IDEA_MODULO;
  70. b >>= 1;
  71. }
  72. while (!(v & 1)) {
  73. v >>= 1;
  74. if (d & 1) {
  75. d -= IDEA_MODULO;
  76. }
  77. d >>= 1;
  78. }
  79. if (u >= v) {
  80. u -= v;
  81. b -= d;
  82. } else {
  83. v -= u;
  84. d -= b;
  85. }
  86. } while (u != 0);
  87. /* d is now the inverse, put positive value if required */
  88. while (d < 0)
  89. d += IDEA_MODULO;
  90. /* d must be < IDEA_MODULO */
  91. while (d >= (int)IDEA_MODULO)
  92. d -= IDEA_MODULO;
  93. return (word16)(d & IDEA_MASK);
  94. }
  95. /* generate the 52 16-bits key sub-blocks from the 128 key */
  96. int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz,
  97. const byte *iv, int dir)
  98. {
  99. word16 idx = 0;
  100. word32 t;
  101. short i;
  102. if (idea == NULL || key == NULL || keySz != IDEA_KEY_SIZE ||
  103. (dir != IDEA_ENCRYPTION && dir != IDEA_DECRYPTION))
  104. return BAD_FUNC_ARG;
  105. /* initial key schedule for 0 -> 7 */
  106. for (i = 0; i < IDEA_ROUNDS; i++) {
  107. idea->skey[i] = (word16)key[idx++] << 8;
  108. idea->skey[i] |= (word16)key[idx++];
  109. }
  110. /* shift phase key schedule for 8 -> 51 */
  111. for (i = IDEA_ROUNDS; i < IDEA_SK_NUM; i++) {
  112. t = (word32)idea->skey[((i+1) & 7) ? i-7 : i-15] << 9;
  113. t |= (word32)idea->skey[((i+2) & 7) < 2 ? i-14 : i-6] >> 7;
  114. idea->skey[i] = (word16)(t & IDEA_MASK);
  115. }
  116. /* compute decryption key from encryption key */
  117. if (dir == IDEA_DECRYPTION) {
  118. word16 enckey[IDEA_SK_NUM];
  119. /* put encryption key in tmp buffer */
  120. XMEMCPY(enckey, idea->skey, sizeof(idea->skey));
  121. idx = 0;
  122. idea->skey[6*IDEA_ROUNDS] = idea_invmod(enckey[idx++]);
  123. idea->skey[6*IDEA_ROUNDS+1] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  124. idea->skey[6*IDEA_ROUNDS+2] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  125. idea->skey[6*IDEA_ROUNDS+3] = idea_invmod(enckey[idx++]);
  126. for (i = 6*(IDEA_ROUNDS-1); i >= 0; i -= 6) {
  127. idea->skey[i+4] = enckey[idx++];
  128. idea->skey[i+5] = enckey[idx++];
  129. idea->skey[i] = idea_invmod(enckey[idx++]);
  130. if (i) {
  131. idea->skey[i+2] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  132. idea->skey[i+1] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  133. }
  134. else {
  135. idea->skey[1] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  136. idea->skey[2] = (IDEA_2EXP16 - enckey[idx++]) & IDEA_MASK;
  137. }
  138. idea->skey[i+3] = idea_invmod(enckey[idx++]);
  139. }
  140. /* erase temporary buffer */
  141. ForceZero(enckey, sizeof(enckey));
  142. }
  143. /* set the iv */
  144. return wc_IdeaSetIV(idea, iv);
  145. }
  146. /* set the IV in the Idea key structure */
  147. int wc_IdeaSetIV(Idea *idea, const byte* iv)
  148. {
  149. if (idea == NULL)
  150. return BAD_FUNC_ARG;
  151. if (iv != NULL)
  152. XMEMCPY(idea->reg, iv, IDEA_BLOCK_SIZE);
  153. else
  154. XMEMSET(idea->reg, 0, IDEA_BLOCK_SIZE);
  155. return 0;
  156. }
  157. /* encryption/decryption for a block (64 bits)
  158. */
  159. int wc_IdeaCipher(Idea *idea, byte* out, const byte* in)
  160. {
  161. word32 t1, t2;
  162. word16 i, skey_idx = 0, idx = 0;
  163. word16 x[4];
  164. if (idea == NULL || out == NULL || in == NULL) {
  165. return BAD_FUNC_ARG;
  166. }
  167. /* put input byte block in word16 */
  168. for (i = 0; i < IDEA_BLOCK_SIZE/2; i++) {
  169. x[i] = (word16)in[idx++] << 8;
  170. x[i] |= (word16)in[idx++];
  171. }
  172. for (i = 0; i < IDEA_ROUNDS; i++) {
  173. x[0] = idea_mult(x[0], idea->skey[skey_idx++]);
  174. x[1] = ((word32)x[1] + (word32)idea->skey[skey_idx++]) & IDEA_MASK;
  175. x[2] = ((word32)x[2] + (word32)idea->skey[skey_idx++]) & IDEA_MASK;
  176. x[3] = idea_mult(x[3], idea->skey[skey_idx++]);
  177. t2 = x[0] ^ x[2];
  178. t2 = idea_mult((word16)t2, idea->skey[skey_idx++]);
  179. t1 = (t2 + (x[1] ^ x[3])) & IDEA_MASK;
  180. t1 = idea_mult((word16)t1, idea->skey[skey_idx++]);
  181. t2 = (t1 + t2) & IDEA_MASK;
  182. x[0] ^= t1;
  183. x[3] ^= t2;
  184. t2 ^= x[1];
  185. x[1] = x[2] ^ (word16)t1;
  186. x[2] = (word16)t2;
  187. }
  188. x[0] = idea_mult(x[0], idea->skey[skey_idx++]);
  189. out[0] = (x[0] >> 8) & 0xFF;
  190. out[1] = x[0] & 0xFF;
  191. x[2] = ((word32)x[2] + (word32)idea->skey[skey_idx++]) & IDEA_MASK;
  192. out[2] = (x[2] >> 8) & 0xFF;
  193. out[3] = x[2] & 0xFF;
  194. x[1] = ((word32)x[1] + (word32)idea->skey[skey_idx++]) & IDEA_MASK;
  195. out[4] = (x[1] >> 8) & 0xFF;
  196. out[5] = x[1] & 0xFF;
  197. x[3] = idea_mult(x[3], idea->skey[skey_idx++]);
  198. out[6] = (x[3] >> 8) & 0xFF;
  199. out[7] = x[3] & 0xFF;
  200. return 0;
  201. }
  202. int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len)
  203. {
  204. int blocks;
  205. int ret;
  206. if (idea == NULL || out == NULL || in == NULL)
  207. return BAD_FUNC_ARG;
  208. blocks = len / IDEA_BLOCK_SIZE;
  209. while (blocks--) {
  210. xorbuf((byte*)idea->reg, in, IDEA_BLOCK_SIZE);
  211. ret = wc_IdeaCipher(idea, (byte*)idea->reg, (byte*)idea->reg);
  212. if (ret != 0) {
  213. return ret;
  214. }
  215. XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE);
  216. out += IDEA_BLOCK_SIZE;
  217. in += IDEA_BLOCK_SIZE;
  218. }
  219. return 0;
  220. }
  221. int wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len)
  222. {
  223. int blocks;
  224. int ret;
  225. if (idea == NULL || out == NULL || in == NULL)
  226. return BAD_FUNC_ARG;
  227. blocks = len / IDEA_BLOCK_SIZE;
  228. while (blocks--) {
  229. XMEMCPY((byte*)idea->tmp, in, IDEA_BLOCK_SIZE);
  230. ret = wc_IdeaCipher(idea, out, (byte*)idea->tmp);
  231. if (ret != 0) {
  232. return ret;
  233. }
  234. xorbuf(out, (byte*)idea->reg, IDEA_BLOCK_SIZE);
  235. XMEMCPY(idea->reg, idea->tmp, IDEA_BLOCK_SIZE);
  236. out += IDEA_BLOCK_SIZE;
  237. in += IDEA_BLOCK_SIZE;
  238. }
  239. return 0;
  240. }
  241. #endif /* HAVE_IDEA */