ccm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * NIST SP800-38C compliant CCM implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * Definition of CCM:
  9. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  10. * RFC 3610 "Counter with CBC-MAC (CCM)"
  11. *
  12. * Related:
  13. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  14. */
  15. #include "common.h"
  16. #if defined(MBEDTLS_CCM_C)
  17. #include "mbedtls/ccm.h"
  18. #include "mbedtls/platform_util.h"
  19. #include "mbedtls/error.h"
  20. #include "mbedtls/constant_time.h"
  21. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  22. #include "block_cipher_internal.h"
  23. #endif
  24. #include <string.h>
  25. #if defined(MBEDTLS_PLATFORM_C)
  26. #include "mbedtls/platform.h"
  27. #else
  28. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  29. #include <stdio.h>
  30. #define mbedtls_printf printf
  31. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  32. #endif /* MBEDTLS_PLATFORM_C */
  33. #if !defined(MBEDTLS_CCM_ALT)
  34. /*
  35. * Initialize context
  36. */
  37. void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
  38. {
  39. memset(ctx, 0, sizeof(mbedtls_ccm_context));
  40. }
  41. int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
  42. mbedtls_cipher_id_t cipher,
  43. const unsigned char *key,
  44. unsigned int keybits)
  45. {
  46. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  47. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  48. mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
  49. if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) {
  50. return MBEDTLS_ERR_CCM_BAD_INPUT;
  51. }
  52. if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) {
  53. return MBEDTLS_ERR_CCM_BAD_INPUT;
  54. }
  55. #else
  56. const mbedtls_cipher_info_t *cipher_info;
  57. cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
  58. MBEDTLS_MODE_ECB);
  59. if (cipher_info == NULL) {
  60. return MBEDTLS_ERR_CCM_BAD_INPUT;
  61. }
  62. if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) {
  63. return MBEDTLS_ERR_CCM_BAD_INPUT;
  64. }
  65. mbedtls_cipher_free(&ctx->cipher_ctx);
  66. if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
  67. return ret;
  68. }
  69. if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
  70. MBEDTLS_ENCRYPT)) != 0) {
  71. return ret;
  72. }
  73. #endif
  74. return ret;
  75. }
  76. /*
  77. * Free context
  78. */
  79. void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
  80. {
  81. if (ctx == NULL) {
  82. return;
  83. }
  84. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  85. mbedtls_block_cipher_free(&ctx->block_cipher_ctx);
  86. #else
  87. mbedtls_cipher_free(&ctx->cipher_ctx);
  88. #endif
  89. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
  90. }
  91. #define CCM_STATE__CLEAR 0
  92. #define CCM_STATE__STARTED (1 << 0)
  93. #define CCM_STATE__LENGTHS_SET (1 << 1)
  94. #define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
  95. #define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
  96. #define CCM_STATE__ERROR (1 << 4)
  97. /*
  98. * Encrypt or decrypt a partial block with CTR
  99. */
  100. static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx,
  101. size_t offset, size_t use_len,
  102. const unsigned char *input,
  103. unsigned char *output)
  104. {
  105. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  106. unsigned char tmp_buf[16] = { 0 };
  107. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  108. ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf);
  109. #else
  110. size_t olen = 0;
  111. ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen);
  112. #endif
  113. if (ret != 0) {
  114. ctx->state |= CCM_STATE__ERROR;
  115. mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
  116. return ret;
  117. }
  118. mbedtls_xor(output, input, tmp_buf + offset, use_len);
  119. mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
  120. return ret;
  121. }
  122. static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx)
  123. {
  124. ctx->state = CCM_STATE__CLEAR;
  125. memset(ctx->y, 0, 16);
  126. memset(ctx->ctr, 0, 16);
  127. }
  128. static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
  129. {
  130. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  131. unsigned char i;
  132. size_t len_left;
  133. #if !defined(MBEDTLS_BLOCK_CIPHER_C)
  134. size_t olen;
  135. #endif
  136. /* length calculation can be done only after both
  137. * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
  138. */
  139. if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) {
  140. return 0;
  141. }
  142. /* CCM expects non-empty tag.
  143. * CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
  144. */
  145. if (ctx->tag_len == 0) {
  146. if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
  147. ctx->plaintext_len = 0;
  148. } else {
  149. return MBEDTLS_ERR_CCM_BAD_INPUT;
  150. }
  151. }
  152. /*
  153. * First block:
  154. * 0 .. 0 flags
  155. * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
  156. * iv_len+1 .. 15 length
  157. *
  158. * With flags as (bits):
  159. * 7 0
  160. * 6 add present?
  161. * 5 .. 3 (t - 2) / 2
  162. * 2 .. 0 q - 1
  163. */
  164. ctx->y[0] |= (ctx->add_len > 0) << 6;
  165. ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3;
  166. ctx->y[0] |= ctx->q - 1;
  167. for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) {
  168. ctx->y[15-i] = MBEDTLS_BYTE_0(len_left);
  169. }
  170. if (len_left > 0) {
  171. ctx->state |= CCM_STATE__ERROR;
  172. return MBEDTLS_ERR_CCM_BAD_INPUT;
  173. }
  174. /* Start CBC-MAC with first block*/
  175. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  176. ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
  177. #else
  178. ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
  179. #endif
  180. if (ret != 0) {
  181. ctx->state |= CCM_STATE__ERROR;
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. int mbedtls_ccm_starts(mbedtls_ccm_context *ctx,
  187. int mode,
  188. const unsigned char *iv,
  189. size_t iv_len)
  190. {
  191. /* Also implies q is within bounds */
  192. if (iv_len < 7 || iv_len > 13) {
  193. return MBEDTLS_ERR_CCM_BAD_INPUT;
  194. }
  195. ctx->mode = mode;
  196. ctx->q = 16 - 1 - (unsigned char) iv_len;
  197. /*
  198. * Prepare counter block for encryption:
  199. * 0 .. 0 flags
  200. * 1 .. iv_len nonce (aka iv)
  201. * iv_len+1 .. 15 counter (initially 1)
  202. *
  203. * With flags as (bits):
  204. * 7 .. 3 0
  205. * 2 .. 0 q - 1
  206. */
  207. memset(ctx->ctr, 0, 16);
  208. ctx->ctr[0] = ctx->q - 1;
  209. memcpy(ctx->ctr + 1, iv, iv_len);
  210. memset(ctx->ctr + 1 + iv_len, 0, ctx->q);
  211. ctx->ctr[15] = 1;
  212. /*
  213. * See ccm_calculate_first_block_if_ready() for block layout description
  214. */
  215. memcpy(ctx->y + 1, iv, iv_len);
  216. ctx->state |= CCM_STATE__STARTED;
  217. return ccm_calculate_first_block_if_ready(ctx);
  218. }
  219. int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx,
  220. size_t total_ad_len,
  221. size_t plaintext_len,
  222. size_t tag_len)
  223. {
  224. /*
  225. * Check length requirements: SP800-38C A.1
  226. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  227. * 'length' checked later (when writing it to the first block)
  228. *
  229. * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
  230. */
  231. if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
  232. return MBEDTLS_ERR_CCM_BAD_INPUT;
  233. }
  234. if (total_ad_len >= 0xFF00) {
  235. return MBEDTLS_ERR_CCM_BAD_INPUT;
  236. }
  237. ctx->plaintext_len = plaintext_len;
  238. ctx->add_len = total_ad_len;
  239. ctx->tag_len = tag_len;
  240. ctx->processed = 0;
  241. ctx->state |= CCM_STATE__LENGTHS_SET;
  242. return ccm_calculate_first_block_if_ready(ctx);
  243. }
  244. int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx,
  245. const unsigned char *add,
  246. size_t add_len)
  247. {
  248. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  249. size_t use_len, offset;
  250. #if !defined(MBEDTLS_BLOCK_CIPHER_C)
  251. size_t olen;
  252. #endif
  253. if (ctx->state & CCM_STATE__ERROR) {
  254. return MBEDTLS_ERR_CCM_BAD_INPUT;
  255. }
  256. if (add_len > 0) {
  257. if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) {
  258. return MBEDTLS_ERR_CCM_BAD_INPUT;
  259. }
  260. if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) {
  261. if (add_len > ctx->add_len) {
  262. return MBEDTLS_ERR_CCM_BAD_INPUT;
  263. }
  264. ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF);
  265. ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF);
  266. ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
  267. } else if (ctx->processed + add_len > ctx->add_len) {
  268. return MBEDTLS_ERR_CCM_BAD_INPUT;
  269. }
  270. while (add_len > 0) {
  271. offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
  272. * holding total auth data length */
  273. use_len = 16 - offset;
  274. if (use_len > add_len) {
  275. use_len = add_len;
  276. }
  277. mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len);
  278. ctx->processed += use_len;
  279. add_len -= use_len;
  280. add += use_len;
  281. if (use_len + offset == 16 || ctx->processed == ctx->add_len) {
  282. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  283. ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
  284. #else
  285. ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
  286. #endif
  287. if (ret != 0) {
  288. ctx->state |= CCM_STATE__ERROR;
  289. return ret;
  290. }
  291. }
  292. }
  293. if (ctx->processed == ctx->add_len) {
  294. ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
  295. ctx->processed = 0; // prepare for mbedtls_ccm_update()
  296. }
  297. }
  298. return 0;
  299. }
  300. int mbedtls_ccm_update(mbedtls_ccm_context *ctx,
  301. const unsigned char *input, size_t input_len,
  302. unsigned char *output, size_t output_size,
  303. size_t *output_len)
  304. {
  305. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  306. unsigned char i;
  307. size_t use_len, offset;
  308. #if !defined(MBEDTLS_BLOCK_CIPHER_C)
  309. size_t olen;
  310. #endif
  311. unsigned char local_output[16];
  312. if (ctx->state & CCM_STATE__ERROR) {
  313. return MBEDTLS_ERR_CCM_BAD_INPUT;
  314. }
  315. /* Check against plaintext length only if performing operation with
  316. * authentication
  317. */
  318. if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) {
  319. return MBEDTLS_ERR_CCM_BAD_INPUT;
  320. }
  321. if (output_size < input_len) {
  322. return MBEDTLS_ERR_CCM_BAD_INPUT;
  323. }
  324. *output_len = input_len;
  325. ret = 0;
  326. while (input_len > 0) {
  327. offset = ctx->processed % 16;
  328. use_len = 16 - offset;
  329. if (use_len > input_len) {
  330. use_len = input_len;
  331. }
  332. ctx->processed += use_len;
  333. if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \
  334. ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) {
  335. mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len);
  336. if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
  337. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  338. ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
  339. #else
  340. ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
  341. #endif
  342. if (ret != 0) {
  343. ctx->state |= CCM_STATE__ERROR;
  344. goto exit;
  345. }
  346. }
  347. ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output);
  348. if (ret != 0) {
  349. goto exit;
  350. }
  351. }
  352. if (ctx->mode == MBEDTLS_CCM_DECRYPT || \
  353. ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) {
  354. /* Since output may be in shared memory, we cannot be sure that
  355. * it will contain what we wrote to it. Therefore, we should avoid using
  356. * it as input to any operations.
  357. * Write decrypted data to local_output to avoid using output variable as
  358. * input in the XOR operation for Y.
  359. */
  360. ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output);
  361. if (ret != 0) {
  362. goto exit;
  363. }
  364. mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len);
  365. memcpy(output, local_output, use_len);
  366. if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
  367. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  368. ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y);
  369. #else
  370. ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen);
  371. #endif
  372. if (ret != 0) {
  373. ctx->state |= CCM_STATE__ERROR;
  374. goto exit;
  375. }
  376. }
  377. }
  378. if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) {
  379. for (i = 0; i < ctx->q; i++) {
  380. if (++(ctx->ctr)[15-i] != 0) {
  381. break;
  382. }
  383. }
  384. }
  385. input_len -= use_len;
  386. input += use_len;
  387. output += use_len;
  388. }
  389. exit:
  390. mbedtls_platform_zeroize(local_output, 16);
  391. return ret;
  392. }
  393. int mbedtls_ccm_finish(mbedtls_ccm_context *ctx,
  394. unsigned char *tag, size_t tag_len)
  395. {
  396. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  397. unsigned char i;
  398. if (ctx->state & CCM_STATE__ERROR) {
  399. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  400. }
  401. if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) {
  402. return MBEDTLS_ERR_CCM_BAD_INPUT;
  403. }
  404. if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) {
  405. return MBEDTLS_ERR_CCM_BAD_INPUT;
  406. }
  407. /*
  408. * Authentication: reset counter and crypt/mask internal tag
  409. */
  410. for (i = 0; i < ctx->q; i++) {
  411. ctx->ctr[15-i] = 0;
  412. }
  413. ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y);
  414. if (ret != 0) {
  415. return ret;
  416. }
  417. if (tag != NULL) {
  418. memcpy(tag, ctx->y, tag_len);
  419. }
  420. mbedtls_ccm_clear_state(ctx);
  421. return 0;
  422. }
  423. /*
  424. * Authenticated encryption or decryption
  425. */
  426. static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
  427. const unsigned char *iv, size_t iv_len,
  428. const unsigned char *add, size_t add_len,
  429. const unsigned char *input, unsigned char *output,
  430. unsigned char *tag, size_t tag_len)
  431. {
  432. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  433. size_t olen;
  434. if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) {
  435. return ret;
  436. }
  437. if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) {
  438. return ret;
  439. }
  440. if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) {
  441. return ret;
  442. }
  443. if ((ret = mbedtls_ccm_update(ctx, input, length,
  444. output, length, &olen)) != 0) {
  445. return ret;
  446. }
  447. if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) {
  448. return ret;
  449. }
  450. return 0;
  451. }
  452. /*
  453. * Authenticated encryption
  454. */
  455. int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
  456. const unsigned char *iv, size_t iv_len,
  457. const unsigned char *add, size_t add_len,
  458. const unsigned char *input, unsigned char *output,
  459. unsigned char *tag, size_t tag_len)
  460. {
  461. return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
  462. add, add_len, input, output, tag, tag_len);
  463. }
  464. int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
  465. const unsigned char *iv, size_t iv_len,
  466. const unsigned char *add, size_t add_len,
  467. const unsigned char *input, unsigned char *output,
  468. unsigned char *tag, size_t tag_len)
  469. {
  470. return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
  471. add, add_len, input, output, tag, tag_len);
  472. }
  473. /*
  474. * Authenticated decryption
  475. */
  476. static int mbedtls_ccm_compare_tags(const unsigned char *tag1,
  477. const unsigned char *tag2,
  478. size_t tag_len)
  479. {
  480. /* Check tag in "constant-time" */
  481. int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len);
  482. if (diff != 0) {
  483. return MBEDTLS_ERR_CCM_AUTH_FAILED;
  484. }
  485. return 0;
  486. }
  487. static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length,
  488. const unsigned char *iv, size_t iv_len,
  489. const unsigned char *add, size_t add_len,
  490. const unsigned char *input, unsigned char *output,
  491. const unsigned char *tag, size_t tag_len)
  492. {
  493. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  494. unsigned char check_tag[16];
  495. if ((ret = ccm_auth_crypt(ctx, mode, length,
  496. iv, iv_len, add, add_len,
  497. input, output, check_tag, tag_len)) != 0) {
  498. return ret;
  499. }
  500. if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) {
  501. mbedtls_platform_zeroize(output, length);
  502. return ret;
  503. }
  504. return 0;
  505. }
  506. int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
  507. const unsigned char *iv, size_t iv_len,
  508. const unsigned char *add, size_t add_len,
  509. const unsigned char *input, unsigned char *output,
  510. const unsigned char *tag, size_t tag_len)
  511. {
  512. return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
  513. iv, iv_len, add, add_len,
  514. input, output, tag, tag_len);
  515. }
  516. int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
  517. const unsigned char *iv, size_t iv_len,
  518. const unsigned char *add, size_t add_len,
  519. const unsigned char *input, unsigned char *output,
  520. const unsigned char *tag, size_t tag_len)
  521. {
  522. return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length,
  523. iv, iv_len, add, add_len,
  524. input, output, tag, tag_len);
  525. }
  526. #endif /* !MBEDTLS_CCM_ALT */
  527. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_CCM_GCM_CAN_AES)
  528. /*
  529. * Examples 1 to 3 from SP800-38C Appendix C
  530. */
  531. #define NB_TESTS 3
  532. #define CCM_SELFTEST_PT_MAX_LEN 24
  533. #define CCM_SELFTEST_CT_MAX_LEN 32
  534. /*
  535. * The data is the same for all tests, only the used length changes
  536. */
  537. static const unsigned char key_test_data[] = {
  538. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  539. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  540. };
  541. static const unsigned char iv_test_data[] = {
  542. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  543. 0x18, 0x19, 0x1a, 0x1b
  544. };
  545. static const unsigned char ad_test_data[] = {
  546. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  547. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  548. 0x10, 0x11, 0x12, 0x13
  549. };
  550. static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
  551. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  552. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  553. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  554. };
  555. static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
  556. static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
  557. static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
  558. static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
  559. static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
  560. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  561. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  562. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  563. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  564. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  565. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  566. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  567. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  568. };
  569. int mbedtls_ccm_self_test(int verbose)
  570. {
  571. mbedtls_ccm_context ctx;
  572. /*
  573. * Some hardware accelerators require the input and output buffers
  574. * would be in RAM, because the flash is not accessible.
  575. * Use buffers on the stack to hold the test vectors data.
  576. */
  577. unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
  578. unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
  579. size_t i;
  580. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  581. mbedtls_ccm_init(&ctx);
  582. if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
  583. 8 * sizeof(key_test_data)) != 0) {
  584. if (verbose != 0) {
  585. mbedtls_printf(" CCM: setup failed");
  586. }
  587. return 1;
  588. }
  589. for (i = 0; i < NB_TESTS; i++) {
  590. if (verbose != 0) {
  591. mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
  592. }
  593. memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
  594. memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
  595. memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
  596. ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
  597. iv_test_data, iv_len_test_data[i],
  598. ad_test_data, add_len_test_data[i],
  599. plaintext, ciphertext,
  600. ciphertext + msg_len_test_data[i],
  601. tag_len_test_data[i]);
  602. if (ret != 0 ||
  603. memcmp(ciphertext, res_test_data[i],
  604. msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
  605. if (verbose != 0) {
  606. mbedtls_printf("failed\n");
  607. }
  608. return 1;
  609. }
  610. memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
  611. ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
  612. iv_test_data, iv_len_test_data[i],
  613. ad_test_data, add_len_test_data[i],
  614. ciphertext, plaintext,
  615. ciphertext + msg_len_test_data[i],
  616. tag_len_test_data[i]);
  617. if (ret != 0 ||
  618. memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
  619. if (verbose != 0) {
  620. mbedtls_printf("failed\n");
  621. }
  622. return 1;
  623. }
  624. if (verbose != 0) {
  625. mbedtls_printf("passed\n");
  626. }
  627. }
  628. mbedtls_ccm_free(&ctx);
  629. if (verbose != 0) {
  630. mbedtls_printf("\n");
  631. }
  632. return 0;
  633. }
  634. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  635. #endif /* MBEDTLS_CCM_C */