blob: 4a8decf5b044ac0d45461f333db3032d99da064a [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
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020045/*
46 * Initialize context
47 */
48int ccm_init( ccm_context *ctx, cipher_id_t cipher,
49 const unsigned char *key, unsigned int keysize )
50{
51 int ret;
52 const cipher_info_t *cipher_info;
53
54 memset( ctx, 0, sizeof( ccm_context ) );
55
56 cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
57 if( cipher_info == NULL )
58 return( POLARSSL_ERR_CCM_BAD_INPUT );
59
60 if( cipher_info->block_size != 16 )
61 return( POLARSSL_ERR_CCM_BAD_INPUT );
62
63 if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
64 return( ret );
65
66 if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
67 POLARSSL_ENCRYPT ) ) != 0 )
68 {
69 return( ret );
70 }
71
72 return( 0 );
73}
74
75/*
76 * Free context
77 */
78void ccm_free( ccm_context *ctx )
79{
80 (void) cipher_free_ctx( &ctx->cipher_ctx );
81 memset( ctx, 0, sizeof( ccm_context ) );
82}
83
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020084#define UPDATE_CBC_MAC( src ) \
85{ \
86 size_t olen; \
87 \
88 for( i = 0; i < 16; i++ ) \
89 src[i] ^= y[i]; \
90 \
91 if( ( ret = cipher_update( &ctx->cipher_ctx, src, 16, \
92 y, &olen ) ) != 0 ) \
93 { \
94 return( ret ); \
95 } \
96}
97
98#define CTR_CRYPT_BLOCK( dst, src ) \
99{ \
100 size_t olen; \
101 \
102 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, \
103 dst, &olen ) ) != 0 ) \
104 { \
105 return( ret ); \
106 } \
107 \
108 for( i = 0; i < 16; i++ ) \
109 dst[i] ^= src[i]; \
110}
111
112/*
113 * Authenticated encryption
114 */
115int ccm_crypt_and_tag( ccm_context *ctx, size_t length,
116 const unsigned char *iv, size_t iv_len,
117 const unsigned char *add, size_t add_len,
118 const unsigned char *input, unsigned char *output,
119 unsigned char *tag, size_t tag_len )
120{
121 int ret;
122 unsigned char i;
123 unsigned char q = 16 - 1 - iv_len;
124 size_t len_left;
125 unsigned char b[16];
126 unsigned char y[16];
127 unsigned char ctr[16];
128 const unsigned char *src;
129 unsigned char *dst;
130
131 /*
132 * Check length requirements: SP800-38C A.1
133 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
134 * 'length' checked later (when writing it to the first block)
135 */
136 if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
137 return( POLARSSL_ERR_CCM_BAD_INPUT );
138
139 /* Also implies q is within bounds */
140 if( iv_len < 7 || iv_len > 13 )
141 return( POLARSSL_ERR_CCM_BAD_INPUT );
142
143 if( add_len > 0xFF00 )
144 return( POLARSSL_ERR_CCM_BAD_INPUT );
145
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
163 memcpy( b + 1, iv, iv_len );
164
165 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
166 b[15-i] = (unsigned char)( len_left & 0xFF );
167
168 if( len_left > 0 )
169 return( POLARSSL_ERR_CCM_BAD_INPUT );
170
171
172 /* Start CBC-MAC with first block */
173 memset( y, 0, 16 );
174 UPDATE_CBC_MAC( b );
175
176 /*
177 * If there is additional data, update CBC-MAC with
178 * add_len, add, 0 (padding to a block boundary)
179 */
180 if( add_len > 0 )
181 {
182 size_t use_len;
183 len_left = add_len;
184 src = add;
185
186 memset( b, 0, 16 );
187 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
188 b[1] = (unsigned char)( ( add_len ) & 0xFF );
189
190 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
191 memcpy( b + 2, src, use_len );
192 len_left -= use_len;
193 src += use_len;
194
195 UPDATE_CBC_MAC( b );
196
197 while( len_left > 16 )
198 {
199 memcpy( b, src, 16 );
200 UPDATE_CBC_MAC( b );
201
202 len_left -= 16;
203 src += 16;
204 }
205
206 if( len_left > 0 )
207 {
208 memset( b, 0, 16 );
209 memcpy( b, src, len_left );
210
211 UPDATE_CBC_MAC( b );
212 }
213 }
214
215 /*
216 * Counter block:
217 * 0 .. 0 flags
218 * 1 .. iv_len nonce (aka iv)
219 * iv_len+1 .. 15 counter (initially 1)
220 *
221 * With flags as (bits):
222 * 7 .. 3 0
223 * 2 .. 0 q - 1
224 */
225 ctr[0] = q - 1;
226 memcpy( ctr + 1, iv, iv_len );
227 memset( ctr + 1 + iv_len, 0, q );
228 ctr[15] = 1;
229
230 len_left = length;
231 src = input;
232 dst = output;
233
234 /*
235 * Authenticate and encrypt message
236 */
237 while( len_left > 16 )
238 {
239 memcpy( b, src, 16 );
240 UPDATE_CBC_MAC( b );
241 CTR_CRYPT_BLOCK( dst, src );
242
243 dst += 16;
244 src += 16;
245 len_left -= 16;
246
247 for( i = 0; i < q; i++ )
248 if( ++ctr[15-i] != 0 )
249 break;
250 }
251
252 if( len_left > 0 )
253 {
254 unsigned char mask[16];
255 size_t olen;
256
257 memset( b, 0, 16 );
258 memcpy( b, src, len_left );
259
260 UPDATE_CBC_MAC( b );
261
262 if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16,
263 mask, &olen ) ) != 0 )
264 {
265 return( ret );
266 }
267
268 for( i = 0; i < len_left; i++ )
269 dst[i] = src[i] ^ mask[i];
270 }
271
272 /*
273 * Authentication: reset counter and encrypt internal tag
274 */
275 for( i = 0; i < q; i++ )
276 ctr[15-i] = 0;
277
278 CTR_CRYPT_BLOCK( b, y );
279 memcpy( tag, b, tag_len );
280
281 return( 0 );
282}
283
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200284
285#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
286
287#if defined(POLARSSL_PLATFORM_C)
288#include "polarssl/platform.h"
289#else
290#define polarssl_printf printf
291#endif
292
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200293/*
294 * Examples 1 to 3 from SP800-38C Appendix C
295 */
296
297#define NB_TESTS 3
298
299/*
300 * The data is the same for all tests, only the used length changes
301 */
302static const unsigned char key[] = {
303 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
304 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
305};
306
307static const unsigned char iv[] = {
308 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
309 0x18, 0x19, 0x1a, 0x1b
310};
311
312static const unsigned char ad[] = {
313 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
314 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
315 0x10, 0x11, 0x12, 0x13
316};
317
318static 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
324static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
325static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
326static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
327static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
328
329static const unsigned char res[NB_TESTS][32] = {
330 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
331 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
332 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
333 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
334 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
335 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
336 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
337 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
338};
339
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200340int ccm_self_test( int verbose )
341{
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200342 ccm_context ctx;
343 unsigned char out[32];
344 size_t i;
345 int ret;
346
347 if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
348 {
349 if( verbose != 0 )
350 polarssl_printf( " CCM: setup failed" );
351
352 return( 1 );
353 }
354
355 for( i = 0; i < NB_TESTS; i++ )
356 {
357 if( verbose != 0 )
358 polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
359
360 ret = ccm_crypt_and_tag( &ctx, msg_len[i],
361 iv, iv_len[i], ad, add_len[i],
362 msg, out,
363 out + msg_len[i], tag_len[i] );
364
365 if( ret != 0 ||
366 memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
367 {
368 if( verbose != 0 )
369 polarssl_printf( "failed\n" );
370
371 return( 1 );
372 }
373
374 if( verbose != 0 )
375 polarssl_printf( "passed\n" );
376 }
377
378 ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200379
380 if( verbose != 0 )
381 polarssl_printf( "\n" );
382
383 return( 0 );
384}
385
386#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
387
388#endif /* POLARSSL_CCM_C */