blob: 91dee6720b29c2a383eb9501a1051920d2914c26 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/*
2 * NIST SP800-38C compliant CCM implementation
3 *
4 * Copyright (C) 2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * Definition of CCM:
28 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
29 * RFC 3610 "Counter with CBC-MAC (CCM)"
30 *
31 * Related:
32 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
33 */
34
35#if !defined(POLARSSL_CONFIG_FILE)
36#include "polarssl/config.h"
37#else
38#include POLARSSL_CONFIG_FILE
39#endif
40
41#if defined(POLARSSL_CCM_C)
42
43#include "polarssl/ccm.h"
44
Paul Bakker34617722014-06-13 17:20:13 +020045/* Implementation that should never be optimized out by the compiler */
46static void polarssl_zeroize( void *v, size_t n ) {
47 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
48}
49
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020050#define CCM_ENCRYPT 0
51#define CCM_DECRYPT 1
52
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020053/*
54 * Initialize context
55 */
56int ccm_init( ccm_context *ctx, cipher_id_t cipher,
57 const unsigned char *key, unsigned int keysize )
58{
59 int ret;
60 const cipher_info_t *cipher_info;
61
62 memset( ctx, 0, sizeof( ccm_context ) );
63
64 cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
65 if( cipher_info == NULL )
66 return( POLARSSL_ERR_CCM_BAD_INPUT );
67
68 if( cipher_info->block_size != 16 )
69 return( POLARSSL_ERR_CCM_BAD_INPUT );
70
71 if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
72 return( ret );
73
74 if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
75 POLARSSL_ENCRYPT ) ) != 0 )
76 {
77 return( ret );
78 }
79
80 return( 0 );
81}
82
83/*
84 * Free context
85 */
86void ccm_free( ccm_context *ctx )
87{
88 (void) cipher_free_ctx( &ctx->cipher_ctx );
Paul Bakker34617722014-06-13 17:20:13 +020089 polarssl_zeroize( ctx, sizeof( ccm_context ) );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090}
91
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020092/*
93 * Macros for common operations.
94 * Results in smaller compiled code than static inline functions.
95 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020096
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +020097/*
98 * Update the CBC-MAC state in y using a block in b
99 * (Always using b as the source helps the compiler optimise a bit better.)
100 */
101#define UPDATE_CBC_MAC \
102 for( i = 0; i < 16; i++ ) \
103 y[i] ^= b[i]; \
104 \
105 if( ( ret = cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
106 return( ret );
107
108/*
109 * Encrypt or decrypt a partial block with CTR
110 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200111 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200112 */
113#define CTR_CRYPT( dst, src, len ) \
114 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 ) \
115 return( ret ); \
116 \
117 for( i = 0; i < len; i++ ) \
118 dst[i] = src[i] ^ b[i];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200119
120/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200121 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200122 */
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200123static int ccm_auth_crypt( ccm_context *ctx, int mode, size_t length,
124 const unsigned char *iv, size_t iv_len,
125 const unsigned char *add, size_t add_len,
126 const unsigned char *input, unsigned char *output,
127 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200128{
129 int ret;
130 unsigned char i;
131 unsigned char q = 16 - 1 - iv_len;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200132 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200133 unsigned char b[16];
134 unsigned char y[16];
135 unsigned char ctr[16];
136 const unsigned char *src;
137 unsigned char *dst;
138
139 /*
140 * Check length requirements: SP800-38C A.1
141 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
142 * 'length' checked later (when writing it to the first block)
143 */
144 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
145 return( POLARSSL_ERR_CCM_BAD_INPUT );
146
147 /* Also implies q is within bounds */
148 if( iv_len < 7 || iv_len > 13 )
149 return( POLARSSL_ERR_CCM_BAD_INPUT );
150
151 if( add_len > 0xFF00 )
152 return( POLARSSL_ERR_CCM_BAD_INPUT );
153
154 /*
155 * First block B_0:
156 * 0 .. 0 flags
157 * 1 .. iv_len nonce (aka iv)
158 * iv_len+1 .. 15 length
159 *
160 * With flags as (bits):
161 * 7 0
162 * 6 add present?
163 * 5 .. 3 (t - 2) / 2
164 * 2 .. 0 q - 1
165 */
166 b[0] = 0;
167 b[0] |= ( add_len > 0 ) << 6;
168 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
169 b[0] |= q - 1;
170
171 memcpy( b + 1, iv, iv_len );
172
173 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
174 b[15-i] = (unsigned char)( len_left & 0xFF );
175
176 if( len_left > 0 )
177 return( POLARSSL_ERR_CCM_BAD_INPUT );
178
179
180 /* Start CBC-MAC with first block */
181 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200182 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200183
184 /*
185 * If there is additional data, update CBC-MAC with
186 * add_len, add, 0 (padding to a block boundary)
187 */
188 if( add_len > 0 )
189 {
190 size_t use_len;
191 len_left = add_len;
192 src = add;
193
194 memset( b, 0, 16 );
195 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
196 b[1] = (unsigned char)( ( add_len ) & 0xFF );
197
198 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
199 memcpy( b + 2, src, use_len );
200 len_left -= use_len;
201 src += use_len;
202
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200203 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200204
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200205 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200206 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200207 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200208
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200209 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200210 memcpy( b, src, use_len );
211 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200212
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200213 len_left -= use_len;
214 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200215 }
216 }
217
218 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200219 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200220 * 0 .. 0 flags
221 * 1 .. iv_len nonce (aka iv)
222 * iv_len+1 .. 15 counter (initially 1)
223 *
224 * With flags as (bits):
225 * 7 .. 3 0
226 * 2 .. 0 q - 1
227 */
228 ctr[0] = q - 1;
229 memcpy( ctr + 1, iv, iv_len );
230 memset( ctr + 1 + iv_len, 0, q );
231 ctr[15] = 1;
232
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200233 /*
234 * Authenticate and {en,de}crypt the message.
235 *
236 * The only difference between encryption and decryption is
237 * the respective order of authentication and {en,de}cryption.
238 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200239 len_left = length;
240 src = input;
241 dst = output;
242
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200243 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200244 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200245 unsigned char use_len = len_left > 16 ? 16 : len_left;
246
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200247 if( mode == CCM_ENCRYPT )
248 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200249 memset( b, 0, 16 );
250 memcpy( b, src, use_len );
251 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200252 }
253
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200254 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200255
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200256 if( mode == CCM_DECRYPT )
257 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200258 memset( b, 0, 16 );
259 memcpy( b, dst, use_len );
260 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200261 }
262
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200263 dst += use_len;
264 src += use_len;
265 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200266
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200267 /*
268 * Increment counter.
269 * No need to check for overflow thanks to the length check above.
270 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200271 for( i = 0; i < q; i++ )
272 if( ++ctr[15-i] != 0 )
273 break;
274 }
275
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200276 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200277 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200278 */
279 for( i = 0; i < q; i++ )
280 ctr[15-i] = 0;
281
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200282 CTR_CRYPT( y, y, 16 );
283 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200284
285 return( 0 );
286}
287
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200288/*
289 * Authenticated encryption
290 */
291int ccm_encrypt_and_tag( 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 unsigned char *tag, size_t tag_len )
296{
297 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
298 add, add_len, input, output, tag, tag_len ) );
299}
300
301/*
302 * Authenticated decryption
303 */
304int ccm_auth_decrypt( ccm_context *ctx, size_t length,
305 const unsigned char *iv, size_t iv_len,
306 const unsigned char *add, size_t add_len,
307 const unsigned char *input, unsigned char *output,
308 const unsigned char *tag, size_t tag_len )
309{
310 int ret;
311 unsigned char check_tag[16];
312 unsigned char i;
313 int diff;
314
315 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
316 iv, iv_len, add, add_len,
317 input, output, check_tag, tag_len ) ) != 0 )
318 {
319 return( ret );
320 }
321
322 /* Check tag in "constant-time" */
323 for( diff = 0, i = 0; i < tag_len; i++ )
324 diff |= tag[i] ^ check_tag[i];
325
326 if( diff != 0 )
327 {
Paul Bakker34617722014-06-13 17:20:13 +0200328 polarssl_zeroize( output, length );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200329 return( POLARSSL_ERR_CCM_AUTH_FAILED );
330 }
331
332 return( 0 );
333}
334
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200335
336#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
337
338#if defined(POLARSSL_PLATFORM_C)
339#include "polarssl/platform.h"
340#else
341#define polarssl_printf printf
342#endif
343
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200344/*
345 * Examples 1 to 3 from SP800-38C Appendix C
346 */
347
348#define NB_TESTS 3
349
350/*
351 * The data is the same for all tests, only the used length changes
352 */
353static const unsigned char key[] = {
354 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
355 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
356};
357
358static const unsigned char iv[] = {
359 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
360 0x18, 0x19, 0x1a, 0x1b
361};
362
363static const unsigned char ad[] = {
364 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
365 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
366 0x10, 0x11, 0x12, 0x13
367};
368
369static const unsigned char msg[] = {
370 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
371 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
372 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
373};
374
375static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
376static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
377static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
378static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
379
380static const unsigned char res[NB_TESTS][32] = {
381 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
382 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
383 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
384 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
385 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
386 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
387 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
388 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
389};
390
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200391int ccm_self_test( int verbose )
392{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200393 ccm_context ctx;
394 unsigned char out[32];
395 size_t i;
396 int ret;
397
398 if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
399 {
400 if( verbose != 0 )
401 polarssl_printf( " CCM: setup failed" );
402
403 return( 1 );
404 }
405
406 for( i = 0; i < NB_TESTS; i++ )
407 {
408 if( verbose != 0 )
409 polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
410
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200411 ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
412 iv, iv_len[i], ad, add_len[i],
413 msg, out,
414 out + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200415
416 if( ret != 0 ||
417 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
418 {
419 if( verbose != 0 )
420 polarssl_printf( "failed\n" );
421
422 return( 1 );
423 }
424
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200425 ret = ccm_auth_decrypt( &ctx, msg_len[i],
426 iv, iv_len[i], ad, add_len[i],
427 res[i], out,
428 res[i] + msg_len[i], tag_len[i] );
429
430 if( ret != 0 ||
431 memcmp( out, msg, msg_len[i] ) != 0 )
432 {
433 if( verbose != 0 )
434 polarssl_printf( "failed\n" );
435
436 return( 1 );
437 }
438
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200439 if( verbose != 0 )
440 polarssl_printf( "passed\n" );
441 }
442
443 ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200444
445 if( verbose != 0 )
446 polarssl_printf( "\n" );
447
448 return( 0 );
449}
450
451#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
452
453#endif /* POLARSSL_CCM_C */