cmac.c 33 KB

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