eax_test.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* EAX Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef EAX_MODE
  14. int eax_test(void)
  15. {
  16. #ifndef LTC_TEST
  17. return CRYPT_NOP;
  18. #else
  19. static const struct {
  20. int keylen,
  21. noncelen,
  22. headerlen,
  23. msglen;
  24. unsigned char key[MAXBLOCKSIZE],
  25. nonce[MAXBLOCKSIZE],
  26. header[MAXBLOCKSIZE],
  27. plaintext[MAXBLOCKSIZE],
  28. ciphertext[MAXBLOCKSIZE],
  29. tag[MAXBLOCKSIZE];
  30. } tests[] = {
  31. /* NULL message */
  32. {
  33. 16, 0, 0, 0,
  34. /* key */
  35. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  36. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  37. /* nonce */
  38. { 0 },
  39. /* header */
  40. { 0 },
  41. /* plaintext */
  42. { 0 },
  43. /* ciphertext */
  44. { 0 },
  45. /* tag */
  46. { 0x9a, 0xd0, 0x7e, 0x7d, 0xbf, 0xf3, 0x01, 0xf5,
  47. 0x05, 0xde, 0x59, 0x6b, 0x96, 0x15, 0xdf, 0xff }
  48. },
  49. /* test with nonce */
  50. {
  51. 16, 16, 0, 0,
  52. /* key */
  53. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  54. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  55. /* nonce */
  56. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  57. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  58. /* header */
  59. { 0 },
  60. /* plaintext */
  61. { 0 },
  62. /* ciphertext */
  63. { 0 },
  64. /* tag */
  65. { 0x1c, 0xe1, 0x0d, 0x3e, 0xff, 0xd4, 0xca, 0xdb,
  66. 0xe2, 0xe4, 0x4b, 0x58, 0xd6, 0x0a, 0xb9, 0xec }
  67. },
  68. /* test with header [no nonce] */
  69. {
  70. 16, 0, 16, 0,
  71. /* key */
  72. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  73. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  74. /* nonce */
  75. { 0 },
  76. /* header */
  77. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  78. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  79. /* plaintext */
  80. { 0 },
  81. /* ciphertext */
  82. { 0 },
  83. /* tag */
  84. { 0x3a, 0x69, 0x8f, 0x7a, 0x27, 0x0e, 0x51, 0xb0,
  85. 0xf6, 0x5b, 0x3d, 0x3e, 0x47, 0x19, 0x3c, 0xff }
  86. },
  87. /* test with header + nonce + plaintext */
  88. {
  89. 16, 16, 16, 32,
  90. /* key */
  91. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  92. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  93. /* nonce */
  94. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  95. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  96. /* header */
  97. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  98. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  99. /* plaintext */
  100. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  101. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  102. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  103. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  104. /* ciphertext */
  105. { 0x29, 0xd8, 0x78, 0xd1, 0xa3, 0xbe, 0x85, 0x7b,
  106. 0x6f, 0xb8, 0xc8, 0xea, 0x59, 0x50, 0xa7, 0x78,
  107. 0x33, 0x1f, 0xbf, 0x2c, 0xcf, 0x33, 0x98, 0x6f,
  108. 0x35, 0xe8, 0xcf, 0x12, 0x1d, 0xcb, 0x30, 0xbc },
  109. /* tag */
  110. { 0x4f, 0xbe, 0x03, 0x38, 0xbe, 0x1c, 0x8c, 0x7e,
  111. 0x1d, 0x7a, 0xe7, 0xe4, 0x5b, 0x92, 0xc5, 0x87 }
  112. },
  113. /* test with header + nonce + plaintext [not even sizes!] */
  114. {
  115. 16, 15, 14, 29,
  116. /* key */
  117. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  118. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  119. /* nonce */
  120. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  121. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e },
  122. /* header */
  123. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  124. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d },
  125. /* plaintext */
  126. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  127. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  128. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  129. 0x18, 0x19, 0x1a, 0x1b, 0x1c },
  130. /* ciphertext */
  131. { 0xdd, 0x25, 0xc7, 0x54, 0xc5, 0xb1, 0x7c, 0x59,
  132. 0x28, 0xb6, 0x9b, 0x73, 0x15, 0x5f, 0x7b, 0xb8,
  133. 0x88, 0x8f, 0xaf, 0x37, 0x09, 0x1a, 0xd9, 0x2c,
  134. 0x8a, 0x24, 0xdb, 0x86, 0x8b },
  135. /* tag */
  136. { 0x0d, 0x1a, 0x14, 0xe5, 0x22, 0x24, 0xff, 0xd2,
  137. 0x3a, 0x05, 0xfa, 0x02, 0xcd, 0xef, 0x52, 0xda }
  138. },
  139. /* Vectors from Brian Gladman */
  140. {
  141. 16, 16, 8, 0,
  142. /* key */
  143. { 0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  144. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78 },
  145. /* nonce */
  146. { 0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  147. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3 },
  148. /* header */
  149. { 0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b },
  150. /* PT */
  151. { 0x00 },
  152. /* CT */
  153. { 0x00 },
  154. /* tag */
  155. { 0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  156. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01 }
  157. },
  158. {
  159. 16, 16, 8, 2,
  160. /* key */
  161. { 0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  162. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4 },
  163. /* nonce */
  164. { 0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  165. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd },
  166. /* header */
  167. { 0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa },
  168. /* PT */
  169. { 0xf7, 0xfb },
  170. /* CT */
  171. { 0x19, 0xdd },
  172. /* tag */
  173. { 0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  174. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5 }
  175. },
  176. {
  177. 16, 16, 8, 5,
  178. /* key */
  179. { 0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  180. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23 },
  181. /* nonce */
  182. { 0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  183. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e },
  184. /* header */
  185. { 0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6 },
  186. /* PT */
  187. { 0x1a, 0x47, 0xcb, 0x49, 0x33 },
  188. /* CT */
  189. { 0xd8, 0x51, 0xd5, 0xba, 0xe0 },
  190. /* Tag */
  191. { 0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  192. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80 }
  193. }
  194. };
  195. int err, x, idx, res;
  196. unsigned long len;
  197. unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE];
  198. /* AES can be under rijndael or aes... try to find it */
  199. if ((idx = find_cipher("aes")) == -1) {
  200. if ((idx = find_cipher("rijndael")) == -1) {
  201. return CRYPT_NOP;
  202. }
  203. }
  204. for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
  205. len = sizeof(outtag);
  206. if ((err = eax_encrypt_authenticate_memory(idx, tests[x].key, tests[x].keylen,
  207. tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen,
  208. tests[x].plaintext, tests[x].msglen, outct, outtag, &len)) != CRYPT_OK) {
  209. return err;
  210. }
  211. if (memcmp(outct, tests[x].ciphertext, tests[x].msglen) || memcmp(outtag, tests[x].tag, len)) {
  212. #if 0
  213. unsigned long y;
  214. printf("\n\nFailure: \nCT:\n");
  215. for (y = 0; y < (unsigned long)tests[x].msglen; ) {
  216. printf("0x%02x", outct[y]);
  217. if (y < (unsigned long)(tests[x].msglen-1)) printf(", ");
  218. if (!(++y % 8)) printf("\n");
  219. }
  220. printf("\nTAG:\n");
  221. for (y = 0; y < len; ) {
  222. printf("0x%02x", outtag[y]);
  223. if (y < len-1) printf(", ");
  224. if (!(++y % 8)) printf("\n");
  225. }
  226. #endif
  227. return CRYPT_FAIL_TESTVECTOR;
  228. }
  229. /* test decrypt */
  230. if ((err = eax_decrypt_verify_memory(idx, tests[x].key, tests[x].keylen,
  231. tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen,
  232. outct, tests[x].msglen, outct, outtag, len, &res)) != CRYPT_OK) {
  233. return err;
  234. }
  235. if ((res != 1) || memcmp(outct, tests[x].plaintext, tests[x].msglen)) {
  236. #if 0
  237. unsigned long y;
  238. printf("\n\nFailure (res == %d): \nPT:\n", res);
  239. for (y = 0; y < (unsigned long)tests[x].msglen; ) {
  240. printf("0x%02x", outct[y]);
  241. if (y < (unsigned long)(tests[x].msglen-1)) printf(", ");
  242. if (!(++y % 8)) printf("\n");
  243. }
  244. printf("\n\n");
  245. #endif
  246. return CRYPT_FAIL_TESTVECTOR;
  247. }
  248. }
  249. return CRYPT_OK;
  250. #endif /* LTC_TEST */
  251. }
  252. #endif /* EAX_MODE */