ccm_test.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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, tomstdenis@gmail.com, http://libtomcrypt.com
  10. */
  11. #include "tomcrypt.h"
  12. /**
  13. @file ccm_test.c
  14. CCM support, process a block of memory, Tom St Denis
  15. */
  16. #ifdef CCM_MODE
  17. int ccm_test(void)
  18. {
  19. #ifndef LTC_TEST
  20. return CRYPT_NOP;
  21. #else
  22. static const struct {
  23. unsigned char key[16];
  24. unsigned char nonce[16];
  25. int noncelen;
  26. unsigned char header[64];
  27. int headerlen;
  28. unsigned char pt[64];
  29. int ptlen;
  30. unsigned char ct[64];
  31. unsigned char tag[16];
  32. int taglen;
  33. } tests[] = {
  34. /* 13 byte nonce, 8 byte auth, 23 byte pt */
  35. {
  36. { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  37. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
  38. { 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0,
  39. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
  40. 13,
  41. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
  42. 8,
  43. { 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  44. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E },
  46. 23,
  47. { 0x58, 0x8C, 0x97, 0x9A, 0x61, 0xC6, 0x63, 0xD2,
  48. 0xF0, 0x66, 0xD0, 0xC2, 0xC0, 0xF9, 0x89, 0x80,
  49. 0x6D, 0x5F, 0x6B, 0x61, 0xDA, 0xC3, 0x84 },
  50. { 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 },
  51. 8
  52. },
  53. /* 13 byte nonce, 12 byte header, 19 byte pt */
  54. {
  55. { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  56. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF },
  57. { 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03, 0xA0,
  58. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 },
  59. 13,
  60. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  61. 0x08, 0x09, 0x0A, 0x0B },
  62. 12,
  63. { 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
  64. 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
  65. 0x1C, 0x1D, 0x1E },
  66. 19,
  67. { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79,
  68. 0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91,
  69. 0xCD, 0xAC, 0x8C },
  70. { 0x96, 0xC8, 0x61, 0xB9, 0xC9, 0xE6, 0x1E, 0xF1 },
  71. 8
  72. },
  73. /* supplied by Brian Gladman */
  74. {
  75. { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  76. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f },
  77. { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 },
  78. 7,
  79. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
  80. 8,
  81. { 0x20, 0x21, 0x22, 0x23 },
  82. 4,
  83. { 0x71, 0x62, 0x01, 0x5b },
  84. { 0x4d, 0xac, 0x25, 0x5d },
  85. 4
  86. },
  87. {
  88. { 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
  89. 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f },
  90. { 0x00, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xb5,
  91. 0x03, 0x97, 0x76, 0xe7, 0x0c },
  92. 13,
  93. { 0x08, 0x40, 0x0f, 0xd2, 0xe1, 0x28, 0xa5, 0x7c,
  94. 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08, 0xab, 0xae,
  95. 0xa5, 0xb8, 0xfc, 0xba, 0x00, 0x00 },
  96. 22,
  97. { 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
  98. 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
  99. 0x7e, 0x78, 0xa0, 0x50 },
  100. 20,
  101. { 0xf3, 0xd0, 0xa2, 0xfe, 0x9a, 0x3d, 0xbf, 0x23,
  102. 0x42, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c,
  103. 0x3c, 0x04, 0xd0, 0x19 },
  104. { 0x78, 0x45, 0xce, 0x0b, 0x16, 0xf9, 0x76, 0x23 },
  105. 8
  106. },
  107. };
  108. unsigned long taglen, x;
  109. unsigned char buf[64], buf2[64], tag2[16], tag[16];
  110. int err, idx;
  111. symmetric_key skey;
  112. idx = find_cipher("aes");
  113. if (idx == -1) {
  114. idx = find_cipher("rijndael");
  115. if (idx == -1) {
  116. return CRYPT_NOP;
  117. }
  118. }
  119. for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
  120. taglen = tests[x].taglen;
  121. if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
  122. return err;
  123. }
  124. if ((err = ccm_memory(idx,
  125. tests[x].key, 16,
  126. &skey,
  127. tests[x].nonce, tests[x].noncelen,
  128. tests[x].header, tests[x].headerlen,
  129. (unsigned char*)tests[x].pt, tests[x].ptlen,
  130. buf,
  131. tag, &taglen, 0)) != CRYPT_OK) {
  132. return err;
  133. }
  134. if (XMEMCMP(buf, tests[x].ct, tests[x].ptlen)) {
  135. return CRYPT_FAIL_TESTVECTOR;
  136. }
  137. if (XMEMCMP(tag, tests[x].tag, tests[x].taglen)) {
  138. return CRYPT_FAIL_TESTVECTOR;
  139. }
  140. if ((err = ccm_memory(idx,
  141. tests[x].key, 16,
  142. NULL,
  143. tests[x].nonce, tests[x].noncelen,
  144. tests[x].header, tests[x].headerlen,
  145. buf2, tests[x].ptlen,
  146. buf,
  147. tag2, &taglen, 1 )) != CRYPT_OK) {
  148. return err;
  149. }
  150. if (XMEMCMP(buf2, tests[x].pt, tests[x].ptlen)) {
  151. return CRYPT_FAIL_TESTVECTOR;
  152. }
  153. if (XMEMCMP(tag2, tests[x].tag, tests[x].taglen)) {
  154. return CRYPT_FAIL_TESTVECTOR;
  155. }
  156. cipher_descriptor[idx].done(&skey);
  157. }
  158. return CRYPT_OK;
  159. #endif
  160. }
  161. #endif
  162. /* $Source: /cvs/libtom/libtomcrypt/src/encauth/ccm/ccm_test.c,v $ */
  163. /* $Revision: 1.8 $ */
  164. /* $Date: 2006/11/21 00:18:23 $ */