blob: 6fac78e430c7da3273a480df50c3a8f392d9ab8c [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
2 * NIST SP800-38C compliant CCM implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020072 unsigned int keybits )
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é-Gonnardb8186a52015-06-18 14:58:58 +020077 cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, 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é-Gonnardb8186a52015-06-18 14:58:58 +020089 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 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;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200146 unsigned char q;
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
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200169 q = 16 - 1 - (unsigned char) iv_len;
170
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200171 /*
172 * First block B_0:
173 * 0 .. 0 flags
174 * 1 .. iv_len nonce (aka iv)
175 * iv_len+1 .. 15 length
176 *
177 * With flags as (bits):
178 * 7 0
179 * 6 add present?
180 * 5 .. 3 (t - 2) / 2
181 * 2 .. 0 q - 1
182 */
183 b[0] = 0;
184 b[0] |= ( add_len > 0 ) << 6;
185 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
186 b[0] |= q - 1;
187
188 memcpy( b + 1, iv, iv_len );
189
190 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
191 b[15-i] = (unsigned char)( len_left & 0xFF );
192
193 if( len_left > 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200195
196
197 /* Start CBC-MAC with first block */
198 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200199 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200200
201 /*
202 * If there is additional data, update CBC-MAC with
203 * add_len, add, 0 (padding to a block boundary)
204 */
205 if( add_len > 0 )
206 {
207 size_t use_len;
208 len_left = add_len;
209 src = add;
210
211 memset( b, 0, 16 );
212 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
213 b[1] = (unsigned char)( ( add_len ) & 0xFF );
214
215 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
216 memcpy( b + 2, src, use_len );
217 len_left -= use_len;
218 src += use_len;
219
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200220 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200221
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200222 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200223 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200224 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200225
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200226 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200227 memcpy( b, src, use_len );
228 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200229
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200230 len_left -= use_len;
231 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200232 }
233 }
234
235 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200236 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200237 * 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
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200250 /*
251 * Authenticate and {en,de}crypt the message.
252 *
253 * The only difference between encryption and decryption is
254 * the respective order of authentication and {en,de}cryption.
255 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200256 len_left = length;
257 src = input;
258 dst = output;
259
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200260 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200261 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200262 size_t use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200263
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200264 if( mode == CCM_ENCRYPT )
265 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200266 memset( b, 0, 16 );
267 memcpy( b, src, use_len );
268 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200269 }
270
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200271 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200273 if( mode == CCM_DECRYPT )
274 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200275 memset( b, 0, 16 );
276 memcpy( b, dst, use_len );
277 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200278 }
279
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200280 dst += use_len;
281 src += use_len;
282 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200283
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200284 /*
285 * Increment counter.
286 * No need to check for overflow thanks to the length check above.
287 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200288 for( i = 0; i < q; i++ )
289 if( ++ctr[15-i] != 0 )
290 break;
291 }
292
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200293 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200294 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200295 */
296 for( i = 0; i < q; i++ )
297 ctr[15-i] = 0;
298
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200299 CTR_CRYPT( y, y, 16 );
300 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200301
302 return( 0 );
303}
304
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200305/*
306 * Authenticated encryption
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200309 const unsigned char *iv, size_t iv_len,
310 const unsigned char *add, size_t add_len,
311 const unsigned char *input, unsigned char *output,
312 unsigned char *tag, size_t tag_len )
313{
314 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
315 add, add_len, input, output, tag, tag_len ) );
316}
317
318/*
319 * Authenticated decryption
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200322 const unsigned char *iv, size_t iv_len,
323 const unsigned char *add, size_t add_len,
324 const unsigned char *input, unsigned char *output,
325 const unsigned char *tag, size_t tag_len )
326{
327 int ret;
328 unsigned char check_tag[16];
329 unsigned char i;
330 int diff;
331
332 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
333 iv, iv_len, add, add_len,
334 input, output, check_tag, tag_len ) ) != 0 )
335 {
336 return( ret );
337 }
338
339 /* Check tag in "constant-time" */
340 for( diff = 0, i = 0; i < tag_len; i++ )
341 diff |= tag[i] ^ check_tag[i];
342
343 if( diff != 0 )
344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_zeroize( output, length );
346 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200347 }
348
349 return( 0 );
350}
351
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200354/*
355 * Examples 1 to 3 from SP800-38C Appendix C
356 */
357
358#define NB_TESTS 3
359
360/*
361 * The data is the same for all tests, only the used length changes
362 */
363static const unsigned char key[] = {
364 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
365 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
366};
367
368static const unsigned char iv[] = {
369 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
370 0x18, 0x19, 0x1a, 0x1b
371};
372
373static const unsigned char ad[] = {
374 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
375 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
376 0x10, 0x11, 0x12, 0x13
377};
378
379static const unsigned char msg[] = {
380 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
381 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
382 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
383};
384
385static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
386static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
387static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
388static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
389
390static const unsigned char res[NB_TESTS][32] = {
391 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
392 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
393 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
394 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
395 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
396 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
397 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
398 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
399};
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_ccm_self_test( int verbose )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ccm_context ctx;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200404 unsigned char out[32];
405 size_t i;
406 int ret;
407
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200408 mbedtls_ccm_init( &ctx );
409
410 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200411 {
412 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_printf( " CCM: setup failed" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200414
415 return( 1 );
416 }
417
418 for( i = 0; i < NB_TESTS; i++ )
419 {
420 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200424 iv, iv_len[i], ad, add_len[i],
425 msg, out,
426 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200427
428 if( ret != 0 ||
429 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
430 {
431 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200433
434 return( 1 );
435 }
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200438 iv, iv_len[i], ad, add_len[i],
439 res[i], out,
440 res[i] + msg_len[i], tag_len[i] );
441
442 if( ret != 0 ||
443 memcmp( out, msg, msg_len[i] ) != 0 )
444 {
445 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200447
448 return( 1 );
449 }
450
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200451 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200453 }
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200456
457 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200459
460 return( 0 );
461}
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_CCM_C */