ccm.c 22 KB

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