cmac.c 32 KB

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