ccm.c 17 KB

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