2014-05-02 15:17:29 +02:00
|
|
|
/*
|
|
|
|
* NIST SP800-38C compliant CCM implementation
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2014-05-02 15:17:29 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#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"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2014-05-02 15:17:29 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
#else
|
2021-07-28 15:08:47 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
|
2021-07-28 15:08:47 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2017-04-04 11:37:15 +02:00
|
|
|
#if !defined(MBEDTLS_CCM_ALT)
|
|
|
|
|
2014-05-06 15:56:07 +02:00
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
|
|
|
|
mbedtls_cipher_id_t cipher,
|
|
|
|
const unsigned char *key,
|
2015-06-18 14:58:58 +02:00
|
|
|
unsigned int keybits )
|
2014-05-06 12:12:45 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2014-05-06 12:12:45 +02:00
|
|
|
|
2019-01-10 09:10:02 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_values( cipher, keybits,
|
|
|
|
MBEDTLS_MODE_ECB );
|
2014-05-06 12:12:45 +02:00
|
|
|
if( cipher_info == NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2014-05-06 12:12:45 +02:00
|
|
|
|
|
|
|
if( cipher_info->block_size != 16 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2014-05-06 12:12:45 +02:00
|
|
|
|
2015-05-27 17:23:30 +02:00
|
|
|
mbedtls_cipher_free( &ctx->cipher_ctx );
|
|
|
|
|
2015-05-14 13:51:45 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
|
2014-05-06 12:12:45 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-06-18 14:58:58 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ENCRYPT ) ) != 0 )
|
2014-05-06 12:12:45 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free context
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
|
2014-05-06 12:12:45 +02:00
|
|
|
{
|
2018-12-11 14:37:51 +01:00
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_cipher_free( &ctx->cipher_ctx );
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
|
2014-05-06 12:12:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
#define CCM_STATE__CLEAR 0
|
2021-07-27 13:49:54 +02:00
|
|
|
#define CCM_STATE__STARTED (1 << 0)
|
2022-05-18 01:30:44 +02:00
|
|
|
#define CCM_STATE__LENGTHS_SET (1 << 1)
|
2021-08-09 15:53:41 +02:00
|
|
|
#define CCM_STATE__AUTH_DATA_STARTED (1 << 2)
|
|
|
|
#define CCM_STATE__AUTH_DATA_FINISHED (1 << 3)
|
2021-07-28 14:14:58 +02:00
|
|
|
#define CCM_STATE__ERROR (1 << 4)
|
2021-06-22 20:34:20 +02:00
|
|
|
|
2021-07-07 11:05:45 +02:00
|
|
|
/*
|
|
|
|
* Encrypt or decrypt a partial block with CTR
|
|
|
|
*/
|
|
|
|
static int mbedtls_ccm_crypt( mbedtls_ccm_context *ctx,
|
|
|
|
size_t offset, size_t use_len,
|
|
|
|
const unsigned char *input,
|
|
|
|
unsigned char *output )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t olen = 0;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char tmp_buf[16] = {0};
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->ctr, 16, tmp_buf,
|
|
|
|
&olen ) ) != 0 )
|
|
|
|
{
|
2021-07-27 16:07:54 +02:00
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
2021-07-27 13:54:55 +02:00
|
|
|
mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
|
2021-07-07 11:05:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < use_len; i++ )
|
|
|
|
output[i] = input[i] ^ tmp_buf[offset + i];
|
|
|
|
|
2021-07-27 13:54:55 +02:00
|
|
|
mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf));
|
2021-07-07 11:05:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx) {
|
|
|
|
ctx->state = CCM_STATE__CLEAR;
|
|
|
|
memset( ctx->y, 0, 16);
|
|
|
|
memset( ctx->ctr, 0, 16);
|
|
|
|
}
|
|
|
|
|
2021-07-27 14:03:53 +02:00
|
|
|
static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx)
|
2014-05-06 12:13:09 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-05-06 12:13:09 +02:00
|
|
|
unsigned char i;
|
2014-05-07 13:14:42 +02:00
|
|
|
size_t len_left, olen;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
/* length calulcation can be done only after both
|
|
|
|
* mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed
|
|
|
|
*/
|
2022-05-18 01:30:44 +02:00
|
|
|
if( !(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET) )
|
2021-06-22 20:34:20 +02:00
|
|
|
return 0;
|
2021-06-22 16:24:28 +02:00
|
|
|
|
2021-10-13 13:37:30 +02:00
|
|
|
/* CCM expects non-empty tag.
|
|
|
|
* CCM* allows empty tag. For CCM* without tag, ignore plaintext length.
|
|
|
|
*/
|
|
|
|
if( ctx->tag_len == 0 )
|
|
|
|
{
|
|
|
|
if( ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT )
|
|
|
|
{
|
|
|
|
ctx->plaintext_len = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 12:44:07 +02:00
|
|
|
|
2014-05-06 12:13:09 +02:00
|
|
|
/*
|
2021-07-12 19:13:52 +02:00
|
|
|
* First block:
|
2021-07-06 12:45:11 +02:00
|
|
|
* 0 .. 0 flags
|
2021-06-22 20:34:20 +02:00
|
|
|
* 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts()
|
2021-07-06 12:45:11 +02:00
|
|
|
* iv_len+1 .. 15 length
|
|
|
|
*
|
|
|
|
* With flags as (bits):
|
|
|
|
* 7 0
|
|
|
|
* 6 add present?
|
|
|
|
* 5 .. 3 (t - 2) / 2
|
|
|
|
* 2 .. 0 q - 1
|
2014-05-06 12:13:09 +02:00
|
|
|
*/
|
2021-07-12 19:13:52 +02:00
|
|
|
ctx->y[0] |= ( ctx->add_len > 0 ) << 6;
|
|
|
|
ctx->y[0] |= ( ( ctx->tag_len - 2 ) / 2 ) << 3;
|
|
|
|
ctx->y[0] |= ctx->q - 1;
|
2021-07-06 12:45:11 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
for( i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8 )
|
2021-08-24 15:14:23 +02:00
|
|
|
ctx->y[15-i] = MBEDTLS_BYTE_0( len_left );
|
2021-06-22 20:34:20 +02:00
|
|
|
|
|
|
|
if( len_left > 0 )
|
2021-07-05 17:09:16 +02:00
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
2018-05-29 12:33:45 +02:00
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2021-07-05 17:09:16 +02:00
|
|
|
}
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
/* Start CBC-MAC with first block*/
|
2021-07-12 19:13:52 +02:00
|
|
|
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
|
|
|
return( ret );
|
|
|
|
}
|
2021-06-22 20:34:20 +02:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_starts( mbedtls_ccm_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const unsigned char *iv,
|
|
|
|
size_t iv_len )
|
|
|
|
{
|
2014-05-06 12:13:09 +02:00
|
|
|
/* Also implies q is within bounds */
|
|
|
|
if( iv_len < 7 || iv_len > 13 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
ctx->mode = mode;
|
2021-06-22 16:24:28 +02:00
|
|
|
ctx->q = 16 - 1 - (unsigned char) iv_len;
|
2015-07-01 15:51:43 +02:00
|
|
|
|
2014-05-06 12:13:09 +02:00
|
|
|
/*
|
2021-06-22 20:34:20 +02:00
|
|
|
* Prepare counter block for encryption:
|
2014-05-06 12:13:09 +02:00
|
|
|
* 0 .. 0 flags
|
|
|
|
* 1 .. iv_len nonce (aka iv)
|
2021-06-22 20:34:20 +02:00
|
|
|
* iv_len+1 .. 15 counter (initially 1)
|
2014-05-06 12:13:09 +02:00
|
|
|
*
|
|
|
|
* With flags as (bits):
|
2021-06-22 20:34:20 +02:00
|
|
|
* 7 .. 3 0
|
2014-05-06 12:13:09 +02:00
|
|
|
* 2 .. 0 q - 1
|
|
|
|
*/
|
2021-06-22 20:34:20 +02:00
|
|
|
memset( ctx->ctr, 0, 16);
|
|
|
|
ctx->ctr[0] = ctx->q - 1;
|
|
|
|
memcpy( ctx->ctr + 1, iv, iv_len );
|
|
|
|
memset( ctx->ctr + 1 + iv_len, 0, ctx->q );
|
|
|
|
ctx->ctr[15] = 1;
|
|
|
|
|
|
|
|
/*
|
2021-07-27 14:03:53 +02:00
|
|
|
* See ccm_calculate_first_block_if_ready() for block layout description
|
2021-06-22 20:34:20 +02:00
|
|
|
*/
|
2021-07-12 19:13:52 +02:00
|
|
|
memcpy( ctx->y + 1, iv, iv_len );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
ctx->state |= CCM_STATE__STARTED;
|
2021-07-27 14:03:53 +02:00
|
|
|
return ccm_calculate_first_block_if_ready(ctx);
|
2021-06-22 20:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_set_lengths( mbedtls_ccm_context *ctx,
|
|
|
|
size_t total_ad_len,
|
|
|
|
size_t plaintext_len,
|
|
|
|
size_t tag_len )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4).
|
|
|
|
*/
|
|
|
|
if( tag_len == 2 || tag_len > 16 || tag_len % 2 != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
if( total_ad_len >= 0xFF00 )
|
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
ctx->plaintext_len = plaintext_len;
|
2021-07-06 12:45:11 +02:00
|
|
|
ctx->add_len = total_ad_len;
|
|
|
|
ctx->tag_len = tag_len;
|
|
|
|
ctx->processed = 0;
|
2021-06-22 20:34:20 +02:00
|
|
|
|
2022-05-18 01:30:44 +02:00
|
|
|
ctx->state |= CCM_STATE__LENGTHS_SET;
|
2021-07-27 14:03:53 +02:00
|
|
|
return ccm_calculate_first_block_if_ready(ctx);
|
2021-06-22 20:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_update_ad( mbedtls_ccm_context *ctx,
|
|
|
|
const unsigned char *add,
|
|
|
|
size_t add_len )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char i;
|
2021-07-06 15:38:35 +02:00
|
|
|
size_t olen, use_len, offset;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-27 16:07:54 +02:00
|
|
|
if( ctx->state & CCM_STATE__ERROR )
|
|
|
|
{
|
2021-09-02 11:50:38 +02:00
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
2021-07-27 16:07:54 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:11:14 +02:00
|
|
|
if( add_len > 0 )
|
2014-05-06 12:13:09 +02:00
|
|
|
{
|
2021-07-28 14:14:58 +02:00
|
|
|
if( ctx->state & CCM_STATE__AUTH_DATA_FINISHED )
|
|
|
|
{
|
2021-09-01 13:26:44 +02:00
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
2021-07-28 14:14:58 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 15:53:41 +02:00
|
|
|
if( !(ctx->state & CCM_STATE__AUTH_DATA_STARTED) )
|
2021-07-06 15:38:35 +02:00
|
|
|
{
|
2021-07-28 14:14:58 +02:00
|
|
|
if ( add_len > ctx->add_len )
|
|
|
|
{
|
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
|
|
|
}
|
|
|
|
|
2021-07-12 19:13:52 +02:00
|
|
|
ctx->y[0] ^= (unsigned char)( ( ctx->add_len >> 8 ) & 0xFF );
|
|
|
|
ctx->y[1] ^= (unsigned char)( ( ctx->add_len ) & 0xFF );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-08-09 15:53:41 +02:00
|
|
|
ctx->state |= CCM_STATE__AUTH_DATA_STARTED;
|
2021-07-06 15:38:35 +02:00
|
|
|
}
|
2021-08-09 15:53:41 +02:00
|
|
|
else if ( ctx->processed + add_len > ctx->add_len )
|
2021-07-28 14:14:58 +02:00
|
|
|
{
|
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
|
|
|
}
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-06 15:38:35 +02:00
|
|
|
while( add_len > 0 )
|
|
|
|
{
|
2021-08-09 15:53:41 +02:00
|
|
|
offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1]
|
|
|
|
* holding total auth data length */
|
2021-07-06 15:38:35 +02:00
|
|
|
use_len = 16 - offset;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-06 15:38:35 +02:00
|
|
|
if( use_len > add_len )
|
|
|
|
use_len = add_len;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-12 19:13:52 +02:00
|
|
|
for( i = 0; i < use_len; i++ )
|
|
|
|
ctx->y[i + offset] ^= add[i];
|
|
|
|
|
2021-07-06 15:38:35 +02:00
|
|
|
ctx->processed += use_len;
|
|
|
|
add_len -= use_len;
|
|
|
|
add += use_len;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-08-09 15:53:41 +02:00
|
|
|
if( use_len + offset == 16 || ctx->processed == ctx->add_len )
|
2021-07-06 15:38:35 +02:00
|
|
|
{
|
2021-07-12 19:13:52 +02:00
|
|
|
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
|
|
|
return( ret );
|
|
|
|
}
|
2021-07-06 15:38:35 +02:00
|
|
|
}
|
2014-05-06 12:13:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 15:53:41 +02:00
|
|
|
if( ctx->processed == ctx->add_len )
|
2021-07-28 14:14:58 +02:00
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__AUTH_DATA_FINISHED;
|
|
|
|
ctx->processed = 0; // prepare for mbedtls_ccm_update()
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 15:38:35 +02:00
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_update( mbedtls_ccm_context *ctx,
|
|
|
|
const unsigned char *input, size_t input_len,
|
|
|
|
unsigned char *output, size_t output_size,
|
|
|
|
size_t *output_len )
|
|
|
|
{
|
2021-08-09 15:55:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-06-22 20:34:20 +02:00
|
|
|
unsigned char i;
|
2021-07-07 13:41:30 +02:00
|
|
|
size_t use_len, offset, olen;
|
2021-06-22 20:34:20 +02:00
|
|
|
|
2021-08-09 11:32:11 +02:00
|
|
|
unsigned char local_output[16];
|
2021-07-28 15:08:47 +02:00
|
|
|
|
2021-07-27 16:07:54 +02:00
|
|
|
if( ctx->state & CCM_STATE__ERROR )
|
|
|
|
{
|
2021-09-02 11:50:38 +02:00
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
2021-07-27 16:07:54 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 13:37:30 +02:00
|
|
|
/* Check against plaintext length only if performing operation with
|
|
|
|
* authentication
|
|
|
|
*/
|
|
|
|
if( ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len )
|
2021-07-28 14:14:58 +02:00
|
|
|
{
|
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:34:20 +02:00
|
|
|
if( output_size < input_len )
|
|
|
|
return( MBEDTLS_ERR_CCM_BAD_INPUT );
|
|
|
|
*output_len = input_len;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-28 15:08:47 +02:00
|
|
|
ret = 0;
|
|
|
|
|
2021-07-07 13:41:30 +02:00
|
|
|
while ( input_len > 0 )
|
2014-05-06 12:13:09 +02:00
|
|
|
{
|
2021-07-07 13:41:30 +02:00
|
|
|
offset = ctx->processed % 16;
|
2014-05-07 13:14:42 +02:00
|
|
|
|
2021-07-07 13:41:30 +02:00
|
|
|
use_len = 16 - offset;
|
2014-05-06 18:06:52 +02:00
|
|
|
|
2021-07-07 13:41:30 +02:00
|
|
|
if( use_len > input_len )
|
|
|
|
use_len = input_len;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-07 13:41:30 +02:00
|
|
|
ctx->processed += use_len;
|
|
|
|
|
2021-07-12 14:52:44 +02:00
|
|
|
if( ctx->mode == MBEDTLS_CCM_ENCRYPT || \
|
|
|
|
ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT )
|
2014-05-06 18:06:52 +02:00
|
|
|
{
|
2021-07-12 19:13:52 +02:00
|
|
|
for( i = 0; i < use_len; i++ )
|
|
|
|
ctx->y[i + offset] ^= input[i];
|
|
|
|
|
2021-07-12 14:52:44 +02:00
|
|
|
if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
|
2021-07-07 13:41:30 +02:00
|
|
|
{
|
2021-07-12 19:13:52 +02:00
|
|
|
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
2021-07-28 15:08:47 +02:00
|
|
|
goto exit;
|
2021-07-12 19:13:52 +02:00
|
|
|
}
|
2021-07-07 13:41:30 +02:00
|
|
|
}
|
2021-07-12 19:13:52 +02:00
|
|
|
|
|
|
|
ret = mbedtls_ccm_crypt( ctx, offset, use_len, input, output );
|
2021-07-12 14:52:44 +02:00
|
|
|
if( ret != 0 )
|
2021-07-28 15:08:47 +02:00
|
|
|
goto exit;
|
2021-07-12 14:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ctx->mode == MBEDTLS_CCM_DECRYPT || \
|
|
|
|
ctx->mode == MBEDTLS_CCM_STAR_DECRYPT )
|
|
|
|
{
|
2021-08-09 16:05:14 +02:00
|
|
|
/* Since output may be in shared memory, we cannot be sure that
|
|
|
|
* it will contain what we wrote to it. Therefore, we should avoid using
|
|
|
|
* it as input to any operations.
|
|
|
|
* Write decrypted data to local_output to avoid using output variable as
|
2021-07-28 15:08:47 +02:00
|
|
|
* input in the XOR operation for Y.
|
|
|
|
*/
|
|
|
|
ret = mbedtls_ccm_crypt( ctx, offset, use_len, input, local_output );
|
2021-07-12 14:52:44 +02:00
|
|
|
if( ret != 0 )
|
2021-07-28 15:08:47 +02:00
|
|
|
goto exit;
|
2021-07-12 14:52:44 +02:00
|
|
|
|
|
|
|
for( i = 0; i < use_len; i++ )
|
2021-07-28 15:08:47 +02:00
|
|
|
ctx->y[i + offset] ^= local_output[i];
|
|
|
|
|
|
|
|
memcpy( output, local_output, use_len );
|
2021-08-09 11:32:11 +02:00
|
|
|
mbedtls_platform_zeroize( local_output, 16 );
|
2021-07-07 13:41:30 +02:00
|
|
|
|
2021-07-12 14:52:44 +02:00
|
|
|
if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
|
2021-07-07 13:41:30 +02:00
|
|
|
{
|
2021-07-12 14:52:44 +02:00
|
|
|
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen ) ) != 0 )
|
|
|
|
{
|
|
|
|
ctx->state |= CCM_STATE__ERROR;
|
2021-07-28 15:08:47 +02:00
|
|
|
goto exit;
|
2021-07-12 14:52:44 +02:00
|
|
|
}
|
2021-07-07 13:41:30 +02:00
|
|
|
}
|
2021-07-12 14:52:44 +02:00
|
|
|
}
|
2014-05-06 18:06:52 +02:00
|
|
|
|
2021-07-12 14:52:44 +02:00
|
|
|
if( use_len + offset == 16 || ctx->processed == ctx->plaintext_len )
|
|
|
|
{
|
2021-07-07 13:41:30 +02:00
|
|
|
for( i = 0; i < ctx->q; i++ )
|
2021-07-12 14:52:44 +02:00
|
|
|
if( ++(ctx->ctr)[15-i] != 0 )
|
|
|
|
break;
|
2021-07-07 13:41:30 +02:00
|
|
|
}
|
2021-07-12 14:52:44 +02:00
|
|
|
|
|
|
|
input_len -= use_len;
|
|
|
|
input += use_len;
|
|
|
|
output += use_len;
|
2014-05-06 12:13:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:08:47 +02:00
|
|
|
exit:
|
2021-08-09 11:32:11 +02:00
|
|
|
mbedtls_platform_zeroize( local_output, 16 );
|
2021-07-28 15:08:47 +02:00
|
|
|
|
|
|
|
return ret;
|
2021-06-22 20:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ccm_finish( mbedtls_ccm_context *ctx,
|
|
|
|
unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
2021-08-09 15:55:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-06-22 20:34:20 +02:00
|
|
|
unsigned char i;
|
|
|
|
|
2021-07-27 16:07:54 +02:00
|
|
|
if( ctx->state & CCM_STATE__ERROR )
|
|
|
|
{
|
2021-08-09 15:37:47 +02:00
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-07-27 16:07:54 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 14:14:58 +02:00
|
|
|
if( ctx->add_len > 0 && !( ctx->state & CCM_STATE__AUTH_DATA_FINISHED ) )
|
|
|
|
{
|
2021-09-01 13:26:44 +02:00
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
2021-07-28 14:14:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len )
|
|
|
|
{
|
2021-09-01 13:26:44 +02:00
|
|
|
return MBEDTLS_ERR_CCM_BAD_INPUT;
|
2021-07-28 14:14:58 +02:00
|
|
|
}
|
|
|
|
|
2014-05-06 12:13:09 +02:00
|
|
|
/*
|
2014-05-07 13:14:42 +02:00
|
|
|
* Authentication: reset counter and crypt/mask internal tag
|
2014-05-06 12:13:09 +02:00
|
|
|
*/
|
2021-06-22 16:24:28 +02:00
|
|
|
for( i = 0; i < ctx->q; i++ )
|
|
|
|
ctx->ctr[15-i] = 0;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2021-07-07 11:05:45 +02:00
|
|
|
ret = mbedtls_ccm_crypt( ctx, 0, 16, ctx->y, ctx->y );
|
|
|
|
if( ret != 0 )
|
|
|
|
return ret;
|
2021-07-28 15:39:51 +02:00
|
|
|
if( tag != NULL )
|
|
|
|
memcpy( tag, ctx->y, tag_len );
|
2021-06-22 20:34:20 +02:00
|
|
|
mbedtls_ccm_clear_state(ctx);
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Authenticated encryption or decryption
|
|
|
|
*/
|
|
|
|
static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, 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 )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t olen;
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ccm_starts( ctx, mode, iv, iv_len ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ccm_set_lengths( ctx, add_len, length, tag_len ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ccm_update_ad( ctx, add, add_len ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ccm_update( ctx, input, length,
|
|
|
|
output, length, &olen ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ccm_finish( ctx, tag, tag_len ) ) != 0 )
|
|
|
|
return( ret );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2021-07-09 12:44:07 +02:00
|
|
|
return( ccm_auth_crypt( ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len,
|
2014-05-06 15:56:07 +02:00
|
|
|
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 )
|
|
|
|
{
|
2021-07-09 12:44:07 +02:00
|
|
|
return( ccm_auth_crypt( ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len,
|
|
|
|
add, add_len, input, output, tag, tag_len ) );
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:10:54 +02:00
|
|
|
/*
|
|
|
|
* Authenticated decryption
|
|
|
|
*/
|
2021-07-09 12:44:07 +02:00
|
|
|
static int mbedtls_ccm_compare_tags(const unsigned char *tag1, const unsigned char *tag2, size_t tag_len)
|
|
|
|
{
|
|
|
|
unsigned char i;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
/* Check tag in "constant-time" */
|
|
|
|
for( diff = 0, i = 0; i < tag_len; i++ )
|
|
|
|
diff |= tag1[i] ^ tag2[i];
|
|
|
|
|
|
|
|
if( diff != 0 )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_CCM_AUTH_FAILED );
|
|
|
|
}
|
2018-05-14 15:31:49 +02:00
|
|
|
|
2021-07-09 12:44:07 +02:00
|
|
|
return( 0 );
|
2018-05-14 15:31:49 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:21:46 +02:00
|
|
|
static int ccm_auth_decrypt( mbedtls_ccm_context *ctx, int mode, 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 )
|
2014-05-06 15:56:07 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-05-06 15:56:07 +02:00
|
|
|
unsigned char check_tag[16];
|
2018-12-11 12:22:16 +01:00
|
|
|
|
2021-07-28 15:21:46 +02:00
|
|
|
if( ( ret = ccm_auth_crypt( ctx, mode, length,
|
2014-05-06 15:56:07 +02:00
|
|
|
iv, iv_len, add, add_len,
|
|
|
|
input, output, check_tag, tag_len ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:44:07 +02:00
|
|
|
if( ( ret = mbedtls_ccm_compare_tags( tag, check_tag, tag_len ) ) != 0 )
|
2014-05-06 15:56:07 +02:00
|
|
|
{
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( output, length );
|
2021-07-09 12:44:07 +02:00
|
|
|
return( ret );
|
2014-05-06 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:21:46 +02:00
|
|
|
int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
|
2018-05-14 15:31:49 +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 )
|
|
|
|
{
|
2021-07-28 15:21:46 +02:00
|
|
|
return ccm_auth_decrypt( ctx, MBEDTLS_CCM_STAR_DECRYPT, length,
|
|
|
|
iv, iv_len, add, add_len,
|
|
|
|
input, output, tag, tag_len );
|
|
|
|
}
|
2021-07-09 12:44:07 +02:00
|
|
|
|
2021-07-28 15:21:46 +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 )
|
|
|
|
{
|
|
|
|
return ccm_auth_decrypt( ctx, MBEDTLS_CCM_DECRYPT, length,
|
|
|
|
iv, iv_len, add, add_len,
|
|
|
|
input, output, tag, tag_len );
|
2018-05-14 15:31:49 +02:00
|
|
|
}
|
2017-04-04 11:37:15 +02:00
|
|
|
#endif /* !MBEDTLS_CCM_ALT */
|
2014-05-02 15:17:29 +02:00
|
|
|
|
2015-04-08 12:49:31 +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
|
2018-04-26 13:15:01 +02:00
|
|
|
#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
|
|
|
|
*/
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char key_test_data[] = {
|
2014-05-06 12:13:09 +02:00
|
|
|
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
|
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
|
|
|
|
};
|
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char iv_test_data[] = {
|
2014-05-06 12:13:09 +02:00
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
|
0x18, 0x19, 0x1a, 0x1b
|
|
|
|
};
|
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char ad_test_data[] = {
|
2014-05-06 12:13:09 +02:00
|
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
|
0x10, 0x11, 0x12, 0x13
|
|
|
|
};
|
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char msg_test_data[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,
|
|
|
|
};
|
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
static const size_t iv_len_test_data [NB_TESTS] = { 7, 8, 12 };
|
|
|
|
static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 };
|
|
|
|
static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 };
|
|
|
|
static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 };
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2018-10-31 20:43:05 +01:00
|
|
|
static const unsigned char res_test_data[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 }
|
|
|
|
};
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ccm_self_test( int verbose )
|
2014-05-02 15:17:29 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ccm_context ctx;
|
2018-04-26 13:15:01 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2015-04-28 18:02:54 +02:00
|
|
|
mbedtls_ccm_init( &ctx );
|
|
|
|
|
2019-01-10 09:10:02 +01:00
|
|
|
if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key_test_data,
|
|
|
|
8 * sizeof key_test_data ) != 0 )
|
2014-05-06 12:13:09 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
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 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " CCM-AES #%u: ", (unsigned int) i + 1 );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2018-04-26 13:15:01 +02:00
|
|
|
memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
|
|
|
|
memset( ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN );
|
2018-10-31 20:43:05 +01:00
|
|
|
memcpy( plaintext, msg_test_data, msg_len_test_data[i] );
|
2018-04-26 13:15:01 +02:00
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len_test_data[i],
|
2018-10-31 20:43:05 +01:00
|
|
|
iv_test_data, iv_len_test_data[i],
|
|
|
|
ad_test_data, add_len_test_data[i],
|
2018-04-26 13:15:01 +02:00
|
|
|
plaintext, ciphertext,
|
2019-01-10 09:10:02 +01:00
|
|
|
ciphertext + msg_len_test_data[i],
|
|
|
|
tag_len_test_data[i] );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
|
|
|
if( ret != 0 ||
|
2019-01-10 09:10:02 +01:00
|
|
|
memcmp( ciphertext, res_test_data[i],
|
|
|
|
msg_len_test_data[i] + tag_len_test_data[i] ) != 0 )
|
2014-05-06 12:13:09 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
|
|
|
return( 1 );
|
|
|
|
}
|
2018-04-26 13:15:01 +02:00
|
|
|
memset( plaintext, 0, CCM_SELFTEST_PT_MAX_LEN );
|
2014-05-06 12:13:09 +02:00
|
|
|
|
2018-10-30 23:00:15 +01:00
|
|
|
ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len_test_data[i],
|
2018-10-31 20:43:05 +01:00
|
|
|
iv_test_data, iv_len_test_data[i],
|
|
|
|
ad_test_data, add_len_test_data[i],
|
2018-04-26 13:15:01 +02:00
|
|
|
ciphertext, plaintext,
|
2019-01-10 09:10:02 +01:00
|
|
|
ciphertext + msg_len_test_data[i],
|
|
|
|
tag_len_test_data[i] );
|
2014-05-06 18:06:52 +02:00
|
|
|
|
|
|
|
if( ret != 0 ||
|
2018-10-31 20:43:05 +01:00
|
|
|
memcmp( plaintext, msg_test_data, msg_len_test_data[i] ) != 0 )
|
2014-05-06 18:06:52 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
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 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2014-05-06 12:13:09 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ccm_free( &ctx );
|
2014-05-02 15:17:29 +02:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2014-05-02 15:17:29 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
|
2014-05-02 15:17:29 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CCM_C */
|