blob: 18a2343ac54464d056bfdd6c47427de28b38faa0 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020047 */
48
49/*
50 * Definition of CCM:
51 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
52 * RFC 3610 "Counter with CBC-MAC (CCM)"
53 *
54 * Related:
55 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
56 */
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/config.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020065
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/ccm.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050067#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020068
Rich Evans00ab4702015-02-06 13:43:58 +000069#include <string.h>
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
72#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000074#else
75#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define mbedtls_printf printf
77#endif /* MBEDTLS_PLATFORM_C */
78#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Rich Evans00ab4702015-02-06 13:43:58 +000079
Steven Cooreman222e2ff2017-04-04 11:37:15 +020080#if !defined(MBEDTLS_CCM_ALT)
81
k-stachowiak26d365e2018-12-11 12:22:16 +010082#define CCM_VALIDATE_RET( cond ) \
83 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT )
84#define CCM_VALIDATE( cond ) \
85 MBEDTLS_INTERNAL_VALIDATE( cond )
86
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020087#define CCM_ENCRYPT 0
88#define CCM_DECRYPT 1
89
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090/*
91 * Initialize context
92 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020093void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
94{
k-stachowiak26d365e2018-12-11 12:22:16 +010095 CCM_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020096 memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
97}
98
99int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
100 mbedtls_cipher_id_t cipher,
101 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200102 unsigned int keybits )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200103{
104 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200106
k-stachowiak26d365e2018-12-11 12:22:16 +0100107 CCM_VALIDATE_RET( ctx != NULL );
108 CCM_VALIDATE_RET( key != NULL );
109
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200110 cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200111 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200113
114 if( cipher_info->block_size != 16 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200116
Manuel Pégourié-Gonnard43b08572015-05-27 17:23:30 +0200117 mbedtls_cipher_free( &ctx->cipher_ctx );
118
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200119 if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200120 return( ret );
121
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200122 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200124 {
125 return( ret );
126 }
127
128 return( 0 );
129}
130
131/*
132 * Free context
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200135{
k-stachowiakfd42d532018-12-11 14:37:51 +0100136 if( ctx == NULL )
137 return;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_cipher_free( &ctx->cipher_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500139 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200140}
141
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200142/*
143 * Macros for common operations.
144 * Results in smaller compiled code than static inline functions.
145 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200146
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200147/*
148 * Update the CBC-MAC state in y using a block in b
149 * (Always using b as the source helps the compiler optimise a bit better.)
150 */
151#define UPDATE_CBC_MAC \
152 for( i = 0; i < 16; i++ ) \
153 y[i] ^= b[i]; \
154 \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200156 return( ret );
157
158/*
159 * Encrypt or decrypt a partial block with CTR
160 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200161 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200162 */
163#define CTR_CRYPT( dst, src, len ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100164 do \
165 { \
166 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
167 16, b, &olen ) ) != 0 ) \
168 { \
169 return( ret ); \
170 } \
171 \
172 for( i = 0; i < (len); i++ ) \
173 (dst)[i] = (src)[i] ^ b[i]; \
174 } while( 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200175
176/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200177 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200180 const unsigned char *iv, size_t iv_len,
181 const unsigned char *add, size_t add_len,
182 const unsigned char *input, unsigned char *output,
183 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200184{
185 int ret;
186 unsigned char i;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200187 unsigned char q;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200188 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200189 unsigned char b[16];
190 unsigned char y[16];
191 unsigned char ctr[16];
192 const unsigned char *src;
193 unsigned char *dst;
194
195 /*
196 * Check length requirements: SP800-38C A.1
197 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
198 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100199 *
200 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200201 */
Janos Follath997e85c2018-05-29 11:33:45 +0100202 if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
Janos Follath997e85c2018-05-29 11:33:45 +0100203 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200204
205 /* Also implies q is within bounds */
206 if( iv_len < 7 || iv_len > 13 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200208
209 if( add_len > 0xFF00 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200211
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200212 q = 16 - 1 - (unsigned char) iv_len;
213
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200214 /*
215 * First block B_0:
216 * 0 .. 0 flags
217 * 1 .. iv_len nonce (aka iv)
218 * iv_len+1 .. 15 length
219 *
220 * With flags as (bits):
221 * 7 0
222 * 6 add present?
223 * 5 .. 3 (t - 2) / 2
224 * 2 .. 0 q - 1
225 */
226 b[0] = 0;
227 b[0] |= ( add_len > 0 ) << 6;
228 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
229 b[0] |= q - 1;
230
231 memcpy( b + 1, iv, iv_len );
232
233 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
234 b[15-i] = (unsigned char)( len_left & 0xFF );
235
236 if( len_left > 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200238
239
240 /* Start CBC-MAC with first block */
241 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200242 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200243
244 /*
245 * If there is additional data, update CBC-MAC with
246 * add_len, add, 0 (padding to a block boundary)
247 */
248 if( add_len > 0 )
249 {
250 size_t use_len;
251 len_left = add_len;
252 src = add;
253
254 memset( b, 0, 16 );
255 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
256 b[1] = (unsigned char)( ( add_len ) & 0xFF );
257
258 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
259 memcpy( b + 2, src, use_len );
260 len_left -= use_len;
261 src += use_len;
262
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200263 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200264
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200265 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200266 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200267 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200268
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200269 memset( b, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200270 memcpy( b, src, use_len );
271 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200273 len_left -= use_len;
274 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200275 }
276 }
277
278 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200279 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200280 * 0 .. 0 flags
281 * 1 .. iv_len nonce (aka iv)
282 * iv_len+1 .. 15 counter (initially 1)
283 *
284 * With flags as (bits):
285 * 7 .. 3 0
286 * 2 .. 0 q - 1
287 */
288 ctr[0] = q - 1;
289 memcpy( ctr + 1, iv, iv_len );
290 memset( ctr + 1 + iv_len, 0, q );
291 ctr[15] = 1;
292
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200293 /*
294 * Authenticate and {en,de}crypt the message.
295 *
296 * The only difference between encryption and decryption is
297 * the respective order of authentication and {en,de}cryption.
298 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200299 len_left = length;
300 src = input;
301 dst = output;
302
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200303 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200304 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200305 size_t use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200306
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200307 if( mode == CCM_ENCRYPT )
308 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200309 memset( b, 0, 16 );
310 memcpy( b, src, use_len );
311 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200312 }
313
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200314 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200315
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200316 if( mode == CCM_DECRYPT )
317 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200318 memset( b, 0, 16 );
319 memcpy( b, dst, use_len );
320 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200321 }
322
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200323 dst += use_len;
324 src += use_len;
325 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200326
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200327 /*
328 * Increment counter.
329 * No need to check for overflow thanks to the length check above.
330 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200331 for( i = 0; i < q; i++ )
332 if( ++ctr[15-i] != 0 )
333 break;
334 }
335
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200336 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200337 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200338 */
339 for( i = 0; i < q; i++ )
340 ctr[15-i] = 0;
341
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200342 CTR_CRYPT( y, y, 16 );
343 memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200344
345 return( 0 );
346}
347
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200348/*
349 * Authenticated encryption
350 */
Janos Follathb5734a22018-05-14 14:31:49 +0100351int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200352 const unsigned char *iv, size_t iv_len,
353 const unsigned char *add, size_t add_len,
354 const unsigned char *input, unsigned char *output,
355 unsigned char *tag, size_t tag_len )
356{
k-stachowiak26d365e2018-12-11 12:22:16 +0100357 CCM_VALIDATE_RET( ctx != NULL );
358 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100359 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
360 CCM_VALIDATE_RET( length == 0 || input != NULL );
361 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100362 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200363 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
364 add, add_len, input, output, tag, tag_len ) );
365}
366
Janos Follathb5734a22018-05-14 14:31:49 +0100367int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
368 const unsigned char *iv, size_t iv_len,
369 const unsigned char *add, size_t add_len,
370 const unsigned char *input, unsigned char *output,
371 unsigned char *tag, size_t tag_len )
372{
k-stachowiak26d365e2018-12-11 12:22:16 +0100373 CCM_VALIDATE_RET( ctx != NULL );
374 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100375 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
376 CCM_VALIDATE_RET( length == 0 || input != NULL );
377 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100378 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Janos Follathb5734a22018-05-14 14:31:49 +0100379 if( tag_len == 0 )
380 return( MBEDTLS_ERR_CCM_BAD_INPUT );
381
382 return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add,
383 add_len, input, output, tag, tag_len ) );
384}
385
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200386/*
387 * Authenticated decryption
388 */
Janos Follathb5734a22018-05-14 14:31:49 +0100389int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200390 const unsigned char *iv, size_t iv_len,
391 const unsigned char *add, size_t add_len,
392 const unsigned char *input, unsigned char *output,
393 const unsigned char *tag, size_t tag_len )
394{
395 int ret;
396 unsigned char check_tag[16];
397 unsigned char i;
398 int diff;
399
k-stachowiak26d365e2018-12-11 12:22:16 +0100400 CCM_VALIDATE_RET( ctx != NULL );
401 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100402 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
403 CCM_VALIDATE_RET( length == 0 || input != NULL );
404 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100405 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak26d365e2018-12-11 12:22:16 +0100406
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200407 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
408 iv, iv_len, add, add_len,
409 input, output, check_tag, tag_len ) ) != 0 )
410 {
411 return( ret );
412 }
413
414 /* Check tag in "constant-time" */
415 for( diff = 0, i = 0; i < tag_len; i++ )
416 diff |= tag[i] ^ check_tag[i];
417
418 if( diff != 0 )
419 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500420 mbedtls_platform_zeroize( output, length );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200422 }
423
424 return( 0 );
425}
426
Janos Follathb5734a22018-05-14 14:31:49 +0100427int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
428 const unsigned char *iv, size_t iv_len,
429 const unsigned char *add, size_t add_len,
430 const unsigned char *input, unsigned char *output,
431 const unsigned char *tag, size_t tag_len )
432{
k-stachowiakf7125342018-12-11 15:57:19 +0100433 CCM_VALIDATE_RET( ctx != NULL );
434 CCM_VALIDATE_RET( iv != NULL );
435 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
436 CCM_VALIDATE_RET( length == 0 || input != NULL );
437 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100438 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiakf7125342018-12-11 15:57:19 +0100439
Janos Follathb5734a22018-05-14 14:31:49 +0100440 if( tag_len == 0 )
441 return( MBEDTLS_ERR_CCM_BAD_INPUT );
442
443 return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add,
444 add_len, input, output, tag, tag_len ) );
445}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200446#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200449/*
450 * Examples 1 to 3 from SP800-38C Appendix C
451 */
452
453#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300454#define CCM_SELFTEST_PT_MAX_LEN 24
455#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200456/*
457 * The data is the same for all tests, only the used length changes
458 */
459static const unsigned char key[] = {
460 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
461 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
462};
463
464static const unsigned char iv[] = {
465 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
466 0x18, 0x19, 0x1a, 0x1b
467};
468
469static const unsigned char ad[] = {
470 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
471 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
472 0x10, 0x11, 0x12, 0x13
473};
474
Ron Eldor1b9b2172018-04-26 14:15:01 +0300475static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200476 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
477 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
478 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
479};
480
481static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
482static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
483static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
484static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
485
Ron Eldor1b9b2172018-04-26 14:15:01 +0300486static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200487 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
488 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
489 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
490 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
491 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
492 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
493 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
494 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
495};
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_ccm_self_test( int verbose )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300500 /*
501 * Some hardware accelerators require the input and output buffers
502 * would be in RAM, because the flash is not accessible.
503 * Use buffers on the stack to hold the test vectors data.
504 */
505 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
506 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200507 size_t i;
508 int ret;
509
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200510 mbedtls_ccm_init( &ctx );
511
512 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200513 {
514 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_printf( " CCM: setup failed" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200516
517 return( 1 );
518 }
519
520 for( i = 0; i < NB_TESTS; i++ )
521 {
522 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200524
Ron Eldor1b9b2172018-04-26 14:15:01 +0300525 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
526 memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
527 memcpy( plaintext, msg, msg_len[i] );
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300530 iv, iv_len[i], ad, add_len[i],
531 plaintext, ciphertext,
532 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200533
534 if( ret != 0 ||
Ron Eldor1b9b2172018-04-26 14:15:01 +0300535 memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200536 {
537 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200539
540 return( 1 );
541 }
Ron Eldor1b9b2172018-04-26 14:15:01 +0300542 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300545 iv, iv_len[i], ad, add_len[i],
546 ciphertext, plaintext,
547 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200548
549 if( ret != 0 ||
Ron Eldor1b9b2172018-04-26 14:15:01 +0300550 memcmp( plaintext, msg, msg_len[i] ) != 0 )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200551 {
552 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200554
555 return( 1 );
556 }
557
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200558 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200560 }
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200563
564 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200566
567 return( 0 );
568}
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_CCM_C */