blob: 9501461acb6b9209dc01ec1e9936dcfffc3097e2 [file] [log] [blame]
Andres Amaya Garciaaf610a02016-12-14 10:13:43 +00001/**
Brian Murray53e23b62016-09-13 14:00:15 -07002 * \file cmac.c
Simon Butcher327398a2016-10-05 14:09:11 +01003 *
Simon Butcher69283e52016-10-06 12:49:58 +01004 * \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES
Robert Cragie3d23b1d2015-12-15 07:38:11 +00005 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Robert Cragie3d23b1d2015-12-15 07:38:11 +00007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000020 */
21
22/*
Brian Murray53e23b62016-09-13 14:00:15 -070023 * References:
Simon Butcher327398a2016-10-05 14:09:11 +010024 *
25 * - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The
26 * CMAC Mode for Authentication
Janos Follathcd13bd22016-12-13 11:51:04 +000027 * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
Simon Butcher327398a2016-10-05 14:09:11 +010028 *
29 * - RFC 4493 - The AES-CMAC Algorithm
30 * https://tools.ietf.org/html/rfc4493
31 *
32 * - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message
33 * Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128)
34 * Algorithm for the Internet Key Exchange Protocol (IKE)
35 * https://tools.ietf.org/html/rfc4615
36 *
37 * Additional test vectors: ISO/IEC 9797-1
38 *
Robert Cragie3d23b1d2015-12-15 07:38:11 +000039 */
40
Gilles Peskinedb09ef62020-06-03 01:43:33 +020041#include "common.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000042
43#if defined(MBEDTLS_CMAC_C)
44
45#include "mbedtls/cmac.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050046#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000047#include "mbedtls/error.h"
Robert Cragie3d23b1d2015-12-15 07:38:11 +000048
49#include <string.h>
50
Brian Murray8b4111c2016-09-13 15:58:46 -070051
Robert Cragie3d23b1d2015-12-15 07:38:11 +000052#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
Brian Murray8b4111c2016-09-13 15:58:46 -070055#include <stdlib.h>
56#define mbedtls_calloc calloc
57#define mbedtls_free free
Simon Butcherd241f1c2016-10-06 10:39:49 +010058#if defined(MBEDTLS_SELF_TEST)
Robert Cragie3d23b1d2015-12-15 07:38:11 +000059#include <stdio.h>
Brian Murray8b4111c2016-09-13 15:58:46 -070060#define mbedtls_printf printf
Brian J Murray2adecba2016-11-06 04:45:15 -080061#endif /* MBEDTLS_SELF_TEST */
Robert Cragie3d23b1d2015-12-15 07:38:11 +000062#endif /* MBEDTLS_PLATFORM_C */
Brian Murray8b4111c2016-09-13 15:58:46 -070063
Ron Eldor621080d2017-12-21 10:57:43 +020064#if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST)
Steven Cooreman63342772017-04-04 11:47:16 +020065
Robert Cragie3d23b1d2015-12-15 07:38:11 +000066/*
Brian Murrayb0c3c432016-05-18 14:29:51 -070067 * Multiplication by u in the Galois field of GF(2^n)
Manuel Pégourié-Gonnarda610b4c2016-01-13 11:28:16 +000068 *
Brian Murray72b69e32016-09-13 14:21:01 -070069 * As explained in NIST SP 800-38B, this can be computed:
Manuel Pégourié-Gonnarda610b4c2016-01-13 11:28:16 +000070 *
Simon Butcher327398a2016-10-05 14:09:11 +010071 * If MSB(p) = 0, then p = (p << 1)
72 * If MSB(p) = 1, then p = (p << 1) ^ R_n
73 * with R_64 = 0x1B and R_128 = 0x87
74 *
75 * Input and output MUST NOT point to the same buffer
Brian J Murray2adecba2016-11-06 04:45:15 -080076 * Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES.
Robert Cragie3d23b1d2015-12-15 07:38:11 +000077 */
Brian Murrayb0c3c432016-05-18 14:29:51 -070078static int cmac_multiply_by_u( unsigned char *output,
79 const unsigned char *input,
Brian Murray53e23b62016-09-13 14:00:15 -070080 size_t blocksize )
Robert Cragie3d23b1d2015-12-15 07:38:11 +000081{
Brian Murrayb0c3c432016-05-18 14:29:51 -070082 const unsigned char R_128 = 0x87;
83 const unsigned char R_64 = 0x1B;
84 unsigned char R_n, mask;
85 unsigned char overflow = 0x00;
Simon Butcher327398a2016-10-05 14:09:11 +010086 int i;
Brian Murrayb0c3c432016-05-18 14:29:51 -070087
Simon Butcher69283e52016-10-06 12:49:58 +010088 if( blocksize == MBEDTLS_AES_BLOCK_SIZE )
Brian Murray6a3c0d22016-05-20 18:25:43 -070089 {
Brian Murrayb0c3c432016-05-18 14:29:51 -070090 R_n = R_128;
Simon Butcher327398a2016-10-05 14:09:11 +010091 }
Simon Butcher69283e52016-10-06 12:49:58 +010092 else if( blocksize == MBEDTLS_DES3_BLOCK_SIZE )
Brian Murray6a3c0d22016-05-20 18:25:43 -070093 {
Brian Murrayb0c3c432016-05-18 14:29:51 -070094 R_n = R_64;
Simon Butcher327398a2016-10-05 14:09:11 +010095 }
96 else
Brian Murray6a3c0d22016-05-20 18:25:43 -070097 {
Simon Butcher327398a2016-10-05 14:09:11 +010098 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Brian Murrayb0c3c432016-05-18 14:29:51 -070099 }
100
Simon B3249cb72016-11-03 01:11:37 +0000101 for( i = (int)blocksize - 1; i >= 0; i-- )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000102 {
Manuel Pégourié-Gonnarda610b4c2016-01-13 11:28:16 +0000103 output[i] = input[i] << 1 | overflow;
104 overflow = input[i] >> 7;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000105 }
Manuel Pégourié-Gonnard3da54022016-01-13 11:00:47 +0000106
Manuel Pégourié-Gonnard475f06f2016-01-13 13:05:03 +0000107 /* mask = ( input[0] >> 7 ) ? 0xff : 0x00
108 * using bit operations to avoid branches */
Simon Butcher327398a2016-10-05 14:09:11 +0100109
Manuel Pégourié-Gonnard475f06f2016-01-13 13:05:03 +0000110 /* MSVC has a warning about unary minus on unsigned, but this is
111 * well-defined and precisely what we want to do here */
112#if defined(_MSC_VER)
113#pragma warning( push )
114#pragma warning( disable : 4146 )
115#endif
116 mask = - ( input[0] >> 7 );
117#if defined(_MSC_VER)
118#pragma warning( pop )
119#endif
120
Simon Butcher327398a2016-10-05 14:09:11 +0100121 output[ blocksize - 1 ] ^= R_n & mask;
122
Brian Murrayb439d452016-05-19 16:02:42 -0700123 return( 0 );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000124}
125
126/*
127 * Generate subkeys
Simon Butcher327398a2016-10-05 14:09:11 +0100128 *
129 * - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000130 */
Simon Butcher327398a2016-10-05 14:09:11 +0100131static int cmac_generate_subkeys( mbedtls_cipher_context_t *ctx,
132 unsigned char* K1, unsigned char* K2 )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000133{
Janos Follath24eed8d2019-11-22 13:21:35 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher69283e52016-10-06 12:49:58 +0100135 unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
Brian Murrayb0c3c432016-05-18 14:29:51 -0700136 size_t olen, block_size;
137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500138 mbedtls_platform_zeroize( L, sizeof( L ) );
Brian Murrayb0c3c432016-05-18 14:29:51 -0700139
Simon Butcher327398a2016-10-05 14:09:11 +0100140 block_size = ctx->cipher_info->block_size;
141
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000142 /* Calculate Ek(0) */
Simon Butcher327398a2016-10-05 14:09:11 +0100143 if( ( ret = mbedtls_cipher_update( ctx, L, block_size, L, &olen ) ) != 0 )
Brian Murrayb0c3c432016-05-18 14:29:51 -0700144 goto exit;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000145
Manuel Pégourié-Gonnard3da54022016-01-13 11:00:47 +0000146 /*
Manuel Pégourié-Gonnarda610b4c2016-01-13 11:28:16 +0000147 * Generate K1 and K2
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000148 */
Simon Butcher327398a2016-10-05 14:09:11 +0100149 if( ( ret = cmac_multiply_by_u( K1, L , block_size ) ) != 0 )
Brian Murrayb0c3c432016-05-18 14:29:51 -0700150 goto exit;
Manuel Pégourié-Gonnard3da54022016-01-13 11:00:47 +0000151
Simon Butcher327398a2016-10-05 14:09:11 +0100152 if( ( ret = cmac_multiply_by_u( K2, K1 , block_size ) ) != 0 )
153 goto exit;
154
155exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500156 mbedtls_platform_zeroize( L, sizeof( L ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100157
158 return( ret );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000159}
Ron Eldor621080d2017-12-21 10:57:43 +0200160#endif /* !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000161
Ron Eldor621080d2017-12-21 10:57:43 +0200162#if !defined(MBEDTLS_CMAC_ALT)
Simon Butcher69283e52016-10-06 12:49:58 +0100163static void cmac_xor_block( unsigned char *output, const unsigned char *input1,
164 const unsigned char *input2,
165 const size_t block_size )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000166{
Hanno Becker61937d42017-04-26 15:01:23 +0100167 size_t idx;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000168
Hanno Becker61937d42017-04-26 15:01:23 +0100169 for( idx = 0; idx < block_size; idx++ )
170 output[ idx ] = input1[ idx ] ^ input2[ idx ];
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000171}
172
Manuel Pégourié-Gonnardd2c3d3e2016-01-13 13:14:04 +0000173/*
174 * Create padded last block from (partial) last block.
175 *
176 * We can't use the padding option from the cipher layer, as it only works for
177 * CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
178 */
Simon Butcher69283e52016-10-06 12:49:58 +0100179static void cmac_pad( unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
Brian Murray53e23b62016-09-13 14:00:15 -0700180 size_t padded_block_len,
Manuel Pégourié-Gonnard7b555f22016-01-13 15:09:09 +0000181 const unsigned char *last_block,
Brian Murrayb0c3c432016-05-18 14:29:51 -0700182 size_t last_block_len )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000183{
184 size_t j;
185
Brian Murrayb0c3c432016-05-18 14:29:51 -0700186 for( j = 0; j < padded_block_len; j++ )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000187 {
Brian Murrayb0c3c432016-05-18 14:29:51 -0700188 if( j < last_block_len )
Manuel Pégourié-Gonnardd2c3d3e2016-01-13 13:14:04 +0000189 padded_block[j] = last_block[j];
Brian Murrayb0c3c432016-05-18 14:29:51 -0700190 else if( j == last_block_len )
Manuel Pégourié-Gonnardd2c3d3e2016-01-13 13:14:04 +0000191 padded_block[j] = 0x80;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000192 else
Manuel Pégourié-Gonnardd2c3d3e2016-01-13 13:14:04 +0000193 padded_block[j] = 0x00;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000194 }
195}
196
Simon Butcher327398a2016-10-05 14:09:11 +0100197int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
Simon Butcher94ffde72016-10-05 15:33:53 +0100198 const unsigned char *key, size_t keybits )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000199{
Simon Butcher327398a2016-10-05 14:09:11 +0100200 mbedtls_cipher_type_t type;
201 mbedtls_cmac_context_t *cmac_ctx;
Simon Butcher327398a2016-10-05 14:09:11 +0100202 int retval;
203
204 if( ctx == NULL || ctx->cipher_info == NULL || key == NULL )
205 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
206
Simon B3249cb72016-11-03 01:11:37 +0000207 if( ( retval = mbedtls_cipher_setkey( ctx, key, (int)keybits,
Simon Butcher327398a2016-10-05 14:09:11 +0100208 MBEDTLS_ENCRYPT ) ) != 0 )
209 return( retval );
210
Simon Butcher327398a2016-10-05 14:09:11 +0100211 type = ctx->cipher_info->type;
212
213 switch( type )
214 {
215 case MBEDTLS_CIPHER_AES_128_ECB:
216 case MBEDTLS_CIPHER_AES_192_ECB:
217 case MBEDTLS_CIPHER_AES_256_ECB:
218 case MBEDTLS_CIPHER_DES_EDE3_ECB:
219 break;
220 default:
221 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
222 }
223
224 /* Allocated and initialise in the cipher context memory for the CMAC
225 * context */
226 cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
227 if( cmac_ctx == NULL )
228 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
229
230 ctx->cmac_ctx = cmac_ctx;
231
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500232 mbedtls_platform_zeroize( cmac_ctx->state, sizeof( cmac_ctx->state ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100233
234 return 0;
235}
236
237int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
238 const unsigned char *input, size_t ilen )
239{
240 mbedtls_cmac_context_t* cmac_ctx;
Brian Murrayb0c3c432016-05-18 14:29:51 -0700241 unsigned char *state;
Simon B3249cb72016-11-03 01:11:37 +0000242 int ret = 0;
243 size_t n, j, olen, block_size;
Brian Murrayb0c3c432016-05-18 14:29:51 -0700244
Simon Butcher327398a2016-10-05 14:09:11 +0100245 if( ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
246 ctx->cmac_ctx == NULL )
247 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Brian Murrayb0c3c432016-05-18 14:29:51 -0700248
Simon Butcher327398a2016-10-05 14:09:11 +0100249 cmac_ctx = ctx->cmac_ctx;
250 block_size = ctx->cipher_info->block_size;
251 state = ctx->cmac_ctx->state;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000252
Simon Butcher6b0774a2016-10-10 21:37:42 +0100253 /* Is there data still to process from the last call, that's greater in
254 * size than a block? */
Simon Butcher327398a2016-10-05 14:09:11 +0100255 if( cmac_ctx->unprocessed_len > 0 &&
Andres AGa592dcc2016-10-06 15:23:39 +0100256 ilen > block_size - cmac_ctx->unprocessed_len )
Brian Murray57863ad2016-05-19 16:38:36 -0700257 {
Simon Butcher327398a2016-10-05 14:09:11 +0100258 memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
259 input,
260 block_size - cmac_ctx->unprocessed_len );
261
262 cmac_xor_block( state, cmac_ctx->unprocessed_block, state, block_size );
263
264 if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
265 &olen ) ) != 0 )
266 {
267 goto exit;
268 }
269
Simon Butcher6b0774a2016-10-10 21:37:42 +0100270 input += block_size - cmac_ctx->unprocessed_len;
271 ilen -= block_size - cmac_ctx->unprocessed_len;
Simon Butcher327398a2016-10-05 14:09:11 +0100272 cmac_ctx->unprocessed_len = 0;
Brian Murray57863ad2016-05-19 16:38:36 -0700273 }
274
Simon Butcher327398a2016-10-05 14:09:11 +0100275 /* n is the number of blocks including any final partial block */
276 n = ( ilen + block_size - 1 ) / block_size;
277
Simon B3249cb72016-11-03 01:11:37 +0000278 /* Iterate across the input data in block sized chunks, excluding any
279 * final partial or complete block */
280 for( j = 1; j < n; j++ )
Brian Murray57863ad2016-05-19 16:38:36 -0700281 {
Simon Butcher327398a2016-10-05 14:09:11 +0100282 cmac_xor_block( state, input, state, block_size );
283
284 if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
285 &olen ) ) != 0 )
286 goto exit;
287
288 ilen -= block_size;
289 input += block_size;
Brian Murray57863ad2016-05-19 16:38:36 -0700290 }
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000291
Simon Butcher327398a2016-10-05 14:09:11 +0100292 /* If there is data left over that wasn't aligned to a block */
293 if( ilen > 0 )
294 {
Simon Butcher6b0774a2016-10-10 21:37:42 +0100295 memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
296 input,
297 ilen );
298 cmac_ctx->unprocessed_len += ilen;
Simon Butcher327398a2016-10-05 14:09:11 +0100299 }
300
301exit:
302 return( ret );
303}
304
305int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
306 unsigned char *output )
307{
308 mbedtls_cmac_context_t* cmac_ctx;
Simon Butcher69283e52016-10-06 12:49:58 +0100309 unsigned char *state, *last_block;
310 unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
311 unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
312 unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX];
Janos Follath24eed8d2019-11-22 13:21:35 +0000313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher327398a2016-10-05 14:09:11 +0100314 size_t olen, block_size;
315
316 if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
317 output == NULL )
318 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
319
320 cmac_ctx = ctx->cmac_ctx;
321 block_size = ctx->cipher_info->block_size;
322 state = cmac_ctx->state;
323
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500324 mbedtls_platform_zeroize( K1, sizeof( K1 ) );
325 mbedtls_platform_zeroize( K2, sizeof( K2 ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100326 cmac_generate_subkeys( ctx, K1, K2 );
327
Simon Butcher69283e52016-10-06 12:49:58 +0100328 last_block = cmac_ctx->unprocessed_block;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000329
330 /* Calculate last block */
Janos Follathe3d882a2016-10-11 10:49:26 +0100331 if( cmac_ctx->unprocessed_len < block_size )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000332 {
Simon Butcher327398a2016-10-05 14:09:11 +0100333 cmac_pad( M_last, block_size, last_block, cmac_ctx->unprocessed_len );
334 cmac_xor_block( M_last, M_last, K2, block_size );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000335 }
336 else
337 {
Manuel Pégourié-Gonnard2c063062016-01-13 14:27:55 +0000338 /* Last block is complete block */
Simon Butcher327398a2016-10-05 14:09:11 +0100339 cmac_xor_block( M_last, last_block, K1, block_size );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000340 }
341
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000342
Simon Butcher327398a2016-10-05 14:09:11 +0100343 cmac_xor_block( state, M_last, state, block_size );
344 if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
345 &olen ) ) != 0 )
346 {
347 goto exit;
348 }
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000349
Simon Butcher327398a2016-10-05 14:09:11 +0100350 memcpy( output, state, block_size );
351
352exit:
353 /* Wipe the generated keys on the stack, and any other transients to avoid
354 * side channel leakage */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500355 mbedtls_platform_zeroize( K1, sizeof( K1 ) );
356 mbedtls_platform_zeroize( K2, sizeof( K2 ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100357
358 cmac_ctx->unprocessed_len = 0;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500359 mbedtls_platform_zeroize( cmac_ctx->unprocessed_block,
360 sizeof( cmac_ctx->unprocessed_block ) );
Simon Butcher327398a2016-10-05 14:09:11 +0100361
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500362 mbedtls_platform_zeroize( state, MBEDTLS_CIPHER_BLKSIZE_MAX );
Simon Butcher327398a2016-10-05 14:09:11 +0100363 return( ret );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000364}
365
Simon Butcher327398a2016-10-05 14:09:11 +0100366int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000367{
Simon Butcher327398a2016-10-05 14:09:11 +0100368 mbedtls_cmac_context_t* cmac_ctx;
Manuel Pégourié-Gonnard3da54022016-01-13 11:00:47 +0000369
Simon Butcher327398a2016-10-05 14:09:11 +0100370 if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
371 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Brian Murrayb0c3c432016-05-18 14:29:51 -0700372
Simon Butcher327398a2016-10-05 14:09:11 +0100373 cmac_ctx = ctx->cmac_ctx;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000374
Simon Butcher327398a2016-10-05 14:09:11 +0100375 /* Reset the internal state */
376 cmac_ctx->unprocessed_len = 0;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500377 mbedtls_platform_zeroize( cmac_ctx->unprocessed_block,
378 sizeof( cmac_ctx->unprocessed_block ) );
379 mbedtls_platform_zeroize( cmac_ctx->state,
380 sizeof( cmac_ctx->state ) );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000381
Simon Butcher327398a2016-10-05 14:09:11 +0100382 return( 0 );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000383}
384
Simon Butcher327398a2016-10-05 14:09:11 +0100385int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
386 const unsigned char *key, size_t keylen,
387 const unsigned char *input, size_t ilen,
388 unsigned char *output )
389{
390 mbedtls_cipher_context_t ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher327398a2016-10-05 14:09:11 +0100392
393 if( cipher_info == NULL || key == NULL || input == NULL || output == NULL )
394 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
395
396 mbedtls_cipher_init( &ctx );
397
398 if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
399 goto exit;
400
401 ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
Simon Butcher327398a2016-10-05 14:09:11 +0100402 if( ret != 0 )
403 goto exit;
Simon Butcher327398a2016-10-05 14:09:11 +0100404
405 ret = mbedtls_cipher_cmac_update( &ctx, input, ilen );
406 if( ret != 0 )
407 goto exit;
408
Simon Butcher69283e52016-10-06 12:49:58 +0100409 ret = mbedtls_cipher_cmac_finish( &ctx, output );
Simon Butcher327398a2016-10-05 14:09:11 +0100410
411exit:
Simon Butcher69283e52016-10-06 12:49:58 +0100412 mbedtls_cipher_free( &ctx );
413
Simon Butcher327398a2016-10-05 14:09:11 +0100414 return( ret );
415}
Simon Butcher327398a2016-10-05 14:09:11 +0100416
Simon Butcher69283e52016-10-06 12:49:58 +0100417#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7b555f22016-01-13 15:09:09 +0000418/*
Simon Butcher69283e52016-10-06 12:49:58 +0100419 * Implementation of AES-CMAC-PRF-128 defined in RFC 4615
Manuel Pégourié-Gonnard7b555f22016-01-13 15:09:09 +0000420 */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700421int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length,
Manuel Pégourié-Gonnard690083c2016-01-13 10:48:02 +0000422 const unsigned char *input, size_t in_len,
Rodrigo Dias Correa2c424572020-11-10 01:38:00 -0300423 unsigned char output[16] )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000424{
Janos Follath24eed8d2019-11-22 13:21:35 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher327398a2016-10-05 14:09:11 +0100426 const mbedtls_cipher_info_t *cipher_info;
Simon Butcher69283e52016-10-06 12:49:58 +0100427 unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
428 unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE];
429
430 if( key == NULL || input == NULL || output == NULL )
431 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000432
Simon Butcher327398a2016-10-05 14:09:11 +0100433 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
434 if( cipher_info == NULL )
435 {
436 /* Failing at this point must be due to a build issue */
437 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
438 goto exit;
439 }
Brian Murrayb0c3c432016-05-18 14:29:51 -0700440
Simon Butcher69283e52016-10-06 12:49:58 +0100441 if( key_length == MBEDTLS_AES_BLOCK_SIZE )
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000442 {
443 /* Use key as is */
Simon Butcher69283e52016-10-06 12:49:58 +0100444 memcpy( int_key, key, MBEDTLS_AES_BLOCK_SIZE );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000445 }
446 else
447 {
Simon Butcher69283e52016-10-06 12:49:58 +0100448 memset( zero_key, 0, MBEDTLS_AES_BLOCK_SIZE );
Manuel Pégourié-Gonnard7b555f22016-01-13 15:09:09 +0000449
Simon Butcher327398a2016-10-05 14:09:11 +0100450 ret = mbedtls_cipher_cmac( cipher_info, zero_key, 128, key,
451 key_length, int_key );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000452 if( ret != 0 )
Brian Murrayb0c3c432016-05-18 14:29:51 -0700453 goto exit;
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000454 }
455
Simon Butcher327398a2016-10-05 14:09:11 +0100456 ret = mbedtls_cipher_cmac( cipher_info, int_key, 128, input, in_len,
457 output );
Manuel Pégourié-Gonnardd6cf7542016-01-13 11:30:00 +0000458
Simon Butcher327398a2016-10-05 14:09:11 +0100459exit:
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500460 mbedtls_platform_zeroize( int_key, sizeof( int_key ) );
Brian Murrayb0c3c432016-05-18 14:29:51 -0700461
Simon Butcher327398a2016-10-05 14:09:11 +0100462 return( ret );
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000463}
Brian Murrayb439d452016-05-19 16:02:42 -0700464#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000465
Steven Cooreman63342772017-04-04 11:47:16 +0200466#endif /* !MBEDTLS_CMAC_ALT */
467
Simon Butcher69283e52016-10-06 12:49:58 +0100468#if defined(MBEDTLS_SELF_TEST)
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000469/*
Janos Follathcd13bd22016-12-13 11:51:04 +0000470 * CMAC test data for SP800-38B
471 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf
472 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf
Brian Murray0f6af732016-05-19 15:59:23 -0700473 *
474 * AES-CMAC-PRF-128 test data from RFC 4615
475 * https://tools.ietf.org/html/rfc4615#page-4
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000476 */
477
Brian Murray0f6af732016-05-19 15:59:23 -0700478#define NB_CMAC_TESTS_PER_KEY 4
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000479#define NB_PRF_TESTS 3
Simon Butcher327398a2016-10-05 14:09:11 +0100480
Brian Murray0f6af732016-05-19 15:59:23 -0700481#if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)
482/* All CMAC test inputs are truncated from the same 64 byte buffer. */
483static const unsigned char test_message[] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000484 /* PT */
485 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
486 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
487 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
488 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
489 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
490 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
491 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
492 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000493};
Simon Butcher69283e52016-10-06 12:49:58 +0100494#endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */
Brian Murray0cf14c12016-05-23 12:49:50 -0700495
Simon Butcher69283e52016-10-06 12:49:58 +0100496#if defined(MBEDTLS_AES_C)
Brian Murray0f6af732016-05-19 15:59:23 -0700497/* Truncation point of message for AES CMAC tests */
Brian Murray57863ad2016-05-19 16:38:36 -0700498static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000499 /* Mlen */
Brian Murray0f6af732016-05-19 15:59:23 -0700500 0,
501 16,
Janos Follathcd13bd22016-12-13 11:51:04 +0000502 20,
Brian Murray0f6af732016-05-19 15:59:23 -0700503 64
504};
505
Janos Follathcd13bd22016-12-13 11:51:04 +0000506/* CMAC-AES128 Test Data */
Brian Murray57863ad2016-05-19 16:38:36 -0700507static const unsigned char aes_128_key[16] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000508 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
509 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
Brian Murray0f6af732016-05-19 15:59:23 -0700510};
Simon Butcher69283e52016-10-06 12:49:58 +0100511static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
Brian Murray0f6af732016-05-19 15:59:23 -0700512 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000513 /* K1 */
514 0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
515 0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
Brian Murray0f6af732016-05-19 15:59:23 -0700516 },
517 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000518 /* K2 */
519 0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
520 0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
Brian Murray0f6af732016-05-19 15:59:23 -0700521 }
522};
Simon Butcher69283e52016-10-06 12:49:58 +0100523static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000524 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000525 /* Example #1 */
526 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
527 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000528 },
529 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000530 /* Example #2 */
531 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
532 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000533 },
534 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000535 /* Example #3 */
536 0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8,
537 0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000538 },
539 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000540 /* Example #4 */
541 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
542 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000543 }
544};
545
Janos Follathcd13bd22016-12-13 11:51:04 +0000546/* CMAC-AES192 Test Data */
Brian Murray57863ad2016-05-19 16:38:36 -0700547static const unsigned char aes_192_key[24] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000548 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
549 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
550 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000551};
Simon Butcher69283e52016-10-06 12:49:58 +0100552static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
Brian Murrayb0c3c432016-05-18 14:29:51 -0700553 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000554 /* K1 */
555 0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27,
556 0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96
Brian Murrayb0c3c432016-05-18 14:29:51 -0700557 },
558 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000559 /* K2 */
560 0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e,
561 0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c
Brian Murrayb0c3c432016-05-18 14:29:51 -0700562 }
563};
Simon Butcher69283e52016-10-06 12:49:58 +0100564static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
Brian Murrayb0c3c432016-05-18 14:29:51 -0700565 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000566 /* Example #1 */
567 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
568 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
Brian Murrayb0c3c432016-05-18 14:29:51 -0700569 },
570 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000571 /* Example #2 */
572 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
573 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
Brian Murrayb0c3c432016-05-18 14:29:51 -0700574 },
575 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000576 /* Example #3 */
577 0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04,
578 0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8
Brian Murrayb0c3c432016-05-18 14:29:51 -0700579 },
580 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000581 /* Example #4 */
582 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
583 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
Brian Murrayb0c3c432016-05-18 14:29:51 -0700584 }
585};
586
Janos Follathcd13bd22016-12-13 11:51:04 +0000587/* CMAC-AES256 Test Data */
Brian Murray57863ad2016-05-19 16:38:36 -0700588static const unsigned char aes_256_key[32] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000589 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
590 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
591 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
592 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
Brian Murray0f6af732016-05-19 15:59:23 -0700593};
Simon Butcher69283e52016-10-06 12:49:58 +0100594static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
Brian Murray0f6af732016-05-19 15:59:23 -0700595 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000596 /* K1 */
597 0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac,
598 0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f
Brian Murray0f6af732016-05-19 15:59:23 -0700599 },
600 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000601 /* K2 */
602 0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58,
603 0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9
Brian Murray0f6af732016-05-19 15:59:23 -0700604 }
605};
Simon Butcher69283e52016-10-06 12:49:58 +0100606static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
Brian Murray0f6af732016-05-19 15:59:23 -0700607 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000608 /* Example #1 */
609 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
610 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
Brian Murray0f6af732016-05-19 15:59:23 -0700611 },
612 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000613 /* Example #2 */
614 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
615 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
Brian Murray0f6af732016-05-19 15:59:23 -0700616 },
617 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000618 /* Example #3 */
619 0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a,
620 0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93
Brian Murray0f6af732016-05-19 15:59:23 -0700621 },
622 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000623 /* Example #4 */
624 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
625 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
Brian Murray0f6af732016-05-19 15:59:23 -0700626 }
627};
628#endif /* MBEDTLS_AES_C */
629
Simon Butcher69283e52016-10-06 12:49:58 +0100630#if defined(MBEDTLS_DES_C)
Brian Murray0f6af732016-05-19 15:59:23 -0700631/* Truncation point of message for 3DES CMAC tests */
Brian Murray57863ad2016-05-19 16:38:36 -0700632static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
Brian Murray0f6af732016-05-19 15:59:23 -0700633 0,
Janos Follathcd13bd22016-12-13 11:51:04 +0000634 16,
Brian Murray0f6af732016-05-19 15:59:23 -0700635 20,
636 32
637};
638
Janos Follathcd13bd22016-12-13 11:51:04 +0000639/* CMAC-TDES (Generation) - 2 Key Test Data */
Brian Murray57863ad2016-05-19 16:38:36 -0700640static const unsigned char des3_2key_key[24] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000641 /* Key1 */
642 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
643 /* Key2 */
644 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01,
645 /* Key3 */
646 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
Brian Murray0f6af732016-05-19 15:59:23 -0700647};
648static const unsigned char des3_2key_subkeys[2][8] = {
649 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000650 /* K1 */
651 0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9
Brian Murray0f6af732016-05-19 15:59:23 -0700652 },
653 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000654 /* K2 */
655 0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2
Brian Murray0f6af732016-05-19 15:59:23 -0700656 }
657};
Simon Butcher69283e52016-10-06 12:49:58 +0100658static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
Brian Murrayb0c3c432016-05-18 14:29:51 -0700659 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000660 /* Sample #1 */
661 0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60
Brian Murrayb0c3c432016-05-18 14:29:51 -0700662 },
663 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000664 /* Sample #2 */
665 0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b
Brian Murrayb0c3c432016-05-18 14:29:51 -0700666 },
667 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000668 /* Sample #3 */
669 0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69
Brian Murrayb0c3c432016-05-18 14:29:51 -0700670 },
671 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000672 /* Sample #4 */
673 0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb
Brian Murrayb0c3c432016-05-18 14:29:51 -0700674 }
675};
676
Janos Follathcd13bd22016-12-13 11:51:04 +0000677/* CMAC-TDES (Generation) - 3 Key Test Data */
Brian Murray57863ad2016-05-19 16:38:36 -0700678static const unsigned char des3_3key_key[24] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000679 /* Key1 */
680 0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef,
681 /* Key2 */
682 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
683 /* Key3 */
684 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
Brian Murray0f6af732016-05-19 15:59:23 -0700685};
686static const unsigned char des3_3key_subkeys[2][8] = {
687 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000688 /* K1 */
689 0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0
Brian Murray0f6af732016-05-19 15:59:23 -0700690 },
691 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000692 /* K2 */
693 0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b
Brian Murray0f6af732016-05-19 15:59:23 -0700694 }
695};
Simon Butcher69283e52016-10-06 12:49:58 +0100696static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
Brian Murrayb0c3c432016-05-18 14:29:51 -0700697 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000698 /* Sample #1 */
699 0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50
Brian Murrayb0c3c432016-05-18 14:29:51 -0700700 },
701 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000702 /* Sample #2 */
703 0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09
Brian Murrayb0c3c432016-05-18 14:29:51 -0700704 },
705 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000706 /* Sample #3 */
707 0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2
Brian Murrayb0c3c432016-05-18 14:29:51 -0700708 },
709 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000710 /* Sample #4 */
711 0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5
Brian Murrayb0c3c432016-05-18 14:29:51 -0700712 }
713};
714
Brian Murray0f6af732016-05-19 15:59:23 -0700715#endif /* MBEDTLS_DES_C */
Brian Murrayb0c3c432016-05-18 14:29:51 -0700716
Simon Butcher69283e52016-10-06 12:49:58 +0100717#if defined(MBEDTLS_AES_C)
Brian Murray0f6af732016-05-19 15:59:23 -0700718/* AES AES-CMAC-PRF-128 Test Data */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000719static const unsigned char PRFK[] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000720 /* Key */
721 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
722 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000723 0xed, 0xcb
724};
725
726/* Sizes in bytes */
727static const size_t PRFKlen[NB_PRF_TESTS] = {
728 18,
729 16,
730 10
731};
732
Janos Follathcd13bd22016-12-13 11:51:04 +0000733/* Message */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000734static const unsigned char PRFM[] = {
Janos Follathcd13bd22016-12-13 11:51:04 +0000735 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
736 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
Manuel Pégourié-Gonnard3da54022016-01-13 11:00:47 +0000737 0x10, 0x11, 0x12, 0x13
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000738};
739
740static const unsigned char PRFT[NB_PRF_TESTS][16] = {
741 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000742 0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b,
743 0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000744 },
745 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000746 0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52,
747 0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000748 },
749 {
Janos Follathcd13bd22016-12-13 11:51:04 +0000750 0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee,
751 0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000752 }
753};
Brian Murray0f6af732016-05-19 15:59:23 -0700754#endif /* MBEDTLS_AES_C */
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000755
Simon Butcher327398a2016-10-05 14:09:11 +0100756static int cmac_test_subkeys( int verbose,
757 const char* testname,
758 const unsigned char* key,
759 int keybits,
760 const unsigned char* subkeys,
761 mbedtls_cipher_type_t cipher_type,
762 int block_size,
763 int num_tests )
764{
Brian Murray2fab5c92016-12-15 18:51:13 -0800765 int i, ret = 0;
Simon Butcher327398a2016-10-05 14:09:11 +0100766 mbedtls_cipher_context_t ctx;
767 const mbedtls_cipher_info_t *cipher_info;
Simon Butcher69283e52016-10-06 12:49:58 +0100768 unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
769 unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
Simon Butcher327398a2016-10-05 14:09:11 +0100770
771 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
772 if( cipher_info == NULL )
773 {
774 /* Failing at this point must be due to a build issue */
Simon Butcher69283e52016-10-06 12:49:58 +0100775 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
Simon Butcher327398a2016-10-05 14:09:11 +0100776 }
777
778 for( i = 0; i < num_tests; i++ )
779 {
780 if( verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200781 mbedtls_printf( " %s CMAC subkey #%d: ", testname, i + 1 );
Simon Butcher327398a2016-10-05 14:09:11 +0100782
Janos Follathd4443582016-10-12 10:00:42 +0100783 mbedtls_cipher_init( &ctx );
784
Simon Butcher327398a2016-10-05 14:09:11 +0100785 if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
786 {
787 if( verbose != 0 )
788 mbedtls_printf( "test execution failed\n" );
789
Janos Follathd4443582016-10-12 10:00:42 +0100790 goto cleanup;
Simon Butcher327398a2016-10-05 14:09:11 +0100791 }
792
793 if( ( ret = mbedtls_cipher_setkey( &ctx, key, keybits,
794 MBEDTLS_ENCRYPT ) ) != 0 )
795 {
Steven Cooreman830d5af2021-01-08 18:01:46 +0100796 /* When CMAC is implemented by an alternative implementation, or
797 * the underlying primitive itself is implemented alternatively,
798 * certain features (e.g. AES-192) may be unavailable. This should
799 * not cause the selftest function to fail. */
800 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ||
801 ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) {
802 if( verbose != 0 )
803 mbedtls_printf( "skipped\n" );
804 goto next_test;
805 }
806
Simon Butcher327398a2016-10-05 14:09:11 +0100807 if( verbose != 0 )
808 mbedtls_printf( "test execution failed\n" );
809
Janos Follathd4443582016-10-12 10:00:42 +0100810 goto cleanup;
Simon Butcher327398a2016-10-05 14:09:11 +0100811 }
812
813 ret = cmac_generate_subkeys( &ctx, K1, K2 );
814 if( ret != 0 )
815 {
816 if( verbose != 0 )
817 mbedtls_printf( "failed\n" );
Janos Follathd4443582016-10-12 10:00:42 +0100818
819 goto cleanup;
Simon Butcher327398a2016-10-05 14:09:11 +0100820 }
821
Simon Butcher420be4e2016-10-07 12:55:43 +0100822 if( ( ret = memcmp( K1, subkeys, block_size ) ) != 0 ||
823 ( ret = memcmp( K2, &subkeys[block_size], block_size ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +0100824 {
825 if( verbose != 0 )
826 mbedtls_printf( "failed\n" );
Janos Follathd4443582016-10-12 10:00:42 +0100827
828 goto cleanup;
Simon Butcher327398a2016-10-05 14:09:11 +0100829 }
830
831 if( verbose != 0 )
832 mbedtls_printf( "passed\n" );
Janos Follathd4443582016-10-12 10:00:42 +0100833
Steven Cooreman830d5af2021-01-08 18:01:46 +0100834next_test:
Janos Follathd4443582016-10-12 10:00:42 +0100835 mbedtls_cipher_free( &ctx );
Simon Butcher327398a2016-10-05 14:09:11 +0100836 }
837
Gilles Peskinedf761d52018-03-01 22:18:14 +0100838 ret = 0;
Janos Follathd4443582016-10-12 10:00:42 +0100839 goto exit;
840
841cleanup:
Simon Butcher69283e52016-10-06 12:49:58 +0100842 mbedtls_cipher_free( &ctx );
843
Janos Follathd4443582016-10-12 10:00:42 +0100844exit:
Simon Butcher327398a2016-10-05 14:09:11 +0100845 return( ret );
846}
847
Simon Butcher69283e52016-10-06 12:49:58 +0100848static int cmac_test_wth_cipher( int verbose,
849 const char* testname,
850 const unsigned char* key,
851 int keybits,
852 const unsigned char* messages,
853 const unsigned int message_lengths[4],
854 const unsigned char* expected_result,
855 mbedtls_cipher_type_t cipher_type,
856 int block_size,
857 int num_tests )
Brian Murray00dc5f02016-05-19 14:23:50 -0700858{
Simon Butcher327398a2016-10-05 14:09:11 +0100859 const mbedtls_cipher_info_t *cipher_info;
Brian Murray2fab5c92016-12-15 18:51:13 -0800860 int i, ret = 0;
Simon Butcher69283e52016-10-06 12:49:58 +0100861 unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
Brian Murray57863ad2016-05-19 16:38:36 -0700862
Simon Butcher327398a2016-10-05 14:09:11 +0100863 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
864 if( cipher_info == NULL )
Brian Murray00dc5f02016-05-19 14:23:50 -0700865 {
Simon Butcher327398a2016-10-05 14:09:11 +0100866 /* Failing at this point must be due to a build issue */
867 ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Brian Murray00dc5f02016-05-19 14:23:50 -0700868 goto exit;
869 }
870
871 for( i = 0; i < num_tests; i++ )
872 {
873 if( verbose != 0 )
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200874 mbedtls_printf( " %s CMAC #%d: ", testname, i + 1 );
Brian Murray00dc5f02016-05-19 14:23:50 -0700875
Simon Butcher327398a2016-10-05 14:09:11 +0100876 if( ( ret = mbedtls_cipher_cmac( cipher_info, key, keybits, messages,
877 message_lengths[i], output ) ) != 0 )
Brian Murray00dc5f02016-05-19 14:23:50 -0700878 {
Steven Cooreman830d5af2021-01-08 18:01:46 +0100879 /* When CMAC is implemented by an alternative implementation, or
880 * the underlying primitive itself is implemented alternatively,
881 * certain features (e.g. AES-192) may be unavailable. This should
882 * not cause the selftest function to fail. */
883 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ||
884 ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) {
885 if( verbose != 0 )
886 mbedtls_printf( "skipped\n" );
887 continue;
888 }
889
Brian Murray00dc5f02016-05-19 14:23:50 -0700890 if( verbose != 0 )
891 mbedtls_printf( "failed\n" );
892 goto exit;
893 }
Brian Murray9ce2e092016-05-24 22:46:43 -0700894
Simon Butcher327398a2016-10-05 14:09:11 +0100895 if( ( ret = memcmp( output, &expected_result[i * block_size], block_size ) ) != 0 )
Brian Murray00dc5f02016-05-19 14:23:50 -0700896 {
897 if( verbose != 0 )
898 mbedtls_printf( "failed\n" );
899 goto exit;
900 }
901
Brian Murray9ce2e092016-05-24 22:46:43 -0700902 if( verbose != 0 )
903 mbedtls_printf( "passed\n" );
Brian Murray00dc5f02016-05-19 14:23:50 -0700904 }
Gilles Peskinedf761d52018-03-01 22:18:14 +0100905 ret = 0;
Simon Butcher327398a2016-10-05 14:09:11 +0100906
Simon Butcher69283e52016-10-06 12:49:58 +0100907exit:
Simon Butcher327398a2016-10-05 14:09:11 +0100908 return( ret );
Brian Murray00dc5f02016-05-19 14:23:50 -0700909}
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000910
Simon Butcher69283e52016-10-06 12:49:58 +0100911#if defined(MBEDTLS_AES_C)
912static int test_aes128_cmac_prf( int verbose )
Brian Murray6a3c0d22016-05-20 18:25:43 -0700913{
Robert Cragie3d23b1d2015-12-15 07:38:11 +0000914 int i;
Janos Follath24eed8d2019-11-22 13:21:35 +0000915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher69283e52016-10-06 12:49:58 +0100916 unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
Simon Butcher327398a2016-10-05 14:09:11 +0100917
Brian Murrayb0c3c432016-05-18 14:29:51 -0700918 for( i = 0; i < NB_PRF_TESTS; i++ )
919 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200920 mbedtls_printf( " AES CMAC 128 PRF #%d: ", i );
Simon Butcher327398a2016-10-05 14:09:11 +0100921 ret = mbedtls_aes_cmac_prf_128( PRFK, PRFKlen[i], PRFM, 20, output );
Brian Murrayb0c3c432016-05-18 14:29:51 -0700922 if( ret != 0 ||
Simon Butcher69283e52016-10-06 12:49:58 +0100923 memcmp( output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE ) != 0 )
Brian Murrayb0c3c432016-05-18 14:29:51 -0700924 {
Simon Butcher327398a2016-10-05 14:09:11 +0100925
Brian Murrayb0c3c432016-05-18 14:29:51 -0700926 if( verbose != 0 )
927 mbedtls_printf( "failed\n" );
928
Brian Murray0f6af732016-05-19 15:59:23 -0700929 return( ret );
Simon Butcher69283e52016-10-06 12:49:58 +0100930 }
931 else if( verbose != 0 )
Brian Murrayb0c3c432016-05-18 14:29:51 -0700932 {
933 mbedtls_printf( "passed\n" );
934 }
935 }
Brian Murray0f6af732016-05-19 15:59:23 -0700936 return( ret );
937}
938#endif /* MBEDTLS_AES_C */
939
940int mbedtls_cmac_self_test( int verbose )
941{
Janos Follath24eed8d2019-11-22 13:21:35 +0000942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher327398a2016-10-05 14:09:11 +0100943
Simon Butcher69283e52016-10-06 12:49:58 +0100944#if defined(MBEDTLS_AES_C)
Simon Butcher327398a2016-10-05 14:09:11 +0100945 /* AES-128 */
946 if( ( ret = cmac_test_subkeys( verbose,
Simon Butcher69283e52016-10-06 12:49:58 +0100947 "AES 128",
948 aes_128_key,
949 128,
950 (const unsigned char*)aes_128_subkeys,
951 MBEDTLS_CIPHER_AES_128_ECB,
952 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +0100953 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +0100954 {
955 return( ret );
956 }
957
Brian Murrayae1cb122016-05-23 15:01:59 -0700958 if( ( ret = cmac_test_wth_cipher( verbose,
959 "AES 128",
960 aes_128_key,
961 128,
962 test_message,
963 aes_message_lengths,
Simon Butcher69283e52016-10-06 12:49:58 +0100964 (const unsigned char*)aes_128_expected_result,
Simon Butcher327398a2016-10-05 14:09:11 +0100965 MBEDTLS_CIPHER_AES_128_ECB,
Simon Butcher69283e52016-10-06 12:49:58 +0100966 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +0100967 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +0100968 {
969 return( ret );
970 }
971
972 /* AES-192 */
973 if( ( ret = cmac_test_subkeys( verbose,
Simon Butcher69283e52016-10-06 12:49:58 +0100974 "AES 192",
975 aes_192_key,
976 192,
977 (const unsigned char*)aes_192_subkeys,
978 MBEDTLS_CIPHER_AES_192_ECB,
979 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +0100980 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -0700981 {
982 return( ret );
983 }
Brian Murray0f6af732016-05-19 15:59:23 -0700984
Brian Murrayae1cb122016-05-23 15:01:59 -0700985 if( ( ret = cmac_test_wth_cipher( verbose,
986 "AES 192",
987 aes_192_key,
988 192,
989 test_message,
990 aes_message_lengths,
Simon Butcher69283e52016-10-06 12:49:58 +0100991 (const unsigned char*)aes_192_expected_result,
Simon Butcher327398a2016-10-05 14:09:11 +0100992 MBEDTLS_CIPHER_AES_192_ECB,
Simon Butcher69283e52016-10-06 12:49:58 +0100993 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +0100994 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +0100995 {
Simon Butcher327398a2016-10-05 14:09:11 +0100996 return( ret );
997 }
998
999 /* AES-256 */
1000 if( ( ret = cmac_test_subkeys( verbose,
Simon Butcher69283e52016-10-06 12:49:58 +01001001 "AES 256",
1002 aes_256_key,
1003 256,
1004 (const unsigned char*)aes_256_subkeys,
1005 MBEDTLS_CIPHER_AES_256_ECB,
1006 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001007 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -07001008 {
1009 return( ret );
1010 }
Brian Murray0f6af732016-05-19 15:59:23 -07001011
Simon Butcher69283e52016-10-06 12:49:58 +01001012 if( ( ret = cmac_test_wth_cipher ( verbose,
Brian Murrayae1cb122016-05-23 15:01:59 -07001013 "AES 256",
1014 aes_256_key,
1015 256,
1016 test_message,
1017 aes_message_lengths,
Simon Butcher69283e52016-10-06 12:49:58 +01001018 (const unsigned char*)aes_256_expected_result,
Simon Butcher327398a2016-10-05 14:09:11 +01001019 MBEDTLS_CIPHER_AES_256_ECB,
Simon Butcher69283e52016-10-06 12:49:58 +01001020 MBEDTLS_AES_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001021 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -07001022 {
1023 return( ret );
1024 }
Brian Murray0f6af732016-05-19 15:59:23 -07001025#endif /* MBEDTLS_AES_C */
1026
Simon Butcher69283e52016-10-06 12:49:58 +01001027#if defined(MBEDTLS_DES_C)
Simon Butcher327398a2016-10-05 14:09:11 +01001028 /* 3DES 2 key */
1029 if( ( ret = cmac_test_subkeys( verbose,
Simon Butcher69283e52016-10-06 12:49:58 +01001030 "3DES 2 key",
1031 des3_2key_key,
1032 192,
1033 (const unsigned char*)des3_2key_subkeys,
1034 MBEDTLS_CIPHER_DES_EDE3_ECB,
1035 MBEDTLS_DES3_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001036 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +01001037 {
1038 return( ret );
1039 }
1040
Brian Murrayae1cb122016-05-23 15:01:59 -07001041 if( ( ret = cmac_test_wth_cipher( verbose,
1042 "3DES 2 key",
1043 des3_2key_key,
1044 192,
1045 test_message,
1046 des3_message_lengths,
Simon Butcher69283e52016-10-06 12:49:58 +01001047 (const unsigned char*)des3_2key_expected_result,
Simon Butcher327398a2016-10-05 14:09:11 +01001048 MBEDTLS_CIPHER_DES_EDE3_ECB,
Simon Butcher69283e52016-10-06 12:49:58 +01001049 MBEDTLS_DES3_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001050 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -07001051 {
1052 return( ret );
1053 }
Brian Murray0f6af732016-05-19 15:59:23 -07001054
Simon Butcher327398a2016-10-05 14:09:11 +01001055 /* 3DES 3 key */
1056 if( ( ret = cmac_test_subkeys( verbose,
Simon Butcher69283e52016-10-06 12:49:58 +01001057 "3DES 3 key",
1058 des3_3key_key,
1059 192,
1060 (const unsigned char*)des3_3key_subkeys,
1061 MBEDTLS_CIPHER_DES_EDE3_ECB,
1062 MBEDTLS_DES3_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001063 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Simon Butcher327398a2016-10-05 14:09:11 +01001064 {
1065 return( ret );
1066 }
1067
Brian Murrayae1cb122016-05-23 15:01:59 -07001068 if( ( ret = cmac_test_wth_cipher( verbose,
1069 "3DES 3 key",
1070 des3_3key_key,
1071 192,
1072 test_message,
1073 des3_message_lengths,
Simon Butcher69283e52016-10-06 12:49:58 +01001074 (const unsigned char*)des3_3key_expected_result,
Simon Butcher327398a2016-10-05 14:09:11 +01001075 MBEDTLS_CIPHER_DES_EDE3_ECB,
Simon Butcher69283e52016-10-06 12:49:58 +01001076 MBEDTLS_DES3_BLOCK_SIZE,
Simon Butcher420be4e2016-10-07 12:55:43 +01001077 NB_CMAC_TESTS_PER_KEY ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -07001078 {
1079 return( ret );
1080 }
Brian Murray0f6af732016-05-19 15:59:23 -07001081#endif /* MBEDTLS_DES_C */
1082
Simon Butcher69283e52016-10-06 12:49:58 +01001083#if defined(MBEDTLS_AES_C)
Simon Butcher420be4e2016-10-07 12:55:43 +01001084 if( ( ret = test_aes128_cmac_prf( verbose ) ) != 0 )
Brian Murray9044b022016-05-19 16:36:56 -07001085 return( ret );
Brian Murray0f6af732016-05-19 15:59:23 -07001086#endif /* MBEDTLS_AES_C */
Brian Murrayb0c3c432016-05-18 14:29:51 -07001087
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001088 if( verbose != 0 )
1089 mbedtls_printf( "\n" );
Brian Murray0f6af732016-05-19 15:59:23 -07001090
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001091 return( 0 );
1092}
1093
Brian Murray0f6af732016-05-19 15:59:23 -07001094#endif /* MBEDTLS_SELF_TEST */
Robert Cragie3d23b1d2015-12-15 07:38:11 +00001095
1096#endif /* MBEDTLS_CMAC_C */