cmac.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /**
  2. * \file cmac.c
  3. *
  4. * \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES
  5. *
  6. * Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. /*
  22. * References:
  23. *
  24. * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The
  25. * CMAC Mode for Authentication
  26. * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
  27. *
  28. * - RFC 4493 - The AES-CMAC Algorithm
  29. * https://tools.ietf.org/html/rfc4493
  30. *
  31. * - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message
  32. * Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128)
  33. * Algorithm for the Internet Key Exchange Protocol (IKE)
  34. * https://tools.ietf.org/html/rfc4615
  35. *
  36. * Additional test vectors: ISO/IEC 9797-1
  37. *
  38. */
  39. #include "common.h"
  40. #if defined(MBEDTLS_CMAC_C)
  41. #include "mbedtls/cmac.h"
  42. #include "mbedtls/platform_util.h"
  43. #include "mbedtls/error.h"
  44. #include "mbedtls/platform.h"
  45. #include <string.h>
  46. #if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST)
  47. /*
  48. * Multiplication by u in the Galois field of GF(2^n)
  49. *
  50. * As explained in NIST SP 800-38B, this can be computed:
  51. *
  52. * If MSB(p) = 0, then p = (p << 1)
  53. * If MSB(p) = 1, then p = (p << 1) ^ R_n
  54. * with R_64 = 0x1B and R_128 = 0x87
  55. *
  56. * Input and output MUST NOT point to the same buffer
  57. * Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES.
  58. */
  59. static int cmac_multiply_by_u(unsigned char *output,
  60. const unsigned char *input,
  61. size_t blocksize)
  62. {
  63. const unsigned char R_128 = 0x87;
  64. const unsigned char R_64 = 0x1B;
  65. unsigned char R_n, mask;
  66. unsigned char overflow = 0x00;
  67. int i;
  68. if (blocksize == MBEDTLS_AES_BLOCK_SIZE) {
  69. R_n = R_128;
  70. } else if (blocksize == MBEDTLS_DES3_BLOCK_SIZE) {
  71. R_n = R_64;
  72. } else {
  73. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  74. }
  75. for (i = (int) blocksize - 1; i >= 0; i--) {
  76. output[i] = input[i] << 1 | overflow;
  77. overflow = input[i] >> 7;
  78. }
  79. /* mask = ( input[0] >> 7 ) ? 0xff : 0x00
  80. * using bit operations to avoid branches */
  81. /* MSVC has a warning about unary minus on unsigned, but this is
  82. * well-defined and precisely what we want to do here */
  83. #if defined(_MSC_VER)
  84. #pragma warning( push )
  85. #pragma warning( disable : 4146 )
  86. #endif
  87. mask = -(input[0] >> 7);
  88. #if defined(_MSC_VER)
  89. #pragma warning( pop )
  90. #endif
  91. output[blocksize - 1] ^= R_n & mask;
  92. return 0;
  93. }
  94. /*
  95. * Generate subkeys
  96. *
  97. * - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
  98. */
  99. static int cmac_generate_subkeys(mbedtls_cipher_context_t *ctx,
  100. unsigned char *K1, unsigned char *K2)
  101. {
  102. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  103. unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
  104. size_t olen, block_size;
  105. mbedtls_platform_zeroize(L, sizeof(L));
  106. block_size = ctx->cipher_info->block_size;
  107. /* Calculate Ek(0) */
  108. if ((ret = mbedtls_cipher_update(ctx, L, block_size, L, &olen)) != 0) {
  109. goto exit;
  110. }
  111. /*
  112. * Generate K1 and K2
  113. */
  114. if ((ret = cmac_multiply_by_u(K1, L, block_size)) != 0) {
  115. goto exit;
  116. }
  117. if ((ret = cmac_multiply_by_u(K2, K1, block_size)) != 0) {
  118. goto exit;
  119. }
  120. exit:
  121. mbedtls_platform_zeroize(L, sizeof(L));
  122. return ret;
  123. }
  124. #endif /* !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) */
  125. #if !defined(MBEDTLS_CMAC_ALT)
  126. /*
  127. * Create padded last block from (partial) last block.
  128. *
  129. * We can't use the padding option from the cipher layer, as it only works for
  130. * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
  131. */
  132. static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
  133. size_t padded_block_len,
  134. const unsigned char *last_block,
  135. size_t last_block_len)
  136. {
  137. size_t j;
  138. for (j = 0; j < padded_block_len; j++) {
  139. if (j < last_block_len) {
  140. padded_block[j] = last_block[j];
  141. } else if (j == last_block_len) {
  142. padded_block[j] = 0x80;
  143. } else {
  144. padded_block[j] = 0x00;
  145. }
  146. }
  147. }
  148. int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
  149. const unsigned char *key, size_t keybits)
  150. {
  151. mbedtls_cipher_type_t type;
  152. mbedtls_cmac_context_t *cmac_ctx;
  153. int retval;
  154. if (ctx == NULL || ctx->cipher_info == NULL || key == NULL) {
  155. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  156. }
  157. if ((retval = mbedtls_cipher_setkey(ctx, key, (int) keybits,
  158. MBEDTLS_ENCRYPT)) != 0) {
  159. return retval;
  160. }
  161. type = ctx->cipher_info->type;
  162. switch (type) {
  163. case MBEDTLS_CIPHER_AES_128_ECB:
  164. case MBEDTLS_CIPHER_AES_192_ECB:
  165. case MBEDTLS_CIPHER_AES_256_ECB:
  166. case MBEDTLS_CIPHER_DES_EDE3_ECB:
  167. break;
  168. default:
  169. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  170. }
  171. /* Allocated and initialise in the cipher context memory for the CMAC
  172. * context */
  173. cmac_ctx = mbedtls_calloc(1, sizeof(mbedtls_cmac_context_t));
  174. if (cmac_ctx == NULL) {
  175. return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
  176. }
  177. ctx->cmac_ctx = cmac_ctx;
  178. mbedtls_platform_zeroize(cmac_ctx->state, sizeof(cmac_ctx->state));
  179. return 0;
  180. }
  181. int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
  182. const unsigned char *input, size_t ilen)
  183. {
  184. mbedtls_cmac_context_t *cmac_ctx;
  185. unsigned char *state;
  186. int ret = 0;
  187. size_t n, j, olen, block_size;
  188. if (ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
  189. ctx->cmac_ctx == NULL) {
  190. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  191. }
  192. cmac_ctx = ctx->cmac_ctx;
  193. block_size = ctx->cipher_info->block_size;
  194. state = ctx->cmac_ctx->state;
  195. /* Is there data still to process from the last call, that's greater in
  196. * size than a block? */
  197. if (cmac_ctx->unprocessed_len > 0 &&
  198. ilen > block_size - cmac_ctx->unprocessed_len) {
  199. memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  200. input,
  201. block_size - cmac_ctx->unprocessed_len);
  202. mbedtls_xor(state, cmac_ctx->unprocessed_block, state, block_size);
  203. if ((ret = mbedtls_cipher_update(ctx, state, block_size, state,
  204. &olen)) != 0) {
  205. goto exit;
  206. }
  207. input += block_size - cmac_ctx->unprocessed_len;
  208. ilen -= block_size - cmac_ctx->unprocessed_len;
  209. cmac_ctx->unprocessed_len = 0;
  210. }
  211. /* n is the number of blocks including any final partial block */
  212. n = (ilen + block_size - 1) / block_size;
  213. /* Iterate across the input data in block sized chunks, excluding any
  214. * final partial or complete block */
  215. for (j = 1; j < n; j++) {
  216. mbedtls_xor(state, input, state, block_size);
  217. if ((ret = mbedtls_cipher_update(ctx, state, block_size, state,
  218. &olen)) != 0) {
  219. goto exit;
  220. }
  221. ilen -= block_size;
  222. input += block_size;
  223. }
  224. /* If there is data left over that wasn't aligned to a block */
  225. if (ilen > 0) {
  226. memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
  227. input,
  228. ilen);
  229. cmac_ctx->unprocessed_len += ilen;
  230. }
  231. exit:
  232. return ret;
  233. }
  234. int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
  235. unsigned char *output)
  236. {
  237. mbedtls_cmac_context_t *cmac_ctx;
  238. unsigned char *state, *last_block;
  239. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  240. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  241. unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX];
  242. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  243. size_t olen, block_size;
  244. if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
  245. output == NULL) {
  246. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  247. }
  248. cmac_ctx = ctx->cmac_ctx;
  249. block_size = ctx->cipher_info->block_size;
  250. state = cmac_ctx->state;
  251. mbedtls_platform_zeroize(K1, sizeof(K1));
  252. mbedtls_platform_zeroize(K2, sizeof(K2));
  253. cmac_generate_subkeys(ctx, K1, K2);
  254. last_block = cmac_ctx->unprocessed_block;
  255. /* Calculate last block */
  256. if (cmac_ctx->unprocessed_len < block_size) {
  257. cmac_pad(M_last, block_size, last_block, cmac_ctx->unprocessed_len);
  258. mbedtls_xor(M_last, M_last, K2, block_size);
  259. } else {
  260. /* Last block is complete block */
  261. mbedtls_xor(M_last, last_block, K1, block_size);
  262. }
  263. mbedtls_xor(state, M_last, state, block_size);
  264. if ((ret = mbedtls_cipher_update(ctx, state, block_size, state,
  265. &olen)) != 0) {
  266. goto exit;
  267. }
  268. memcpy(output, state, block_size);
  269. exit:
  270. /* Wipe the generated keys on the stack, and any other transients to avoid
  271. * side channel leakage */
  272. mbedtls_platform_zeroize(K1, sizeof(K1));
  273. mbedtls_platform_zeroize(K2, sizeof(K2));
  274. cmac_ctx->unprocessed_len = 0;
  275. mbedtls_platform_zeroize(cmac_ctx->unprocessed_block,
  276. sizeof(cmac_ctx->unprocessed_block));
  277. mbedtls_platform_zeroize(state, MBEDTLS_CIPHER_BLKSIZE_MAX);
  278. return ret;
  279. }
  280. int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
  281. {
  282. mbedtls_cmac_context_t *cmac_ctx;
  283. if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL) {
  284. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  285. }
  286. cmac_ctx = ctx->cmac_ctx;
  287. /* Reset the internal state */
  288. cmac_ctx->unprocessed_len = 0;
  289. mbedtls_platform_zeroize(cmac_ctx->unprocessed_block,
  290. sizeof(cmac_ctx->unprocessed_block));
  291. mbedtls_platform_zeroize(cmac_ctx->state,
  292. sizeof(cmac_ctx->state));
  293. return 0;
  294. }
  295. int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info,
  296. const unsigned char *key, size_t keylen,
  297. const unsigned char *input, size_t ilen,
  298. unsigned char *output)
  299. {
  300. mbedtls_cipher_context_t ctx;
  301. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  302. if (cipher_info == NULL || key == NULL || input == NULL || output == NULL) {
  303. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  304. }
  305. mbedtls_cipher_init(&ctx);
  306. if ((ret = mbedtls_cipher_setup(&ctx, cipher_info)) != 0) {
  307. goto exit;
  308. }
  309. ret = mbedtls_cipher_cmac_starts(&ctx, key, keylen);
  310. if (ret != 0) {
  311. goto exit;
  312. }
  313. ret = mbedtls_cipher_cmac_update(&ctx, input, ilen);
  314. if (ret != 0) {
  315. goto exit;
  316. }
  317. ret = mbedtls_cipher_cmac_finish(&ctx, output);
  318. exit:
  319. mbedtls_cipher_free(&ctx);
  320. return ret;
  321. }
  322. #if defined(MBEDTLS_AES_C)
  323. /*
  324. * Implementation of AES-CMAC-PRF-128 defined in RFC 4615
  325. */
  326. int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length,
  327. const unsigned char *input, size_t in_len,
  328. unsigned char output[16])
  329. {
  330. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  331. const mbedtls_cipher_info_t *cipher_info;
  332. unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
  333. unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE];
  334. if (key == NULL || input == NULL || output == NULL) {
  335. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  336. }
  337. cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
  338. if (cipher_info == NULL) {
  339. /* Failing at this point must be due to a build issue */
  340. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  341. goto exit;
  342. }
  343. if (key_length == MBEDTLS_AES_BLOCK_SIZE) {
  344. /* Use key as is */
  345. memcpy(int_key, key, MBEDTLS_AES_BLOCK_SIZE);
  346. } else {
  347. memset(zero_key, 0, MBEDTLS_AES_BLOCK_SIZE);
  348. ret = mbedtls_cipher_cmac(cipher_info, zero_key, 128, key,
  349. key_length, int_key);
  350. if (ret != 0) {
  351. goto exit;
  352. }
  353. }
  354. ret = mbedtls_cipher_cmac(cipher_info, int_key, 128, input, in_len,
  355. output);
  356. exit:
  357. mbedtls_platform_zeroize(int_key, sizeof(int_key));
  358. return ret;
  359. }
  360. #endif /* MBEDTLS_AES_C */
  361. #endif /* !MBEDTLS_CMAC_ALT */
  362. #if defined(MBEDTLS_SELF_TEST)
  363. /*
  364. * CMAC test data for SP800-38B
  365. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf
  366. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf
  367. *
  368. * AES-CMAC-PRF-128 test data from RFC 4615
  369. * https://tools.ietf.org/html/rfc4615#page-4
  370. */
  371. #define NB_CMAC_TESTS_PER_KEY 4
  372. #define NB_PRF_TESTS 3
  373. #if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)
  374. /* All CMAC test inputs are truncated from the same 64 byte buffer. */
  375. static const unsigned char test_message[] = {
  376. /* PT */
  377. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  378. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  379. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  380. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  381. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  382. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  383. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  384. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  385. };
  386. #endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */
  387. #if defined(MBEDTLS_AES_C)
  388. /* Truncation point of message for AES CMAC tests */
  389. static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  390. /* Mlen */
  391. 0,
  392. 16,
  393. 20,
  394. 64
  395. };
  396. /* CMAC-AES128 Test Data */
  397. static const unsigned char aes_128_key[16] = {
  398. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  399. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  400. };
  401. static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  402. {
  403. /* K1 */
  404. 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
  405. 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
  406. },
  407. {
  408. /* K2 */
  409. 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
  410. 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
  411. }
  412. };
  413. static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] =
  414. {
  415. {
  416. /* Example #1 */
  417. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  418. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  419. },
  420. {
  421. /* Example #2 */
  422. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  423. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  424. },
  425. {
  426. /* Example #3 */
  427. 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8,
  428. 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde
  429. },
  430. {
  431. /* Example #4 */
  432. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  433. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  434. }
  435. };
  436. /* CMAC-AES192 Test Data */
  437. static const unsigned char aes_192_key[24] = {
  438. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  439. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  440. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  441. };
  442. static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  443. {
  444. /* K1 */
  445. 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27,
  446. 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96
  447. },
  448. {
  449. /* K2 */
  450. 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e,
  451. 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c
  452. }
  453. };
  454. static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] =
  455. {
  456. {
  457. /* Example #1 */
  458. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  459. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  460. },
  461. {
  462. /* Example #2 */
  463. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  464. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  465. },
  466. {
  467. /* Example #3 */
  468. 0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04,
  469. 0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8
  470. },
  471. {
  472. /* Example #4 */
  473. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  474. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  475. }
  476. };
  477. /* CMAC-AES256 Test Data */
  478. static const unsigned char aes_256_key[32] = {
  479. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  480. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  481. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  482. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  483. };
  484. static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
  485. {
  486. /* K1 */
  487. 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac,
  488. 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f
  489. },
  490. {
  491. /* K2 */
  492. 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58,
  493. 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9
  494. }
  495. };
  496. static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] =
  497. {
  498. {
  499. /* Example #1 */
  500. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  501. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  502. },
  503. {
  504. /* Example #2 */
  505. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  506. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  507. },
  508. {
  509. /* Example #3 */
  510. 0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a,
  511. 0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93
  512. },
  513. {
  514. /* Example #4 */
  515. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  516. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  517. }
  518. };
  519. #endif /* MBEDTLS_AES_C */
  520. #if defined(MBEDTLS_DES_C)
  521. /* Truncation point of message for 3DES CMAC tests */
  522. static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
  523. 0,
  524. 16,
  525. 20,
  526. 32
  527. };
  528. /* CMAC-TDES (Generation) - 2 Key Test Data */
  529. static const unsigned char des3_2key_key[24] = {
  530. /* Key1 */
  531. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  532. /* Key2 */
  533. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01,
  534. /* Key3 */
  535. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
  536. };
  537. static const unsigned char des3_2key_subkeys[2][8] = {
  538. {
  539. /* K1 */
  540. 0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9
  541. },
  542. {
  543. /* K2 */
  544. 0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2
  545. }
  546. };
  547. static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE]
  548. = {
  549. {
  550. /* Sample #1 */
  551. 0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60
  552. },
  553. {
  554. /* Sample #2 */
  555. 0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b
  556. },
  557. {
  558. /* Sample #3 */
  559. 0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69
  560. },
  561. {
  562. /* Sample #4 */
  563. 0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb
  564. }
  565. };
  566. /* CMAC-TDES (Generation) - 3 Key Test Data */
  567. static const unsigned char des3_3key_key[24] = {
  568. /* Key1 */
  569. 0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef,
  570. /* Key2 */
  571. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  572. /* Key3 */
  573. 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
  574. };
  575. static const unsigned char des3_3key_subkeys[2][8] = {
  576. {
  577. /* K1 */
  578. 0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0
  579. },
  580. {
  581. /* K2 */
  582. 0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b
  583. }
  584. };
  585. static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE]
  586. = {
  587. {
  588. /* Sample #1 */
  589. 0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50
  590. },
  591. {
  592. /* Sample #2 */
  593. 0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09
  594. },
  595. {
  596. /* Sample #3 */
  597. 0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2
  598. },
  599. {
  600. /* Sample #4 */
  601. 0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5
  602. }
  603. };
  604. #endif /* MBEDTLS_DES_C */
  605. #if defined(MBEDTLS_AES_C)
  606. /* AES AES-CMAC-PRF-128 Test Data */
  607. static const unsigned char PRFK[] = {
  608. /* Key */
  609. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  610. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  611. 0xed, 0xcb
  612. };
  613. /* Sizes in bytes */
  614. static const size_t PRFKlen[NB_PRF_TESTS] = {
  615. 18,
  616. 16,
  617. 10
  618. };
  619. /* Message */
  620. static const unsigned char PRFM[] = {
  621. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  622. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  623. 0x10, 0x11, 0x12, 0x13
  624. };
  625. static const unsigned char PRFT[NB_PRF_TESTS][16] = {
  626. {
  627. 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b,
  628. 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a
  629. },
  630. {
  631. 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52,
  632. 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d
  633. },
  634. {
  635. 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee,
  636. 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d
  637. }
  638. };
  639. #endif /* MBEDTLS_AES_C */
  640. static int cmac_test_subkeys(int verbose,
  641. const char *testname,
  642. const unsigned char *key,
  643. int keybits,
  644. const unsigned char *subkeys,
  645. mbedtls_cipher_type_t cipher_type,
  646. int block_size,
  647. int num_tests)
  648. {
  649. int i, ret = 0;
  650. mbedtls_cipher_context_t ctx;
  651. const mbedtls_cipher_info_t *cipher_info;
  652. unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
  653. unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
  654. cipher_info = mbedtls_cipher_info_from_type(cipher_type);
  655. if (cipher_info == NULL) {
  656. /* Failing at this point must be due to a build issue */
  657. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  658. }
  659. for (i = 0; i < num_tests; i++) {
  660. if (verbose != 0) {
  661. mbedtls_printf(" %s CMAC subkey #%d: ", testname, i + 1);
  662. }
  663. mbedtls_cipher_init(&ctx);
  664. if ((ret = mbedtls_cipher_setup(&ctx, cipher_info)) != 0) {
  665. if (verbose != 0) {
  666. mbedtls_printf("test execution failed\n");
  667. }
  668. goto cleanup;
  669. }
  670. if ((ret = mbedtls_cipher_setkey(&ctx, key, keybits,
  671. MBEDTLS_ENCRYPT)) != 0) {
  672. /* When CMAC is implemented by an alternative implementation, or
  673. * the underlying primitive itself is implemented alternatively,
  674. * AES-192 may be unavailable. This should not cause the selftest
  675. * function to fail. */
  676. if ((ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ||
  677. ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) &&
  678. cipher_type == MBEDTLS_CIPHER_AES_192_ECB) {
  679. if (verbose != 0) {
  680. mbedtls_printf("skipped\n");
  681. }
  682. goto next_test;
  683. }
  684. if (verbose != 0) {
  685. mbedtls_printf("test execution failed\n");
  686. }
  687. goto cleanup;
  688. }
  689. ret = cmac_generate_subkeys(&ctx, K1, K2);
  690. if (ret != 0) {
  691. if (verbose != 0) {
  692. mbedtls_printf("failed\n");
  693. }
  694. goto cleanup;
  695. }
  696. if ((ret = memcmp(K1, subkeys, block_size)) != 0 ||
  697. (ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) {
  698. if (verbose != 0) {
  699. mbedtls_printf("failed\n");
  700. }
  701. goto cleanup;
  702. }
  703. if (verbose != 0) {
  704. mbedtls_printf("passed\n");
  705. }
  706. next_test:
  707. mbedtls_cipher_free(&ctx);
  708. }
  709. ret = 0;
  710. goto exit;
  711. cleanup:
  712. mbedtls_cipher_free(&ctx);
  713. exit:
  714. return ret;
  715. }
  716. static int cmac_test_wth_cipher(int verbose,
  717. const char *testname,
  718. const unsigned char *key,
  719. int keybits,
  720. const unsigned char *messages,
  721. const unsigned int message_lengths[4],
  722. const unsigned char *expected_result,
  723. mbedtls_cipher_type_t cipher_type,
  724. int block_size,
  725. int num_tests)
  726. {
  727. const mbedtls_cipher_info_t *cipher_info;
  728. int i, ret = 0;
  729. unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
  730. cipher_info = mbedtls_cipher_info_from_type(cipher_type);
  731. if (cipher_info == NULL) {
  732. /* Failing at this point must be due to a build issue */
  733. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  734. goto exit;
  735. }
  736. for (i = 0; i < num_tests; i++) {
  737. if (verbose != 0) {
  738. mbedtls_printf(" %s CMAC #%d: ", testname, i + 1);
  739. }
  740. if ((ret = mbedtls_cipher_cmac(cipher_info, key, keybits, messages,
  741. message_lengths[i], output)) != 0) {
  742. /* When CMAC is implemented by an alternative implementation, or
  743. * the underlying primitive itself is implemented alternatively,
  744. * AES-192 and/or 3DES may be unavailable. This should not cause
  745. * the selftest function to fail. */
  746. if ((ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ||
  747. ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) &&
  748. (cipher_type == MBEDTLS_CIPHER_AES_192_ECB ||
  749. cipher_type == MBEDTLS_CIPHER_DES_EDE3_ECB)) {
  750. if (verbose != 0) {
  751. mbedtls_printf("skipped\n");
  752. }
  753. continue;
  754. }
  755. if (verbose != 0) {
  756. mbedtls_printf("failed\n");
  757. }
  758. goto exit;
  759. }
  760. if ((ret = memcmp(output, &expected_result[i * block_size], block_size)) != 0) {
  761. if (verbose != 0) {
  762. mbedtls_printf("failed\n");
  763. }
  764. goto exit;
  765. }
  766. if (verbose != 0) {
  767. mbedtls_printf("passed\n");
  768. }
  769. }
  770. ret = 0;
  771. exit:
  772. return ret;
  773. }
  774. #if defined(MBEDTLS_AES_C)
  775. static int test_aes128_cmac_prf(int verbose)
  776. {
  777. int i;
  778. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  779. unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
  780. for (i = 0; i < NB_PRF_TESTS; i++) {
  781. mbedtls_printf(" AES CMAC 128 PRF #%d: ", i);
  782. ret = mbedtls_aes_cmac_prf_128(PRFK, PRFKlen[i], PRFM, 20, output);
  783. if (ret != 0 ||
  784. memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) {
  785. if (verbose != 0) {
  786. mbedtls_printf("failed\n");
  787. }
  788. return ret;
  789. } else if (verbose != 0) {
  790. mbedtls_printf("passed\n");
  791. }
  792. }
  793. return ret;
  794. }
  795. #endif /* MBEDTLS_AES_C */
  796. int mbedtls_cmac_self_test(int verbose)
  797. {
  798. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  799. #if defined(MBEDTLS_AES_C)
  800. /* AES-128 */
  801. if ((ret = cmac_test_subkeys(verbose,
  802. "AES 128",
  803. aes_128_key,
  804. 128,
  805. (const unsigned char *) aes_128_subkeys,
  806. MBEDTLS_CIPHER_AES_128_ECB,
  807. MBEDTLS_AES_BLOCK_SIZE,
  808. NB_CMAC_TESTS_PER_KEY)) != 0) {
  809. return ret;
  810. }
  811. if ((ret = cmac_test_wth_cipher(verbose,
  812. "AES 128",
  813. aes_128_key,
  814. 128,
  815. test_message,
  816. aes_message_lengths,
  817. (const unsigned char *) aes_128_expected_result,
  818. MBEDTLS_CIPHER_AES_128_ECB,
  819. MBEDTLS_AES_BLOCK_SIZE,
  820. NB_CMAC_TESTS_PER_KEY)) != 0) {
  821. return ret;
  822. }
  823. /* AES-192 */
  824. if ((ret = cmac_test_subkeys(verbose,
  825. "AES 192",
  826. aes_192_key,
  827. 192,
  828. (const unsigned char *) aes_192_subkeys,
  829. MBEDTLS_CIPHER_AES_192_ECB,
  830. MBEDTLS_AES_BLOCK_SIZE,
  831. NB_CMAC_TESTS_PER_KEY)) != 0) {
  832. return ret;
  833. }
  834. if ((ret = cmac_test_wth_cipher(verbose,
  835. "AES 192",
  836. aes_192_key,
  837. 192,
  838. test_message,
  839. aes_message_lengths,
  840. (const unsigned char *) aes_192_expected_result,
  841. MBEDTLS_CIPHER_AES_192_ECB,
  842. MBEDTLS_AES_BLOCK_SIZE,
  843. NB_CMAC_TESTS_PER_KEY)) != 0) {
  844. return ret;
  845. }
  846. /* AES-256 */
  847. if ((ret = cmac_test_subkeys(verbose,
  848. "AES 256",
  849. aes_256_key,
  850. 256,
  851. (const unsigned char *) aes_256_subkeys,
  852. MBEDTLS_CIPHER_AES_256_ECB,
  853. MBEDTLS_AES_BLOCK_SIZE,
  854. NB_CMAC_TESTS_PER_KEY)) != 0) {
  855. return ret;
  856. }
  857. if ((ret = cmac_test_wth_cipher(verbose,
  858. "AES 256",
  859. aes_256_key,
  860. 256,
  861. test_message,
  862. aes_message_lengths,
  863. (const unsigned char *) aes_256_expected_result,
  864. MBEDTLS_CIPHER_AES_256_ECB,
  865. MBEDTLS_AES_BLOCK_SIZE,
  866. NB_CMAC_TESTS_PER_KEY)) != 0) {
  867. return ret;
  868. }
  869. #endif /* MBEDTLS_AES_C */
  870. #if defined(MBEDTLS_DES_C)
  871. /* 3DES 2 key */
  872. if ((ret = cmac_test_subkeys(verbose,
  873. "3DES 2 key",
  874. des3_2key_key,
  875. 192,
  876. (const unsigned char *) des3_2key_subkeys,
  877. MBEDTLS_CIPHER_DES_EDE3_ECB,
  878. MBEDTLS_DES3_BLOCK_SIZE,
  879. NB_CMAC_TESTS_PER_KEY)) != 0) {
  880. return ret;
  881. }
  882. if ((ret = cmac_test_wth_cipher(verbose,
  883. "3DES 2 key",
  884. des3_2key_key,
  885. 192,
  886. test_message,
  887. des3_message_lengths,
  888. (const unsigned char *) des3_2key_expected_result,
  889. MBEDTLS_CIPHER_DES_EDE3_ECB,
  890. MBEDTLS_DES3_BLOCK_SIZE,
  891. NB_CMAC_TESTS_PER_KEY)) != 0) {
  892. return ret;
  893. }
  894. /* 3DES 3 key */
  895. if ((ret = cmac_test_subkeys(verbose,
  896. "3DES 3 key",
  897. des3_3key_key,
  898. 192,
  899. (const unsigned char *) des3_3key_subkeys,
  900. MBEDTLS_CIPHER_DES_EDE3_ECB,
  901. MBEDTLS_DES3_BLOCK_SIZE,
  902. NB_CMAC_TESTS_PER_KEY)) != 0) {
  903. return ret;
  904. }
  905. if ((ret = cmac_test_wth_cipher(verbose,
  906. "3DES 3 key",
  907. des3_3key_key,
  908. 192,
  909. test_message,
  910. des3_message_lengths,
  911. (const unsigned char *) des3_3key_expected_result,
  912. MBEDTLS_CIPHER_DES_EDE3_ECB,
  913. MBEDTLS_DES3_BLOCK_SIZE,
  914. NB_CMAC_TESTS_PER_KEY)) != 0) {
  915. return ret;
  916. }
  917. #endif /* MBEDTLS_DES_C */
  918. #if defined(MBEDTLS_AES_C)
  919. if ((ret = test_aes128_cmac_prf(verbose)) != 0) {
  920. return ret;
  921. }
  922. #endif /* MBEDTLS_AES_C */
  923. if (verbose != 0) {
  924. mbedtls_printf("\n");
  925. }
  926. return 0;
  927. }
  928. #endif /* MBEDTLS_SELF_TEST */
  929. #endif /* MBEDTLS_CMAC_C */