2016-12-14 11:13:43 +01:00
|
|
|
/**
|
2016-09-13 23:00:15 +02:00
|
|
|
* \file cmac.c
|
2016-10-05 15:09:11 +02:00
|
|
|
*
|
2016-10-06 13:49:58 +02:00
|
|
|
* \brief NIST SP800-38B compliant CMAC implementation for AES and 3DES
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
2020-08-19 16:54:51 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2015-12-15 08:38:11 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-09-13 23:00:15 +02:00
|
|
|
* References:
|
2016-10-05 15:09:11 +02:00
|
|
|
*
|
|
|
|
* - NIST SP 800-38B Recommendation for Block Cipher Modes of Operation: The
|
|
|
|
* CMAC Mode for Authentication
|
2016-12-13 12:51:04 +01:00
|
|
|
* http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
|
2016-10-05 15:09:11 +02:00
|
|
|
*
|
|
|
|
* - RFC 4493 - The AES-CMAC Algorithm
|
|
|
|
* https://tools.ietf.org/html/rfc4493
|
|
|
|
*
|
|
|
|
* - RFC 4615 - The Advanced Encryption Standard-Cipher-based Message
|
|
|
|
* Authentication Code-Pseudo-Random Function-128 (AES-CMAC-PRF-128)
|
|
|
|
* Algorithm for the Internet Key Exchange Protocol (IKE)
|
|
|
|
* https://tools.ietf.org/html/rfc4615
|
|
|
|
*
|
|
|
|
* Additional test vectors: ISO/IEC 9797-1
|
|
|
|
*
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
|
|
|
|
|
|
#include "mbedtls/cmac.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-14 00:58:46 +02:00
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
2016-09-14 00:58:46 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
2016-10-06 11:39:49 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2015-12-15 08:38:11 +01:00
|
|
|
#include <stdio.h>
|
2016-09-14 00:58:46 +02:00
|
|
|
#define mbedtls_printf printf
|
2016-11-06 13:45:15 +01:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2015-12-15 08:38:11 +01:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2016-09-14 00:58:46 +02:00
|
|
|
|
2017-12-21 09:57:43 +01:00
|
|
|
#if !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST)
|
2017-04-04 11:47:16 +02:00
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2016-10-05 15:09:11 +02:00
|
|
|
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-05-18 23:29:51 +02:00
|
|
|
* Multiplication by u in the Galois field of GF(2^n)
|
2016-01-13 12:28:16 +01:00
|
|
|
*
|
2016-09-13 23:21:01 +02:00
|
|
|
* As explained in NIST SP 800-38B, this can be computed:
|
2016-01-13 12:28:16 +01:00
|
|
|
*
|
2016-10-05 15:09:11 +02:00
|
|
|
* If MSB(p) = 0, then p = (p << 1)
|
|
|
|
* If MSB(p) = 1, then p = (p << 1) ^ R_n
|
|
|
|
* with R_64 = 0x1B and R_128 = 0x87
|
|
|
|
*
|
|
|
|
* Input and output MUST NOT point to the same buffer
|
2016-11-06 13:45:15 +01:00
|
|
|
* Block size must be 8 bytes or 16 bytes - the block sizes for DES and AES.
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-05-18 23:29:51 +02:00
|
|
|
static int cmac_multiply_by_u( unsigned char *output,
|
|
|
|
const unsigned char *input,
|
2016-09-13 23:00:15 +02:00
|
|
|
size_t blocksize )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-05-18 23:29:51 +02:00
|
|
|
const unsigned char R_128 = 0x87;
|
|
|
|
const unsigned char R_64 = 0x1B;
|
|
|
|
unsigned char R_n, mask;
|
|
|
|
unsigned char overflow = 0x00;
|
2016-10-05 15:09:11 +02:00
|
|
|
int i;
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
if( blocksize == MBEDTLS_AES_BLOCK_SIZE )
|
2016-05-21 03:25:43 +02:00
|
|
|
{
|
2016-05-18 23:29:51 +02:00
|
|
|
R_n = R_128;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
2016-10-06 13:49:58 +02:00
|
|
|
else if( blocksize == MBEDTLS_DES3_BLOCK_SIZE )
|
2016-05-21 03:25:43 +02:00
|
|
|
{
|
2016-05-18 23:29:51 +02:00
|
|
|
R_n = R_64;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
else
|
2016-05-21 03:25:43 +02:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 02:11:37 +01:00
|
|
|
for( i = (int)blocksize - 1; i >= 0; i-- )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-01-13 12:28:16 +01:00
|
|
|
output[i] = input[i] << 1 | overflow;
|
|
|
|
overflow = input[i] >> 7;
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
2016-01-13 12:00:47 +01:00
|
|
|
|
2016-01-13 14:05:03 +01:00
|
|
|
/* mask = ( input[0] >> 7 ) ? 0xff : 0x00
|
|
|
|
* using bit operations to avoid branches */
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-01-13 14:05:03 +01:00
|
|
|
/* MSVC has a warning about unary minus on unsigned, but this is
|
|
|
|
* well-defined and precisely what we want to do here */
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4146 )
|
|
|
|
#endif
|
|
|
|
mask = - ( input[0] >> 7 );
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
output[ blocksize - 1 ] ^= R_n & mask;
|
|
|
|
|
2016-05-20 01:02:42 +02:00
|
|
|
return( 0 );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate subkeys
|
2016-10-05 15:09:11 +02:00
|
|
|
*
|
|
|
|
* - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
static int cmac_generate_subkeys( mbedtls_cipher_context_t *ctx,
|
|
|
|
unsigned char* K1, unsigned char* K2 )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-05-20 01:38:36 +02:00
|
|
|
int ret;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-05-18 23:29:51 +02:00
|
|
|
size_t olen, block_size;
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
mbedtls_zeroize( L, sizeof( L ) );
|
|
|
|
|
|
|
|
block_size = ctx->cipher_info->block_size;
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
/* Calculate Ek(0) */
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = mbedtls_cipher_update( ctx, L, block_size, L, &olen ) ) != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
goto exit;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-01-13 12:00:47 +01:00
|
|
|
/*
|
2016-01-13 12:28:16 +01:00
|
|
|
* Generate K1 and K2
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = cmac_multiply_by_u( K1, L , block_size ) ) != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
goto exit;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = cmac_multiply_by_u( K2, K1 , block_size ) ) != 0 )
|
|
|
|
goto exit;
|
2016-05-24 05:17:04 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
exit:
|
|
|
|
mbedtls_zeroize( L, sizeof( L ) );
|
2016-05-24 05:17:04 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
2017-12-21 09:57:43 +01:00
|
|
|
#endif /* !defined(MBEDTLS_CMAC_ALT) || defined(MBEDTLS_SELF_TEST) */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2017-12-21 09:57:43 +01:00
|
|
|
#if !defined(MBEDTLS_CMAC_ALT)
|
2016-10-06 13:49:58 +02:00
|
|
|
static void cmac_xor_block( unsigned char *output, const unsigned char *input1,
|
|
|
|
const unsigned char *input2,
|
|
|
|
const size_t block_size )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2017-04-26 16:01:23 +02:00
|
|
|
size_t idx;
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2017-04-26 16:01:23 +02:00
|
|
|
for( idx = 0; idx < block_size; idx++ )
|
|
|
|
output[ idx ] = input1[ idx ] ^ input2[ idx ];
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
2016-01-13 14:14:04 +01:00
|
|
|
/*
|
|
|
|
* Create padded last block from (partial) last block.
|
|
|
|
*
|
|
|
|
* We can't use the padding option from the cipher layer, as it only works for
|
|
|
|
* CBC and we use ECB mode, and anyway we need to XOR K1 or K2 in addition.
|
|
|
|
*/
|
2016-10-06 13:49:58 +02:00
|
|
|
static void cmac_pad( unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
|
2016-09-13 23:00:15 +02:00
|
|
|
size_t padded_block_len,
|
2016-01-13 16:09:09 +01:00
|
|
|
const unsigned char *last_block,
|
2016-05-18 23:29:51 +02:00
|
|
|
size_t last_block_len )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
|
|
|
size_t j;
|
|
|
|
|
2016-05-18 23:29:51 +02:00
|
|
|
for( j = 0; j < padded_block_len; j++ )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-05-18 23:29:51 +02:00
|
|
|
if( j < last_block_len )
|
2016-01-13 14:14:04 +01:00
|
|
|
padded_block[j] = last_block[j];
|
2016-05-18 23:29:51 +02:00
|
|
|
else if( j == last_block_len )
|
2016-01-13 14:14:04 +01:00
|
|
|
padded_block[j] = 0x80;
|
2015-12-15 08:38:11 +01:00
|
|
|
else
|
2016-01-13 14:14:04 +01:00
|
|
|
padded_block[j] = 0x00;
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
|
2016-10-05 16:33:53 +02:00
|
|
|
const unsigned char *key, size_t keybits )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
mbedtls_cipher_type_t type;
|
|
|
|
mbedtls_cmac_context_t *cmac_ctx;
|
|
|
|
int retval;
|
2016-01-13 16:03:05 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ctx == NULL || ctx->cipher_info == NULL || key == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-01-13 16:03:05 +01:00
|
|
|
|
2016-11-03 02:11:37 +01:00
|
|
|
if( ( retval = mbedtls_cipher_setkey( ctx, key, (int)keybits,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_ENCRYPT ) ) != 0 )
|
|
|
|
return( retval );
|
|
|
|
|
|
|
|
type = ctx->cipher_info->type;
|
|
|
|
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case MBEDTLS_CIPHER_AES_128_ECB:
|
|
|
|
case MBEDTLS_CIPHER_AES_192_ECB:
|
|
|
|
case MBEDTLS_CIPHER_AES_256_ECB:
|
|
|
|
case MBEDTLS_CIPHER_DES_EDE3_ECB:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocated and initialise in the cipher context memory for the CMAC
|
|
|
|
* context */
|
|
|
|
cmac_ctx = mbedtls_calloc( 1, sizeof( mbedtls_cmac_context_t ) );
|
|
|
|
if( cmac_ctx == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
|
|
|
|
|
|
|
|
ctx->cmac_ctx = cmac_ctx;
|
|
|
|
|
|
|
|
mbedtls_zeroize( cmac_ctx->state, sizeof( cmac_ctx->state ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
|
|
|
|
const unsigned char *input, size_t ilen )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
mbedtls_cmac_context_t* cmac_ctx;
|
2016-05-18 23:29:51 +02:00
|
|
|
unsigned char *state;
|
2016-11-03 02:11:37 +01:00
|
|
|
int ret = 0;
|
|
|
|
size_t n, j, olen, block_size;
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
|
|
|
|
ctx->cmac_ctx == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_ctx = ctx->cmac_ctx;
|
|
|
|
block_size = ctx->cipher_info->block_size;
|
|
|
|
state = ctx->cmac_ctx->state;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-10 22:37:42 +02:00
|
|
|
/* Is there data still to process from the last call, that's greater in
|
|
|
|
* size than a block? */
|
2016-10-05 15:09:11 +02:00
|
|
|
if( cmac_ctx->unprocessed_len > 0 &&
|
2016-10-06 16:23:39 +02:00
|
|
|
ilen > block_size - cmac_ctx->unprocessed_len )
|
2016-05-20 01:38:36 +02:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
|
|
|
|
input,
|
|
|
|
block_size - cmac_ctx->unprocessed_len );
|
|
|
|
|
|
|
|
cmac_xor_block( state, cmac_ctx->unprocessed_block, state, block_size );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
|
|
|
|
&olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:37:42 +02:00
|
|
|
input += block_size - cmac_ctx->unprocessed_len;
|
|
|
|
ilen -= block_size - cmac_ctx->unprocessed_len;
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_ctx->unprocessed_len = 0;
|
2016-05-20 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
/* n is the number of blocks including any final partial block */
|
|
|
|
n = ( ilen + block_size - 1 ) / block_size;
|
|
|
|
|
2016-11-03 02:11:37 +01:00
|
|
|
/* Iterate across the input data in block sized chunks, excluding any
|
|
|
|
* final partial or complete block */
|
|
|
|
for( j = 1; j < n; j++ )
|
2016-05-20 01:38:36 +02:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_xor_block( state, input, state, block_size );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
|
|
|
|
&olen ) ) != 0 )
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
ilen -= block_size;
|
|
|
|
input += block_size;
|
2016-05-20 01:38:36 +02:00
|
|
|
}
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
/* If there is data left over that wasn't aligned to a block */
|
|
|
|
if( ilen > 0 )
|
|
|
|
{
|
2016-10-10 22:37:42 +02:00
|
|
|
memcpy( &cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
|
|
|
|
input,
|
|
|
|
ilen );
|
|
|
|
cmac_ctx->unprocessed_len += ilen;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
mbedtls_cmac_context_t* cmac_ctx;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char *state, *last_block;
|
|
|
|
unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
unsigned char M_last[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-10-05 15:09:11 +02:00
|
|
|
int ret;
|
|
|
|
size_t olen, block_size;
|
|
|
|
|
|
|
|
if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
|
|
|
|
output == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-01-13 15:27:55 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_ctx = ctx->cmac_ctx;
|
|
|
|
block_size = ctx->cipher_info->block_size;
|
|
|
|
state = cmac_ctx->state;
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_zeroize( K1, sizeof( K1 ) );
|
|
|
|
mbedtls_zeroize( K2, sizeof( K2 ) );
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_generate_subkeys( ctx, K1, K2 );
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
last_block = cmac_ctx->unprocessed_block;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
/* Calculate last block */
|
2016-10-11 11:49:26 +02:00
|
|
|
if( cmac_ctx->unprocessed_len < block_size )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_pad( M_last, block_size, last_block, cmac_ctx->unprocessed_len );
|
|
|
|
cmac_xor_block( M_last, M_last, K2, block_size );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-13 15:27:55 +01:00
|
|
|
/* Last block is complete block */
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_xor_block( M_last, last_block, K1, block_size );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
cmac_xor_block( state, M_last, state, block_size );
|
|
|
|
if( ( ret = mbedtls_cipher_update( ctx, state, block_size, state,
|
|
|
|
&olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
memcpy( output, state, block_size );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* Wipe the generated keys on the stack, and any other transients to avoid
|
|
|
|
* side channel leakage */
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_zeroize( K1, sizeof( K1 ) );
|
|
|
|
mbedtls_zeroize( K2, sizeof( K2 ) );
|
2016-10-05 15:09:11 +02:00
|
|
|
|
|
|
|
cmac_ctx->unprocessed_len = 0;
|
|
|
|
mbedtls_zeroize( cmac_ctx->unprocessed_block,
|
2016-10-06 13:49:58 +02:00
|
|
|
sizeof( cmac_ctx->unprocessed_block ) );
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_zeroize( state, MBEDTLS_CIPHER_BLKSIZE_MAX );
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
|
|
|
|
{
|
|
|
|
mbedtls_cmac_context_t* cmac_ctx;
|
2016-01-13 16:03:05 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
cmac_ctx = ctx->cmac_ctx;
|
|
|
|
|
|
|
|
/* Reset the internal state */
|
|
|
|
cmac_ctx->unprocessed_len = 0;
|
|
|
|
mbedtls_zeroize( cmac_ctx->unprocessed_block,
|
2016-10-06 16:23:39 +02:00
|
|
|
sizeof( cmac_ctx->unprocessed_block ) );
|
2016-10-06 11:39:49 +02:00
|
|
|
mbedtls_zeroize( cmac_ctx->state,
|
|
|
|
sizeof( cmac_ctx->state ) );
|
2016-10-05 15:09:11 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
|
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
mbedtls_cipher_context_t ctx;
|
2015-12-15 08:38:11 +01:00
|
|
|
int ret;
|
2016-01-13 12:00:47 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( cipher_info == NULL || key == NULL || input == NULL || output == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
goto exit;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
|
|
|
|
if( ret != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
goto exit;
|
2016-10-05 15:09:11 +02:00
|
|
|
|
|
|
|
ret = mbedtls_cipher_cmac_update( &ctx, input, ilen );
|
|
|
|
if( ret != 0 )
|
2016-05-25 07:46:43 +02:00
|
|
|
goto exit;
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
ret = mbedtls_cipher_cmac_finish( &ctx, output );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
exit:
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_cipher_free( &ctx );
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-01-13 16:09:09 +01:00
|
|
|
/*
|
2016-10-06 13:49:58 +02:00
|
|
|
* Implementation of AES-CMAC-PRF-128 defined in RFC 4615
|
2016-01-13 16:09:09 +01:00
|
|
|
*/
|
2016-05-18 23:29:51 +02:00
|
|
|
int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length,
|
2016-01-13 11:48:02 +01:00
|
|
|
const unsigned char *input, size_t in_len,
|
2020-11-10 05:38:00 +01:00
|
|
|
unsigned char output[16] )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2016-10-05 15:09:11 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
|
|
|
|
unsigned char int_key[MBEDTLS_AES_BLOCK_SIZE];
|
|
|
|
|
|
|
|
if( key == NULL || input == NULL || output == NULL )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
|
|
|
|
if( cipher_info == NULL )
|
|
|
|
{
|
|
|
|
/* Failing at this point must be due to a build issue */
|
|
|
|
ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
if( key_length == MBEDTLS_AES_BLOCK_SIZE )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
|
|
|
/* Use key as is */
|
2016-10-06 13:49:58 +02:00
|
|
|
memcpy( int_key, key, MBEDTLS_AES_BLOCK_SIZE );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-06 13:49:58 +02:00
|
|
|
memset( zero_key, 0, MBEDTLS_AES_BLOCK_SIZE );
|
2016-01-13 16:09:09 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
ret = mbedtls_cipher_cmac( cipher_info, zero_key, 128, key,
|
|
|
|
key_length, int_key );
|
2015-12-15 08:38:11 +01:00
|
|
|
if( ret != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
goto exit;
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
ret = mbedtls_cipher_cmac( cipher_info, int_key, 128, input, in_len,
|
|
|
|
output );
|
2016-01-13 12:30:00 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
exit:
|
|
|
|
mbedtls_zeroize( int_key, sizeof( int_key ) );
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
2016-05-20 01:02:42 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2017-04-04 11:47:16 +02:00
|
|
|
#endif /* !MBEDTLS_CMAC_ALT */
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2015-12-15 08:38:11 +01:00
|
|
|
/*
|
2016-12-13 12:51:04 +01:00
|
|
|
* CMAC test data for SP800-38B
|
|
|
|
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CMAC.pdf
|
|
|
|
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/TDES_CMAC.pdf
|
2016-05-20 00:59:23 +02:00
|
|
|
*
|
|
|
|
* AES-CMAC-PRF-128 test data from RFC 4615
|
|
|
|
* https://tools.ietf.org/html/rfc4615#page-4
|
2015-12-15 08:38:11 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
#define NB_CMAC_TESTS_PER_KEY 4
|
2015-12-15 08:38:11 +01:00
|
|
|
#define NB_PRF_TESTS 3
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
#if defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C)
|
|
|
|
/* All CMAC test inputs are truncated from the same 64 byte buffer. */
|
|
|
|
static const unsigned char test_message[] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
/* PT */
|
|
|
|
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
|
|
|
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
|
|
|
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
|
|
|
|
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
|
|
|
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
|
|
|
|
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
|
|
|
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
|
|
|
|
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
#endif /* MBEDTLS_AES_C || MBEDTLS_DES_C */
|
2016-05-23 21:49:50 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-05-20 00:59:23 +02:00
|
|
|
/* Truncation point of message for AES CMAC tests */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned int aes_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Mlen */
|
2016-05-20 00:59:23 +02:00
|
|
|
0,
|
|
|
|
16,
|
2016-12-13 12:51:04 +01:00
|
|
|
20,
|
2016-05-20 00:59:23 +02:00
|
|
|
64
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* CMAC-AES128 Test Data */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned char aes_128_key[16] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
|
|
|
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_128_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
|
2016-05-19 23:23:50 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K1 */
|
|
|
|
0xfb, 0xee, 0xd6, 0x18, 0x35, 0x71, 0x33, 0x66,
|
|
|
|
0x7c, 0x85, 0xe0, 0x8f, 0x72, 0x36, 0xa8, 0xde
|
2016-05-19 23:23:50 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K2 */
|
|
|
|
0xf7, 0xdd, 0xac, 0x30, 0x6a, 0xe2, 0x66, 0xcc,
|
|
|
|
0xf9, 0x0b, 0xc1, 0x1e, 0xe4, 0x6d, 0x51, 0x3b
|
2016-05-19 23:23:50 +02:00
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_128_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
|
2016-05-19 23:23:50 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #1 */
|
|
|
|
0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
|
|
|
|
0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
|
2016-05-19 23:23:50 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #2 */
|
|
|
|
0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
|
|
|
|
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
|
2016-05-19 23:23:50 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #3 */
|
|
|
|
0x7d, 0x85, 0x44, 0x9e, 0xa6, 0xea, 0x19, 0xc8,
|
|
|
|
0x23, 0xa7, 0xbf, 0x78, 0x83, 0x7d, 0xfa, 0xde
|
2016-05-19 23:23:50 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #4 */
|
|
|
|
0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
|
|
|
|
0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
|
2016-05-19 23:23:50 +02:00
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* CMAC-AES192 Test Data */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned char aes_192_key[24] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
|
|
|
|
0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
|
|
|
|
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
|
2016-05-20 00:59:23 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_192_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
|
2016-05-19 23:23:50 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K1 */
|
|
|
|
0x44, 0x8a, 0x5b, 0x1c, 0x93, 0x51, 0x4b, 0x27,
|
|
|
|
0x3e, 0xe6, 0x43, 0x9d, 0xd4, 0xda, 0xa2, 0x96
|
2016-05-19 23:23:50 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K2 */
|
|
|
|
0x89, 0x14, 0xb6, 0x39, 0x26, 0xa2, 0x96, 0x4e,
|
|
|
|
0x7d, 0xcc, 0x87, 0x3b, 0xa9, 0xb5, 0x45, 0x2c
|
2016-05-19 23:23:50 +02:00
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_192_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #1 */
|
|
|
|
0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
|
|
|
|
0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
|
2015-12-15 08:38:11 +01:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #2 */
|
|
|
|
0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
|
|
|
|
0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
|
2015-12-15 08:38:11 +01:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #3 */
|
|
|
|
0x3d, 0x75, 0xc1, 0x94, 0xed, 0x96, 0x07, 0x04,
|
|
|
|
0x44, 0xa9, 0xfa, 0x7e, 0xc7, 0x40, 0xec, 0xf8
|
2015-12-15 08:38:11 +01:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #4 */
|
|
|
|
0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
|
|
|
|
0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* CMAC-AES256 Test Data */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned char aes_256_key[32] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
|
|
|
|
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
|
|
|
|
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
|
|
|
|
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
|
2015-12-15 08:38:11 +01:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_256_subkeys[2][MBEDTLS_AES_BLOCK_SIZE] = {
|
2016-05-20 00:59:23 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K1 */
|
|
|
|
0xca, 0xd1, 0xed, 0x03, 0x29, 0x9e, 0xed, 0xac,
|
|
|
|
0x2e, 0x9a, 0x99, 0x80, 0x86, 0x21, 0x50, 0x2f
|
2016-05-20 00:59:23 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K2 */
|
|
|
|
0x95, 0xa3, 0xda, 0x06, 0x53, 0x3d, 0xdb, 0x58,
|
|
|
|
0x5d, 0x35, 0x33, 0x01, 0x0c, 0x42, 0xa0, 0xd9
|
2016-05-20 00:59:23 +02:00
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char aes_256_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_AES_BLOCK_SIZE] = {
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #1 */
|
|
|
|
0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
|
|
|
|
0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #2 */
|
|
|
|
0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
|
|
|
|
0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #3 */
|
|
|
|
0x15, 0x67, 0x27, 0xdc, 0x08, 0x78, 0x94, 0x4a,
|
|
|
|
0x02, 0x3c, 0x1f, 0xe0, 0x3b, 0xad, 0x6d, 0x93
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Example #4 */
|
|
|
|
0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
|
|
|
|
0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
};
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2016-05-20 00:59:23 +02:00
|
|
|
/* Truncation point of message for 3DES CMAC tests */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned int des3_message_lengths[NB_CMAC_TESTS_PER_KEY] = {
|
2016-05-20 00:59:23 +02:00
|
|
|
0,
|
2016-12-13 12:51:04 +01:00
|
|
|
16,
|
2016-05-20 00:59:23 +02:00
|
|
|
20,
|
|
|
|
32
|
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* CMAC-TDES (Generation) - 2 Key Test Data */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned char des3_2key_key[24] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Key1 */
|
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
|
|
|
/* Key2 */
|
|
|
|
0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xEF, 0x01,
|
|
|
|
/* Key3 */
|
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
|
2016-05-20 00:59:23 +02:00
|
|
|
};
|
|
|
|
static const unsigned char des3_2key_subkeys[2][8] = {
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K1 */
|
|
|
|
0x0d, 0xd2, 0xcb, 0x7a, 0x3d, 0x88, 0x88, 0xd9
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K2 */
|
|
|
|
0x1b, 0xa5, 0x96, 0xf4, 0x7b, 0x11, 0x11, 0xb2
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char des3_2key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #1 */
|
|
|
|
0x79, 0xce, 0x52, 0xa7, 0xf7, 0x86, 0xa9, 0x60
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #2 */
|
|
|
|
0xcc, 0x18, 0xa0, 0xb7, 0x9a, 0xf2, 0x41, 0x3b
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #3 */
|
|
|
|
0xc0, 0x6d, 0x37, 0x7e, 0xcd, 0x10, 0x19, 0x69
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #4 */
|
|
|
|
0x9c, 0xd3, 0x35, 0x80, 0xf9, 0xb6, 0x4d, 0xfb
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* CMAC-TDES (Generation) - 3 Key Test Data */
|
2016-05-20 01:38:36 +02:00
|
|
|
static const unsigned char des3_3key_key[24] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Key1 */
|
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xaa, 0xcd, 0xef,
|
|
|
|
/* Key2 */
|
|
|
|
0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
|
|
|
|
/* Key3 */
|
|
|
|
0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
|
2016-05-20 00:59:23 +02:00
|
|
|
};
|
|
|
|
static const unsigned char des3_3key_subkeys[2][8] = {
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K1 */
|
|
|
|
0x9d, 0x74, 0xe7, 0x39, 0x33, 0x17, 0x96, 0xc0
|
2016-05-20 00:59:23 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* K2 */
|
|
|
|
0x3a, 0xe9, 0xce, 0x72, 0x66, 0x2f, 0x2d, 0x9b
|
2016-05-20 00:59:23 +02:00
|
|
|
}
|
|
|
|
};
|
2016-10-06 13:49:58 +02:00
|
|
|
static const unsigned char des3_3key_expected_result[NB_CMAC_TESTS_PER_KEY][MBEDTLS_DES3_BLOCK_SIZE] = {
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #1 */
|
|
|
|
0x7d, 0xb0, 0xd3, 0x7d, 0xf9, 0x36, 0xc5, 0x50
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #2 */
|
|
|
|
0x30, 0x23, 0x9c, 0xf1, 0xf5, 0x2e, 0x66, 0x09
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #3 */
|
|
|
|
0x6c, 0x9f, 0x3e, 0xe4, 0x92, 0x3f, 0x6b, 0xe2
|
2016-05-18 23:29:51 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Sample #4 */
|
|
|
|
0x99, 0x42, 0x9b, 0xd0, 0xbF, 0x79, 0x04, 0xe5
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-05-20 00:59:23 +02:00
|
|
|
/* AES AES-CMAC-PRF-128 Test Data */
|
2015-12-15 08:38:11 +01:00
|
|
|
static const unsigned char PRFK[] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Key */
|
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
2015-12-15 08:38:11 +01:00
|
|
|
0xed, 0xcb
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sizes in bytes */
|
|
|
|
static const size_t PRFKlen[NB_PRF_TESTS] = {
|
|
|
|
18,
|
|
|
|
16,
|
|
|
|
10
|
|
|
|
};
|
|
|
|
|
2016-12-13 12:51:04 +01:00
|
|
|
/* Message */
|
2015-12-15 08:38:11 +01:00
|
|
|
static const unsigned char PRFM[] = {
|
2016-12-13 12:51:04 +01:00
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
2016-01-13 12:00:47 +01:00
|
|
|
0x10, 0x11, 0x12, 0x13
|
2015-12-15 08:38:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char PRFT[NB_PRF_TESTS][16] = {
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
0x84, 0xa3, 0x48, 0xa4, 0xa4, 0x5d, 0x23, 0x5b,
|
|
|
|
0xab, 0xff, 0xfc, 0x0d, 0x2b, 0x4d, 0xa0, 0x9a
|
2015-12-15 08:38:11 +01:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
0x98, 0x0a, 0xe8, 0x7b, 0x5f, 0x4c, 0x9c, 0x52,
|
|
|
|
0x14, 0xf5, 0xb6, 0xa8, 0x45, 0x5e, 0x4c, 0x2d
|
2015-12-15 08:38:11 +01:00
|
|
|
},
|
|
|
|
{
|
2016-12-13 12:51:04 +01:00
|
|
|
0x29, 0x0d, 0x9e, 0x11, 0x2e, 0xdb, 0x09, 0xee,
|
|
|
|
0x14, 0x1f, 0xcf, 0x64, 0xc0, 0xb7, 0x2f, 0x3d
|
2015-12-15 08:38:11 +01:00
|
|
|
}
|
|
|
|
};
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
static int cmac_test_subkeys( int verbose,
|
|
|
|
const char* testname,
|
|
|
|
const unsigned char* key,
|
|
|
|
int keybits,
|
|
|
|
const unsigned char* subkeys,
|
|
|
|
mbedtls_cipher_type_t cipher_type,
|
|
|
|
int block_size,
|
|
|
|
int num_tests )
|
|
|
|
{
|
2016-12-16 03:51:13 +01:00
|
|
|
int i, ret = 0;
|
2016-10-05 15:09:11 +02:00
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
unsigned char K2[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-10-05 15:09:11 +02:00
|
|
|
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
|
|
|
|
if( cipher_info == NULL )
|
|
|
|
{
|
|
|
|
/* Failing at this point must be due to a build issue */
|
2016-10-06 13:49:58 +02:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < num_tests; i++ )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_printf( " %s CMAC subkey #%u: ", testname, i + 1 );
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-10-12 11:00:42 +02:00
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "test execution failed\n" );
|
|
|
|
|
2016-10-12 11:00:42 +02:00
|
|
|
goto cleanup;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_cipher_setkey( &ctx, key, keybits,
|
|
|
|
MBEDTLS_ENCRYPT ) ) != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "test execution failed\n" );
|
|
|
|
|
2016-10-12 11:00:42 +02:00
|
|
|
goto cleanup;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = cmac_generate_subkeys( &ctx, K1, K2 );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
2016-10-12 11:00:42 +02:00
|
|
|
|
|
|
|
goto cleanup;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 13:55:43 +02:00
|
|
|
if( ( ret = memcmp( K1, subkeys, block_size ) ) != 0 ||
|
|
|
|
( ret = memcmp( K2, &subkeys[block_size], block_size ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
2016-10-12 11:00:42 +02:00
|
|
|
|
|
|
|
goto cleanup;
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "passed\n" );
|
2016-10-12 11:00:42 +02:00
|
|
|
|
|
|
|
mbedtls_cipher_free( &ctx );
|
2016-10-05 15:09:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 22:18:14 +01:00
|
|
|
ret = 0;
|
2016-10-12 11:00:42 +02:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
cleanup:
|
2016-10-06 13:49:58 +02:00
|
|
|
mbedtls_cipher_free( &ctx );
|
|
|
|
|
2016-10-12 11:00:42 +02:00
|
|
|
exit:
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
static int cmac_test_wth_cipher( int verbose,
|
|
|
|
const char* testname,
|
|
|
|
const unsigned char* key,
|
|
|
|
int keybits,
|
|
|
|
const unsigned char* messages,
|
|
|
|
const unsigned int message_lengths[4],
|
|
|
|
const unsigned char* expected_result,
|
|
|
|
mbedtls_cipher_type_t cipher_type,
|
|
|
|
int block_size,
|
|
|
|
int num_tests )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2016-12-16 03:51:13 +01:00
|
|
|
int i, ret = 0;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
2016-05-20 01:38:36 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
|
|
|
|
if( cipher_info == NULL )
|
2015-12-15 08:38:11 +01:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
/* Failing at this point must be due to a build issue */
|
|
|
|
ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
|
2016-05-19 23:23:50 +02:00
|
|
|
goto exit;
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
|
2016-05-19 23:23:50 +02:00
|
|
|
for( i = 0; i < num_tests; i++ )
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2016-10-06 16:23:39 +02:00
|
|
|
mbedtls_printf( " %s CMAC #%u: ", testname, i + 1 );
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = mbedtls_cipher_cmac( cipher_info, key, keybits, messages,
|
|
|
|
message_lengths[i], output ) ) != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
2016-05-19 23:23:50 +02:00
|
|
|
goto exit;
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
2016-05-25 07:46:43 +02:00
|
|
|
|
2016-10-05 15:09:11 +02:00
|
|
|
if( ( ret = memcmp( output, &expected_result[i * block_size], block_size ) ) != 0 )
|
2016-05-18 23:29:51 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
2016-05-19 23:23:50 +02:00
|
|
|
goto exit;
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 07:46:43 +02:00
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "passed\n" );
|
2016-05-18 23:29:51 +02:00
|
|
|
}
|
2018-03-01 22:18:14 +01:00
|
|
|
ret = 0;
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
exit:
|
2016-10-05 15:09:11 +02:00
|
|
|
return( ret );
|
2016-05-19 23:23:50 +02:00
|
|
|
}
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
|
|
|
static int test_aes128_cmac_prf( int verbose )
|
2016-05-21 03:25:43 +02:00
|
|
|
{
|
2016-05-19 23:23:50 +02:00
|
|
|
int i;
|
|
|
|
int ret;
|
2016-10-06 13:49:58 +02:00
|
|
|
unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
for( i = 0; i < NB_PRF_TESTS; i++ )
|
|
|
|
{
|
|
|
|
mbedtls_printf( " AES CMAC 128 PRF #%u: ", i );
|
2016-10-05 15:09:11 +02:00
|
|
|
ret = mbedtls_aes_cmac_prf_128( PRFK, PRFKlen[i], PRFM, 20, output );
|
2016-05-20 00:59:23 +02:00
|
|
|
if( ret != 0 ||
|
2016-10-06 13:49:58 +02:00
|
|
|
memcmp( output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE ) != 0 )
|
2016-05-20 00:59:23 +02:00
|
|
|
{
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "failed\n" );
|
|
|
|
|
|
|
|
return( ret );
|
2016-10-06 13:49:58 +02:00
|
|
|
}
|
|
|
|
else if( verbose != 0 )
|
2016-05-20 00:59:23 +02:00
|
|
|
{
|
|
|
|
mbedtls_printf( "passed\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_AES_C */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
int mbedtls_cmac_self_test( int verbose )
|
|
|
|
{
|
|
|
|
int ret;
|
2016-10-05 15:09:11 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-10-05 15:09:11 +02:00
|
|
|
/* AES-128 */
|
|
|
|
if( ( ret = cmac_test_subkeys( verbose,
|
2016-10-06 13:49:58 +02:00
|
|
|
"AES 128",
|
|
|
|
aes_128_key,
|
|
|
|
128,
|
|
|
|
(const unsigned char*)aes_128_subkeys,
|
|
|
|
MBEDTLS_CIPHER_AES_128_ECB,
|
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:01:59 +02:00
|
|
|
if( ( ret = cmac_test_wth_cipher( verbose,
|
|
|
|
"AES 128",
|
|
|
|
aes_128_key,
|
|
|
|
128,
|
|
|
|
test_message,
|
|
|
|
aes_message_lengths,
|
2016-10-06 13:49:58 +02:00
|
|
|
(const unsigned char*)aes_128_expected_result,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_CIPHER_AES_128_ECB,
|
2016-10-06 13:49:58 +02:00
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AES-192 */
|
|
|
|
if( ( ret = cmac_test_subkeys( verbose,
|
2016-10-06 13:49:58 +02:00
|
|
|
"AES 192",
|
|
|
|
aes_192_key,
|
|
|
|
192,
|
|
|
|
(const unsigned char*)aes_192_subkeys,
|
|
|
|
MBEDTLS_CIPHER_AES_192_ECB,
|
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:01:59 +02:00
|
|
|
if( ( ret = cmac_test_wth_cipher( verbose,
|
|
|
|
"AES 192",
|
|
|
|
aes_192_key,
|
|
|
|
192,
|
|
|
|
test_message,
|
|
|
|
aes_message_lengths,
|
2016-10-06 13:49:58 +02:00
|
|
|
(const unsigned char*)aes_192_expected_result,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_CIPHER_AES_192_ECB,
|
2016-10-06 13:49:58 +02:00
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AES-256 */
|
|
|
|
if( ( ret = cmac_test_subkeys( verbose,
|
2016-10-06 13:49:58 +02:00
|
|
|
"AES 256",
|
|
|
|
aes_256_key,
|
|
|
|
256,
|
|
|
|
(const unsigned char*)aes_256_subkeys,
|
|
|
|
MBEDTLS_CIPHER_AES_256_ECB,
|
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
if( ( ret = cmac_test_wth_cipher ( verbose,
|
2016-05-24 00:01:59 +02:00
|
|
|
"AES 256",
|
|
|
|
aes_256_key,
|
|
|
|
256,
|
|
|
|
test_message,
|
|
|
|
aes_message_lengths,
|
2016-10-06 13:49:58 +02:00
|
|
|
(const unsigned char*)aes_256_expected_result,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_CIPHER_AES_256_ECB,
|
2016-10-06 13:49:58 +02:00
|
|
|
MBEDTLS_AES_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2016-05-19 23:23:50 +02:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2016-10-05 15:09:11 +02:00
|
|
|
/* 3DES 2 key */
|
|
|
|
if( ( ret = cmac_test_subkeys( verbose,
|
2016-10-06 13:49:58 +02:00
|
|
|
"3DES 2 key",
|
|
|
|
des3_2key_key,
|
|
|
|
192,
|
|
|
|
(const unsigned char*)des3_2key_subkeys,
|
|
|
|
MBEDTLS_CIPHER_DES_EDE3_ECB,
|
|
|
|
MBEDTLS_DES3_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:01:59 +02:00
|
|
|
if( ( ret = cmac_test_wth_cipher( verbose,
|
|
|
|
"3DES 2 key",
|
|
|
|
des3_2key_key,
|
|
|
|
192,
|
|
|
|
test_message,
|
|
|
|
des3_message_lengths,
|
2016-10-06 13:49:58 +02:00
|
|
|
(const unsigned char*)des3_2key_expected_result,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_CIPHER_DES_EDE3_ECB,
|
2016-10-06 13:49:58 +02:00
|
|
|
MBEDTLS_DES3_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-10-05 15:09:11 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3DES 3 key */
|
|
|
|
if( ( ret = cmac_test_subkeys( verbose,
|
2016-10-06 13:49:58 +02:00
|
|
|
"3DES 3 key",
|
|
|
|
des3_3key_key,
|
|
|
|
192,
|
|
|
|
(const unsigned char*)des3_3key_subkeys,
|
|
|
|
MBEDTLS_CIPHER_DES_EDE3_ECB,
|
|
|
|
MBEDTLS_DES3_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2016-05-24 00:01:59 +02:00
|
|
|
if( ( ret = cmac_test_wth_cipher( verbose,
|
|
|
|
"3DES 3 key",
|
|
|
|
des3_3key_key,
|
|
|
|
192,
|
|
|
|
test_message,
|
|
|
|
des3_message_lengths,
|
2016-10-06 13:49:58 +02:00
|
|
|
(const unsigned char*)des3_3key_expected_result,
|
2016-10-05 15:09:11 +02:00
|
|
|
MBEDTLS_CIPHER_DES_EDE3_ECB,
|
2016-10-06 13:49:58 +02:00
|
|
|
MBEDTLS_DES3_BLOCK_SIZE,
|
2016-10-07 13:55:43 +02:00
|
|
|
NB_CMAC_TESTS_PER_KEY ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
2016-10-06 13:49:58 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2016-10-07 13:55:43 +02:00
|
|
|
if( ( ret = test_aes128_cmac_prf( verbose ) ) != 0 )
|
2016-05-20 01:36:56 +02:00
|
|
|
return( ret );
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2016-05-18 23:29:51 +02:00
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
if( verbose != 0 )
|
|
|
|
mbedtls_printf( "\n" );
|
2016-05-20 00:59:23 +02:00
|
|
|
|
2015-12-15 08:38:11 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2016-05-20 00:59:23 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2015-12-15 08:38:11 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_CMAC_C */
|