idea.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* idea.c - IDEA function
  2. * Copyright 1997, 1998, 1999, 2001 Werner Koch (dd9jn)
  3. * Copyright 2013 g10 Code GmbH
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * WERNER KOCH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Werner Koch shall not be
  23. * used in advertising or otherwise to promote the sale, use or other dealings
  24. * in this Software without prior written authorization from Werner Koch.
  25. *
  26. * Patents on IDEA have expired:
  27. * Europe: EP0482154 on 2011-05-16,
  28. * Japan: JP3225440 on 2011-05-16,
  29. * U.S.: 5,214,703 on 2012-01-07.
  30. */
  31. /*
  32. * Please see http://www.noepatents.org/ to learn why software patents
  33. * are bad for society and what you can do to fight them.
  34. *
  35. * The code herein is based on the one from:
  36. * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
  37. * ISBN 0-471-11709-9.
  38. */
  39. #include <config.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <assert.h>
  44. #include "types.h" /* for byte and u32 typedefs */
  45. #include "g10lib.h"
  46. #include "cipher.h"
  47. #define IDEA_KEYSIZE 16
  48. #define IDEA_BLOCKSIZE 8
  49. #define IDEA_ROUNDS 8
  50. #define IDEA_KEYLEN (6*IDEA_ROUNDS+4)
  51. typedef struct {
  52. u16 ek[IDEA_KEYLEN];
  53. u16 dk[IDEA_KEYLEN];
  54. int have_dk;
  55. } IDEA_context;
  56. static const char *selftest(void);
  57. static u16
  58. mul_inv( u16 x )
  59. {
  60. u16 t0, t1;
  61. u16 q, y;
  62. if( x < 2 )
  63. return x;
  64. t1 = 0x10001UL / x;
  65. y = 0x10001UL % x;
  66. if( y == 1 )
  67. return (1-t1) & 0xffff;
  68. t0 = 1;
  69. do {
  70. q = x / y;
  71. x = x % y;
  72. t0 += q * t1;
  73. if( x == 1 )
  74. return t0;
  75. q = y / x;
  76. y = y % x;
  77. t1 += q * t0;
  78. } while( y != 1 );
  79. return (1-t1) & 0xffff;
  80. }
  81. static void
  82. expand_key( const byte *userkey, u16 *ek )
  83. {
  84. int i,j;
  85. for(j=0; j < 8; j++ ) {
  86. ek[j] = (*userkey << 8) + userkey[1];
  87. userkey += 2;
  88. }
  89. for(i=0; j < IDEA_KEYLEN; j++ ) {
  90. i++;
  91. ek[i+7] = ek[i&7] << 9 | ek[(i+1)&7] >> 7;
  92. ek += i & 8;
  93. i &= 7;
  94. }
  95. }
  96. static void
  97. invert_key( u16 *ek, u16 dk[IDEA_KEYLEN] )
  98. {
  99. int i;
  100. u16 t1, t2, t3;
  101. u16 temp[IDEA_KEYLEN];
  102. u16 *p = temp + IDEA_KEYLEN;
  103. t1 = mul_inv( *ek++ );
  104. t2 = -*ek++;
  105. t3 = -*ek++;
  106. *--p = mul_inv( *ek++ );
  107. *--p = t3;
  108. *--p = t2;
  109. *--p = t1;
  110. for(i=0; i < IDEA_ROUNDS-1; i++ ) {
  111. t1 = *ek++;
  112. *--p = *ek++;
  113. *--p = t1;
  114. t1 = mul_inv( *ek++ );
  115. t2 = -*ek++;
  116. t3 = -*ek++;
  117. *--p = mul_inv( *ek++ );
  118. *--p = t2;
  119. *--p = t3;
  120. *--p = t1;
  121. }
  122. t1 = *ek++;
  123. *--p = *ek++;
  124. *--p = t1;
  125. t1 = mul_inv( *ek++ );
  126. t2 = -*ek++;
  127. t3 = -*ek++;
  128. *--p = mul_inv( *ek++ );
  129. *--p = t3;
  130. *--p = t2;
  131. *--p = t1;
  132. memcpy(dk, temp, sizeof(temp) );
  133. memset(temp, 0, sizeof(temp) ); /* burn temp */
  134. }
  135. static void
  136. cipher( byte *outbuf, const byte *inbuf, u16 *key )
  137. {
  138. u16 s2, s3;
  139. u16 in[4];
  140. int r = IDEA_ROUNDS;
  141. #define x1 (in[0])
  142. #define x2 (in[1])
  143. #define x3 (in[2])
  144. #define x4 (in[3])
  145. #define MUL(x,y) \
  146. do {u16 _t16; u32 _t32; \
  147. if( (_t16 = (y)) ) { \
  148. if( (x = (x)&0xffff) ) { \
  149. _t32 = (u32)x * _t16; \
  150. x = _t32 & 0xffff; \
  151. _t16 = _t32 >> 16; \
  152. x = ((x)-_t16) + (x<_t16?1:0); \
  153. } \
  154. else { \
  155. x = 1 - _t16; \
  156. } \
  157. } \
  158. else { \
  159. x = 1 - x; \
  160. } \
  161. } while(0)
  162. memcpy (in, inbuf, sizeof in);
  163. #ifndef WORDS_BIGENDIAN
  164. x1 = (x1>>8) | (x1<<8);
  165. x2 = (x2>>8) | (x2<<8);
  166. x3 = (x3>>8) | (x3<<8);
  167. x4 = (x4>>8) | (x4<<8);
  168. #endif
  169. do {
  170. MUL(x1, *key++);
  171. x2 += *key++;
  172. x3 += *key++;
  173. MUL(x4, *key++ );
  174. s3 = x3;
  175. x3 ^= x1;
  176. MUL(x3, *key++);
  177. s2 = x2;
  178. x2 ^=x4;
  179. x2 += x3;
  180. MUL(x2, *key++);
  181. x3 += x2;
  182. x1 ^= x2;
  183. x4 ^= x3;
  184. x2 ^= s3;
  185. x3 ^= s2;
  186. } while( --r );
  187. MUL(x1, *key++);
  188. x3 += *key++;
  189. x2 += *key++;
  190. MUL(x4, *key);
  191. #ifndef WORDS_BIGENDIAN
  192. x1 = (x1>>8) | (x1<<8);
  193. x2 = (x2>>8) | (x2<<8);
  194. x3 = (x3>>8) | (x3<<8);
  195. x4 = (x4>>8) | (x4<<8);
  196. #endif
  197. memcpy (outbuf+0, &x1, 2);
  198. memcpy (outbuf+2, &x3, 2);
  199. memcpy (outbuf+4, &x2, 2);
  200. memcpy (outbuf+6, &x4, 2);
  201. #undef MUL
  202. #undef x1
  203. #undef x2
  204. #undef x3
  205. #undef x4
  206. }
  207. static int
  208. do_setkey( IDEA_context *c, const byte *key, unsigned int keylen )
  209. {
  210. static int initialized = 0;
  211. static const char *selftest_failed = 0;
  212. if( !initialized ) {
  213. initialized = 1;
  214. selftest_failed = selftest();
  215. if( selftest_failed )
  216. log_error( "%s\n", selftest_failed );
  217. }
  218. if( selftest_failed )
  219. return GPG_ERR_SELFTEST_FAILED;
  220. assert(keylen == 16);
  221. c->have_dk = 0;
  222. expand_key( key, c->ek );
  223. invert_key( c->ek, c->dk );
  224. return 0;
  225. }
  226. static gcry_err_code_t
  227. idea_setkey (void *context, const byte *key, unsigned int keylen)
  228. {
  229. IDEA_context *ctx = context;
  230. int rc = do_setkey (ctx, key, keylen);
  231. _gcry_burn_stack (23+6*sizeof(void*));
  232. return rc;
  233. }
  234. static void
  235. encrypt_block( IDEA_context *c, byte *outbuf, const byte *inbuf )
  236. {
  237. cipher( outbuf, inbuf, c->ek );
  238. }
  239. static void
  240. idea_encrypt (void *context, byte *out, const byte *in)
  241. {
  242. IDEA_context *ctx = context;
  243. encrypt_block (ctx, out, in);
  244. _gcry_burn_stack (24+3*sizeof (void*));
  245. }
  246. static void
  247. decrypt_block( IDEA_context *c, byte *outbuf, const byte *inbuf )
  248. {
  249. if( !c->have_dk ) {
  250. c->have_dk = 1;
  251. invert_key( c->ek, c->dk );
  252. }
  253. cipher( outbuf, inbuf, c->dk );
  254. }
  255. static void
  256. idea_decrypt (void *context, byte *out, const byte *in)
  257. {
  258. IDEA_context *ctx = context;
  259. decrypt_block (ctx, out, in);
  260. _gcry_burn_stack (24+3*sizeof (void*));
  261. }
  262. static const char *
  263. selftest( void )
  264. {
  265. static struct {
  266. byte key[16];
  267. byte plain[8];
  268. byte cipher[8];
  269. } test_vectors[] = {
  270. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  271. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  272. { 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03 },
  273. { 0x11, 0xFB, 0xED, 0x2B, 0x01, 0x98, 0x6D, 0xE5 } },
  274. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  275. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  276. { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
  277. { 0x54, 0x0E, 0x5F, 0xEA, 0x18, 0xC2, 0xF8, 0xB1 } },
  278. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  279. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  280. { 0x00, 0x19, 0x32, 0x4B, 0x64, 0x7D, 0x96, 0xAF },
  281. { 0x9F, 0x0A, 0x0A, 0xB6, 0xE1, 0x0C, 0xED, 0x78 } },
  282. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  283. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  284. { 0xF5, 0x20, 0x2D, 0x5B, 0x9C, 0x67, 0x1B, 0x08 },
  285. { 0xCF, 0x18, 0xFD, 0x73, 0x55, 0xE2, 0xC5, 0xC5 } },
  286. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  287. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  288. { 0xFA, 0xE6, 0xD2, 0xBE, 0xAA, 0x96, 0x82, 0x6E },
  289. { 0x85, 0xDF, 0x52, 0x00, 0x56, 0x08, 0x19, 0x3D } },
  290. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  291. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  292. { 0x0A, 0x14, 0x1E, 0x28, 0x32, 0x3C, 0x46, 0x50 },
  293. { 0x2F, 0x7D, 0xE7, 0x50, 0x21, 0x2F, 0xB7, 0x34 } },
  294. { { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04,
  295. 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08 },
  296. { 0x05, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x23, 0x28 },
  297. { 0x7B, 0x73, 0x14, 0x92, 0x5D, 0xE5, 0x9C, 0x09 } },
  298. { { 0x00, 0x05, 0x00, 0x0A, 0x00, 0x0F, 0x00, 0x14,
  299. 0x00, 0x19, 0x00, 0x1E, 0x00, 0x23, 0x00, 0x28 },
  300. { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
  301. { 0x3E, 0xC0, 0x47, 0x80, 0xBE, 0xFF, 0x6E, 0x20 } },
  302. { { 0x3A, 0x98, 0x4E, 0x20, 0x00, 0x19, 0x5D, 0xB3,
  303. 0x2E, 0xE5, 0x01, 0xC8, 0xC4, 0x7C, 0xEA, 0x60 },
  304. { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
  305. { 0x97, 0xBC, 0xD8, 0x20, 0x07, 0x80, 0xDA, 0x86 } },
  306. { { 0x00, 0x64, 0x00, 0xC8, 0x01, 0x2C, 0x01, 0x90,
  307. 0x01, 0xF4, 0x02, 0x58, 0x02, 0xBC, 0x03, 0x20 },
  308. { 0x05, 0x32, 0x0A, 0x64, 0x14, 0xC8, 0x19, 0xFA },
  309. { 0x65, 0xBE, 0x87, 0xE7, 0xA2, 0x53, 0x8A, 0xED } },
  310. { { 0x9D, 0x40, 0x75, 0xC1, 0x03, 0xBC, 0x32, 0x2A,
  311. 0xFB, 0x03, 0xE7, 0xBE, 0x6A, 0xB3, 0x00, 0x06 },
  312. { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 },
  313. { 0xF5, 0xDB, 0x1A, 0xC4, 0x5E, 0x5E, 0xF9, 0xF9 } }
  314. };
  315. IDEA_context c;
  316. byte buffer[8];
  317. int i;
  318. for(i=0; i < DIM(test_vectors); i++ ) {
  319. do_setkey( &c, test_vectors[i].key, 16 );
  320. encrypt_block( &c, buffer, test_vectors[i].plain );
  321. if( memcmp( buffer, test_vectors[i].cipher, 8 ) )
  322. return "IDEA test encryption failed.";
  323. decrypt_block( &c, buffer, test_vectors[i].cipher );
  324. if( memcmp( buffer, test_vectors[i].plain, 8 ) )
  325. return "IDEA test decryption failed.";
  326. }
  327. return NULL;
  328. }
  329. gcry_cipher_spec_t _gcry_cipher_spec_idea =
  330. {
  331. "IDEA", NULL, NULL, IDEA_BLOCKSIZE, 128,
  332. sizeof (IDEA_context),
  333. idea_setkey, idea_encrypt, idea_decrypt
  334. };