test_cmac_mode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* test_cmac_mode.c - TinyCrypt AES-CMAC tests (including SP 800-38B 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-CMAC test (including SP 800-38B):
  34. *
  35. * Scenarios tested include:
  36. * - CMAC test #1 (GF(2^128) double))
  37. * - CMAC test #2 null msg (SP 800-38B test vector #1)
  38. * - CMAC test #3 1 block msg (SP 800-38B test vector #2)
  39. * - CMAC test #4 320 bit msg (SP 800-38B test vector #3)
  40. * - CMAC test #5 512 bit msg (SP 800-38B test vector #4)
  41. */
  42. #include <tinycrypt/cmac_mode.h>
  43. #include <tinycrypt/constants.h>
  44. #include <tinycrypt/aes.h>
  45. #include <test_utils.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #define BUF_LEN 16
  49. static void show(const char *label, const uint8_t *s, size_t slen)
  50. {
  51. unsigned int i;
  52. TC_PRINT("%s\t", label);
  53. for (i = 0; i < slen; ++i) {
  54. TC_PRINT("%02x", s[i]);
  55. }
  56. TC_PRINT("\n");
  57. }
  58. extern void gf_double(uint8_t *out, uint8_t *in);
  59. static int verify_gf_2_128_double(uint8_t *K1, uint8_t *K2,
  60. struct tc_cmac_struct s)
  61. {
  62. int result = TC_PASS;
  63. TC_PRINT("Performing CMAC test #1 (GF(2^128) double):\n");
  64. uint8_t zero[BUF_LEN];
  65. uint8_t L[BUF_LEN];
  66. const uint8_t l[BUF_LEN] = {
  67. 0x7d, 0xf7, 0x6b, 0x0c, 0x1a, 0xb8, 0x99, 0xb3,
  68. 0x3e, 0x42, 0xf0, 0x47, 0xb9, 0x1b, 0x54, 0x6f
  69. };
  70. const uint8_t k1[BUF_LEN] = {
  71. 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
  72. 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
  73. };
  74. const uint8_t k2[BUF_LEN] = {
  75. 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
  76. 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
  77. };
  78. (void) memset(zero, '\0', sizeof(zero));
  79. tc_aes_encrypt(L, zero, s.sched);
  80. if (memcmp(L, l, BUF_LEN) != 0) {
  81. TC_ERROR("%s: AES encryption failed\n", __func__);
  82. show("expected L =", l, sizeof(l));
  83. show("computed L =", L, sizeof(L));
  84. return TC_FAIL;
  85. }
  86. gf_double(K1, L);
  87. if (memcmp(K1, k1, BUF_LEN) != 0) {
  88. TC_ERROR("%s: gf_2_128_double failed when msb = 0\n", __func__);
  89. show("expected K1 =", k1, sizeof(k1));
  90. show("computed K1 =", K1, sizeof(k1));
  91. return TC_FAIL;
  92. }
  93. gf_double(K2, K1);
  94. if (memcmp(K2, k2, BUF_LEN) != 0) {
  95. TC_ERROR("%s: gf_2_128_double failed when msb = 1\n", __func__);
  96. show("expected K2 =", k2, sizeof(k2));
  97. show("computed K2 =", K2, sizeof(k2));
  98. return TC_FAIL;
  99. }
  100. TC_END_RESULT(result);
  101. return result;
  102. }
  103. static int verify_cmac_null_msg(TCCmacState_t s)
  104. {
  105. int result = TC_PASS;
  106. TC_PRINT("Performing CMAC test #2 (SP 800-38B test vector #1):\n");
  107. const uint8_t tag[BUF_LEN] = {
  108. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  109. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  110. };
  111. uint8_t Tag[BUF_LEN];
  112. (void) tc_cmac_init(s);
  113. (void) tc_cmac_update(s, (const uint8_t *) 0, 0);
  114. (void) tc_cmac_final(Tag, s);
  115. if (memcmp(Tag, tag, BUF_LEN) != 0) {
  116. TC_ERROR("%s: aes_cmac failed with null msg = 1\n", __func__);
  117. show("expected Tag =", tag, sizeof(tag));
  118. show("computed Tag =", Tag, sizeof(Tag));
  119. return TC_FAIL;
  120. }
  121. TC_END_RESULT(result);
  122. return result;
  123. }
  124. static int verify_cmac_1_block_msg(TCCmacState_t s)
  125. {
  126. int result = TC_PASS;
  127. TC_PRINT("Performing CMAC test #3 (SP 800-38B test vector #2):\n");
  128. const uint8_t msg[BUF_LEN] = {
  129. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  130. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  131. };
  132. const uint8_t tag[BUF_LEN] = {
  133. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  134. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  135. };
  136. uint8_t Tag[BUF_LEN];
  137. (void) tc_cmac_init(s);
  138. (void) tc_cmac_update(s, msg, sizeof(msg));
  139. (void) tc_cmac_final(Tag, s);
  140. if (memcmp(Tag, tag, BUF_LEN) != 0) {
  141. TC_ERROR("%s: aes_cmac failed with 1 block msg\n", __func__);
  142. show("aes_cmac failed with 1 block msg =", msg, sizeof(msg));
  143. show("expected Tag =", tag, sizeof(tag));
  144. show("computed Tag =", Tag, sizeof(Tag));
  145. return TC_FAIL;
  146. }
  147. TC_END_RESULT(result);
  148. return result;
  149. }
  150. static int verify_cmac_320_bit_msg(TCCmacState_t s)
  151. {
  152. int result = TC_PASS;
  153. TC_PRINT("Performing CMAC test #4 (SP 800-38B test vector #3):\n");
  154. const uint8_t msg[40] = {
  155. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  156. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  157. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  158. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  159. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11
  160. };
  161. const uint8_t tag[BUF_LEN] = {
  162. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  163. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  164. };
  165. uint8_t Tag[BUF_LEN];
  166. (void) tc_cmac_init(s);
  167. (void) tc_cmac_update(s, msg, sizeof(msg));
  168. (void) tc_cmac_final(Tag, s);
  169. if (memcmp(Tag, tag, BUF_LEN) != 0) {
  170. TC_ERROR("%s: aes_cmac failed with 320 bit msg\n", __func__);
  171. show("aes_cmac failed with 320 bit msg =", msg, sizeof(msg));
  172. show("expected Tag =", tag, sizeof(tag));
  173. show("computed Tag =", Tag, sizeof(Tag));
  174. return TC_FAIL;
  175. }
  176. TC_END_RESULT(result);
  177. return result;
  178. }
  179. static int verify_cmac_512_bit_msg(TCCmacState_t s)
  180. {
  181. int result = TC_PASS;
  182. TC_PRINT("Performing CMAC test #5 (SP 800-38B test vector #4)\n");
  183. const uint8_t msg[64] = {
  184. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  185. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  186. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  187. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  188. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  189. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  190. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  191. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  192. };
  193. const uint8_t tag[BUF_LEN] = {
  194. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  195. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  196. };
  197. uint8_t Tag[BUF_LEN];
  198. (void)tc_cmac_init(s);
  199. (void)tc_cmac_update(s, msg, sizeof(msg));
  200. (void)tc_cmac_final(Tag, s);
  201. if (memcmp(Tag, tag, BUF_LEN) != 0) {
  202. TC_ERROR("%s: aes_cmac failed with 512 bit msg\n", __func__);
  203. show("aes_cmac failed with 512 bit msg =", msg, sizeof(msg));
  204. show("expected Tag =", tag, sizeof(tag));
  205. show("computed Tag =", Tag, sizeof(Tag));
  206. return TC_FAIL;
  207. }
  208. TC_END_RESULT(result);
  209. return result;
  210. }
  211. /*
  212. * Main task to test CMAC
  213. * effects: returns 1 if all tests pass
  214. * exceptions: returns a negative value if some test fails
  215. */
  216. int main(void)
  217. {
  218. int result = TC_PASS;
  219. struct tc_cmac_struct state;
  220. struct tc_aes_key_sched_struct sched;
  221. const uint8_t key[BUF_LEN] = {
  222. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  223. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  224. };
  225. uint8_t K1[BUF_LEN], K2[BUF_LEN];
  226. TC_START("Performing CMAC tests:");
  227. (void) tc_cmac_setup(&state, key, &sched);
  228. result = verify_gf_2_128_double(K1, K2, state);
  229. if (result == TC_FAIL) {
  230. /* terminate test */
  231. TC_ERROR("CMAC test #1 (128 double) failed.\n");
  232. goto exitTest;
  233. }
  234. (void) tc_cmac_setup(&state, key, &sched);
  235. result = verify_cmac_null_msg(&state);
  236. if (result == TC_FAIL) {
  237. /* terminate test */
  238. TC_ERROR("CMAC test #2 (null msg) failed.\n");
  239. goto exitTest;
  240. }
  241. (void) tc_cmac_setup(&state, key, &sched);
  242. result = verify_cmac_1_block_msg(&state);
  243. if (result == TC_FAIL) {
  244. /* terminate test */
  245. TC_ERROR("CMAC test #3 (1 block msg)failed.\n");
  246. goto exitTest;
  247. }
  248. (void) tc_cmac_setup(&state, key, &sched);
  249. result = verify_cmac_320_bit_msg(&state);
  250. if (result == TC_FAIL) {
  251. /* terminate test */
  252. TC_ERROR("CMAC test #4 (320 bit msg) failed.\n");
  253. goto exitTest;
  254. }
  255. (void) tc_cmac_setup(&state, key, &sched);
  256. result = verify_cmac_512_bit_msg(&state);
  257. if (result == TC_FAIL) {
  258. /* terminate test */
  259. TC_ERROR("CMAC test #5 (512 bit msg)failed.\n");
  260. goto exitTest;
  261. }
  262. TC_PRINT("All CMAC tests succeeded!\n");
  263. exitTest:
  264. TC_END_RESULT(result);
  265. TC_END_REPORT(result);
  266. return result;
  267. }