test_ccm_mode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* test_ccm_mode.c - TinyCrypt AES-CCM tests (RFC 3610 tests) */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * DESCRIPTION
  33. * This module tests the following AES-CCM Mode routines:
  34. *
  35. * Scenarios tested include:
  36. * - AES128 CCM mode encryption RFC 3610 test vector #1
  37. * - AES128 CCM mode encryption RFC 3610 test vector #2
  38. * - AES128 CCM mode encryption RFC 3610 test vector #3
  39. * - AES128 CCM mode encryption RFC 3610 test vector #7
  40. * - AES128 CCM mode encryption RFC 3610 test vector #8
  41. * - AES128 CCM mode encryption RFC 3610 test vector #9
  42. * - AES128 CCM mode encryption No associated data
  43. * - AES128 CCM mode encryption No payload data
  44. */
  45. #include <tinycrypt/ccm_mode.h>
  46. #include <tinycrypt/constants.h>
  47. #include <test_utils.h>
  48. #include <string.h>
  49. #define TC_CCM_MAX_CT_SIZE 50
  50. #define TC_CCM_MAX_PT_SIZE 25
  51. #define NUM_NIST_KEYS 16
  52. #define NONCE_LEN 13
  53. #define HEADER_LEN 8
  54. #define M_LEN8 8
  55. #define M_LEN10 10
  56. #define DATA_BUF_LEN23 23
  57. #define DATA_BUF_LEN24 24
  58. #define DATA_BUF_LEN25 25
  59. #define EXPECTED_BUF_LEN31 31
  60. #define EXPECTED_BUF_LEN32 32
  61. #define EXPECTED_BUF_LEN33 33
  62. #define EXPECTED_BUF_LEN34 34
  63. #define EXPECTED_BUF_LEN35 35
  64. int do_test(const uint8_t *key, uint8_t *nonce,
  65. size_t nlen, const uint8_t *hdr,
  66. size_t hlen, const uint8_t *data,
  67. size_t dlen, const uint8_t *expected,
  68. size_t elen, const int mlen)
  69. {
  70. int result = TC_PASS;
  71. uint8_t ciphertext[TC_CCM_MAX_CT_SIZE];
  72. uint8_t decrypted[TC_CCM_MAX_PT_SIZE];
  73. struct tc_ccm_mode_struct c;
  74. struct tc_aes_key_sched_struct sched;
  75. tc_aes128_set_encrypt_key(&sched, key);
  76. result = tc_ccm_config(&c, &sched, nonce, nlen, mlen);
  77. if (result == 0) {
  78. TC_ERROR("CCM config failed in %s.\n", __func__);
  79. result = TC_FAIL;
  80. goto exitTest1;
  81. }
  82. result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr,
  83. hlen, data, dlen, &c);
  84. if (result == 0) {
  85. TC_ERROR("ccm_encrypt failed in %s.\n", __func__);
  86. result = TC_FAIL;
  87. goto exitTest1;
  88. }
  89. if (memcmp(expected, ciphertext, elen) != 0) {
  90. TC_ERROR("ccm_encrypt produced wrong ciphertext in %s.\n",
  91. __func__);
  92. show_str("\t\tExpected", expected, elen);
  93. show_str("\t\tComputed", ciphertext, elen);
  94. result = TC_FAIL;
  95. goto exitTest1;
  96. }
  97. result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr,
  98. hlen, ciphertext, dlen+mlen, &c);
  99. if (result == 0) {
  100. TC_ERROR("ccm_decrypt failed in %s.\n", __func__);
  101. show_str("\t\tExpected", data, dlen);
  102. show_str("\t\tComputed", decrypted, sizeof(decrypted));
  103. result = TC_FAIL;
  104. goto exitTest1;
  105. }
  106. result = TC_PASS;
  107. exitTest1:
  108. TC_END_RESULT(result);
  109. return result;
  110. }
  111. int test_vector_1(void)
  112. {
  113. int result = TC_PASS;
  114. /* RFC 3610 test vector #1 */
  115. const uint8_t key[NUM_NIST_KEYS] = {
  116. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  117. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  118. };
  119. uint8_t nonce[NONCE_LEN] = {
  120. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  121. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  122. };
  123. const uint8_t hdr[HEADER_LEN] = {
  124. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  125. };
  126. const uint8_t data[DATA_BUF_LEN23] = {
  127. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  128. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  129. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  130. };
  131. const uint8_t expected[EXPECTED_BUF_LEN31] = {
  132. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  133. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  134. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17,
  135. 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  136. };
  137. uint16_t mlen = M_LEN8;
  138. TC_PRINT("%s: Performing CCM test #1 (RFC 3610 test vector #1):\n",
  139. __func__);
  140. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  141. data, sizeof(data), expected, sizeof(expected), mlen);
  142. return result;
  143. }
  144. int test_vector_2(void)
  145. {
  146. int result = TC_PASS;
  147. /* RFC 3610 test vector #2 */
  148. const uint8_t key[NUM_NIST_KEYS] = {
  149. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  150. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  151. };
  152. uint8_t nonce[NONCE_LEN] = {
  153. 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0xa0,
  154. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  155. };
  156. const uint8_t hdr[HEADER_LEN] = {
  157. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  158. };
  159. const uint8_t data[DATA_BUF_LEN24] = {
  160. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  161. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  162. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  163. };
  164. const uint8_t expected[EXPECTED_BUF_LEN32] = {
  165. 0x72, 0xc9, 0x1a, 0x36, 0xe1, 0x35, 0xf8, 0xcf,
  166. 0x29, 0x1c, 0xa8, 0x94, 0x08, 0x5c, 0x87, 0xe3,
  167. 0xcc, 0x15, 0xc4, 0x39, 0xc9, 0xe4, 0x3a, 0x3b,
  168. 0xa0, 0x91, 0xd5, 0x6e, 0x10, 0x40, 0x09, 0x16
  169. };
  170. uint16_t mlen = M_LEN8;
  171. TC_PRINT("%s: Performing CCM test #2 (RFC 3610 test vector #2):\n",
  172. __func__);
  173. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  174. data, sizeof(data), expected, sizeof(expected), mlen);
  175. return result;
  176. }
  177. int test_vector_3(void)
  178. {
  179. int result = TC_PASS;
  180. /* RFC 3610 test vector #3 */
  181. const uint8_t key[NUM_NIST_KEYS] = {
  182. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  183. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  184. };
  185. uint8_t nonce[NONCE_LEN] = {
  186. 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x02, 0xa0,
  187. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  188. };
  189. const uint8_t hdr[HEADER_LEN] = {
  190. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  191. };
  192. const uint8_t data[DATA_BUF_LEN25] = {
  193. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  194. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  195. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  196. 0x20
  197. };
  198. const uint8_t expected[EXPECTED_BUF_LEN33] = {
  199. 0x51, 0xb1, 0xe5, 0xf4, 0x4a, 0x19, 0x7d, 0x1d,
  200. 0xa4, 0x6b, 0x0f, 0x8e, 0x2d, 0x28, 0x2a, 0xe8,
  201. 0x71, 0xe8, 0x38, 0xbb, 0x64, 0xda, 0x85, 0x96,
  202. 0x57, 0x4a, 0xda, 0xa7, 0x6f, 0xbd, 0x9f, 0xb0,
  203. 0xc5
  204. };
  205. uint16_t mlen = M_LEN8;
  206. TC_PRINT("%s: Performing CCM test #3 (RFC 3610 test vector #3):\n",
  207. __func__);
  208. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), data,
  209. sizeof(data), expected, sizeof(expected), mlen);
  210. return result;
  211. }
  212. int test_vector_4(void)
  213. {
  214. int result = TC_PASS;
  215. /* RFC 3610 test vector #7 */
  216. const uint8_t key[NUM_NIST_KEYS] = {
  217. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  218. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  219. };
  220. uint8_t nonce[NONCE_LEN] = {
  221. 0x00, 0x00, 0x00, 0x09, 0x08, 0x07, 0x06, 0xa0,
  222. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  223. };
  224. const uint8_t hdr[HEADER_LEN] = {
  225. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  226. };
  227. const uint8_t data[DATA_BUF_LEN23] = {
  228. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  229. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  230. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  231. };
  232. const uint8_t expected[EXPECTED_BUF_LEN33] = {
  233. 0x01, 0x35, 0xD1, 0xB2, 0xC9, 0x5F, 0x41, 0xD5,
  234. 0xD1, 0xD4, 0xFE, 0xC1, 0x85, 0xD1, 0x66, 0xB8,
  235. 0x09, 0x4E, 0x99, 0x9D, 0xFE, 0xD9, 0x6C, 0x04,
  236. 0x8C, 0x56, 0x60, 0x2C, 0x97, 0xAC, 0xBB, 0x74,
  237. 0x90
  238. };
  239. uint16_t mlen = M_LEN10;
  240. TC_PRINT("%s: Performing CCM test #4 (RFC 3610 test vector #7):\n",
  241. __func__);
  242. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  243. data, sizeof(data), expected, sizeof(expected), mlen);
  244. return result;
  245. }
  246. int test_vector_5(void)
  247. {
  248. int result = TC_PASS;
  249. /* RFC 3610 test vector #8 */
  250. const uint8_t key[NUM_NIST_KEYS] = {
  251. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  252. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  253. };
  254. uint8_t nonce[NONCE_LEN] = {
  255. 0x00, 0x00, 0x00, 0x0A, 0x09, 0x08, 0x07, 0xA0,
  256. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
  257. };
  258. const uint8_t hdr[HEADER_LEN] = {
  259. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  260. };
  261. const uint8_t data[DATA_BUF_LEN24] = {
  262. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  263. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  264. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  265. };
  266. const uint8_t expected[EXPECTED_BUF_LEN34] = {
  267. 0x7B, 0x75, 0x39, 0x9A, 0xC0, 0x83, 0x1D, 0xD2,
  268. 0xF0, 0xBB, 0xD7, 0x58, 0x79, 0xA2, 0xFD, 0x8F,
  269. 0x6C, 0xAE, 0x6B, 0x6C, 0xD9, 0xB7, 0xDB, 0x24,
  270. 0xC1, 0x7B, 0x44, 0x33, 0xF4, 0x34, 0x96, 0x3F,
  271. 0x34, 0xB4
  272. };
  273. uint16_t mlen = M_LEN10;
  274. TC_PRINT("%s: Performing CCM test #5 (RFC 3610 test vector #8):\n",
  275. __func__);
  276. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  277. data, sizeof(data), expected, sizeof(expected), mlen);
  278. return result;
  279. }
  280. int test_vector_6(void)
  281. {
  282. int result = TC_PASS;
  283. /* RFC 3610 test vector #9 */
  284. const uint8_t key[NUM_NIST_KEYS] = {
  285. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  286. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  287. };
  288. uint8_t nonce[NONCE_LEN] = {
  289. 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0,
  290. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
  291. };
  292. const uint8_t hdr[HEADER_LEN] = {
  293. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  294. };
  295. const uint8_t data[DATA_BUF_LEN25] = {
  296. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  297. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  298. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  299. 0x20
  300. };
  301. const uint8_t expected[EXPECTED_BUF_LEN35] = {
  302. 0x82, 0x53, 0x1a, 0x60, 0xCC, 0x24, 0x94, 0x5a,
  303. 0x4b, 0x82, 0x79, 0x18, 0x1a, 0xb5, 0xc8, 0x4d,
  304. 0xf2, 0x1c, 0xe7, 0xf9, 0xb7, 0x3f, 0x42, 0xe1,
  305. 0x97, 0xea, 0x9c, 0x07, 0xe5, 0x6b, 0x5e, 0xb1,
  306. 0x7e, 0x5f, 0x4e
  307. };
  308. uint16_t mlen = M_LEN10;
  309. TC_PRINT("%s: Performing CCM test #6 (RFC 3610 test vector #9):\n",
  310. __func__);
  311. result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr),
  312. data, sizeof(data), expected, sizeof(expected), mlen);
  313. return result;
  314. }
  315. int test_vector_7(void)
  316. {
  317. int result = TC_PASS;
  318. /* Test based on RFC 3610 test vector #9 but with no associated data */
  319. const uint8_t key[NUM_NIST_KEYS] = {
  320. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  321. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  322. };
  323. uint8_t nonce[NONCE_LEN] = {
  324. 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0,
  325. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
  326. };
  327. uint8_t *hdr = NULL;
  328. uint8_t data[DATA_BUF_LEN25] = {
  329. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  330. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  331. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  332. 0x20
  333. };
  334. struct tc_ccm_mode_struct c;
  335. struct tc_aes_key_sched_struct sched;
  336. uint8_t decrypted[TC_CCM_MAX_PT_SIZE];
  337. uint8_t ciphertext[TC_CCM_MAX_CT_SIZE];
  338. uint16_t mlen = M_LEN10;
  339. TC_PRINT("%s: Performing CCM test #7 (no associated data):\n",
  340. __func__);
  341. tc_aes128_set_encrypt_key(&sched, key);
  342. if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) {
  343. TC_ERROR("ccm_config failed in %s.\n", __func__);
  344. result = TC_FAIL;
  345. goto exitTest1;
  346. }
  347. result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr,
  348. 0, data, sizeof(data), &c);
  349. if (result == 0) {
  350. TC_ERROR("ccm_encryption failed in %s.\n", __func__);
  351. result = TC_FAIL;
  352. goto exitTest1;
  353. }
  354. result = tc_ccm_decryption_verification (decrypted, TC_CCM_MAX_PT_SIZE, hdr,
  355. 0, ciphertext, sizeof(data)+mlen, &c);
  356. if (result == 0) {
  357. TC_ERROR("ccm_decrypt failed in %s.\n", __func__);
  358. show_str("\t\tExpected", data, sizeof(data));
  359. show_str("\t\tComputed", decrypted, sizeof(decrypted));
  360. result = TC_FAIL;
  361. goto exitTest1;
  362. }
  363. result = TC_PASS;
  364. exitTest1:
  365. TC_END_RESULT(result);
  366. return result;
  367. }
  368. int test_vector_8(void)
  369. {
  370. int result = TC_PASS;
  371. /* Test based on RFC 3610 test vector #9 but with no payload data */
  372. const uint8_t key[NUM_NIST_KEYS] = {
  373. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  374. 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  375. };
  376. uint8_t nonce[NONCE_LEN] = {
  377. 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0,
  378. 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
  379. };
  380. const uint8_t hdr[8] = {
  381. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  382. };
  383. uint8_t *data = NULL;
  384. struct tc_ccm_mode_struct c;
  385. struct tc_aes_key_sched_struct sched;
  386. uint8_t decrypted[TC_CCM_MAX_PT_SIZE];
  387. uint8_t ciphertext[TC_CCM_MAX_CT_SIZE];
  388. uint16_t mlen = M_LEN10;
  389. TC_PRINT("%s: Performing CCM test #8 (no payload data):\n", __func__);
  390. tc_aes128_set_encrypt_key(&sched, key);
  391. if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) {
  392. TC_ERROR("CCM config failed in %s.\n", __func__);
  393. result = TC_FAIL;
  394. goto exitTest1;
  395. }
  396. result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr,
  397. sizeof(hdr), data, 0, &c);
  398. if (result == 0) {
  399. TC_ERROR("ccm_encrypt failed in %s.\n", __func__);
  400. result = TC_FAIL;
  401. goto exitTest1;
  402. }
  403. result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr,
  404. sizeof(hdr), ciphertext, mlen, &c);
  405. if (result == 0) {
  406. TC_ERROR("ccm_decrypt failed in %s.\n", __func__);
  407. show_str("\t\tExpected", data, sizeof(data));
  408. show_str("\t\tComputed", decrypted, sizeof(decrypted));
  409. result = TC_FAIL;
  410. goto exitTest1;
  411. }
  412. result = TC_PASS;
  413. exitTest1:
  414. TC_END_RESULT(result);
  415. return result;
  416. }
  417. /*
  418. * Main task to test CCM
  419. */
  420. int main(void)
  421. {
  422. int result = TC_PASS;
  423. TC_START("Performing CCM tests:");
  424. result = test_vector_1();
  425. if (result == TC_FAIL) { /* terminate test */
  426. TC_ERROR("CCM test #1 (RFC 3610 test vector #1) failed.\n");
  427. goto exitTest;
  428. }
  429. result = test_vector_2();
  430. if (result == TC_FAIL) { /* terminate test */
  431. TC_ERROR("CCM test #2 failed.\n");
  432. goto exitTest;
  433. }
  434. result = test_vector_3();
  435. if (result == TC_FAIL) { /* terminate test */
  436. TC_ERROR("CCM test #3 failed.\n");
  437. goto exitTest;
  438. }
  439. result = test_vector_4();
  440. if (result == TC_FAIL) { /* terminate test */
  441. TC_ERROR("CCM test #4 failed.\n");
  442. goto exitTest;
  443. }
  444. result = test_vector_5();
  445. if (result == TC_FAIL) { /* terminate test */
  446. TC_ERROR("CCM test #5 failed.\n");
  447. goto exitTest;
  448. }
  449. result = test_vector_6();
  450. if (result == TC_FAIL) { /* terminate test */
  451. TC_ERROR("CCM test #6 failed.\n");
  452. goto exitTest;
  453. }
  454. result = test_vector_7();
  455. if (result == TC_FAIL) { /* terminate test */
  456. TC_ERROR("CCM test #7 failed.\n");
  457. goto exitTest;
  458. }
  459. result = test_vector_8();
  460. if (result == TC_FAIL) { /* terminate test */
  461. TC_ERROR("CCM test #8 (no payload data) failed.\n");
  462. goto exitTest;
  463. }
  464. TC_PRINT("All CCM tests succeeded!\n");
  465. exitTest:
  466. TC_END_RESULT(result);
  467. TC_END_REPORT(result);
  468. return result;
  469. }