twofish.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* Implementation of Twofish by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef TWOFISH
  14. /* first TWOFISH_ALL_TABLES must ensure TWOFISH_TABLES is defined */
  15. #ifdef TWOFISH_ALL_TABLES
  16. #ifndef TWOFISH_TABLES
  17. #define TWOFISH_TABLES
  18. #endif
  19. #endif
  20. const struct _cipher_descriptor twofish_desc =
  21. {
  22. "twofish",
  23. 7,
  24. 16, 32, 16, 16,
  25. &twofish_setup,
  26. &twofish_ecb_encrypt,
  27. &twofish_ecb_decrypt,
  28. &twofish_test,
  29. &twofish_keysize
  30. };
  31. /* the two polynomials */
  32. #define MDS_POLY 0x169
  33. #define RS_POLY 0x14D
  34. /* The 4x4 MDS Linear Transform */
  35. static const unsigned char MDS[4][4] = {
  36. { 0x01, 0xEF, 0x5B, 0x5B },
  37. { 0x5B, 0xEF, 0xEF, 0x01 },
  38. { 0xEF, 0x5B, 0x01, 0xEF },
  39. { 0xEF, 0x01, 0xEF, 0x5B }
  40. };
  41. /* The 4x8 RS Linear Transform */
  42. static const unsigned char RS[4][8] = {
  43. { 0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E },
  44. { 0xA4, 0x56, 0x82, 0xF3, 0X1E, 0XC6, 0X68, 0XE5 },
  45. { 0X02, 0XA1, 0XFC, 0XC1, 0X47, 0XAE, 0X3D, 0X19 },
  46. { 0XA4, 0X55, 0X87, 0X5A, 0X58, 0XDB, 0X9E, 0X03 }
  47. };
  48. /* sbox usage orderings */
  49. static const unsigned char qord[4][5] = {
  50. { 1, 1, 0, 0, 1 },
  51. { 0, 1, 1, 0, 0 },
  52. { 0, 0, 0, 1, 1 },
  53. { 1, 0, 1, 1, 0 }
  54. };
  55. #ifdef TWOFISH_TABLES
  56. #include "twofish_tab.c"
  57. #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
  58. #else
  59. /* The Q-box tables */
  60. static const unsigned char qbox[2][4][16] = {
  61. {
  62. { 0x8, 0x1, 0x7, 0xD, 0x6, 0xF, 0x3, 0x2, 0x0, 0xB, 0x5, 0x9, 0xE, 0xC, 0xA, 0x4 },
  63. { 0xE, 0XC, 0XB, 0X8, 0X1, 0X2, 0X3, 0X5, 0XF, 0X4, 0XA, 0X6, 0X7, 0X0, 0X9, 0XD },
  64. { 0XB, 0XA, 0X5, 0XE, 0X6, 0XD, 0X9, 0X0, 0XC, 0X8, 0XF, 0X3, 0X2, 0X4, 0X7, 0X1 },
  65. { 0XD, 0X7, 0XF, 0X4, 0X1, 0X2, 0X6, 0XE, 0X9, 0XB, 0X3, 0X0, 0X8, 0X5, 0XC, 0XA }
  66. },
  67. {
  68. { 0X2, 0X8, 0XB, 0XD, 0XF, 0X7, 0X6, 0XE, 0X3, 0X1, 0X9, 0X4, 0X0, 0XA, 0XC, 0X5 },
  69. { 0X1, 0XE, 0X2, 0XB, 0X4, 0XC, 0X3, 0X7, 0X6, 0XD, 0XA, 0X5, 0XF, 0X9, 0X0, 0X8 },
  70. { 0X4, 0XC, 0X7, 0X5, 0X1, 0X6, 0X9, 0XA, 0X0, 0XE, 0XD, 0X8, 0X2, 0XB, 0X3, 0XF },
  71. { 0xB, 0X9, 0X5, 0X1, 0XC, 0X3, 0XD, 0XE, 0X6, 0X4, 0X7, 0XF, 0X2, 0X0, 0X8, 0XA }
  72. }
  73. };
  74. /* computes S_i[x] */
  75. #ifdef CLEAN_STACK
  76. static ulong32 _sbox(int i, ulong32 x)
  77. #else
  78. static ulong32 sbox(int i, ulong32 x)
  79. #endif
  80. {
  81. unsigned char a0,b0,a1,b1,a2,b2,a3,b3,a4,b4,y;
  82. /* a0,b0 = [x/16], x mod 16 */
  83. a0 = (unsigned char)((x>>4)&15);
  84. b0 = (unsigned char)((x)&15);
  85. /* a1 = a0 ^ b0 */
  86. a1 = a0 ^ b0;
  87. /* b1 = a0 ^ ROR(b0, 1) ^ 8a0 */
  88. b1 = (a0 ^ ((b0<<3)|(b0>>1)) ^ (a0<<3)) & 15;
  89. /* a2,b2 = t0[a1], t1[b1] */
  90. a2 = qbox[i][0][(int)a1];
  91. b2 = qbox[i][1][(int)b1];
  92. /* a3 = a2 ^ b2 */
  93. a3 = a2 ^ b2;
  94. /* b3 = a2 ^ ROR(b2, 1) ^ 8a2 */
  95. b3 = (a2 ^ ((b2<<3)|(b2>>1)) ^ (a2<<3)) & 15;
  96. /* a4,b4 = t2[a3], t3[b3] */
  97. a4 = qbox[i][2][(int)a3];
  98. b4 = qbox[i][3][(int)b3];
  99. /* y = 16b4 + a4 */
  100. y = (b4 << 4) + a4;
  101. /* return result */
  102. return (ulong32)y;
  103. }
  104. #ifdef CLEAN_STACK
  105. static ulong32 sbox(int i, ulong32 x)
  106. {
  107. ulong32 y;
  108. y = _sbox(i, x);
  109. burn_stack(sizeof(unsigned char) * 11);
  110. return y;
  111. }
  112. #endif /* CLEAN_STACK */
  113. #endif /* TWOFISH_TABLES */
  114. /* computes ab mod p */
  115. static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
  116. {
  117. ulong32 result, B[2], P[2];
  118. P[1] = p;
  119. B[1] = b;
  120. result = P[0] = B[0] = 0;
  121. /* unrolled branchless GF multiplier */
  122. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  123. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  124. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  125. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  126. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  127. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  128. result ^= B[a&1]; a >>= 1; B[1] = P[B[1]>>7] ^ (B[1] << 1);
  129. result ^= B[a&1];
  130. return result;
  131. }
  132. /* computes [y0 y1 y2 y3] = MDS . [x0] */
  133. #ifndef TWOFISH_TABLES
  134. static ulong32 mds_column_mult(unsigned char in, int col)
  135. {
  136. ulong32 x01, x5B, xEF;
  137. x01 = in;
  138. x5B = gf_mult(in, 0x5B, MDS_POLY);
  139. xEF = gf_mult(in, 0xEF, MDS_POLY);
  140. switch (col) {
  141. case 0:
  142. return (x01 << 0 ) |
  143. (x5B << 8 ) |
  144. (xEF << 16) |
  145. (xEF << 24);
  146. case 1:
  147. return (xEF << 0 ) |
  148. (xEF << 8 ) |
  149. (x5B << 16) |
  150. (x01 << 24);
  151. case 2:
  152. return (x5B << 0 ) |
  153. (xEF << 8 ) |
  154. (x01 << 16) |
  155. (xEF << 24);
  156. case 3:
  157. return (x5B << 0 ) |
  158. (x01 << 8 ) |
  159. (xEF << 16) |
  160. (x5B << 24);
  161. }
  162. /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
  163. return 0;
  164. }
  165. #else /* !TWOFISH_TABLES */
  166. #define mds_column_mult(x, i) mds_tab[i][x]
  167. #endif /* TWOFISH_TABLES */
  168. /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
  169. static void mds_mult(const unsigned char *in, unsigned char *out)
  170. {
  171. int x;
  172. ulong32 tmp;
  173. for (tmp = x = 0; x < 4; x++) {
  174. tmp ^= mds_column_mult(in[x], x);
  175. }
  176. STORE32L(tmp, out);
  177. }
  178. #ifdef TWOFISH_ALL_TABLES
  179. /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
  180. static void rs_mult(const unsigned char *in, unsigned char *out)
  181. {
  182. ulong32 tmp;
  183. tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
  184. rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
  185. STORE32L(tmp, out);
  186. }
  187. #else /* !TWOFISH_ALL_TABLES */
  188. /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
  189. static void rs_mult(const unsigned char *in, unsigned char *out)
  190. {
  191. int x, y;
  192. for (x = 0; x < 4; x++) {
  193. out[x] = 0;
  194. for (y = 0; y < 8; y++) {
  195. out[x] ^= gf_mult(in[y], RS[x][y], RS_POLY);
  196. }
  197. }
  198. }
  199. #endif
  200. /* computes h(x) */
  201. static void h_func(const unsigned char *in, unsigned char *out, unsigned char *M, int k, int offset)
  202. {
  203. int x;
  204. unsigned char y[4];
  205. for (x = 0; x < 4; x++) {
  206. y[x] = in[x];
  207. }
  208. switch (k) {
  209. case 4:
  210. y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (6 + offset) + 0]);
  211. y[1] = (unsigned char)(sbox(0, (ulong32)y[1]) ^ M[4 * (6 + offset) + 1]);
  212. y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (6 + offset) + 2]);
  213. y[3] = (unsigned char)(sbox(1, (ulong32)y[3]) ^ M[4 * (6 + offset) + 3]);
  214. case 3:
  215. y[0] = (unsigned char)(sbox(1, (ulong32)y[0]) ^ M[4 * (4 + offset) + 0]);
  216. y[1] = (unsigned char)(sbox(1, (ulong32)y[1]) ^ M[4 * (4 + offset) + 1]);
  217. y[2] = (unsigned char)(sbox(0, (ulong32)y[2]) ^ M[4 * (4 + offset) + 2]);
  218. y[3] = (unsigned char)(sbox(0, (ulong32)y[3]) ^ M[4 * (4 + offset) + 3]);
  219. case 2:
  220. y[0] = (unsigned char)(sbox(1, sbox(0, sbox(0, (ulong32)y[0]) ^ M[4 * (2 + offset) + 0]) ^ M[4 * (0 + offset) + 0]));
  221. y[1] = (unsigned char)(sbox(0, sbox(0, sbox(1, (ulong32)y[1]) ^ M[4 * (2 + offset) + 1]) ^ M[4 * (0 + offset) + 1]));
  222. y[2] = (unsigned char)(sbox(1, sbox(1, sbox(0, (ulong32)y[2]) ^ M[4 * (2 + offset) + 2]) ^ M[4 * (0 + offset) + 2]));
  223. y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
  224. }
  225. mds_mult(y, out);
  226. }
  227. #ifndef TWOFISH_SMALL
  228. /* for GCC we don't use pointer aliases */
  229. #if defined(__GNUC__)
  230. #define S1 key->twofish.S[0]
  231. #define S2 key->twofish.S[1]
  232. #define S3 key->twofish.S[2]
  233. #define S4 key->twofish.S[3]
  234. #endif
  235. /* the G function */
  236. #define g_func(x, dum) (S1[byte(x,0)] ^ S2[byte(x,1)] ^ S3[byte(x,2)] ^ S4[byte(x,3)])
  237. #define g1_func(x, dum) (S2[byte(x,0)] ^ S3[byte(x,1)] ^ S4[byte(x,2)] ^ S1[byte(x,3)])
  238. #else
  239. #ifdef CLEAN_STACK
  240. static ulong32 _g_func(ulong32 x, symmetric_key *key)
  241. #else
  242. static ulong32 g_func(ulong32 x, symmetric_key *key)
  243. #endif
  244. {
  245. unsigned char g, i, y, z;
  246. ulong32 res;
  247. res = 0;
  248. for (y = 0; y < 4; y++) {
  249. z = key->twofish.start;
  250. /* do unkeyed substitution */
  251. g = sbox(qord[y][z++], (x >> (8*y)) & 255);
  252. /* first subkey */
  253. i = 0;
  254. /* do key mixing+sbox until z==5 */
  255. while (z != 5) {
  256. g = g ^ key->twofish.S[4*i++ + y];
  257. g = sbox(qord[y][z++], g);
  258. }
  259. /* multiply g by a column of the MDS */
  260. res ^= mds_column_mult(g, y);
  261. }
  262. return res;
  263. }
  264. #define g1_func(x, key) g_func(ROL(x, 8), key)
  265. #ifdef CLEAN_STACK
  266. static ulong32 g_func(ulong32 x, symmetric_key *key)
  267. {
  268. ulong32 y;
  269. y = _g_func(x, key);
  270. burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
  271. return y;
  272. }
  273. #endif /* CLEAN_STACK */
  274. #endif /* TWOFISH_SMALL */
  275. #ifdef CLEAN_STACK
  276. static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  277. #else
  278. int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  279. #endif
  280. {
  281. #ifndef TWOFISH_SMALL
  282. unsigned char S[4*4], tmpx0, tmpx1;
  283. #endif
  284. int k, x, y;
  285. unsigned char tmp[4], tmp2[4], M[8*4];
  286. ulong32 A, B;
  287. _ARGCHK(key != NULL);
  288. _ARGCHK(skey != NULL);
  289. /* invalid arguments? */
  290. if (num_rounds != 16 && num_rounds != 0) {
  291. return CRYPT_INVALID_ROUNDS;
  292. }
  293. if (keylen != 16 && keylen != 24 && keylen != 32) {
  294. return CRYPT_INVALID_KEYSIZE;
  295. }
  296. /* k = keysize/64 [but since our keysize is in bytes...] */
  297. k = keylen / 8;
  298. /* copy the key into M */
  299. for (x = 0; x < keylen; x++) {
  300. M[x] = key[x] & 255;
  301. }
  302. /* create the S[..] words */
  303. #ifndef TWOFISH_SMALL
  304. for (x = 0; x < k; x++) {
  305. rs_mult(M+(x*8), S+(x*4));
  306. }
  307. #else
  308. for (x = 0; x < k; x++) {
  309. rs_mult(M+(x*8), skey->twofish.S+(x*4));
  310. }
  311. #endif
  312. /* make subkeys */
  313. for (x = 0; x < 20; x++) {
  314. /* A = h(p * 2x, Me) */
  315. for (y = 0; y < 4; y++) {
  316. tmp[y] = x+x;
  317. }
  318. h_func(tmp, tmp2, M, k, 0);
  319. LOAD32L(A, tmp2);
  320. /* B = ROL(h(p * (2x + 1), Mo), 8) */
  321. for (y = 0; y < 4; y++) {
  322. tmp[y] = (unsigned char)(x+x+1);
  323. }
  324. h_func(tmp, tmp2, M, k, 1);
  325. LOAD32L(B, tmp2);
  326. B = ROL(B, 8);
  327. /* K[2i] = A + B */
  328. skey->twofish.K[x+x] = (A + B) & 0xFFFFFFFFUL;
  329. /* K[2i+1] = (A + 2B) <<< 9 */
  330. skey->twofish.K[x+x+1] = ROL(B + B + A, 9);
  331. }
  332. #ifndef TWOFISH_SMALL
  333. /* make the sboxes (large ram variant) */
  334. if (k == 2) {
  335. for (x = 0; x < 256; x++) {
  336. tmpx0 = sbox(0, x);
  337. tmpx1 = sbox(1, x);
  338. skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, tmpx0 ^ S[0]) ^ S[4])),0);
  339. skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, tmpx1 ^ S[1]) ^ S[5])),1);
  340. skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, tmpx0 ^ S[2]) ^ S[6])),2);
  341. skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, tmpx1 ^ S[3]) ^ S[7])),3);
  342. }
  343. } else if (k == 3) {
  344. for (x = 0; x < 256; x++) {
  345. tmpx0 = sbox(0, x);
  346. tmpx1 = sbox(1, x);
  347. skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, tmpx1 ^ S[0]) ^ S[4]) ^ S[8])),0);
  348. skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, tmpx1 ^ S[1]) ^ S[5]) ^ S[9])),1);
  349. skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10])),2);
  350. skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, tmpx0 ^ S[3]) ^ S[7]) ^ S[11])),3);
  351. }
  352. } else {
  353. for (x = 0; x < 256; x++) {
  354. tmpx0 = sbox(0, x);
  355. tmpx1 = sbox(1, x);
  356. skey->twofish.S[0][x] = mds_column_mult(sbox(1, (sbox(0, sbox(0, sbox(1, tmpx1 ^ S[0]) ^ S[4]) ^ S[8]) ^ S[12])),0);
  357. skey->twofish.S[1][x] = mds_column_mult(sbox(0, (sbox(0, sbox(1, sbox(1, tmpx0 ^ S[1]) ^ S[5]) ^ S[9]) ^ S[13])),1);
  358. skey->twofish.S[2][x] = mds_column_mult(sbox(1, (sbox(1, sbox(0, sbox(0, tmpx0 ^ S[2]) ^ S[6]) ^ S[10]) ^ S[14])),2);
  359. skey->twofish.S[3][x] = mds_column_mult(sbox(0, (sbox(1, sbox(1, sbox(0, tmpx1 ^ S[3]) ^ S[7]) ^ S[11]) ^ S[15])),3);
  360. }
  361. }
  362. #else
  363. /* where to start in the sbox layers */
  364. /* small ram variant */
  365. switch (k) {
  366. case 4 : skey->twofish.start = 0; break;
  367. case 3 : skey->twofish.start = 1; break;
  368. default: skey->twofish.start = 2; break;
  369. }
  370. #endif
  371. return CRYPT_OK;
  372. }
  373. #ifdef CLEAN_STACK
  374. int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
  375. {
  376. int x;
  377. x = _twofish_setup(key, keylen, num_rounds, skey);
  378. burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
  379. return x;
  380. }
  381. #endif
  382. #ifdef CLEAN_STACK
  383. static void _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  384. #else
  385. void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  386. #endif
  387. {
  388. ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
  389. int r;
  390. #if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
  391. ulong32 *S1, *S2, *S3, *S4;
  392. #endif
  393. _ARGCHK(pt != NULL);
  394. _ARGCHK(ct != NULL);
  395. _ARGCHK(key != NULL);
  396. #if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
  397. S1 = key->twofish.S[0];
  398. S2 = key->twofish.S[1];
  399. S3 = key->twofish.S[2];
  400. S4 = key->twofish.S[3];
  401. #endif
  402. LOAD32L(a,&pt[0]); LOAD32L(b,&pt[4]);
  403. LOAD32L(c,&pt[8]); LOAD32L(d,&pt[12]);
  404. a ^= key->twofish.K[0];
  405. b ^= key->twofish.K[1];
  406. c ^= key->twofish.K[2];
  407. d ^= key->twofish.K[3];
  408. k = key->twofish.K + 8;
  409. for (r = 8; r != 0; --r) {
  410. t2 = g1_func(b, key);
  411. t1 = g_func(a, key) + t2;
  412. c = ROR(c ^ (t1 + k[0]), 1);
  413. d = ROL(d, 1) ^ (t2 + t1 + k[1]);
  414. t2 = g1_func(d, key);
  415. t1 = g_func(c, key) + t2;
  416. a = ROR(a ^ (t1 + k[2]), 1);
  417. b = ROL(b, 1) ^ (t2 + t1 + k[3]);
  418. k += 4;
  419. }
  420. /* output with "undo last swap" */
  421. ta = c ^ key->twofish.K[4];
  422. tb = d ^ key->twofish.K[5];
  423. tc = a ^ key->twofish.K[6];
  424. td = b ^ key->twofish.K[7];
  425. /* store output */
  426. STORE32L(ta,&ct[0]); STORE32L(tb,&ct[4]);
  427. STORE32L(tc,&ct[8]); STORE32L(td,&ct[12]);
  428. }
  429. #ifdef CLEAN_STACK
  430. void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
  431. {
  432. _twofish_ecb_encrypt(pt, ct, key);
  433. burn_stack(sizeof(ulong32) * 10 + sizeof(int));
  434. }
  435. #endif
  436. #ifdef CLEAN_STACK
  437. static void _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  438. #else
  439. void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  440. #endif
  441. {
  442. ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
  443. int r;
  444. #if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
  445. ulong32 *S1, *S2, *S3, *S4;
  446. #endif
  447. _ARGCHK(pt != NULL);
  448. _ARGCHK(ct != NULL);
  449. _ARGCHK(key != NULL);
  450. #if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
  451. S1 = key->twofish.S[0];
  452. S2 = key->twofish.S[1];
  453. S3 = key->twofish.S[2];
  454. S4 = key->twofish.S[3];
  455. #endif
  456. /* load input */
  457. LOAD32L(ta,&ct[0]); LOAD32L(tb,&ct[4]);
  458. LOAD32L(tc,&ct[8]); LOAD32L(td,&ct[12]);
  459. /* undo undo final swap */
  460. a = tc ^ key->twofish.K[6];
  461. b = td ^ key->twofish.K[7];
  462. c = ta ^ key->twofish.K[4];
  463. d = tb ^ key->twofish.K[5];
  464. k = key->twofish.K + 36;
  465. for (r = 8; r != 0; --r) {
  466. t2 = g1_func(d, key);
  467. t1 = g_func(c, key) + t2;
  468. a = ROL(a, 1) ^ (t1 + k[2]);
  469. b = ROR(b ^ (t2 + t1 + k[3]), 1);
  470. t2 = g1_func(b, key);
  471. t1 = g_func(a, key) + t2;
  472. c = ROL(c, 1) ^ (t1 + k[0]);
  473. d = ROR(d ^ (t2 + t1 + k[1]), 1);
  474. k -= 4;
  475. }
  476. /* pre-white */
  477. a ^= key->twofish.K[0];
  478. b ^= key->twofish.K[1];
  479. c ^= key->twofish.K[2];
  480. d ^= key->twofish.K[3];
  481. /* store */
  482. STORE32L(a, &pt[0]); STORE32L(b, &pt[4]);
  483. STORE32L(c, &pt[8]); STORE32L(d, &pt[12]);
  484. }
  485. #ifdef CLEAN_STACK
  486. void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
  487. {
  488. _twofish_ecb_decrypt(ct, pt, key);
  489. burn_stack(sizeof(ulong32) * 10 + sizeof(int));
  490. }
  491. #endif
  492. int twofish_test(void)
  493. {
  494. #ifndef LTC_TEST
  495. return CRYPT_NOP;
  496. #else
  497. static const struct {
  498. int keylen;
  499. unsigned char key[32], pt[16], ct[16];
  500. } tests[] = {
  501. { 16,
  502. { 0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32,
  503. 0xB6, 0xBF, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A },
  504. { 0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E,
  505. 0x86, 0xCB, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19 },
  506. { 0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85,
  507. 0x8F, 0xAA, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3 }
  508. }, {
  509. 24,
  510. { 0x88, 0xB2, 0xB2, 0x70, 0x6B, 0x10, 0x5E, 0x36,
  511. 0xB4, 0x46, 0xBB, 0x6D, 0x73, 0x1A, 0x1E, 0x88,
  512. 0xEF, 0xA7, 0x1F, 0x78, 0x89, 0x65, 0xBD, 0x44 },
  513. { 0x39, 0xDA, 0x69, 0xD6, 0xBA, 0x49, 0x97, 0xD5,
  514. 0x85, 0xB6, 0xDC, 0x07, 0x3C, 0xA3, 0x41, 0xB2 },
  515. { 0x18, 0x2B, 0x02, 0xD8, 0x14, 0x97, 0xEA, 0x45,
  516. 0xF9, 0xDA, 0xAC, 0xDC, 0x29, 0x19, 0x3A, 0x65 }
  517. }, {
  518. 32,
  519. { 0xD4, 0x3B, 0xB7, 0x55, 0x6E, 0xA3, 0x2E, 0x46,
  520. 0xF2, 0xA2, 0x82, 0xB7, 0xD4, 0x5B, 0x4E, 0x0D,
  521. 0x57, 0xFF, 0x73, 0x9D, 0x4D, 0xC9, 0x2C, 0x1B,
  522. 0xD7, 0xFC, 0x01, 0x70, 0x0C, 0xC8, 0x21, 0x6F },
  523. { 0x90, 0xAF, 0xE9, 0x1B, 0xB2, 0x88, 0x54, 0x4F,
  524. 0x2C, 0x32, 0xDC, 0x23, 0x9B, 0x26, 0x35, 0xE6 },
  525. { 0x6C, 0xB4, 0x56, 0x1C, 0x40, 0xBF, 0x0A, 0x97,
  526. 0x05, 0x93, 0x1C, 0xB6, 0xD4, 0x08, 0xE7, 0xFA }
  527. }
  528. };
  529. symmetric_key key;
  530. unsigned char tmp[2][16];
  531. int err, i, y;
  532. for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
  533. if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
  534. return err;
  535. }
  536. twofish_ecb_encrypt(tests[i].pt, tmp[0], &key);
  537. twofish_ecb_decrypt(tmp[0], tmp[1], &key);
  538. if (memcmp(tmp[0], tests[i].ct, 16) != 0 || memcmp(tmp[1], tests[i].pt, 16) != 0) {
  539. return CRYPT_FAIL_TESTVECTOR;
  540. }
  541. /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
  542. for (y = 0; y < 16; y++) tmp[0][y] = 0;
  543. for (y = 0; y < 1000; y++) twofish_ecb_encrypt(tmp[0], tmp[0], &key);
  544. for (y = 0; y < 1000; y++) twofish_ecb_decrypt(tmp[0], tmp[0], &key);
  545. for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
  546. }
  547. return CRYPT_OK;
  548. #endif
  549. }
  550. int twofish_keysize(int *desired_keysize)
  551. {
  552. _ARGCHK(desired_keysize);
  553. if (*desired_keysize < 16)
  554. return CRYPT_INVALID_KEYSIZE;
  555. if (*desired_keysize < 24) {
  556. *desired_keysize = 16;
  557. return CRYPT_OK;
  558. } else if (*desired_keysize < 32) {
  559. *desired_keysize = 24;
  560. return CRYPT_OK;
  561. } else {
  562. *desired_keysize = 32;
  563. return CRYPT_OK;
  564. }
  565. }
  566. #endif