ccm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * NIST SP800-38C compliant CCM implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * Definition of CCM:
  25. * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  26. * RFC 3610 "Counter with CBC-MAC (CCM)"
  27. *
  28. * Related:
  29. * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  30. */
  31. #if !defined(MBEDTLS_CONFIG_FILE)
  32. #include "mbedtls/config.h"
  33. #else
  34. #include MBEDTLS_CONFIG_FILE
  35. #endif
  36. #if defined(MBEDTLS_CCM_C)
  37. #include "mbedtls/ccm.h"
  38. #include <string.h>
  39. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  40. #if defined(MBEDTLS_PLATFORM_C)
  41. #include "mbedtls/platform.h"
  42. #else
  43. #include <stdio.h>
  44. #define mbedtls_printf printf
  45. #endif /* MBEDTLS_PLATFORM_C */
  46. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  47. /* Implementation that should never be optimized out by the compiler */
  48. static void mbedtls_zeroize( void *v, size_t n ) {
  49. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  50. }
  51. #define CCM_ENCRYPT 0
  52. #define CCM_DECRYPT 1
  53. /*
  54. * Initialize context
  55. */
  56. void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
  57. {
  58. memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
  59. }
  60. int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
  61. mbedtls_cipher_id_t cipher,
  62. const unsigned char *key,
  63. unsigned int keybits )
  64. {
  65. int ret;
  66. const mbedtls_cipher_info_t *cipher_info;
  67. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  68. if( cipher_info == NULL )
  69. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  70. if( cipher_info->block_size != 16 )
  71. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  72. mbedtls_cipher_free( &ctx->cipher_ctx );
  73. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  74. return( ret );
  75. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  76. MBEDTLS_ENCRYPT ) ) != 0 )
  77. {
  78. return( ret );
  79. }
  80. return( 0 );
  81. }
  82. /*
  83. * Free context
  84. */
  85. void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
  86. {
  87. mbedtls_cipher_free( &ctx->cipher_ctx );
  88. mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
  89. }
  90. /*
  91. * Macros for common operations.
  92. * Results in smaller compiled code than static inline functions.
  93. */
  94. /*
  95. * Update the CBC-MAC state in y using a block in b
  96. * (Always using b as the source helps the compiler optimise a bit better.)
  97. */
  98. #define UPDATE_CBC_MAC \
  99. for( i = 0; i < 16; i++ ) \
  100. y[i] ^= b[i]; \
  101. \
  102. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
  103. return( ret );
  104. /*
  105. * Encrypt or decrypt a partial block with CTR
  106. * Warning: using b for temporary storage! src and dst must not be b!
  107. * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  108. */
  109. #define CTR_CRYPT( dst, src, len ) \
  110. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
  111. return( ret ); \
  112. \
  113. for( i = 0; i < len; i++ ) \
  114. dst[i] = src[i] ^ b[i];
  115. /*
  116. * Authenticated encryption or decryption
  117. */
  118. static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
  119. const unsigned char *iv, size_t iv_len,
  120. const unsigned char *add, size_t add_len,
  121. const unsigned char *input, unsigned char *output,
  122. unsigned char *tag, size_t tag_len )
  123. {
  124. int ret;
  125. unsigned char i;
  126. unsigned char q;
  127. size_t len_left, olen;
  128. unsigned char b[16];
  129. unsigned char y[16];
  130. unsigned char ctr[16];
  131. const unsigned char *src;
  132. unsigned char *dst;
  133. /*
  134. * Check length requirements: SP800-38C A.1
  135. * Additional requirement: a < 2^16 - 2^8 to simplify the code.
  136. * 'length' checked later (when writing it to the first block)
  137. */
  138. if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
  139. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  140. /* Also implies q is within bounds */
  141. if( iv_len < 7 || iv_len > 13 )
  142. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  143. if( add_len > 0xFF00 )
  144. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  145. q = 16 - 1 - (unsigned char) iv_len;
  146. /*
  147. * First block B_0:
  148. * 0 .. 0 flags
  149. * 1 .. iv_len nonce (aka iv)
  150. * iv_len+1 .. 15 length
  151. *
  152. * With flags as (bits):
  153. * 7 0
  154. * 6 add present?
  155. * 5 .. 3 (t - 2) / 2
  156. * 2 .. 0 q - 1
  157. */
  158. b[0] = 0;
  159. b[0] |= ( add_len > 0 ) << 6;
  160. b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
  161. b[0] |= q - 1;
  162. memcpy( b + 1, iv, iv_len );
  163. for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
  164. b[15-i] = (unsigned char)( len_left & 0xFF );
  165. if( len_left > 0 )
  166. return( MBEDTLS_ERR_CCM_BAD_INPUT );
  167. /* Start CBC-MAC with first block */
  168. memset( y, 0, 16 );
  169. UPDATE_CBC_MAC;
  170. /*
  171. * If there is additional data, update CBC-MAC with
  172. * add_len, add, 0 (padding to a block boundary)
  173. */
  174. if( add_len > 0 )
  175. {
  176. size_t use_len;
  177. len_left = add_len;
  178. src = add;
  179. memset( b, 0, 16 );
  180. b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
  181. b[1] = (unsigned char)( ( add_len ) & 0xFF );
  182. use_len = len_left < 16 - 2 ? len_left : 16 - 2;
  183. memcpy( b + 2, src, use_len );
  184. len_left -= use_len;
  185. src += use_len;
  186. UPDATE_CBC_MAC;
  187. while( len_left > 0 )
  188. {
  189. use_len = len_left > 16 ? 16 : len_left;
  190. memset( b, 0, 16 );
  191. memcpy( b, src, use_len );
  192. UPDATE_CBC_MAC;
  193. len_left -= use_len;
  194. src += use_len;
  195. }
  196. }
  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. ctr[0] = q - 1;
  208. memcpy( ctr + 1, iv, iv_len );
  209. memset( ctr + 1 + iv_len, 0, q );
  210. ctr[15] = 1;
  211. /*
  212. * Authenticate and {en,de}crypt the message.
  213. *
  214. * The only difference between encryption and decryption is
  215. * the respective order of authentication and {en,de}cryption.
  216. */
  217. len_left = length;
  218. src = input;
  219. dst = output;
  220. while( len_left > 0 )
  221. {
  222. size_t use_len = len_left > 16 ? 16 : len_left;
  223. if( mode == CCM_ENCRYPT )
  224. {
  225. memset( b, 0, 16 );
  226. memcpy( b, src, use_len );
  227. UPDATE_CBC_MAC;
  228. }
  229. CTR_CRYPT( dst, src, use_len );
  230. if( mode == CCM_DECRYPT )
  231. {
  232. memset( b, 0, 16 );
  233. memcpy( b, dst, use_len );
  234. UPDATE_CBC_MAC;
  235. }
  236. dst += use_len;
  237. src += use_len;
  238. len_left -= use_len;
  239. /*
  240. * Increment counter.
  241. * No need to check for overflow thanks to the length check above.
  242. */
  243. for( i = 0; i < q; i++ )
  244. if( ++ctr[15-i] != 0 )
  245. break;
  246. }
  247. /*
  248. * Authentication: reset counter and crypt/mask internal tag
  249. */
  250. for( i = 0; i < q; i++ )
  251. ctr[15-i] = 0;
  252. CTR_CRYPT( y, y, 16 );
  253. memcpy( tag, y, tag_len );
  254. return( 0 );
  255. }
  256. /*
  257. * Authenticated encryption
  258. */
  259. int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
  260. const unsigned char *iv, size_t iv_len,
  261. const unsigned char *add, size_t add_len,
  262. const unsigned char *input, unsigned char *output,
  263. unsigned char *tag, size_t tag_len )
  264. {
  265. return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
  266. add, add_len, input, output, tag, tag_len ) );
  267. }
  268. /*
  269. * Authenticated decryption
  270. */
  271. int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
  272. const unsigned char *iv, size_t iv_len,
  273. const unsigned char *add, size_t add_len,
  274. const unsigned char *input, unsigned char *output,
  275. const unsigned char *tag, size_t tag_len )
  276. {
  277. int ret;
  278. unsigned char check_tag[16];
  279. unsigned char i;
  280. int diff;
  281. if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
  282. iv, iv_len, add, add_len,
  283. input, output, check_tag, tag_len ) ) != 0 )
  284. {
  285. return( ret );
  286. }
  287. /* Check tag in "constant-time" */
  288. for( diff = 0, i = 0; i < tag_len; i++ )
  289. diff |= tag[i] ^ check_tag[i];
  290. if( diff != 0 )
  291. {
  292. mbedtls_zeroize( output, length );
  293. return( MBEDTLS_ERR_CCM_AUTH_FAILED );
  294. }
  295. return( 0 );
  296. }
  297. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  298. /*
  299. * Examples 1 to 3 from SP800-38C Appendix C
  300. */
  301. #define NB_TESTS 3
  302. /*
  303. * The data is the same for all tests, only the used length changes
  304. */
  305. static const unsigned char key[] = {
  306. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  307. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
  308. };
  309. static const unsigned char iv[] = {
  310. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  311. 0x18, 0x19, 0x1a, 0x1b
  312. };
  313. static const unsigned char ad[] = {
  314. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  315. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  316. 0x10, 0x11, 0x12, 0x13
  317. };
  318. static const unsigned char msg[] = {
  319. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  320. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  321. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  322. };
  323. static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
  324. static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
  325. static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
  326. static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
  327. static const unsigned char res[NB_TESTS][32] = {
  328. { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
  329. { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
  330. 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
  331. 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
  332. { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
  333. 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
  334. 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
  335. 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
  336. };
  337. int mbedtls_ccm_self_test( int verbose )
  338. {
  339. mbedtls_ccm_context ctx;
  340. unsigned char out[32];
  341. size_t i;
  342. int ret;
  343. mbedtls_ccm_init( &ctx );
  344. if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
  345. {
  346. if( verbose != 0 )
  347. mbedtls_printf( " CCM: setup failed" );
  348. return( 1 );
  349. }
  350. for( i = 0; i < NB_TESTS; i++ )
  351. {
  352. if( verbose != 0 )
  353. mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
  354. ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
  355. iv, iv_len[i], ad, add_len[i],
  356. msg, out,
  357. out + msg_len[i], tag_len[i] );
  358. if( ret != 0 ||
  359. memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
  360. {
  361. if( verbose != 0 )
  362. mbedtls_printf( "failed\n" );
  363. return( 1 );
  364. }
  365. ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
  366. iv, iv_len[i], ad, add_len[i],
  367. res[i], out,
  368. res[i] + msg_len[i], tag_len[i] );
  369. if( ret != 0 ||
  370. memcmp( out, msg, msg_len[i] ) != 0 )
  371. {
  372. if( verbose != 0 )
  373. mbedtls_printf( "failed\n" );
  374. return( 1 );
  375. }
  376. if( verbose != 0 )
  377. mbedtls_printf( "passed\n" );
  378. }
  379. mbedtls_ccm_free( &ctx );
  380. if( verbose != 0 )
  381. mbedtls_printf( "\n" );
  382. return( 0 );
  383. }
  384. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  385. #endif /* MBEDTLS_CCM_C */