cmac.c 33 KB

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