2011-01-06 15:37:30 +00:00
|
|
|
/**
|
|
|
|
* \file cipher.c
|
2014-02-01 21:50:26 +00:00
|
|
|
*
|
2015-01-22 16:11:05 +00:00
|
|
|
* \brief Generic cipher wrapper for mbed TLS
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2015-07-27 09:11:48 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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.
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2011-01-06 15:37:30 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_C)
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/cipher.h"
|
2015-05-26 09:04:15 +00:00
|
|
|
#include "mbedtls/cipher_internal.h"
|
2018-04-17 14:51:09 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
#include "mbedtls/chachapoly.h"
|
2016-05-17 23:33:28 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/gcm.h"
|
2013-08-30 16:34:08 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CCM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/ccm.h"
|
2014-05-13 11:18:17 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-15 22:56:20 +00:00
|
|
|
#if defined(MBEDTLS_CHACHA20_C)
|
|
|
|
#include "mbedtls/chacha20.h"
|
|
|
|
#endif
|
|
|
|
|
2016-10-05 13:09:11 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
|
|
#include "mbedtls/cmac.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
/* Compare the contents of two buffers in constant time.
|
|
|
|
* Returns 0 if the contents are bitwise identical, otherwise returns
|
2016-05-18 16:38:22 +00:00
|
|
|
* a non-zero value.
|
|
|
|
* This is currently only used by GCM and ChaCha20+Poly1305.
|
|
|
|
*/
|
2016-05-17 23:33:28 +00:00
|
|
|
static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
|
|
|
|
{
|
|
|
|
const unsigned char *p1 = (const unsigned char*) v1;
|
|
|
|
const unsigned char *p2 = (const unsigned char*) v2;
|
|
|
|
size_t i;
|
|
|
|
unsigned char diff;
|
|
|
|
|
|
|
|
for( diff = 0, i = 0; i < len; i++ )
|
|
|
|
diff |= p1[i] ^ p2[i];
|
|
|
|
|
|
|
|
return (int)diff;
|
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
2016-05-17 23:33:28 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
static int supported_init = 0;
|
2011-01-16 21:27:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const int *mbedtls_cipher_list( void )
|
2013-09-18 13:12:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_definition_t *def;
|
2013-09-18 13:12:07 +00:00
|
|
|
int *type;
|
2012-07-04 17:10:40 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
if( ! supported_init )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
def = mbedtls_cipher_definitions;
|
|
|
|
type = mbedtls_cipher_supported;
|
2012-07-04 17:10:40 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
while( def->type != 0 )
|
|
|
|
*type++ = (*def++).type;
|
2012-07-04 17:10:40 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
*type = 0;
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
supported_init = 1;
|
|
|
|
}
|
2011-01-16 21:27:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( mbedtls_cipher_supported );
|
2011-01-16 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_definition_t *def;
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
|
2013-09-18 13:12:07 +00:00
|
|
|
if( def->type == cipher_type )
|
|
|
|
return( def->info );
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_definition_t *def;
|
2013-09-18 13:12:07 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
if( NULL == cipher_name )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
|
2015-05-28 15:06:07 +00:00
|
|
|
if( ! strcmp( def->info->name, cipher_name ) )
|
2013-09-18 13:12:07 +00:00
|
|
|
return( def->info );
|
2012-02-06 16:45:10 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
|
2015-06-18 12:58:58 +00:00
|
|
|
int key_bitlen,
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_mode_t mode )
|
2013-09-08 22:08:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_definition_t *def;
|
2013-09-08 22:08:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
|
2013-09-18 13:12:07 +00:00
|
|
|
if( def->info->base->cipher == cipher_id &&
|
2015-06-18 13:28:12 +00:00
|
|
|
def->info->key_bitlen == (unsigned) key_bitlen &&
|
2013-09-18 13:12:07 +00:00
|
|
|
def->info->mode == mode )
|
|
|
|
return( def->info );
|
2013-09-08 22:08:26 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( NULL );
|
2013-09-08 22:08:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
|
2014-07-01 12:53:22 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
|
2014-07-01 12:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
|
2014-07-01 12:53:22 +00:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2016-10-05 13:09:11 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
|
|
if( ctx->cmac_ctx )
|
|
|
|
{
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx->cmac_ctx,
|
|
|
|
sizeof( mbedtls_cmac_context_t ) );
|
2016-10-05 13:09:11 +00:00
|
|
|
mbedtls_free( ctx->cmac_ctx );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-01 12:53:22 +00:00
|
|
|
if( ctx->cipher_ctx )
|
|
|
|
ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
|
|
|
|
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
|
2014-07-01 12:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 11:51:45 +00:00
|
|
|
int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
|
|
|
if( NULL == cipher_info || NULL == ctx )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2011-06-09 14:27:58 +00:00
|
|
|
if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
ctx->cipher_info = cipher_info;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
2013-07-25 10:31:10 +00:00
|
|
|
/*
|
|
|
|
* Ignore possible errors caused by a cipher mode that doesn't use padding
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
|
|
|
(void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
|
2013-08-14 10:21:18 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
(void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
2013-07-25 10:31:10 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
|
2015-06-18 12:58:58 +00:00
|
|
|
int key_bitlen, const mbedtls_operation_t operation )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
|
2015-06-18 13:28:12 +00:00
|
|
|
(int) ctx->cipher_info->key_bitlen != key_bitlen )
|
2014-06-23 10:10:59 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-23 10:10:59 +00:00
|
|
|
}
|
2013-09-16 09:47:43 +00:00
|
|
|
|
2015-06-18 13:28:12 +00:00
|
|
|
ctx->key_bitlen = key_bitlen;
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->operation = operation;
|
|
|
|
|
2011-06-09 14:27:58 +00:00
|
|
|
/*
|
2018-04-22 21:58:07 +00:00
|
|
|
* For OFB, CFB and CTR mode always use the encryption key schedule
|
2011-06-09 14:27:58 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_ENCRYPT == operation ||
|
|
|
|
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
|
2018-04-22 21:58:07 +00:00
|
|
|
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
|
|
|
return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
|
2015-06-18 13:28:12 +00:00
|
|
|
ctx->key_bitlen );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_DECRYPT == operation )
|
2011-06-09 14:27:58 +00:00
|
|
|
return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
|
2015-06-18 13:28:12 +00:00
|
|
|
ctx->key_bitlen );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
|
2013-09-03 11:04:44 +00:00
|
|
|
const unsigned char *iv, size_t iv_len )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-09-03 11:25:52 +00:00
|
|
|
size_t actual_iv_size;
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-10-24 14:54:25 +00:00
|
|
|
/* avoid buffer overflow in ctx->iv */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( iv_len > MBEDTLS_MAX_IV_LENGTH )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2013-10-24 14:54:25 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
|
2013-09-03 11:25:52 +00:00
|
|
|
actual_iv_size = iv_len;
|
|
|
|
else
|
2013-10-24 14:54:25 +00:00
|
|
|
{
|
2013-09-03 11:25:52 +00:00
|
|
|
actual_iv_size = ctx->cipher_info->iv_size;
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2013-10-24 14:54:25 +00:00
|
|
|
/* avoid reading past the end of input buffer */
|
|
|
|
if( actual_iv_size > iv_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-10-24 14:54:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 22:56:20 +00:00
|
|
|
#if defined(MBEDTLS_CHACHA20_C)
|
|
|
|
if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
|
|
|
|
{
|
|
|
|
if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
|
|
|
|
iv,
|
|
|
|
0U ) ) /* Initial counter value */
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-03 11:25:52 +00:00
|
|
|
memcpy( ctx->iv, iv, actual_iv_size );
|
|
|
|
ctx->iv_size = actual_iv_size;
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-03 11:04:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
|
2013-09-03 11:04:44 +00:00
|
|
|
{
|
2013-09-03 11:54:12 +00:00
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->unprocessed_len = 0;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-03 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
|
2013-09-03 11:54:12 +00:00
|
|
|
const unsigned char *ad, size_t ad_len )
|
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2016-05-17 23:33:28 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
|
2013-08-30 16:34:08 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
|
2013-09-03 11:04:44 +00:00
|
|
|
ctx->iv, ctx->iv_size, ad, ad_len );
|
2013-08-30 16:34:08 +00:00
|
|
|
}
|
2016-05-17 23:33:28 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
|
|
|
|
{
|
|
|
|
int result;
|
2018-05-07 08:43:27 +00:00
|
|
|
mbedtls_chachapoly_mode_t mode;
|
2016-05-17 23:33:28 +00:00
|
|
|
|
|
|
|
mode = ( ctx->operation == MBEDTLS_ENCRYPT )
|
2018-05-07 08:43:27 +00:00
|
|
|
? MBEDTLS_CHACHAPOLY_ENCRYPT
|
|
|
|
: MBEDTLS_CHACHAPOLY_DECRYPT;
|
2016-05-17 23:33:28 +00:00
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
|
2016-05-17 23:33:28 +00:00
|
|
|
ctx->iv,
|
|
|
|
mode );
|
|
|
|
if ( result != 0 )
|
|
|
|
return( result );
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
|
2018-05-09 07:34:25 +00:00
|
|
|
ad, ad_len );
|
2016-05-17 23:33:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-08-30 16:34:08 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
|
2014-05-01 12:18:25 +00:00
|
|
|
size_t ilen, unsigned char *output, size_t *olen )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2011-06-09 15:42:02 +00:00
|
|
|
int ret;
|
2016-05-31 13:03:54 +00:00
|
|
|
size_t block_size = 0;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
|
2011-01-20 16:35:05 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-20 16:35:05 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-12-16 14:24:50 +00:00
|
|
|
*olen = 0;
|
2016-05-31 13:03:54 +00:00
|
|
|
block_size = mbedtls_cipher_get_block_size( ctx );
|
2013-12-16 14:24:50 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
|
2013-09-08 21:04:04 +00:00
|
|
|
{
|
2016-05-31 13:03:54 +00:00
|
|
|
if( ilen != block_size )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
|
|
|
|
ctx->operation, input, output ) ) )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-09-08 21:04:04 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-08 21:04:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
|
2013-09-05 11:38:15 +00:00
|
|
|
{
|
|
|
|
*olen = ilen;
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
|
2013-10-11 16:58:55 +00:00
|
|
|
output );
|
2013-09-05 11:38:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-10 10:30:19 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
|
2016-05-15 22:56:20 +00:00
|
|
|
{
|
|
|
|
*olen = ilen;
|
2018-05-10 10:30:19 +00:00
|
|
|
return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
|
|
|
|
ilen, input, output );
|
2016-05-15 22:56:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-05-10 10:30:19 +00:00
|
|
|
if ( 0 == block_size )
|
2016-05-17 23:33:28 +00:00
|
|
|
{
|
2018-05-10 10:30:19 +00:00
|
|
|
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
|
2016-05-17 23:33:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 10:30:19 +00:00
|
|
|
if( input == output &&
|
|
|
|
( ctx->unprocessed_len != 0 || ilen % block_size ) )
|
2016-05-17 23:33:28 +00:00
|
|
|
{
|
2018-05-10 10:30:19 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-05-17 23:33:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-09-13 12:41:45 +00:00
|
|
|
size_t copy_len = 0;
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
/*
|
|
|
|
* If there is not enough data for a full block, cache it.
|
|
|
|
*/
|
2017-04-29 03:01:49 +00:00
|
|
|
if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
|
2017-01-17 23:04:22 +00:00
|
|
|
ilen <= block_size - ctx->unprocessed_len ) ||
|
2017-04-29 03:01:49 +00:00
|
|
|
( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
|
|
|
|
ilen < block_size - ctx->unprocessed_len ) ||
|
2015-04-08 10:49:31 +00:00
|
|
|
( ctx->operation == MBEDTLS_ENCRYPT &&
|
2017-01-17 23:04:22 +00:00
|
|
|
ilen < block_size - ctx->unprocessed_len ) )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
|
|
|
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
|
|
|
|
ilen );
|
|
|
|
|
|
|
|
ctx->unprocessed_len += ilen;
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process cached data first
|
|
|
|
*/
|
2016-05-31 13:03:54 +00:00
|
|
|
if( 0 != ctx->unprocessed_len )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2016-05-31 13:03:54 +00:00
|
|
|
copy_len = block_size - ctx->unprocessed_len;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
|
|
|
|
copy_len );
|
|
|
|
|
2011-06-09 15:42:02 +00:00
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
|
2016-05-31 13:03:54 +00:00
|
|
|
ctx->operation, block_size, ctx->iv,
|
2011-06-09 15:42:02 +00:00
|
|
|
ctx->unprocessed_data, output ) ) )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 13:03:54 +00:00
|
|
|
*olen += block_size;
|
|
|
|
output += block_size;
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->unprocessed_len = 0;
|
|
|
|
|
|
|
|
input += copy_len;
|
|
|
|
ilen -= copy_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache final, incomplete block
|
|
|
|
*/
|
|
|
|
if( 0 != ilen )
|
|
|
|
{
|
2016-05-31 13:03:54 +00:00
|
|
|
if( 0 == block_size )
|
|
|
|
{
|
|
|
|
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
|
|
|
|
}
|
|
|
|
|
2017-04-29 03:01:49 +00:00
|
|
|
/* Encryption: only cache partial blocks
|
|
|
|
* Decryption w/ padding: always keep at least one whole block
|
|
|
|
* Decryption w/o padding: only cache partial blocks
|
|
|
|
*/
|
2016-05-31 13:03:54 +00:00
|
|
|
copy_len = ilen % block_size;
|
2017-04-29 03:01:49 +00:00
|
|
|
if( copy_len == 0 &&
|
|
|
|
ctx->operation == MBEDTLS_DECRYPT &&
|
|
|
|
NULL != ctx->add_padding)
|
|
|
|
{
|
2016-05-31 13:03:54 +00:00
|
|
|
copy_len = block_size;
|
2017-04-29 03:01:49 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
|
|
|
|
copy_len );
|
|
|
|
|
|
|
|
ctx->unprocessed_len += copy_len;
|
|
|
|
ilen -= copy_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process remaining full blocks
|
|
|
|
*/
|
|
|
|
if( ilen )
|
|
|
|
{
|
2011-06-09 15:42:02 +00:00
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
|
|
|
|
ctx->operation, ilen, ctx->iv, input, output ) ) )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2013-08-30 16:34:08 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
*olen += ilen;
|
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
2012-07-04 17:10:40 +00:00
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
|
2011-06-09 14:27:58 +00:00
|
|
|
ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
|
2011-06-09 15:42:02 +00:00
|
|
|
input, output ) ) )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CFB */
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2018-04-22 21:58:07 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
|
|
|
|
{
|
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
|
|
|
|
ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_OFB */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
2011-06-09 15:42:02 +00:00
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
|
2011-06-09 14:27:58 +00:00
|
|
|
ilen, &ctx->unprocessed_len, ctx->iv,
|
2011-06-09 15:42:02 +00:00
|
|
|
ctx->unprocessed_data, input, output ) ) )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2018-04-30 16:17:41 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
|
|
|
|
{
|
|
|
|
if( ctx->unprocessed_len > 0 ) {
|
|
|
|
/* We can only process an entire data unit at a time. */
|
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
|
|
|
|
ctx->operation, ilen, ctx->iv, input, output );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
|
|
|
|
if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
|
2013-08-28 11:50:42 +00:00
|
|
|
{
|
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
|
|
|
|
ilen, input, output ) ) )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2013-08-28 11:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-08-28 11:50:42 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_STREAM */
|
2013-08-28 11:50:42 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
2013-07-26 10:46:02 +00:00
|
|
|
/*
|
|
|
|
* PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
static void add_pkcs_padding( unsigned char *output, size_t output_len,
|
|
|
|
size_t data_len )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t padding_len = output_len - data_len;
|
2013-10-27 16:21:14 +00:00
|
|
|
unsigned char i;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
for( i = 0; i < padding_len; i++ )
|
2011-04-24 08:57:21 +00:00
|
|
|
output[data_len + i] = (unsigned char) padding_len;
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 10:31:10 +00:00
|
|
|
static int get_pkcs_padding( unsigned char *input, size_t input_len,
|
|
|
|
size_t *data_len )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-10-27 16:21:14 +00:00
|
|
|
size_t i, pad_idx;
|
|
|
|
unsigned char padding_len, bad = 0;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2011-01-20 16:35:05 +00:00
|
|
|
if( NULL == input || NULL == data_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
padding_len = input[input_len - 1];
|
2013-10-27 16:21:14 +00:00
|
|
|
*data_len = input_len - padding_len;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-10-27 16:21:14 +00:00
|
|
|
/* Avoid logical || since it results in a branch */
|
|
|
|
bad |= padding_len > input_len;
|
|
|
|
bad |= padding_len == 0;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-10-27 16:21:14 +00:00
|
|
|
/* The number of bytes checked must be independent of padding_len,
|
|
|
|
* so pick input_len, which is usually 8 or 16 (one block) */
|
|
|
|
pad_idx = input_len - padding_len;
|
|
|
|
for( i = 0; i < input_len; i++ )
|
|
|
|
bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
|
2013-07-26 10:46:02 +00:00
|
|
|
/*
|
|
|
|
* One and zeros padding: fill with 80 00 ... 00
|
|
|
|
*/
|
|
|
|
static void add_one_and_zeros_padding( unsigned char *output,
|
|
|
|
size_t output_len, size_t data_len )
|
|
|
|
{
|
|
|
|
size_t padding_len = output_len - data_len;
|
|
|
|
unsigned char i = 0;
|
|
|
|
|
|
|
|
output[data_len] = 0x80;
|
|
|
|
for( i = 1; i < padding_len; i++ )
|
|
|
|
output[data_len + i] = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
|
|
|
|
size_t *data_len )
|
|
|
|
{
|
2013-10-27 17:25:03 +00:00
|
|
|
size_t i;
|
|
|
|
unsigned char done = 0, prev_done, bad;
|
2013-07-26 10:46:02 +00:00
|
|
|
|
|
|
|
if( NULL == input || NULL == data_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2017-12-23 22:40:08 +00:00
|
|
|
bad = 0x80;
|
2013-10-27 17:25:03 +00:00
|
|
|
*data_len = 0;
|
|
|
|
for( i = input_len; i > 0; i-- )
|
|
|
|
{
|
|
|
|
prev_done = done;
|
2017-12-23 22:40:08 +00:00
|
|
|
done |= ( input[i - 1] != 0 );
|
2013-10-27 17:25:03 +00:00
|
|
|
*data_len |= ( i - 1 ) * ( done != prev_done );
|
2017-12-23 22:40:08 +00:00
|
|
|
bad ^= input[i - 1] * ( done != prev_done );
|
2013-10-27 17:25:03 +00:00
|
|
|
}
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2013-07-26 10:46:02 +00:00
|
|
|
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
|
2013-07-26 12:55:18 +00:00
|
|
|
/*
|
|
|
|
* Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
|
|
|
|
*/
|
|
|
|
static void add_zeros_and_len_padding( unsigned char *output,
|
|
|
|
size_t output_len, size_t data_len )
|
|
|
|
{
|
|
|
|
size_t padding_len = output_len - data_len;
|
|
|
|
unsigned char i = 0;
|
|
|
|
|
|
|
|
for( i = 1; i < padding_len; i++ )
|
|
|
|
output[data_len + i - 1] = 0x00;
|
|
|
|
output[output_len - 1] = (unsigned char) padding_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
|
|
|
|
size_t *data_len )
|
|
|
|
{
|
2013-10-27 16:32:43 +00:00
|
|
|
size_t i, pad_idx;
|
|
|
|
unsigned char padding_len, bad = 0;
|
2013-07-26 12:55:18 +00:00
|
|
|
|
|
|
|
if( NULL == input || NULL == data_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-26 12:55:18 +00:00
|
|
|
|
|
|
|
padding_len = input[input_len - 1];
|
2013-10-27 16:32:43 +00:00
|
|
|
*data_len = input_len - padding_len;
|
2013-07-26 12:55:18 +00:00
|
|
|
|
2013-10-27 16:32:43 +00:00
|
|
|
/* Avoid logical || since it results in a branch */
|
|
|
|
bad |= padding_len > input_len;
|
|
|
|
bad |= padding_len == 0;
|
2013-07-26 12:55:18 +00:00
|
|
|
|
2013-10-27 16:32:43 +00:00
|
|
|
/* The number of bytes checked must be independent of padding_len */
|
|
|
|
pad_idx = input_len - padding_len;
|
|
|
|
for( i = 0; i < input_len - 1; i++ )
|
|
|
|
bad |= input[i] * ( i >= pad_idx );
|
2013-07-26 12:55:18 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2013-07-26 12:55:18 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
|
2013-07-26 12:55:18 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
|
2013-07-26 14:05:14 +00:00
|
|
|
/*
|
|
|
|
* Zero padding: fill with 00 ... 00
|
|
|
|
*/
|
|
|
|
static void add_zeros_padding( unsigned char *output,
|
|
|
|
size_t output_len, size_t data_len )
|
|
|
|
{
|
2013-10-11 16:58:55 +00:00
|
|
|
size_t i;
|
2013-07-26 14:05:14 +00:00
|
|
|
|
|
|
|
for( i = data_len; i < output_len; i++ )
|
|
|
|
output[i] = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_zeros_padding( unsigned char *input, size_t input_len,
|
|
|
|
size_t *data_len )
|
|
|
|
{
|
2013-10-27 17:26:39 +00:00
|
|
|
size_t i;
|
|
|
|
unsigned char done = 0, prev_done;
|
|
|
|
|
2013-07-26 14:05:14 +00:00
|
|
|
if( NULL == input || NULL == data_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-26 14:05:14 +00:00
|
|
|
|
2013-10-27 17:26:39 +00:00
|
|
|
*data_len = 0;
|
|
|
|
for( i = input_len; i > 0; i-- )
|
|
|
|
{
|
|
|
|
prev_done = done;
|
|
|
|
done |= ( input[i-1] != 0 );
|
|
|
|
*data_len |= i * ( done != prev_done );
|
|
|
|
}
|
2013-07-26 14:05:14 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-07-26 14:05:14 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
|
2013-07-26 14:05:14 +00:00
|
|
|
|
2013-07-26 14:50:44 +00:00
|
|
|
/*
|
|
|
|
* No padding: don't pad :)
|
|
|
|
*
|
2015-04-08 10:49:31 +00:00
|
|
|
* There is no add_padding function (check for NULL in mbedtls_cipher_finish)
|
2013-07-26 14:50:44 +00:00
|
|
|
* but a trivial get_padding function
|
|
|
|
*/
|
|
|
|
static int get_no_padding( unsigned char *input, size_t input_len,
|
|
|
|
size_t *data_len )
|
|
|
|
{
|
|
|
|
if( NULL == input || NULL == data_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-26 14:50:44 +00:00
|
|
|
|
|
|
|
*data_len = input_len;
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-07-26 14:50:44 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
2013-07-26 14:50:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
|
2013-09-03 14:19:22 +00:00
|
|
|
unsigned char *output, size_t *olen )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
*olen = 0;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
|
2018-04-22 21:58:07 +00:00
|
|
|
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
|
|
|
|
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
|
2018-04-30 16:17:41 +00:00
|
|
|
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
|
2011-06-09 14:27:58 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
|
|
|
|
( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
|
2016-05-15 22:56:20 +00:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
|
2013-09-08 21:04:04 +00:00
|
|
|
{
|
|
|
|
if( ctx->unprocessed_len != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-08 21:04:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
|
|
if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-09-13 12:41:45 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_ENCRYPT == ctx->operation )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-07-26 14:50:44 +00:00
|
|
|
/* check for 'no padding' mode */
|
|
|
|
if( NULL == ctx->add_padding )
|
|
|
|
{
|
|
|
|
if( 0 != ctx->unprocessed_len )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
|
2013-07-26 14:50:44 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-07-26 14:50:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->unprocessed_len );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-07-26 14:50:44 +00:00
|
|
|
/*
|
|
|
|
* For decrypt operations, expect a full block,
|
|
|
|
* or an empty block if no padding
|
|
|
|
*/
|
|
|
|
if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-07-26 14:50:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cipher block */
|
2011-06-09 15:42:02 +00:00
|
|
|
if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
|
2015-04-08 10:49:31 +00:00
|
|
|
ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
|
2011-06-09 15:42:02 +00:00
|
|
|
ctx->unprocessed_data, output ) ) )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( ret );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output size for decryption */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_DECRYPT == ctx->operation )
|
|
|
|
return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
|
2013-07-25 10:31:10 +00:00
|
|
|
olen );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* Set output size for encryption */
|
2015-04-08 10:49:31 +00:00
|
|
|
*olen = mbedtls_cipher_get_block_size( ctx );
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2013-09-13 12:41:45 +00:00
|
|
|
#else
|
|
|
|
((void) output);
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
|
|
int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
|
2013-07-25 10:31:10 +00:00
|
|
|
{
|
|
|
|
if( NULL == ctx ||
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
|
2013-07-25 10:31:10 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-25 10:31:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 10:04:26 +00:00
|
|
|
switch( mode )
|
2013-07-25 10:31:10 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
|
|
|
case MBEDTLS_PADDING_PKCS7:
|
2013-07-25 10:31:10 +00:00
|
|
|
ctx->add_padding = add_pkcs_padding;
|
|
|
|
ctx->get_padding = get_pkcs_padding;
|
2013-08-14 10:04:26 +00:00
|
|
|
break;
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
|
|
|
|
case MBEDTLS_PADDING_ONE_AND_ZEROS:
|
2013-07-26 10:46:02 +00:00
|
|
|
ctx->add_padding = add_one_and_zeros_padding;
|
|
|
|
ctx->get_padding = get_one_and_zeros_padding;
|
2013-08-14 10:04:26 +00:00
|
|
|
break;
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
|
|
|
|
case MBEDTLS_PADDING_ZEROS_AND_LEN:
|
2013-07-26 12:55:18 +00:00
|
|
|
ctx->add_padding = add_zeros_and_len_padding;
|
|
|
|
ctx->get_padding = get_zeros_and_len_padding;
|
2013-08-14 10:04:26 +00:00
|
|
|
break;
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
|
|
|
|
case MBEDTLS_PADDING_ZEROS:
|
2013-07-26 14:05:14 +00:00
|
|
|
ctx->add_padding = add_zeros_padding;
|
|
|
|
ctx->get_padding = get_zeros_padding;
|
2013-08-14 10:04:26 +00:00
|
|
|
break;
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
case MBEDTLS_PADDING_NONE:
|
2013-07-26 14:50:44 +00:00
|
|
|
ctx->add_padding = NULL;
|
|
|
|
ctx->get_padding = get_no_padding;
|
2013-08-14 10:04:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2013-07-26 14:50:44 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-07-25 10:31:10 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
2013-07-25 10:31:10 +00:00
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
|
2013-09-03 14:19:22 +00:00
|
|
|
unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_ENCRYPT != ctx->operation )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2016-05-17 23:33:28 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
|
|
|
|
return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
|
2016-05-17 23:33:28 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
|
|
|
|
{
|
|
|
|
/* Don't allow truncated MAC for Poly1305 */
|
|
|
|
if ( tag_len != 16U )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
|
2016-05-17 23:33:28 +00:00
|
|
|
tag );
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-03 14:35:53 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2013-09-03 14:19:22 +00:00
|
|
|
}
|
2014-05-01 11:03:14 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
|
2013-09-03 14:19:22 +00:00
|
|
|
const unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
2016-05-17 23:33:28 +00:00
|
|
|
unsigned char check_tag[16];
|
2013-09-03 14:19:22 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info ||
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_DECRYPT != ctx->operation )
|
2013-09-03 14:35:53 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:35:53 +00:00
|
|
|
}
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2016-05-17 23:33:28 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
|
2013-09-03 14:35:53 +00:00
|
|
|
{
|
|
|
|
if( tag_len > sizeof( check_tag ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
|
2013-10-11 16:58:55 +00:00
|
|
|
check_tag, tag_len ) ) )
|
|
|
|
{
|
2013-09-03 14:35:53 +00:00
|
|
|
return( ret );
|
2013-10-11 16:58:55 +00:00
|
|
|
}
|
2013-09-03 14:35:53 +00:00
|
|
|
|
|
|
|
/* Check the tag in "constant-time" */
|
2016-05-17 23:33:28 +00:00
|
|
|
if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
|
|
|
|
{
|
|
|
|
/* Don't allow truncated MAC for Poly1305 */
|
|
|
|
if ( tag_len != sizeof( check_tag ) )
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
2018-05-07 08:43:27 +00:00
|
|
|
ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
|
2016-05-17 23:33:28 +00:00
|
|
|
check_tag );
|
|
|
|
if ( ret != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2016-05-17 23:33:28 +00:00
|
|
|
/* Check the tag in "constant-time" */
|
|
|
|
if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2013-09-03 14:19:22 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2014-05-12 11:46:08 +00:00
|
|
|
/*
|
|
|
|
* Packet-oriented wrapper for non-AEAD modes
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
|
2014-05-12 11:46:08 +00:00
|
|
|
const unsigned char *iv, size_t iv_len,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output, size_t *olen )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t finish_olen;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
|
2014-05-12 11:46:08 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
|
2014-05-12 11:46:08 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
|
2014-05-12 11:46:08 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
|
2014-05-12 11:46:08 +00:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
*olen += finish_olen;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_AEAD)
|
2014-05-13 10:19:29 +00:00
|
|
|
/*
|
|
|
|
* Packet-oriented encryption for AEAD modes
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
|
2014-05-13 10:19:29 +00:00
|
|
|
const unsigned char *iv, size_t iv_len,
|
|
|
|
const unsigned char *ad, size_t ad_len,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output, size_t *olen,
|
|
|
|
unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
|
|
|
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
|
2014-05-13 10:19:29 +00:00
|
|
|
{
|
|
|
|
*olen = ilen;
|
2015-04-08 10:49:31 +00:00
|
|
|
return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
|
2014-05-13 10:19:29 +00:00
|
|
|
iv, iv_len, ad, ad_len, input, output,
|
|
|
|
tag_len, tag ) );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
|
2014-05-13 11:18:17 +00:00
|
|
|
{
|
|
|
|
*olen = ilen;
|
2015-04-08 10:49:31 +00:00
|
|
|
return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
|
2014-05-13 11:18:17 +00:00
|
|
|
iv, iv_len, ad, ad_len, input, output,
|
|
|
|
tag, tag_len ) );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CCM_C */
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
|
|
|
|
{
|
2018-05-08 07:38:09 +00:00
|
|
|
/* ChachaPoly has fixed length nonce and MAC (tag) */
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( ( iv_len != ctx->cipher_info->iv_size ) ||
|
2018-05-08 07:38:09 +00:00
|
|
|
( tag_len != 16U ) )
|
2016-05-17 23:33:28 +00:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
2018-06-04 10:18:19 +00:00
|
|
|
return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
|
2018-05-08 07:38:09 +00:00
|
|
|
ilen, iv, ad, ad_len, input, output, tag ) );
|
2016-05-17 23:33:28 +00:00
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2014-05-13 10:19:29 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2014-05-13 10:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Packet-oriented decryption for AEAD modes
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
|
2014-05-13 10:19:29 +00:00
|
|
|
const unsigned char *iv, size_t iv_len,
|
|
|
|
const unsigned char *ad, size_t ad_len,
|
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output, size_t *olen,
|
|
|
|
const unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
|
|
|
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
|
2014-05-13 10:19:29 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*olen = ilen;
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
|
2014-05-13 10:19:29 +00:00
|
|
|
iv, iv_len, ad, ad_len,
|
|
|
|
tag, tag_len, input, output );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
|
|
|
|
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
|
2014-05-13 10:19:29 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_GCM_C */
|
|
|
|
#if defined(MBEDTLS_CCM_C)
|
|
|
|
if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
|
2014-05-13 11:18:17 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*olen = ilen;
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
|
2014-05-13 11:18:17 +00:00
|
|
|
iv, iv_len, ad, ad_len,
|
|
|
|
input, output, tag, tag_len );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
|
|
|
|
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
|
2014-05-13 11:18:17 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CCM_C */
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-05-08 07:38:09 +00:00
|
|
|
/* ChachaPoly has fixed length nonce and MAC (tag) */
|
2016-05-17 23:33:28 +00:00
|
|
|
if ( ( iv_len != ctx->cipher_info->iv_size ) ||
|
2018-05-08 07:38:09 +00:00
|
|
|
( tag_len != 16U ) )
|
2016-05-17 23:33:28 +00:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = ilen;
|
2018-05-08 07:38:09 +00:00
|
|
|
ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
|
|
|
|
iv, ad, ad_len, tag, input, output );
|
2016-05-17 23:33:28 +00:00
|
|
|
|
2018-05-08 07:38:09 +00:00
|
|
|
if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
|
|
|
|
ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
|
2016-05-17 23:33:28 +00:00
|
|
|
|
2018-05-08 07:38:09 +00:00
|
|
|
return( ret );
|
2016-05-17 23:33:28 +00:00
|
|
|
}
|
2018-05-07 08:43:27 +00:00
|
|
|
#endif /* MBEDTLS_CHACHAPOLY_C */
|
2014-05-13 10:19:29 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2014-05-13 10:19:29 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_AEAD */
|
2014-05-13 10:19:29 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_C */
|