blob: aa2d6754b498c839172387e92acd21c98246a9bc [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
2 * NIST SP800-38C compliant CCM implementation
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02007 *
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/config.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
45#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000047#else
48#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define mbedtls_printf printf
50#endif /* MBEDTLS_PLATFORM_C */
51#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Rich Evans00ab4702015-02-06 13:43:58 +000052
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020055 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020058#define CCM_ENCRYPT 0
59#define CCM_DECRYPT 1
60
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020061/*
62 * Initialize context
63 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020064void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
65{
66 memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
67}
68
69int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
70 mbedtls_cipher_id_t cipher,
71 const unsigned char *key,
72 unsigned int keysize )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020073{
74 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 cipher_info = mbedtls_cipher_info_from_values( cipher, keysize, MBEDTLS_MODE_ECB );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020078 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020080
81 if( cipher_info->block_size != 16 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020083
Manuel Pégourié-Gonnard43b08572015-05-27 17:23:30 +020084 mbedtls_cipher_free( &ctx->cipher_ctx );
85
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020086 if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020087 return( ret );
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keysize,
90 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020091 {
92 return( ret );
93 }
94
95 return( 0 );
96}
97
98/*
99 * Free context
100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_cipher_free( &ctx->cipher_ctx );
104 mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200105}
106
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200107/*
108 * Macros for common operations.
109 * Results in smaller compiled code than static inline functions.
110 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200111
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200112/*
113 * Update the CBC-MAC state in y using a block in b
114 * (Always using b as the source helps the compiler optimise a bit better.)
115 */
116#define UPDATE_CBC_MAC \
117 for( i = 0; i < 16; i++ ) \
118 y[i] ^= b[i]; \
119 \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200121 return( ret );
122
123/*
124 * Encrypt or decrypt a partial block with CTR
125 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200126 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200127 */
128#define CTR_CRYPT( dst, src, len ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200130 return( ret ); \
131 \
132 for( i = 0; i < len; i++ ) \
133 dst[i] = src[i] ^ b[i];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200134
135/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200136 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200139 const unsigned char *iv, size_t iv_len,
140 const unsigned char *add, size_t add_len,
141 const unsigned char *input, unsigned char *output,
142 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200143{
144 int ret;
145 unsigned char i;
146 unsigned char q = 16 - 1 - iv_len;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200147 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200148 unsigned char b[16];
149 unsigned char y[16];
150 unsigned char ctr[16];
151 const unsigned char *src;
152 unsigned char *dst;
153
154 /*
155 * Check length requirements: SP800-38C A.1
156 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
157 * 'length' checked later (when writing it to the first block)
158 */
159 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200161
162 /* Also implies q is within bounds */
163 if( iv_len < 7 || iv_len > 13 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200165
166 if( add_len > 0xFF00 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200168
169 /*
170 * First block B_0:
171 * 0 .. 0 flags
172 * 1 .. iv_len nonce (aka iv)
173 * iv_len+1 .. 15 length
174 *
175 * With flags as (bits):
176 * 7 0
177 * 6 add present?
178 * 5 .. 3 (t - 2) / 2
179 * 2 .. 0 q - 1
180 */
181 b[0] = 0;
182 b[0] |= ( add_len > 0 ) << 6;
183 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
184 b[0] |= q - 1;
185
186 memcpy( b + 1, iv, iv_len );
187
188 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
189 b[15-i] = (unsigned char)( len_left & 0xFF );
190
191 if( len_left > 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200193
194
195 /* Start CBC-MAC with first block */
196 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200197 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200198
199 /*
200 * If there is additional data, update CBC-MAC with
201 * add_len, add, 0 (padding to a block boundary)
202 */
203 if( add_len > 0 )
204 {
205 size_t use_len;
206 len_left = add_len;
207 src = add;
208
209 memset( b, 0, 16 );
210 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
211 b[1] = (unsigned char)( ( add_len ) & 0xFF );
212
213 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
214 memcpy( b + 2, src, use_len );
215 len_left -= use_len;
216 src += use_len;
217
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200218 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200219
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200220 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200221 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200222 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200223
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200224 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200225 memcpy( b, src, use_len );
226 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200227
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200228 len_left -= use_len;
229 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200230 }
231 }
232
233 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200234 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200235 * 0 .. 0 flags
236 * 1 .. iv_len nonce (aka iv)
237 * iv_len+1 .. 15 counter (initially 1)
238 *
239 * With flags as (bits):
240 * 7 .. 3 0
241 * 2 .. 0 q - 1
242 */
243 ctr[0] = q - 1;
244 memcpy( ctr + 1, iv, iv_len );
245 memset( ctr + 1 + iv_len, 0, q );
246 ctr[15] = 1;
247
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200248 /*
249 * Authenticate and {en,de}crypt the message.
250 *
251 * The only difference between encryption and decryption is
252 * the respective order of authentication and {en,de}cryption.
253 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200254 len_left = length;
255 src = input;
256 dst = output;
257
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200258 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200259 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200260 unsigned char use_len = len_left > 16 ? 16 : len_left;
261
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200262 if( mode == CCM_ENCRYPT )
263 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200264 memset( b, 0, 16 );
265 memcpy( b, src, use_len );
266 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200267 }
268
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200269 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200270
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200271 if( mode == CCM_DECRYPT )
272 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200273 memset( b, 0, 16 );
274 memcpy( b, dst, use_len );
275 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200276 }
277
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200278 dst += use_len;
279 src += use_len;
280 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200281
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200282 /*
283 * Increment counter.
284 * No need to check for overflow thanks to the length check above.
285 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286 for( i = 0; i < q; i++ )
287 if( ++ctr[15-i] != 0 )
288 break;
289 }
290
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200291 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200292 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200293 */
294 for( i = 0; i < q; i++ )
295 ctr[15-i] = 0;
296
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200297 CTR_CRYPT( y, y, 16 );
298 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200299
300 return( 0 );
301}
302
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200303/*
304 * Authenticated encryption
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200307 const unsigned char *iv, size_t iv_len,
308 const unsigned char *add, size_t add_len,
309 const unsigned char *input, unsigned char *output,
310 unsigned char *tag, size_t tag_len )
311{
312 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
313 add, add_len, input, output, tag, tag_len ) );
314}
315
316/*
317 * Authenticated decryption
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200320 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 int ret;
326 unsigned char check_tag[16];
327 unsigned char i;
328 int diff;
329
330 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
331 iv, iv_len, add, add_len,
332 input, output, check_tag, tag_len ) ) != 0 )
333 {
334 return( ret );
335 }
336
337 /* Check tag in "constant-time" */
338 for( diff = 0, i = 0; i < tag_len; i++ )
339 diff |= tag[i] ^ check_tag[i];
340
341 if( diff != 0 )
342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_zeroize( output, length );
344 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200345 }
346
347 return( 0 );
348}
349
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200352/*
353 * Examples 1 to 3 from SP800-38C Appendix C
354 */
355
356#define NB_TESTS 3
357
358/*
359 * The data is the same for all tests, only the used length changes
360 */
361static const unsigned char key[] = {
362 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
363 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
364};
365
366static const unsigned char iv[] = {
367 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
368 0x18, 0x19, 0x1a, 0x1b
369};
370
371static const unsigned char ad[] = {
372 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
373 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
374 0x10, 0x11, 0x12, 0x13
375};
376
377static const unsigned char msg[] = {
378 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
379 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
380 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
381};
382
383static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
384static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
385static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
386static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
387
388static const unsigned char res[NB_TESTS][32] = {
389 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
390 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
391 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
392 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
393 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
394 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
395 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
396 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
397};
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_ccm_self_test( int verbose )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200402 unsigned char out[32];
403 size_t i;
404 int ret;
405
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200406 mbedtls_ccm_init( &ctx );
407
408 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200409 {
410 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_printf( " CCM: setup failed" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200412
413 return( 1 );
414 }
415
416 for( i = 0; i < NB_TESTS; i++ )
417 {
418 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200422 iv, iv_len[i], ad, add_len[i],
423 msg, out,
424 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200425
426 if( ret != 0 ||
427 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
428 {
429 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200431
432 return( 1 );
433 }
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200436 iv, iv_len[i], ad, add_len[i],
437 res[i], out,
438 res[i] + msg_len[i], tag_len[i] );
439
440 if( ret != 0 ||
441 memcmp( out, msg, msg_len[i] ) != 0 )
442 {
443 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200445
446 return( 1 );
447 }
448
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200449 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200451 }
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200454
455 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200457
458 return( 0 );
459}
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_CCM_C */