blob: edd7b8f8d2504019fbfbf9d22da87390603cfa86 [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.)
159 */
160#define UPDATE_CBC_MAC \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100161 mbedtls_generate_permutation( perm_table, 16 ); \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200162 for( i = 0; i < 16; i++ ) \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100163 { \
164 y[perm_table[i]] ^= b[perm_table[i]]; \
165 } \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200166 \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200168 return( ret );
169
170/*
171 * Encrypt or decrypt a partial block with CTR
172 * Warning: using b for temporary storage! src and dst must not be b!
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200173 * This avoids allocating one more 16 bytes buffer while allowing src == dst.
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200174 */
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100175#define CTR_CRYPT( dst, src, len ) \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100176 do \
177 { \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100178 mbedtls_generate_permutation( perm_table, len ); \
179 mbedtls_generate_masks( mask_table, len ); \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100180 if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
181 16, b, &olen ) ) != 0 ) \
182 { \
183 return( ret ); \
184 } \
185 \
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100186 for( i = 0; i < len; i++ ) \
187 { \
188 (dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\
189 (dst)[perm_table[i]] ^= b[perm_table[i]]; \
190 (dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \
191 } \
Hanno Beckerd6028a12018-10-15 12:01:35 +0100192 } while( 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200193
194/*
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200195 * Authenticated encryption or decryption
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200198 const unsigned char *iv, size_t iv_len,
199 const unsigned char *add, size_t add_len,
200 const unsigned char *input, unsigned char *output,
201 unsigned char *tag, size_t tag_len )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200202{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400203 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200204 unsigned char i;
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200205 unsigned char q;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200206 size_t len_left, olen;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200207 unsigned char b[16];
208 unsigned char y[16];
209 unsigned char ctr[16];
Andrzej Kurek8bef87e2020-10-30 12:18:21 +0100210 unsigned char perm_table[16];
211 unsigned char mask_table[16];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200212 const unsigned char *src;
213 unsigned char *dst;
214
215 /*
216 * Check length requirements: SP800-38C A.1
217 * Additional requirement: a < 2^16 - 2^8 to simplify the code.
218 * 'length' checked later (when writing it to the first block)
Janos Follath997e85c2018-05-29 11:33:45 +0100219 *
220 * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200221 */
Janos Follath997e85c2018-05-29 11:33:45 +0100222 if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
Janos Follath997e85c2018-05-29 11:33:45 +0100223 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200224
225 /* Also implies q is within bounds */
226 if( iv_len < 7 || iv_len > 13 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200228
229 if( add_len > 0xFF00 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200231
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200232 q = 16 - 1 - (unsigned char) iv_len;
233
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200234 /*
235 * First block B_0:
236 * 0 .. 0 flags
237 * 1 .. iv_len nonce (aka iv)
238 * iv_len+1 .. 15 length
239 *
240 * With flags as (bits):
241 * 7 0
242 * 6 add present?
243 * 5 .. 3 (t - 2) / 2
244 * 2 .. 0 q - 1
245 */
246 b[0] = 0;
247 b[0] |= ( add_len > 0 ) << 6;
248 b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
249 b[0] |= q - 1;
250
Teppo Järvelin91d79382019-10-02 09:09:31 +0300251 mbedtls_platform_memcpy( b + 1, iv, iv_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200252
253 for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
254 b[15-i] = (unsigned char)( len_left & 0xFF );
255
256 if( len_left > 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_CCM_BAD_INPUT );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200258
259
260 /* Start CBC-MAC with first block */
261 memset( y, 0, 16 );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200262 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200263
264 /*
265 * If there is additional data, update CBC-MAC with
266 * add_len, add, 0 (padding to a block boundary)
267 */
268 if( add_len > 0 )
269 {
270 size_t use_len;
271 len_left = add_len;
272 src = add;
273
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200274 mbedtls_platform_memset( b, 0, 16 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200275 b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
276 b[1] = (unsigned char)( ( add_len ) & 0xFF );
277
278 use_len = len_left < 16 - 2 ? len_left : 16 - 2;
Teppo Järvelin91d79382019-10-02 09:09:31 +0300279 mbedtls_platform_memcpy( b + 2, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200280 len_left -= use_len;
281 src += use_len;
282
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200283 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200284
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200285 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200286 {
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200287 use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200288
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200289 mbedtls_platform_memset( b, 0, 16 );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300290 mbedtls_platform_memcpy( b, src, use_len );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200291 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200292
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200293 len_left -= use_len;
294 src += use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200295 }
296 }
297
298 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200299 * Prepare counter block for encryption:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200300 * 0 .. 0 flags
301 * 1 .. iv_len nonce (aka iv)
302 * iv_len+1 .. 15 counter (initially 1)
303 *
304 * With flags as (bits):
305 * 7 .. 3 0
306 * 2 .. 0 q - 1
307 */
308 ctr[0] = q - 1;
Teppo Järvelin91d79382019-10-02 09:09:31 +0300309 mbedtls_platform_memcpy( ctr + 1, iv, iv_len );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200310 mbedtls_platform_memset( ctr + 1 + iv_len, 0, q );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200311 ctr[15] = 1;
312
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200313 /*
314 * Authenticate and {en,de}crypt the message.
315 *
316 * The only difference between encryption and decryption is
317 * the respective order of authentication and {en,de}cryption.
318 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200319 len_left = length;
320 src = input;
321 dst = output;
322
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200323 while( len_left > 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200324 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200325 size_t use_len = len_left > 16 ? 16 : len_left;
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200326
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200327 if( mode == CCM_ENCRYPT )
328 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200329 mbedtls_platform_memset( b, 0, 16 );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300330 mbedtls_platform_memcpy( b, src, use_len );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200331 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200332 }
333
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200334 CTR_CRYPT( dst, src, use_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200335
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200336 if( mode == CCM_DECRYPT )
337 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200338 mbedtls_platform_memset( b, 0, 16 );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300339 mbedtls_platform_memcpy( b, dst, use_len );
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200340 UPDATE_CBC_MAC;
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200341 }
342
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200343 dst += use_len;
344 src += use_len;
345 len_left -= use_len;
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200346
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200347 /*
348 * Increment counter.
349 * No need to check for overflow thanks to the length check above.
350 */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200351 for( i = 0; i < q; i++ )
352 if( ++ctr[15-i] != 0 )
353 break;
354 }
355
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200356 /*
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200357 * Authentication: reset counter and crypt/mask internal tag
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200358 */
359 for( i = 0; i < q; i++ )
360 ctr[15-i] = 0;
361
Manuel Pégourié-Gonnardaed60652014-05-07 13:14:42 +0200362 CTR_CRYPT( y, y, 16 );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300363 mbedtls_platform_memcpy( tag, y, tag_len );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200364
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400365 return( ret );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200366}
367
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200368/*
369 * Authenticated encryption
370 */
Janos Follathb5734a22018-05-14 14:31:49 +0100371int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200372 const unsigned char *iv, size_t iv_len,
373 const unsigned char *add, size_t add_len,
374 const unsigned char *input, unsigned char *output,
375 unsigned char *tag, size_t tag_len )
376{
k-stachowiak26d365e2018-12-11 12:22:16 +0100377 CCM_VALIDATE_RET( ctx != NULL );
378 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100379 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
380 CCM_VALIDATE_RET( length == 0 || input != NULL );
381 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100382 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200383 return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
384 add, add_len, input, output, tag, tag_len ) );
385}
386
Janos Follathb5734a22018-05-14 14:31:49 +0100387int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
388 const unsigned char *iv, size_t iv_len,
389 const unsigned char *add, size_t add_len,
390 const unsigned char *input, unsigned char *output,
391 unsigned char *tag, size_t tag_len )
392{
k-stachowiak26d365e2018-12-11 12:22:16 +0100393 CCM_VALIDATE_RET( ctx != NULL );
394 CCM_VALIDATE_RET( iv != NULL );
k-stachowiakff8a0982018-12-11 15:56:55 +0100395 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
396 CCM_VALIDATE_RET( length == 0 || input != NULL );
397 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100398 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
Janos Follathb5734a22018-05-14 14:31:49 +0100399 if( tag_len == 0 )
400 return( MBEDTLS_ERR_CCM_BAD_INPUT );
401
402 return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add,
403 add_len, input, output, tag, tag_len ) );
404}
405
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200406/*
407 * Authenticated decryption
408 */
Janos Follathb5734a22018-05-14 14:31:49 +0100409int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200410 const unsigned char *iv, size_t iv_len,
411 const unsigned char *add, size_t add_len,
412 const unsigned char *input, unsigned char *output,
413 const unsigned char *tag, size_t tag_len )
414{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400415 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200416 unsigned char check_tag[16];
417 unsigned char i;
418 int diff;
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 );
k-stachowiak26d365e2018-12-11 12:22:16 +0100426
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200427 if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
428 iv, iv_len, add, add_len,
429 input, output, check_tag, tag_len ) ) != 0 )
430 {
431 return( ret );
432 }
433
434 /* Check tag in "constant-time" */
435 for( diff = 0, i = 0; i < tag_len; i++ )
436 diff |= tag[i] ^ check_tag[i];
437
438 if( diff != 0 )
439 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500440 mbedtls_platform_zeroize( output, length );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_CCM_AUTH_FAILED );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200442 }
443
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400444 return( ret );
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200445}
446
Janos Follathb5734a22018-05-14 14:31:49 +0100447int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
448 const unsigned char *iv, size_t iv_len,
449 const unsigned char *add, size_t add_len,
450 const unsigned char *input, unsigned char *output,
451 const unsigned char *tag, size_t tag_len )
452{
k-stachowiakf7125342018-12-11 15:57:19 +0100453 CCM_VALIDATE_RET( ctx != NULL );
454 CCM_VALIDATE_RET( iv != NULL );
455 CCM_VALIDATE_RET( add_len == 0 || add != NULL );
456 CCM_VALIDATE_RET( length == 0 || input != NULL );
457 CCM_VALIDATE_RET( length == 0 || output != NULL );
k-stachowiak6adb0572018-12-18 10:22:34 +0100458 CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
k-stachowiakf7125342018-12-11 15:57:19 +0100459
Janos Follathb5734a22018-05-14 14:31:49 +0100460 if( tag_len == 0 )
461 return( MBEDTLS_ERR_CCM_BAD_INPUT );
462
463 return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add,
464 add_len, input, output, tag, tag_len ) );
465}
Steven Cooreman222e2ff2017-04-04 11:37:15 +0200466#endif /* !MBEDTLS_CCM_ALT */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200469/*
470 * Examples 1 to 3 from SP800-38C Appendix C
471 */
472
473#define NB_TESTS 3
Ron Eldor1b9b2172018-04-26 14:15:01 +0300474#define CCM_SELFTEST_PT_MAX_LEN 24
475#define CCM_SELFTEST_CT_MAX_LEN 32
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200476/*
477 * The data is the same for all tests, only the used length changes
478 */
479static const unsigned char key[] = {
480 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
481 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
482};
483
484static const unsigned char iv[] = {
485 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
486 0x18, 0x19, 0x1a, 0x1b
487};
488
489static const unsigned char ad[] = {
490 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
491 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
492 0x10, 0x11, 0x12, 0x13
493};
494
Ron Eldor1b9b2172018-04-26 14:15:01 +0300495static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200496 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
497 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
498 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
499};
500
501static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
502static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
503static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
504static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
505
Ron Eldor1b9b2172018-04-26 14:15:01 +0300506static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200507 { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
508 { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
509 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
510 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
511 { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
512 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
513 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
514 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
515};
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_ccm_self_test( int verbose )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_ccm_context ctx;
Ron Eldor1b9b2172018-04-26 14:15:01 +0300520 /*
521 * Some hardware accelerators require the input and output buffers
522 * would be in RAM, because the flash is not accessible.
523 * Use buffers on the stack to hold the test vectors data.
524 */
525 unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
526 unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200527 size_t i;
528 int ret;
529
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200530 mbedtls_ccm_init( &ctx );
531
532 if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200533 {
534 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_printf( " CCM: setup failed" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200536
537 return( 1 );
538 }
539
540 for( i = 0; i < NB_TESTS; i++ )
541 {
542 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200544
Teppo Järvelinb5c46712019-10-04 13:35:55 +0300545 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
546 memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
547 memcpy( plaintext, msg, msg_len[i] );
Ron Eldor1b9b2172018-04-26 14:15:01 +0300548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300550 iv, iv_len[i], ad, add_len[i],
551 plaintext, ciphertext,
552 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200553
554 if( ret != 0 ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300555 memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 )
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200556 {
557 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200559
560 return( 1 );
561 }
Teppo Järvelind49d2b62019-10-30 13:48:12 +0200562 memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
Ron Eldor1b9b2172018-04-26 14:15:01 +0300565 iv, iv_len[i], ad, add_len[i],
566 ciphertext, plaintext,
567 ciphertext + msg_len[i], tag_len[i] );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200568
569 if( ret != 0 ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300570 memcmp( plaintext, msg, msg_len[i] ) != 0 )
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200571 {
572 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_printf( "failed\n" );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200574
575 return( 1 );
576 }
577
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200578 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200580 }
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_ccm_free( &ctx );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200583
584 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200586
587 return( 0 );
588}
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_CCM_C */