blob: d785866f2df86db28f1f21dc131dc8fdcb2b2ae9 [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é-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020020 */
21
22/*
23 * Definition of CCM:
24 * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
25 * RFC 3610 "Counter with CBC-MAC (CCM)"
26 *
27 * Related:
28 * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
29 */
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/config.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020038
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ccm.h"
Andrzej Kurekfd56f402020-05-25 11:52:05 -040040#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050041#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
46#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/platform.h"
Rich Evans00ab4702015-02-06 13:43:58 +000048#else
49#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define mbedtls_printf printf
51#endif /* MBEDTLS_PLATFORM_C */
52#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Rich Evans00ab4702015-02-06 13:43:58 +000053
Steven Cooreman222e2ff2017-04-04 11:37:15 +020054#if !defined(MBEDTLS_CCM_ALT)
55
k-stachowiak26d365e2018-12-11 12:22:16 +010056#define CCM_VALIDATE_RET( cond ) \
57 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT )
58#define CCM_VALIDATE( cond ) \
59 MBEDTLS_INTERNAL_VALIDATE( cond )
60
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +020061#define CCM_ENCRYPT 0
62#define CCM_DECRYPT 1
63
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020064/*
65 * Initialize context
66 */
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020067void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
68{
k-stachowiak26d365e2018-12-11 12:22:16 +010069 CCM_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +020070 memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
71}
72
73int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
74 mbedtls_cipher_id_t cipher,
75 const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020076 unsigned int keybits )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020077{
Andrzej Kurekfd56f402020-05-25 11:52:05 -040078 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020080
k-stachowiak26d365e2018-12-11 12:22:16 +010081 CCM_VALIDATE_RET( ctx != NULL );
82 CCM_VALIDATE_RET( key != NULL );
83
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020084 cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020085 if( cipher_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020087
88 if( cipher_info->block_size != 16 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020090
Manuel Pégourié-Gonnard43b08572015-05-27 17:23:30 +020091 mbedtls_cipher_free( &ctx->cipher_ctx );
92
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020093 if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020094 return( ret );
95
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020096 if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020098 {
99 return( ret );
100 }
101
Andrzej Kurekafec8852020-07-15 16:31:27 -0400102 return( ret );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200103}
104
105/*
106 * Free context
107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200109{
k-stachowiakfd42d532018-12-11 14:37:51 +0100110 if( ctx == NULL )
111 return;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_cipher_free( &ctx->cipher_ctx );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500113 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +0200114}
115
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100116/* Durstenfeld's version of Fisher-Yates shuffle */
117static void mbedtls_generate_permutation( unsigned char* table, size_t size )
118{
119 size_t i, j;
120
121 for( i = 0; i < size; i++ )
122 {
123 table[i] = (unsigned char) i;
124 }
125
126 if( size < 2 )
127 {
128 return;
129 }
130
131 for( i = size - 1; i > 0; i-- )
132 {
133 unsigned char tmp;
134 j = mbedtls_platform_random_uint32() % ( i + 1 );
135 tmp = table[i];
136 table[i] = table[j];
137 table[j] = tmp;
138 }
139}
140
141static void mbedtls_generate_masks( unsigned char* table, size_t size )
142{
143 size_t i;
144
145 for( i = 0; i < size; i++ )
146 {
147 table[i] = mbedtls_platform_random_uint32() % ( 256 );
148 }
149}
150
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200151/*
152 * Macros for common operations.
153 * Results in smaller compiled code than static inline functions.
154 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200155
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200156/*
157 * Update the CBC-MAC state in y using a block in b
158 * (Always using b as the source helps the compiler optimise a bit better.)
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100159 * Initial b masking happens outside of this macro due to various sources of it.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200160 */
161#define UPDATE_CBC_MAC \
162 for( i = 0; i < 16; i++ ) \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100163 { \
164 y[perm_table[i]] ^= b[perm_table[i]]; \
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100165 y[perm_table[i]] ^= mask_table[perm_table[i]]; \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100166 } \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200167 \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200169 return( ret );
170
171/*
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100172 * Copy src to dst starting at a random offset, while masking the whole dst buffer.
173 */
174#define COPY_MASK( dst, src, mask, len_src, len_dst ) \
175 do \
176 { \
177 unsigned j, offset = mbedtls_platform_random_uint32() % 256; \
178 for( i = 0; i < len_src; i++ ) \
179 { \
180 j = (i + offset) % len_src; \
181 (dst)[j] = (src)[j] ^ (mask)[j]; \
182 } \
183 for( ; i < len_dst; i++ ) \
184 (dst)[i] ^= (mask)[i]; \
185 } while( 0 )
186/*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200187 * Encrypt or decrypt a partial block with CTR
188 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200189 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200190 */
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100191#define CTR_CRYPT( dst, src, len ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100192 do \
193 { \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100194 mbedtls_generate_permutation( perm_table, len ); \
195 mbedtls_generate_masks( mask_table, len ); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100196 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
197 16, b, &olen ) ) != 0 ) \
198 { \
199 return( ret ); \
200 } \
201 \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100202 for( i = 0; i < len; i++ ) \
203 { \
204 (dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\
205 (dst)[perm_table[i]] ^= b[perm_table[i]]; \
206 (dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \
207 } \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100208 } while( 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200209
210/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200211 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200214 const unsigned char *iv, size_t iv_len,
215 const unsigned char *add, size_t add_len,
216 const unsigned char *input, unsigned char *output,
217 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200218{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400219 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200220 unsigned char i;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200221 unsigned char q;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200222 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200223 unsigned char b[16];
224 unsigned char y[16];
225 unsigned char ctr[16];
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100226 unsigned char perm_table[16];
227 unsigned char mask_table[16];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200228 const unsigned char *src;
229 unsigned char *dst;
230
231 /*
232 * Check length requirements: SP800-38C A.1
233 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
234 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100235 *
236 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200237 */
Janos Follath997e85c2018-05-29 11:33:45 +0100238 if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
Janos Follath997e85c2018-05-29 11:33:45 +0100239 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200240
241 /* Also implies q is within bounds */
242 if( iv_len < 7 || iv_len > 13 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200244
245 if( add_len > 0xFF00 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200247
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200248 q = 16 - 1 - (unsigned char) iv_len;
249
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200250 /*
251 * First block B_0:
252 * 0 .. 0 flags
253 * 1 .. iv_len nonce (aka iv)
254 * iv_len+1 .. 15 length
255 *
256 * With flags as (bits):
257 * 7 0
258 * 6 add present?
259 * 5 .. 3 (t - 2) / 2
260 * 2 .. 0 q - 1
261 */
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100262 mbedtls_generate_masks( mask_table, 16 );
263 mbedtls_generate_permutation( perm_table, 16 );
264 b[0] = (unsigned char) ( ( ( add_len > 0 ) << 6 ) |
265 ( ( ( tag_len - 2 ) / 2 ) << 3 ) |
266 ( q - 1 ) ) ^ mask_table[0];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200267
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100268 for( i = 0; i < iv_len; i++ )
269 b[i+1] = iv[i] ^ mask_table[i+1];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200270 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100271 b[15-i] = (unsigned char)( ( len_left & 0xFF ) ) ^ mask_table[15-i];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200272
273 if( len_left > 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200275
276
277 /* Start CBC-MAC with first block */
278 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200279 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200280
281 /*
282 * If there is additional data, update CBC-MAC with
283 * add_len, add, 0 (padding to a block boundary)
284 */
285 if( add_len > 0 )
286 {
287 size_t use_len;
288 len_left = add_len;
289 src = add;
290
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100291 mbedtls_generate_masks( mask_table, 16 );
292 mbedtls_generate_permutation( perm_table, 16 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200293 mbedtls_platform_memset( b, 0, 16 );
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100294 b[0] = (unsigned char)( ( ( add_len >> 8 ) & 0xFF ) ^ mask_table[0] );
295 b[1] = (unsigned char)( ( ( add_len ) & 0xFF ) ^ mask_table[1] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200296
297 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100298
299 COPY_MASK( b+2, src, mask_table+2, use_len, 14 );
300
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200301 len_left -= use_len;
302 src += use_len;
303
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200304 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200305
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200306 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200307 {
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100308 mbedtls_generate_masks( mask_table, 16 );
309 mbedtls_generate_permutation( perm_table, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200310 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200311
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200312 mbedtls_platform_memset( b, 0, 16 );
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100313 COPY_MASK( b, src, mask_table, use_len, 16);
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200314 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200315
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200316 len_left -= use_len;
317 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200318 }
319 }
320
321 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200322 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200323 * 0 .. 0 flags
324 * 1 .. iv_len nonce (aka iv)
325 * iv_len+1 .. 15 counter (initially 1)
326 *
327 * With flags as (bits):
328 * 7 .. 3 0
329 * 2 .. 0 q - 1
330 */
331 ctr[0] = q - 1;
Teppo Järvelin91d79382019-10-02 09:09:31 +0300332 mbedtls_platform_memcpy( ctr + 1, iv, iv_len );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200333 mbedtls_platform_memset( ctr + 1 + iv_len, 0, q );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200334 ctr[15] = 1;
335
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200336 /*
337 * Authenticate and {en,de}crypt the message.
338 *
339 * The only difference between encryption and decryption is
340 * the respective order of authentication and {en,de}cryption.
341 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200342 len_left = length;
343 src = input;
344 dst = output;
345
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200346 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200347 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200348 size_t use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200349
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200350 if( mode == CCM_ENCRYPT )
351 {
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100352 mbedtls_generate_masks( mask_table, 16 );
353 mbedtls_generate_permutation( perm_table, 16 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200354 mbedtls_platform_memset( b, 0, 16 );
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100355 COPY_MASK( b, src, mask_table, use_len, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200356 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200357 }
358
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200359 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200360
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200361 if( mode == CCM_DECRYPT )
362 {
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100363 mbedtls_generate_masks( mask_table, 16 );
364 mbedtls_generate_permutation( perm_table, 16 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200365 mbedtls_platform_memset( b, 0, 16 );
Andrzej Kurek0fa427b2020-10-30 14:41:12 +0100366 COPY_MASK( b, dst, mask_table, use_len, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200367 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200368 }
369
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200370 dst += use_len;
371 src += use_len;
372 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200373
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200374 /*
375 * Increment counter.
376 * No need to check for overflow thanks to the length check above.
377 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200378 for( i = 0; i < q; i++ )
379 if( ++ctr[15-i] != 0 )
380 break;
381 }
382
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200383 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200384 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200385 */
386 for( i = 0; i < q; i++ )
387 ctr[15-i] = 0;
388
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200389 CTR_CRYPT( y, y, 16 );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300390 mbedtls_platform_memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200391
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400392 return( ret );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200393}
394
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200395/*
396 * Authenticated encryption
397 */
Janos Follathb5734a22018-05-14 14:31:49 +0100398int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200399 const unsigned char *iv, size_t iv_len,
400 const unsigned char *add, size_t add_len,
401 const unsigned char *input, unsigned char *output,
402 unsigned char *tag, size_t tag_len )
403{
k-stachowiak26d365e2018-12-11 12:22:16 +0100404 CCM_VALIDATE_RET( ctx != NULL );
405 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100406 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
407 CCM_VALIDATE_RET( length == 0 || input != NULL );
408 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100409 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200410 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
411 add, add_len, input, output, tag, tag_len ) );
412}
413
Janos Follathb5734a22018-05-14 14:31:49 +0100414int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
415 const unsigned char *iv, size_t iv_len,
416 const unsigned char *add, size_t add_len,
417 const unsigned char *input, unsigned char *output,
418 unsigned char *tag, size_t tag_len )
419{
k-stachowiak26d365e2018-12-11 12:22:16 +0100420 CCM_VALIDATE_RET( ctx != NULL );
421 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100422 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
423 CCM_VALIDATE_RET( length == 0 || input != NULL );
424 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100425 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Janos Follathb5734a22018-05-14 14:31:49 +0100426 if( tag_len == 0 )
427 return( MBEDTLS_ERR_CCM_BAD_INPUT );
428
429 return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add,
430 add_len, input, output, tag, tag_len ) );
431}
432
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200433/*
434 * Authenticated decryption
435 */
Janos Follathb5734a22018-05-14 14:31:49 +0100436int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200437 const unsigned char *iv, size_t iv_len,
438 const unsigned char *add, size_t add_len,
439 const unsigned char *input, unsigned char *output,
440 const unsigned char *tag, size_t tag_len )
441{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400442 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200443 unsigned char check_tag[16];
444 unsigned char i;
445 int diff;
446
k-stachowiak26d365e2018-12-11 12:22:16 +0100447 CCM_VALIDATE_RET( ctx != NULL );
448 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100449 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
450 CCM_VALIDATE_RET( length == 0 || input != NULL );
451 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100452 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiak26d365e2018-12-11 12:22:16 +0100453
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200454 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
455 iv, iv_len, add, add_len,
456 input, output, check_tag, tag_len ) ) != 0 )
457 {
458 return( ret );
459 }
460
461 /* Check tag in "constant-time" */
462 for( diff = 0, i = 0; i < tag_len; i++ )
463 diff |= tag[i] ^ check_tag[i];
464
465 if( diff != 0 )
466 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500467 mbedtls_platform_zeroize( output, length );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200469 }
470
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400471 return( ret );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200472}
473
Janos Follathb5734a22018-05-14 14:31:49 +0100474int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
475 const unsigned char *iv, size_t iv_len,
476 const unsigned char *add, size_t add_len,
477 const unsigned char *input, unsigned char *output,
478 const unsigned char *tag, size_t tag_len )
479{
k-stachowiakf7125342018-12-11 15:57:19 +0100480 CCM_VALIDATE_RET( ctx != NULL );
481 CCM_VALIDATE_RET( iv != NULL );
482 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
483 CCM_VALIDATE_RET( length == 0 || input != NULL );
484 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100485 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiakf7125342018-12-11 15:57:19 +0100486
Janos Follathb5734a22018-05-14 14:31:49 +0100487 if( tag_len == 0 )
488 return( MBEDTLS_ERR_CCM_BAD_INPUT );
489
490 return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add,
491 add_len, input, output, tag, tag_len ) );
492}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200493#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200496/*
497 * Examples 1 to 3 from SP800-38C Appendix C
498 */
499
500#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300501#define CCM_SELFTEST_PT_MAX_LEN 24
502#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200503/*
504 * The data is the same for all tests, only the used length changes
505 */
506static const unsigned char key[] = {
507 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
508 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
509};
510
511static const unsigned char iv[] = {
512 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
513 0x18, 0x19, 0x1a, 0x1b
514};
515
516static const unsigned char ad[] = {
517 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
518 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
519 0x10, 0x11, 0x12, 0x13
520};
521
Ron Eldor1b9b2172018-04-26 14:15:01 +0300522static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200523 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
524 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
525 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
526};
527
528static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
529static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
530static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
531static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
532
Ron Eldor1b9b2172018-04-26 14:15:01 +0300533static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200534 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
535 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
536 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
537 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
538 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
539 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
540 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
541 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
542};
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544int mbedtls_ccm_self_test( int verbose )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300547 /*
548 * Some hardware accelerators require the input and output buffers
549 * would be in RAM, because the flash is not accessible.
550 * Use buffers on the stack to hold the test vectors data.
551 */
552 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
553 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200554 size_t i;
555 int ret;
556
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200557 mbedtls_ccm_init( &ctx );
558
559 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200560 {
561 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_printf( " CCM: setup failed" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200563
564 return( 1 );
565 }
566
567 for( i = 0; i < NB_TESTS; i++ )
568 {
569 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200571
Teppo Järvelinb5c46712019-10-04 13:35:55 +0300572 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
573 memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
574 memcpy( plaintext, msg, msg_len[i] );
Ron Eldor1b9b2172018-04-26 14:15:01 +0300575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300577 iv, iv_len[i], ad, add_len[i],
578 plaintext, ciphertext,
579 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200580
581 if( ret != 0 ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300582 memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200583 {
584 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200586
587 return( 1 );
588 }
Teppo Järvelind49d2b62019-10-30 13:48:12 +0200589 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300592 iv, iv_len[i], ad, add_len[i],
593 ciphertext, plaintext,
594 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200595
596 if( ret != 0 ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300597 memcmp( plaintext, msg, msg_len[i] ) != 0 )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200598 {
599 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200601
602 return( 1 );
603 }
604
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200605 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200607 }
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200610
611 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200613
614 return( 0 );
615}
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_CCM_C */