ocb_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /* OCB Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef OCB_MODE
  14. int ocb_test(void)
  15. {
  16. #ifndef LTC_TEST
  17. return CRYPT_NOP;
  18. #else
  19. static const struct {
  20. int ptlen;
  21. unsigned char key[16], nonce[16], pt[34], ct[34], tag[16];
  22. } tests[] = {
  23. /* OCB-AES-128-0B */
  24. {
  25. 0,
  26. /* key */
  27. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  28. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  29. /* nonce */
  30. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  32. /* pt */
  33. { 0 },
  34. /* ct */
  35. { 0 },
  36. /* tag */
  37. { 0x15, 0xd3, 0x7d, 0xd7, 0xc8, 0x90, 0xd5, 0xd6,
  38. 0xac, 0xab, 0x92, 0x7b, 0xc0, 0xdc, 0x60, 0xee },
  39. },
  40. /* OCB-AES-128-3B */
  41. {
  42. 3,
  43. /* key */
  44. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  45. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  46. /* nonce */
  47. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  49. /* pt */
  50. { 0x00, 0x01, 0x02 },
  51. /* ct */
  52. { 0xfc, 0xd3, 0x7d },
  53. /* tag */
  54. { 0x02, 0x25, 0x47, 0x39, 0xa5, 0xe3, 0x56, 0x5a,
  55. 0xe2, 0xdc, 0xd6, 0x2c, 0x65, 0x97, 0x46, 0xba },
  56. },
  57. /* OCB-AES-128-16B */
  58. {
  59. 16,
  60. /* key */
  61. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  62. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  63. /* nonce */
  64. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  66. /* pt */
  67. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  68. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  69. /* ct */
  70. { 0x37, 0xdf, 0x8c, 0xe1, 0x5b, 0x48, 0x9b, 0xf3,
  71. 0x1d, 0x0f, 0xc4, 0x4d, 0xa1, 0xfa, 0xf6, 0xd6 },
  72. /* tag */
  73. { 0xdf, 0xb7, 0x63, 0xeb, 0xdb, 0x5f, 0x0e, 0x71,
  74. 0x9c, 0x7b, 0x41, 0x61, 0x80, 0x80, 0x04, 0xdf },
  75. },
  76. /* OCB-AES-128-20B */
  77. {
  78. 20,
  79. /* key */
  80. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  81. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  82. /* nonce */
  83. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  85. /* pt */
  86. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  87. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  88. 0x10, 0x11, 0x12, 0x13 },
  89. /* ct */
  90. { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
  91. 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
  92. 0x70, 0x03, 0xeb, 0x55},
  93. /* tag */
  94. { 0x75, 0x30, 0x84, 0x14, 0x4e, 0xb6, 0x3b, 0x77,
  95. 0x0b, 0x06, 0x3c, 0x2e, 0x23, 0xcd, 0xa0, 0xbb },
  96. },
  97. /* OCB-AES-128-32B */
  98. {
  99. 32,
  100. /* key */
  101. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  102. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  103. /* nonce */
  104. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  106. /* pt */
  107. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  108. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  109. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  110. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  111. /* ct */
  112. { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
  113. 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
  114. 0x4a, 0xfc, 0xbb, 0x7f, 0xed, 0xc0, 0x8c, 0xa8,
  115. 0x65, 0x4c, 0x6d, 0x30, 0x4d, 0x16, 0x12, 0xfa },
  116. /* tag */
  117. { 0xc1, 0x4c, 0xbf, 0x2c, 0x1a, 0x1f, 0x1c, 0x3c,
  118. 0x13, 0x7e, 0xad, 0xea, 0x1f, 0x2f, 0x2f, 0xcf },
  119. },
  120. /* OCB-AES-128-34B */
  121. {
  122. 34,
  123. /* key */
  124. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  125. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
  126. /* nonce */
  127. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  129. /* pt */
  130. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  131. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  132. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  133. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  134. 0x20, 0x21 },
  135. /* ct */
  136. { 0x01, 0xa0, 0x75, 0xf0, 0xd8, 0x15, 0xb1, 0xa4,
  137. 0xe9, 0xc8, 0x81, 0xa1, 0xbc, 0xff, 0xc3, 0xeb,
  138. 0xd4, 0x90, 0x3d, 0xd0, 0x02, 0x5b, 0xa4, 0xaa,
  139. 0x83, 0x7c, 0x74, 0xf1, 0x21, 0xb0, 0x26, 0x0f,
  140. 0xa9, 0x5d },
  141. /* tag */
  142. { 0xcf, 0x83, 0x41, 0xbb, 0x10, 0x82, 0x0c, 0xcf,
  143. 0x14, 0xbd, 0xec, 0x56, 0xb8, 0xd7, 0xd6, 0xab },
  144. },
  145. };
  146. int err, x, idx, res;
  147. unsigned long len;
  148. unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE];
  149. /* AES can be under rijndael or aes... try to find it */
  150. if ((idx = find_cipher("aes")) == -1) {
  151. if ((idx = find_cipher("rijndael")) == -1) {
  152. return CRYPT_NOP;
  153. }
  154. }
  155. for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
  156. len = sizeof(outtag);
  157. if ((err = ocb_encrypt_authenticate_memory(idx, tests[x].key, 16,
  158. tests[x].nonce, tests[x].pt, tests[x].ptlen, outct, outtag, &len)) != CRYPT_OK) {
  159. return err;
  160. }
  161. if (memcmp(outtag, tests[x].tag, len) || memcmp(outct, tests[x].ct, tests[x].ptlen)) {
  162. #if 0
  163. unsigned long y;
  164. printf("\n\nFailure: \nCT:\n");
  165. for (y = 0; y < (unsigned long)tests[x].ptlen; ) {
  166. printf("0x%02x", outct[y]);
  167. if (y < (unsigned long)(tests[x].ptlen-1)) printf(", ");
  168. if (!(++y % 8)) printf("\n");
  169. }
  170. printf("\nTAG:\n");
  171. for (y = 0; y < len; ) {
  172. printf("0x%02x", outtag[y]);
  173. if (y < len-1) printf(", ");
  174. if (!(++y % 8)) printf("\n");
  175. }
  176. #endif
  177. return CRYPT_FAIL_TESTVECTOR;
  178. }
  179. if ((err = ocb_decrypt_verify_memory(idx, tests[x].key, 16, tests[x].nonce, outct, tests[x].ptlen,
  180. outct, tests[x].tag, len, &res)) != CRYPT_OK) {
  181. return err;
  182. }
  183. if ((res != 1) || memcmp(tests[x].pt, outct, tests[x].ptlen)) {
  184. #if 0
  185. unsigned long y;
  186. printf("\n\nFailure-decrypt: \nPT:\n");
  187. for (y = 0; y < (unsigned long)tests[x].ptlen; ) {
  188. printf("0x%02x", outct[y]);
  189. if (y < (unsigned long)(tests[x].ptlen-1)) printf(", ");
  190. if (!(++y % 8)) printf("\n");
  191. }
  192. printf("\nres = %d\n\n", res);
  193. #endif
  194. }
  195. }
  196. return CRYPT_OK;
  197. #endif /* LTC_TEST */
  198. }
  199. #endif /* OCB_MODE */
  200. /* some comments
  201. -- it's hard to seek
  202. -- hard to stream [you can't emit ciphertext until full block]
  203. -- The setup is somewhat complicated...
  204. */