mbedtls/library/ccm.c

708 lines
24 KiB
C
Raw Permalink Normal View History

2014-05-02 15:17:29 +02:00
/*
* NIST SP800-38C compliant CCM implementation
*
2015-07-27 11:11:48 +02:00
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
2015-09-04 14:21:07 +02:00
* SPDX-License-Identifier: Apache-2.0
2014-05-02 15:17:29 +02:00
*
2015-09-04 14:21:07 +02: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
2014-05-02 15:17:29 +02:00
*
2015-09-04 14:21:07 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
2014-05-02 15:17:29 +02:00
*
2015-09-04 14:21:07 +02:00
* 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.
2014-05-02 15:17:29 +02:00
*
2015-09-04 14:21:07 +02:00
* This file is part of mbed TLS (https://tls.mbed.org)
2014-05-02 15:17:29 +02:00
*/
/*
* Definition of CCM:
* http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
* RFC 3610 "Counter with CBC-MAC (CCM)"
*
* Related:
* RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
*/
#if !defined(MBEDTLS_CONFIG_FILE)
2015-03-09 18:05:11 +01:00
#include "mbedtls/config.h"
2014-05-02 15:17:29 +02:00
#else
#include MBEDTLS_CONFIG_FILE
2014-05-02 15:17:29 +02:00
#endif
#if defined(MBEDTLS_CCM_C)
2014-05-02 15:17:29 +02:00
2015-03-09 18:05:11 +01:00
#include "mbedtls/ccm.h"
#include "mbedtls/platform.h"
#include "mbedtls/platform_util.h"
2014-05-02 15:17:29 +02:00
#include <stdint.h>
#include <string.h>
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_PLATFORM_C)
2015-03-09 18:05:11 +01:00
#include "mbedtls/platform.h"
#else
#include <stdio.h>
#define mbedtls_printf printf
#endif /* MBEDTLS_PLATFORM_C */
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
#if !defined(MBEDTLS_CCM_ALT)
2018-12-11 12:22:16 +01:00
#define CCM_VALIDATE_RET( cond ) \
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT )
#define CCM_VALIDATE( cond ) \
MBEDTLS_INTERNAL_VALIDATE( cond )
2014-05-06 15:56:07 +02:00
#define CCM_ENCRYPT 0
#define CCM_DECRYPT 1
2014-05-06 12:12:45 +02:00
/*
* Initialize context
*/
2015-04-28 18:02:54 +02:00
void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
{
2018-12-11 12:22:16 +01:00
CCM_VALIDATE( ctx != NULL );
2015-04-28 18:02:54 +02:00
memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
}
int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
mbedtls_cipher_id_t cipher,
const unsigned char *key,
unsigned int keybits )
2014-05-06 12:12:45 +02:00
{
int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
const mbedtls_cipher_info_t *cipher_info;
2014-05-06 12:12:45 +02:00
2018-12-11 12:22:16 +01:00
CCM_VALIDATE_RET( ctx != NULL );
CCM_VALIDATE_RET( key != NULL );
cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
2014-05-06 12:12:45 +02:00
if( cipher_info == NULL )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:12:45 +02:00
if( cipher_info->block_size != 16 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:12:45 +02:00
mbedtls_cipher_free( &ctx->cipher_ctx );
if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
2014-05-06 12:12:45 +02:00
return( ret );
if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
MBEDTLS_ENCRYPT ) ) != 0 )
2014-05-06 12:12:45 +02:00
{
return( ret );
}
return( ret );
2014-05-06 12:12:45 +02:00
}
/*
* Free context
*/
void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
2014-05-06 12:12:45 +02:00
{
if( ctx == NULL )
return;
mbedtls_cipher_free( &ctx->cipher_ctx );
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
2014-05-06 12:12:45 +02:00
}
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
/* Durstenfeld's version of Fisher-Yates shuffle */
static void mbedtls_generate_permutation( unsigned char* table, size_t size )
{
size_t i, j;
for( i = 0; i < size; i++ )
{
table[i] = (unsigned char) i;
}
if( size < 2 )
{
return;
}
for( i = size - 1; i > 0; i-- )
{
unsigned char tmp;
j = mbedtls_platform_random_in_range( (uint32_t) i + 1 );
tmp = table[i];
table[i] = table[j];
table[j] = tmp;
}
}
static void mbedtls_generate_masks( unsigned char* table, size_t size )
{
size_t i;
for( i = 0; i < size; i++ )
{
table[i] = mbedtls_platform_random_in_range( 256 );
}
}
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
/*
* Macros for common operations.
* Results in smaller compiled code than static inline functions.
*/
2014-05-06 12:13:09 +02:00
/*
* Update the CBC-MAC state in y using a block in b
* (Always using b as the source helps the compiler optimise a bit better.)
* Initial b masking happens outside of this macro due to various sources of it.
*/
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
#define UPDATE_CBC_MAC \
for( i = 0; i < 16; i++ ) \
{ \
y[perm_table[i]] ^= b[perm_table[i]]; \
y[perm_table[i]] ^= mask_table[perm_table[i]]; \
} \
\
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
return( ret );
#else
#define UPDATE_CBC_MAC \
for( i = 0; i < 16; i++ ) \
y[i] ^= b[i]; \
\
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
return( ret );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
/*
* Copy src to dst starting at a random offset, while masking the whole dst buffer.
*/
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
#define COPY_MASK( dst, src, mask, len_src, len_dst ) \
do \
{ \
unsigned j, offset = mbedtls_platform_random_in_range( 256 ); \
mbedtls_generate_masks( mask_table, 16 ); \
mbedtls_generate_permutation( perm_table, 16 ); \
for( i = 0; i < len_src; i++ ) \
{ \
j = (i + offset) % len_src; \
(dst)[j] = (src)[j] ^ (mask)[j]; \
} \
for( ; i < len_dst; i++ ) \
(dst)[i] ^= (mask)[i]; \
} while( 0 )
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
/*
* Encrypt or decrypt a partial block with CTR
* Warning: using b for temporary storage! src and dst must not be b!
2014-05-07 14:43:46 +02:00
* This avoids allocating one more 16 bytes buffer while allowing src == dst.
*/
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
#define CTR_CRYPT( dst, src, len ) \
do \
{ \
mbedtls_generate_permutation( perm_table, len ); \
mbedtls_generate_masks( mask_table, len ); \
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
16, b, &olen ) ) != 0 ) \
{ \
return( ret ); \
} \
\
for( i = 0; i < (len); i++ ) \
{ \
(dst)[perm_table[i]] = (src)[perm_table[i]] ^ mask_table[perm_table[i]];\
(dst)[perm_table[i]] ^= b[perm_table[i]]; \
(dst)[perm_table[i]] ^= mask_table[perm_table[i]]; \
} \
} while( 0 )
#else
#define CTR_CRYPT( dst, src, len ) \
do \
{ \
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, \
16, b, &olen ) ) != 0 ) \
{ \
return( ret ); \
} \
\
for( i = 0; i < (len); i++ ) \
(dst)[i] = (src)[i] ^ b[i]; \
} while( 0 )
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
2014-05-06 12:13:09 +02:00
/*
2014-05-06 15:56:07 +02:00
* Authenticated encryption or decryption
2014-05-06 12:13:09 +02:00
*/
static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
2014-05-06 15:56:07 +02:00
const unsigned char *iv, size_t iv_len,
const unsigned char *add, size_t add_len,
const unsigned char *input, unsigned char *output,
unsigned char *tag, size_t tag_len )
2014-05-06 12:13:09 +02:00
{
int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
uint_fast8_t i;
uint_fast8_t q;
size_t len_left, olen;
2014-05-06 12:13:09 +02:00
unsigned char b[16];
unsigned char y[16];
unsigned char ctr[16];
const unsigned char *src;
unsigned char *dst;
volatile size_t flow_ctrl = 0;
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
unsigned char perm_table[16];
unsigned char mask_table[16];
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
2014-05-06 12:13:09 +02:00
/*
* Check length requirements: SP800-38C A.1
* Additional requirement: a < 2^16 - 2^8 to simplify the code.
* 'length' checked later (when writing it to the first block)
2018-05-29 12:33:45 +02:00
*
* Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
2014-05-06 12:13:09 +02:00
*/
2018-05-29 12:33:45 +02:00
if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:13:09 +02:00
/* Also implies q is within bounds */
if( iv_len < 7 || iv_len > 13 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:13:09 +02:00
if( add_len > 0xFF00 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:13:09 +02:00
mbedtls_platform_zeroize( b, 16 );
mbedtls_platform_zeroize( y, 16 );
mbedtls_platform_zeroize( ctr, 16 );
q = (uint_fast8_t) (16 - 1 - iv_len);
flow_ctrl++; /* 1 */
2014-05-06 12:13:09 +02:00
/*
* First block B_0:
* 0 .. 0 flags
* 1 .. iv_len nonce (aka iv)
* iv_len+1 .. 15 length
*
* With flags as (bits):
* 7 0
* 6 add present?
* 5 .. 3 (t - 2) / 2
* 2 .. 0 q - 1
*/
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
mbedtls_generate_masks( mask_table, 16 );
mbedtls_generate_permutation( perm_table, 16 );
b[0] = (unsigned char) ( ( ( add_len > 0 ) << 6 ) |
( ( ( tag_len - 2 ) / 2 ) << 3 ) |
( q - 1 ) ) ^ mask_table[0];
for( i = 0; i < iv_len; i++ )
{
b[i+1] = iv[i] ^ mask_table[i+1];
flow_ctrl++; /* iv_len + 1 */
}
2014-05-06 12:13:09 +02:00
for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
b[15-i] = (unsigned char)( ( len_left & 0xFF ) ) ^ mask_table[15-i];
#else
b[0] = 0;
b[0] |= ( add_len > 0 ) << 6;
b[0] |= ( ( tag_len - 2 ) / 2 ) << 3;
b[0] |= q - 1;
mbedtls_platform_memcpy( b + 1, iv, iv_len );
flow_ctrl += iv_len; /* iv_len + 1 */
for( i = 0, len_left = length; i < q; i++, len_left >>= 8 )
b[15-i] = (unsigned char)( len_left & 0xFF );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
2014-05-06 12:13:09 +02:00
if( len_left > 0 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
2014-05-06 12:13:09 +02:00
/* Start CBC-MAC with first block */
memset( y, 0, 16 );
UPDATE_CBC_MAC;
flow_ctrl++; /* iv_len + 2 */
2014-05-06 12:13:09 +02:00
/*
* If there is additional data, update CBC-MAC with
* add_len, add, 0 (padding to a block boundary)
*/
if( add_len > 0 )
{
size_t use_len;
len_left = add_len;
src = add;
mbedtls_platform_memset( b, 0, 16 );
2014-05-06 12:13:09 +02:00
use_len = len_left < 16 - 2 ? len_left : 16 - 2;
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
COPY_MASK( b+2, src, mask_table+2, use_len, 14 );
b[0] = (unsigned char)( ( ( add_len >> 8 ) & 0xFF ) ^ mask_table[0] );
b[1] = (unsigned char)( ( ( add_len ) & 0xFF ) ^ mask_table[1] );
#else
b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF );
b[1] = (unsigned char)( ( add_len ) & 0xFF );
mbedtls_platform_memcpy( b + 2, src, use_len );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
2014-05-06 12:13:09 +02:00
len_left -= use_len;
src += use_len;
UPDATE_CBC_MAC;
flow_ctrl++; /* iv_len + 2 + ( add_len? 1 : 0 ) */
2014-05-06 12:13:09 +02:00
while( len_left > 0 )
2014-05-06 12:13:09 +02:00
{
use_len = len_left > 16 ? 16 : len_left;
mbedtls_platform_memset( b, 0, 16 );
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
COPY_MASK( b, src, mask_table, use_len, 16 );
#else
mbedtls_platform_memcpy( b, src, use_len );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
UPDATE_CBC_MAC;
2014-05-06 12:13:09 +02:00
len_left -= use_len;
src += use_len;
2014-05-06 12:13:09 +02:00
}
}
/*
* Prepare counter block for encryption:
2014-05-06 12:13:09 +02:00
* 0 .. 0 flags
* 1 .. iv_len nonce (aka iv)
* iv_len+1 .. 15 counter (initially 1)
*
* With flags as (bits):
* 7 .. 3 0
* 2 .. 0 q - 1
*/
ctr[0] = q - 1;
mbedtls_platform_memcpy( ctr + 1, iv, iv_len );
mbedtls_platform_memset( ctr + 1 + iv_len, 0, q );
2014-05-06 12:13:09 +02:00
ctr[15] = 1;
flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) */
2014-05-06 12:13:09 +02:00
/*
* Authenticate and {en,de}crypt the message.
*
* The only difference between encryption and decryption is
* the respective order of authentication and {en,de}cryption.
*/
2014-05-06 12:13:09 +02:00
len_left = length;
src = input;
dst = output;
while( len_left > 0 )
2014-05-06 12:13:09 +02:00
{
size_t use_len = len_left > 16 ? 16 : len_left;
2014-05-06 18:06:52 +02:00
if( mode == CCM_ENCRYPT )
{
mbedtls_platform_memset( b, 0, 16 );
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
COPY_MASK( b, src, mask_table, use_len, 16 );
#else
mbedtls_platform_memcpy( b, src, use_len );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
UPDATE_CBC_MAC;
flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) + encryptions */
2014-05-06 18:06:52 +02:00
}
CTR_CRYPT( dst, src, use_len );
2014-05-06 12:13:09 +02:00
2014-05-06 18:06:52 +02:00
if( mode == CCM_DECRYPT )
{
mbedtls_platform_memset( b, 0, 16 );
#if defined(MBEDTLS_CCM_SHUFFLING_MASKING)
COPY_MASK( b, dst, mask_table, use_len, 16 );
#else
mbedtls_platform_memcpy( b, dst, use_len );
#endif /* MBEDTLS_CCM_SHUFFLING_MASKING */
UPDATE_CBC_MAC;
flow_ctrl++; /* iv_len + 3 + ( add_len? 1 : 0 ) + decryptions */
2014-05-06 18:06:52 +02:00
}
dst += use_len;
src += use_len;
len_left -= use_len;
2014-05-06 12:13:09 +02:00
2014-05-06 18:06:52 +02:00
/*
* Increment counter.
* No need to check for overflow thanks to the length check above.
*/
2014-05-06 12:13:09 +02:00
for( i = 0; i < q; i++ )
if( ++ctr[15-i] != 0 )
break;
}
flow_ctrl++; /* iv_len + 4 + ( add_len? 1 : 0 ) + enc/dec */
2014-05-06 12:13:09 +02:00
/*
* Authentication: reset counter and crypt/mask internal tag
2014-05-06 12:13:09 +02:00
*/
for( i = 0; i < q; i++ )
ctr[15-i] = 0;
CTR_CRYPT( y, y, 16 );
mbedtls_platform_memcpy( tag, y, tag_len );
2014-05-06 12:13:09 +02:00
flow_ctrl++; /* iv_len + 5 + ( add_len? 1 : 0 ) + enc/dec */
mbedtls_platform_zeroize( b, 16 );
mbedtls_platform_zeroize( y, 16 );
mbedtls_platform_zeroize( ctr, 16 );
{
size_t operations = length / 16;
operations += ( length % 16 ? 1 : 0 );
operations += ( add_len > 0 ? 1 : 0 );
/* See comments above on steps in calculating flow_ctrl */
if( flow_ctrl == iv_len + 5 + operations )
{
mbedtls_platform_random_delay();
if( flow_ctrl == iv_len + 5 + operations )
return( ret );
}
}
return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2014-05-06 12:13:09 +02:00
}
2014-05-06 15:56:07 +02:00
/*
* Authenticated encryption
*/
2018-05-14 15:31:49 +02:00
int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
2014-05-06 15:56:07 +02:00
const unsigned char *iv, size_t iv_len,
const unsigned char *add, size_t add_len,
const unsigned char *input, unsigned char *output,
unsigned char *tag, size_t tag_len )
{
2018-12-11 12:22:16 +01:00
CCM_VALIDATE_RET( ctx != NULL );
CCM_VALIDATE_RET( iv != NULL );
CCM_VALIDATE_RET( add_len == 0 || add != NULL );
CCM_VALIDATE_RET( length == 0 || input != NULL );
CCM_VALIDATE_RET( length == 0 || output != NULL );
CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
2014-05-06 15:56:07 +02:00
return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len,
add, add_len, input, output, tag, tag_len ) );
}
2018-05-14 15:31:49 +02:00
int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
const unsigned char *iv, size_t iv_len,
const unsigned char *add, size_t add_len,
const unsigned char *input, unsigned char *output,
unsigned char *tag, size_t tag_len )
{
2018-12-11 12:22:16 +01:00
CCM_VALIDATE_RET( ctx != NULL );
CCM_VALIDATE_RET( iv != NULL );
CCM_VALIDATE_RET( add_len == 0 || add != NULL );
CCM_VALIDATE_RET( length == 0 || input != NULL );
CCM_VALIDATE_RET( length == 0 || output != NULL );
CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
2018-05-14 15:31:49 +02:00
if( tag_len == 0 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
return( mbedtls_ccm_star_encrypt_and_tag( ctx, length, iv, iv_len, add,
add_len, input, output, tag, tag_len ) );
}
2014-05-06 15:56:07 +02:00
/*
* Authenticated decryption
*/
2018-05-14 15:31:49 +02:00
int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
2014-05-06 15:56:07 +02:00
const unsigned char *iv, size_t iv_len,
const unsigned char *add, size_t add_len,
const unsigned char *input, unsigned char *output,
const unsigned char *tag, size_t tag_len )
{
int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
2014-05-06 15:56:07 +02:00
unsigned char check_tag[16];
uint_fast8_t i;
2014-05-06 15:56:07 +02:00
int diff;
2018-12-11 12:22:16 +01:00
CCM_VALIDATE_RET( ctx != NULL );
CCM_VALIDATE_RET( iv != NULL );
CCM_VALIDATE_RET( add_len == 0 || add != NULL );
CCM_VALIDATE_RET( length == 0 || input != NULL );
CCM_VALIDATE_RET( length == 0 || output != NULL );
CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
2018-12-11 12:22:16 +01:00
2014-05-06 15:56:07 +02:00
if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length,
iv, iv_len, add, add_len,
input, output, check_tag, tag_len ) ) != 0 )
{
return( ret );
}
/* Check tag in "constant-time" */
for( diff = 0, i = 0; i < tag_len; i++ )
diff |= tag[i] ^ check_tag[i];
if( diff != 0 )
{
mbedtls_platform_zeroize( output, length );
return( MBEDTLS_ERR_CCM_AUTH_FAILED );
2014-05-06 15:56:07 +02:00
}
return( ret );
2014-05-06 15:56:07 +02:00
}
2018-05-14 15:31:49 +02:00
int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
const unsigned char *iv, size_t iv_len,
const unsigned char *add, size_t add_len,
const unsigned char *input, unsigned char *output,
const unsigned char *tag, size_t tag_len )
{
2018-12-11 15:57:19 +01:00
CCM_VALIDATE_RET( ctx != NULL );
CCM_VALIDATE_RET( iv != NULL );
CCM_VALIDATE_RET( add_len == 0 || add != NULL );
CCM_VALIDATE_RET( length == 0 || input != NULL );
CCM_VALIDATE_RET( length == 0 || output != NULL );
CCM_VALIDATE_RET( tag_len == 0 || tag != NULL );
2018-12-11 15:57:19 +01:00
2018-05-14 15:31:49 +02:00
if( tag_len == 0 )
return( MBEDTLS_ERR_CCM_BAD_INPUT );
return( mbedtls_ccm_star_auth_decrypt( ctx, length, iv, iv_len, add,
add_len, input, output, tag, tag_len ) );
}
#endif /* !MBEDTLS_CCM_ALT */
2014-05-02 15:17:29 +02:00
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
2014-05-06 12:13:09 +02:00
/*
* Examples 1 to 3 from SP800-38C Appendix C
*/
#define NB_TESTS 3
#define CCM_SELFTEST_PT_MAX_LEN 24
#define CCM_SELFTEST_CT_MAX_LEN 32
2014-05-06 12:13:09 +02:00
/*
* The data is the same for all tests, only the used length changes
*/
static const unsigned char key[] = {
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
};
static const unsigned char iv[] = {
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b
};
static const unsigned char ad[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13
};
static const unsigned char msg[CCM_SELFTEST_PT_MAX_LEN] = {
2014-05-06 12:13:09 +02:00
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
};
static const size_t iv_len [NB_TESTS] = { 7, 8, 12 };
static const size_t add_len[NB_TESTS] = { 8, 16, 20 };
static const size_t msg_len[NB_TESTS] = { 4, 16, 24 };
static const size_t tag_len[NB_TESTS] = { 4, 6, 8 };
static const unsigned char res[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = {
2014-05-06 12:13:09 +02:00
{ 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d },
{ 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62,
0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d,
0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd },
{ 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a,
0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b,
0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5,
0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
};
int mbedtls_ccm_self_test( int verbose )
2014-05-02 15:17:29 +02:00
{
mbedtls_ccm_context ctx;
/*
* Some hardware accelerators require the input and output buffers
* would be in RAM, because the flash is not accessible.
* Use buffers on the stack to hold the test vectors data.
*/
unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN];
unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN];
2014-05-06 12:13:09 +02:00
size_t i;
int ret;
2015-04-28 18:02:54 +02:00
mbedtls_ccm_init( &ctx );
if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
2014-05-06 12:13:09 +02:00
{
if( verbose != 0 )
mbedtls_printf( " CCM: setup failed" );
2014-05-06 12:13:09 +02:00
return( 1 );
}
for( i = 0; i < NB_TESTS; i++ )
{
if( verbose != 0 )
mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
2014-05-06 12:13:09 +02:00
memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
memcpy( plaintext, msg, msg_len[i] );
ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
iv, iv_len[i], ad, add_len[i],
plaintext, ciphertext,
ciphertext + msg_len[i], tag_len[i] );
2014-05-06 12:13:09 +02:00
if( ret != 0 ||
memcmp( ciphertext, res[i], msg_len[i] + tag_len[i] ) != 0 )
2014-05-06 12:13:09 +02:00
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
2014-05-06 12:13:09 +02:00
return( 1 );
}
memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
2014-05-06 12:13:09 +02:00
ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
iv, iv_len[i], ad, add_len[i],
ciphertext, plaintext,
ciphertext + msg_len[i], tag_len[i] );
2014-05-06 18:06:52 +02:00
if( ret != 0 ||
memcmp( plaintext, msg, msg_len[i] ) != 0 )
2014-05-06 18:06:52 +02:00
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
2014-05-06 18:06:52 +02:00
return( 1 );
}
2014-05-06 12:13:09 +02:00
if( verbose != 0 )
mbedtls_printf( "passed\n" );
2014-05-06 12:13:09 +02:00
}
mbedtls_ccm_free( &ctx );
2014-05-02 15:17:29 +02:00
if( verbose != 0 )
mbedtls_printf( "\n" );
2014-05-02 15:17:29 +02:00
return( 0 );
}
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
2014-05-02 15:17:29 +02:00
#endif /* MBEDTLS_CCM_C */