ccm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. #include <string.h>
  22. #include "mbedtls/platform.h"
  23. #if !defined(MBEDTLS_CCM_ALT)
  24. #define CCM_VALIDATE_RET(cond) \
  25. MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CCM_BAD_INPUT)
  26. #define CCM_VALIDATE(cond) \
  27. MBEDTLS_INTERNAL_VALIDATE(cond)
  28. #define CCM_ENCRYPT 0
  29. #define CCM_DECRYPT 1
  30. /*
  31. * Initialize context
  32. */
  33. void mbedtls_ccm_init(mbedtls_ccm_context *ctx)
  34. {
  35. CCM_VALIDATE(ctx != NULL);
  36. memset(ctx, 0, sizeof(mbedtls_ccm_context));
  37. }
  38. int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx,
  39. mbedtls_cipher_id_t cipher,
  40. const unsigned char *key,
  41. unsigned int keybits)
  42. {
  43. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  44. const mbedtls_cipher_info_t *cipher_info;
  45. CCM_VALIDATE_RET(ctx != NULL);
  46. CCM_VALIDATE_RET(key != NULL);
  47. cipher_info = mbedtls_cipher_info_from_values(cipher, keybits,
  48. MBEDTLS_MODE_ECB);
  49. if (cipher_info == NULL) {
  50. return MBEDTLS_ERR_CCM_BAD_INPUT;
  51. }
  52. if (cipher_info->block_size != 16) {
  53. return MBEDTLS_ERR_CCM_BAD_INPUT;
  54. }
  55. mbedtls_cipher_free(&ctx->cipher_ctx);
  56. if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) {
  57. return ret;
  58. }
  59. if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits,
  60. MBEDTLS_ENCRYPT)) != 0) {
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. /*
  66. * Free context
  67. */
  68. void mbedtls_ccm_free(mbedtls_ccm_context *ctx)
  69. {
  70. if (ctx == NULL) {
  71. return;
  72. }
  73. mbedtls_cipher_free(&ctx->cipher_ctx);
  74. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context));
  75. }
  76. /*
  77. * Macros for common operations.
  78. * Results in smaller compiled code than static inline functions.
  79. */
  80. /*
  81. * Update the CBC-MAC state in y using a block in b
  82. * (Always using b as the source helps the compiler optimise a bit better.)
  83. */
  84. #define UPDATE_CBC_MAC \
  85. for (i = 0; i < 16; i++) \
  86. y[i] ^= b[i]; \
  87. \
  88. if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, y, 16, y, &olen)) != 0) \
  89. return ret;
  90. /*
  91. * Encrypt or decrypt a partial block with CTR
  92. * Warning: using b for temporary storage! src and dst must not be b!
  93. * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  94. */
  95. #define CTR_CRYPT(dst, src, len) \
  96. do \
  97. { \
  98. if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctr, \
  99. 16, b, &olen)) != 0) \
  100. { \
  101. return ret; \
  102. } \
  103. \
  104. for (i = 0; i < (len); i++) \
  105. (dst)[i] = (src)[i] ^ b[i]; \
  106. } while (0)
  107. /*
  108. * Authenticated encryption or decryption
  109. */
  110. static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length,
  111. const unsigned char *iv, size_t iv_len,
  112. const unsigned char *add, size_t add_len,
  113. const unsigned char *input, unsigned char *output,
  114. unsigned char *tag, size_t tag_len)
  115. {
  116. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  117. unsigned char i;
  118. unsigned char q;
  119. size_t len_left, olen;
  120. unsigned char b[16];
  121. unsigned char y[16];
  122. unsigned char ctr[16];
  123. const unsigned char *src;
  124. unsigned char *dst;
  125. /*
  126. * Check length requirements: SP800-38C A.1
  127. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  128. * 'length' checked later (when writing it to the first block)
  129. *
  130. * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
  131. */
  132. if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) {
  133. return MBEDTLS_ERR_CCM_BAD_INPUT;
  134. }
  135. /* Also implies q is within bounds */
  136. if (iv_len < 7 || iv_len > 13) {
  137. return MBEDTLS_ERR_CCM_BAD_INPUT;
  138. }
  139. if (add_len >= 0xFF00) {
  140. return MBEDTLS_ERR_CCM_BAD_INPUT;
  141. }
  142. q = 16 - 1 - (unsigned char) iv_len;
  143. /*
  144. * First block B_0:
  145. * 0 .. 0 flags
  146. * 1 .. iv_len nonce (aka iv)
  147. * iv_len+1 .. 15 length
  148. *
  149. * With flags as (bits):
  150. * 7 0
  151. * 6 add present?
  152. * 5 .. 3 (t - 2) / 2
  153. * 2 .. 0 q - 1
  154. */
  155. b[0] = 0;
  156. b[0] |= (add_len > 0) << 6;
  157. b[0] |= ((tag_len - 2) / 2) << 3;
  158. b[0] |= q - 1;
  159. memcpy(b + 1, iv, iv_len);
  160. for (i = 0, len_left = length; i < q; i++, len_left >>= 8) {
  161. b[15-i] = MBEDTLS_BYTE_0(len_left);
  162. }
  163. if (len_left > 0) {
  164. return MBEDTLS_ERR_CCM_BAD_INPUT;
  165. }
  166. /* Start CBC-MAC with first block */
  167. memset(y, 0, 16);
  168. UPDATE_CBC_MAC;
  169. /*
  170. * If there is additional data, update CBC-MAC with
  171. * add_len, add, 0 (padding to a block boundary)
  172. */
  173. if (add_len > 0) {
  174. size_t use_len;
  175. len_left = add_len;
  176. src = add;
  177. memset(b, 0, 16);
  178. MBEDTLS_PUT_UINT16_BE(add_len, b, 0);
  179. use_len = len_left < 16 - 2 ? len_left : 16 - 2;
  180. memcpy(b + 2, src, use_len);
  181. len_left -= use_len;
  182. src += use_len;
  183. UPDATE_CBC_MAC;
  184. while (len_left > 0) {
  185. use_len = len_left > 16 ? 16 : len_left;
  186. memset(b, 0, 16);
  187. memcpy(b, src, use_len);
  188. UPDATE_CBC_MAC;
  189. len_left -= use_len;
  190. src += use_len;
  191. }
  192. }
  193. /*
  194. * Prepare counter block for encryption:
  195. * 0 .. 0 flags
  196. * 1 .. iv_len nonce (aka iv)
  197. * iv_len+1 .. 15 counter (initially 1)
  198. *
  199. * With flags as (bits):
  200. * 7 .. 3 0
  201. * 2 .. 0 q - 1
  202. */
  203. ctr[0] = q - 1;
  204. memcpy(ctr + 1, iv, iv_len);
  205. memset(ctr + 1 + iv_len, 0, q);
  206. ctr[15] = 1;
  207. /*
  208. * Authenticate and {en,de}crypt the message.
  209. *
  210. * The only difference between encryption and decryption is
  211. * the respective order of authentication and {en,de}cryption.
  212. */
  213. len_left = length;
  214. src = input;
  215. dst = output;
  216. while (len_left > 0) {
  217. size_t use_len = len_left > 16 ? 16 : len_left;
  218. if (mode == CCM_ENCRYPT) {
  219. memset(b, 0, 16);
  220. memcpy(b, src, use_len);
  221. UPDATE_CBC_MAC;
  222. }
  223. CTR_CRYPT(dst, src, use_len);
  224. if (mode == CCM_DECRYPT) {
  225. memset(b, 0, 16);
  226. memcpy(b, dst, use_len);
  227. UPDATE_CBC_MAC;
  228. }
  229. dst += use_len;
  230. src += use_len;
  231. len_left -= use_len;
  232. /*
  233. * Increment counter.
  234. * No need to check for overflow thanks to the length check above.
  235. */
  236. for (i = 0; i < q; i++) {
  237. if (++ctr[15-i] != 0) {
  238. break;
  239. }
  240. }
  241. }
  242. /*
  243. * Authentication: reset counter and crypt/mask internal tag
  244. */
  245. for (i = 0; i < q; i++) {
  246. ctr[15-i] = 0;
  247. }
  248. CTR_CRYPT(y, y, 16);
  249. memcpy(tag, y, tag_len);
  250. return 0;
  251. }
  252. /*
  253. * Authenticated encryption
  254. */
  255. int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
  256. const unsigned char *iv, size_t iv_len,
  257. const unsigned char *add, size_t add_len,
  258. const unsigned char *input, unsigned char *output,
  259. unsigned char *tag, size_t tag_len)
  260. {
  261. CCM_VALIDATE_RET(ctx != NULL);
  262. CCM_VALIDATE_RET(iv != NULL);
  263. CCM_VALIDATE_RET(add_len == 0 || add != NULL);
  264. CCM_VALIDATE_RET(length == 0 || input != NULL);
  265. CCM_VALIDATE_RET(length == 0 || output != NULL);
  266. CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
  267. return ccm_auth_crypt(ctx, CCM_ENCRYPT, length, iv, iv_len,
  268. add, add_len, input, output, tag, tag_len);
  269. }
  270. int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length,
  271. const unsigned char *iv, size_t iv_len,
  272. const unsigned char *add, size_t add_len,
  273. const unsigned char *input, unsigned char *output,
  274. unsigned char *tag, size_t tag_len)
  275. {
  276. CCM_VALIDATE_RET(ctx != NULL);
  277. CCM_VALIDATE_RET(iv != NULL);
  278. CCM_VALIDATE_RET(add_len == 0 || add != NULL);
  279. CCM_VALIDATE_RET(length == 0 || input != NULL);
  280. CCM_VALIDATE_RET(length == 0 || output != NULL);
  281. CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
  282. if (tag_len == 0) {
  283. return MBEDTLS_ERR_CCM_BAD_INPUT;
  284. }
  285. return mbedtls_ccm_star_encrypt_and_tag(ctx, length, iv, iv_len, add,
  286. add_len, input, output, tag, tag_len);
  287. }
  288. /*
  289. * Authenticated decryption
  290. */
  291. int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
  292. const unsigned char *iv, size_t iv_len,
  293. const unsigned char *add, size_t add_len,
  294. const unsigned char *input, unsigned char *output,
  295. const unsigned char *tag, size_t tag_len)
  296. {
  297. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  298. unsigned char check_tag[16];
  299. int diff;
  300. CCM_VALIDATE_RET(ctx != NULL);
  301. CCM_VALIDATE_RET(iv != NULL);
  302. CCM_VALIDATE_RET(add_len == 0 || add != NULL);
  303. CCM_VALIDATE_RET(length == 0 || input != NULL);
  304. CCM_VALIDATE_RET(length == 0 || output != NULL);
  305. CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
  306. if ((ret = ccm_auth_crypt(ctx, CCM_DECRYPT, length,
  307. iv, iv_len, add, add_len,
  308. input, output, check_tag, tag_len)) != 0) {
  309. return ret;
  310. }
  311. /* Check tag in "constant-time" */
  312. diff = mbedtls_ct_memcmp(tag, check_tag, tag_len);
  313. if (diff != 0) {
  314. mbedtls_platform_zeroize(output, length);
  315. return MBEDTLS_ERR_CCM_AUTH_FAILED;
  316. }
  317. return 0;
  318. }
  319. int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length,
  320. const unsigned char *iv, size_t iv_len,
  321. const unsigned char *add, size_t add_len,
  322. const unsigned char *input, unsigned char *output,
  323. const unsigned char *tag, size_t tag_len)
  324. {
  325. CCM_VALIDATE_RET(ctx != NULL);
  326. CCM_VALIDATE_RET(iv != NULL);
  327. CCM_VALIDATE_RET(add_len == 0 || add != NULL);
  328. CCM_VALIDATE_RET(length == 0 || input != NULL);
  329. CCM_VALIDATE_RET(length == 0 || output != NULL);
  330. CCM_VALIDATE_RET(tag_len == 0 || tag != NULL);
  331. if (tag_len == 0) {
  332. return MBEDTLS_ERR_CCM_BAD_INPUT;
  333. }
  334. return mbedtls_ccm_star_auth_decrypt(ctx, length, iv, iv_len, add,
  335. add_len, input, output, tag, tag_len);
  336. }
  337. #endif /* !MBEDTLS_CCM_ALT */
  338. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  339. /*
  340. * Examples 1 to 3 from SP800-38C Appendix C
  341. */
  342. #define NB_TESTS 3
  343. #define CCM_SELFTEST_PT_MAX_LEN 24
  344. #define CCM_SELFTEST_CT_MAX_LEN 32
  345. /*
  346. * The data is the same for all tests, only the used length changes
  347. */
  348. static const unsigned char key_test_data[] = {
  349. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  350. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  351. };
  352. static const unsigned char iv_test_data[] = {
  353. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  354. 0x18, 0x19, 0x1a, 0x1b
  355. };
  356. static const unsigned char ad_test_data[] = {
  357. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  358. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  359. 0x10, 0x11, 0x12, 0x13
  360. };
  361. static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = {
  362. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  363. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  364. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  365. };
  366. static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 };
  367. static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
  368. static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
  369. static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
  370. static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
  371. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  372. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  373. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  374. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  375. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  376. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  377. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  378. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  379. };
  380. int mbedtls_ccm_self_test(int verbose)
  381. {
  382. mbedtls_ccm_context ctx;
  383. /*
  384. * Some hardware accelerators require the input and output buffers
  385. * would be in RAM, because the flash is not accessible.
  386. * Use buffers on the stack to hold the test vectors data.
  387. */
  388. unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
  389. unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
  390. size_t i;
  391. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  392. mbedtls_ccm_init(&ctx);
  393. if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
  394. 8 * sizeof(key_test_data)) != 0) {
  395. if (verbose != 0) {
  396. mbedtls_printf(" CCM: setup failed");
  397. }
  398. return 1;
  399. }
  400. for (i = 0; i < NB_TESTS; i++) {
  401. if (verbose != 0) {
  402. mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1);
  403. }
  404. memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
  405. memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN);
  406. memcpy(plaintext, msg_test_data, msg_len_test_data[i]);
  407. ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i],
  408. iv_test_data, iv_len_test_data[i],
  409. ad_test_data, add_len_test_data[i],
  410. plaintext, ciphertext,
  411. ciphertext + msg_len_test_data[i],
  412. tag_len_test_data[i]);
  413. if (ret != 0 ||
  414. memcmp(ciphertext, res_test_data[i],
  415. msg_len_test_data[i] + tag_len_test_data[i]) != 0) {
  416. if (verbose != 0) {
  417. mbedtls_printf("failed\n");
  418. }
  419. return 1;
  420. }
  421. memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN);
  422. ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i],
  423. iv_test_data, iv_len_test_data[i],
  424. ad_test_data, add_len_test_data[i],
  425. ciphertext, plaintext,
  426. ciphertext + msg_len_test_data[i],
  427. tag_len_test_data[i]);
  428. if (ret != 0 ||
  429. memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) {
  430. if (verbose != 0) {
  431. mbedtls_printf("failed\n");
  432. }
  433. return 1;
  434. }
  435. if (verbose != 0) {
  436. mbedtls_printf("passed\n");
  437. }
  438. }
  439. mbedtls_ccm_free(&ctx);
  440. if (verbose != 0) {
  441. mbedtls_printf("\n");
  442. }
  443. return 0;
  444. }
  445. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  446. #endif /* MBEDTLS_CCM_C */