Update external polarssl to 1.3.8

There were some fixes back on March 13th, 2014 for fixing compiling on MIPS64.
Also some fixes on June 25th, 2014 for SPARC64 fixes.

Probably more things, but those are what I care about.
This commit is contained in:
Ryan Houdek
2014-09-08 01:58:33 -05:00
parent 6ea82790ba
commit a48e284317
130 changed files with 12346 additions and 4632 deletions

View File

@ -1,11 +1,11 @@
/**
* \file cipher.c
*
*
* \brief Generic cipher wrapper for PolarSSL
*
* \author Adriaan de Jong <dejong@fox-it.com>
*
* Copyright (C) 2006-2013, Brainspark B.V.
* Copyright (C) 2006-2014, Brainspark B.V.
*
* This file is part of PolarSSL (http://www.polarssl.org)
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
@ -27,7 +27,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if !defined(POLARSSL_CONFIG_FILE)
#include "polarssl/config.h"
#else
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_CIPHER_C)
@ -38,6 +42,10 @@
#include "polarssl/gcm.h"
#endif
#if defined(POLARSSL_CCM_C)
#include "polarssl/ccm.h"
#endif
#include <stdlib.h>
#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
@ -49,6 +57,11 @@
#define strcasecmp _stricmp
#endif
/* Implementation that should never be optimized out by the compiler */
static void polarssl_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
static int supported_init = 0;
const int *cipher_list( void )
@ -69,7 +82,7 @@ const int *cipher_list( void )
supported_init = 1;
}
return supported_ciphers;
return( supported_ciphers );
}
const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
@ -80,7 +93,7 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
if( def->type == cipher_type )
return( def->info );
return NULL;
return( NULL );
}
const cipher_info_t *cipher_info_from_string( const char *cipher_name )
@ -88,13 +101,13 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
const cipher_definition_t *def;
if( NULL == cipher_name )
return NULL;
return( NULL );
for( def = cipher_definitions; def->info != NULL; def++ )
if( ! strcasecmp( def->info->name, cipher_name ) )
return( def->info );
return NULL;
return( NULL );
}
const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
@ -109,18 +122,34 @@ const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
def->info->mode == mode )
return( def->info );
return NULL;
return( NULL );
}
void cipher_init( cipher_context_t *ctx )
{
memset( ctx, 0, sizeof( cipher_context_t ) );
}
void cipher_free( cipher_context_t *ctx )
{
if( ctx == NULL )
return;
if( ctx->cipher_ctx )
ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
polarssl_zeroize( ctx, sizeof(cipher_context_t) );
}
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
{
if( NULL == cipher_info || NULL == ctx )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
memset( ctx, 0, sizeof( cipher_context_t ) );
if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
ctx->cipher_info = cipher_info;
@ -135,27 +164,28 @@ int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
#endif
#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
return 0;
return( 0 );
}
/* Deprecated, redirects to cipher_free() */
int cipher_free_ctx( cipher_context_t *ctx )
{
if( ctx == NULL || ctx->cipher_info == NULL )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
cipher_free( ctx );
ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
return 0;
return( 0 );
}
int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
int key_length, const operation_t operation )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
if( (int) ctx->cipher_info->key_length != key_length )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
(int) ctx->cipher_info->key_length != key_length )
{
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
ctx->key_length = key_length;
ctx->operation = operation;
@ -175,7 +205,7 @@ int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
ctx->key_length );
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
int cipher_set_iv( cipher_context_t *ctx,
@ -184,13 +214,13 @@ int cipher_set_iv( cipher_context_t *ctx,
size_t actual_iv_size;
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
/* avoid buffer overflow in ctx->iv */
if( iv_len > POLARSSL_MAX_IV_LENGTH )
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
if( ctx->cipher_info->accepts_variable_iv_size )
if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
actual_iv_size = iv_len;
else
{
@ -198,52 +228,50 @@ int cipher_set_iv( cipher_context_t *ctx,
/* avoid reading past the end of input buffer */
if( actual_iv_size > iv_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
memcpy( ctx->iv, iv, actual_iv_size );
ctx->iv_size = actual_iv_size;
return 0;
return( 0 );
}
int cipher_reset( cipher_context_t *ctx )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
ctx->unprocessed_len = 0;
return 0;
return( 0 );
}
#if defined(POLARSSL_CIPHER_MODE_AEAD)
#if defined(POLARSSL_GCM_C)
int cipher_update_ad( cipher_context_t *ctx,
const unsigned char *ad, size_t ad_len )
{
if( NULL == ctx || NULL == ctx->cipher_info )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
ctx->iv, ctx->iv_size, ad, ad_len );
}
#endif
return 0;
return( 0 );
}
#endif /* POLARSSL_CIPHER_MODE_AEAD */
#endif /* POLARSSL_GCM_C */
int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen )
int cipher_update( cipher_context_t *ctx, const unsigned char *input,
size_t ilen, unsigned char *output, size_t *olen )
{
int ret;
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
*olen = 0;
@ -251,17 +279,17 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
{
if( ilen != cipher_get_block_size( ctx ) )
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
*olen = ilen;
if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
ctx->operation, input, output ) ) )
{
return ret;
return( ret );
}
return 0;
return( 0 );
}
#if defined(POLARSSL_GCM_C)
@ -276,7 +304,7 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
if( input == output &&
( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
#if defined(POLARSSL_CIPHER_MODE_CBC)
@ -296,7 +324,7 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
ilen );
ctx->unprocessed_len += ilen;
return 0;
return( 0 );
}
/*
@ -313,7 +341,7 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
ctx->unprocessed_data, output ) ) )
{
return ret;
return( ret );
}
*olen += cipher_get_block_size( ctx );
@ -331,7 +359,7 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
{
copy_len = ilen % cipher_get_block_size( ctx );
if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
copy_len = cipher_get_block_size(ctx);
copy_len = cipher_get_block_size( ctx );
memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
copy_len );
@ -348,13 +376,13 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
ctx->operation, ilen, ctx->iv, input, output ) ) )
{
return ret;
return( ret );
}
*olen += ilen;
}
return 0;
return( 0 );
}
#endif /* POLARSSL_CIPHER_MODE_CBC */
@ -365,14 +393,14 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
input, output ) ) )
{
return ret;
return( ret );
}
*olen = ilen;
return 0;
return( 0 );
}
#endif
#endif /* POLARSSL_CIPHER_MODE_CFB */
#if defined(POLARSSL_CIPHER_MODE_CTR)
if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
@ -381,14 +409,14 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
ilen, &ctx->unprocessed_len, ctx->iv,
ctx->unprocessed_data, input, output ) ) )
{
return ret;
return( ret );
}
*olen = ilen;
return 0;
return( 0 );
}
#endif
#endif /* POLARSSL_CIPHER_MODE_CTR */
#if defined(POLARSSL_CIPHER_MODE_STREAM)
if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
@ -396,16 +424,16 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
ilen, input, output ) ) )
{
return ret;
return( ret );
}
*olen = ilen;
return 0;
return( 0 );
}
#endif
#endif /* POLARSSL_CIPHER_MODE_STREAM */
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
}
#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
@ -430,7 +458,7 @@ static int get_pkcs_padding( unsigned char *input, size_t input_len,
unsigned char padding_len, bad = 0;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
padding_len = input[input_len - 1];
*data_len = input_len - padding_len;
@ -445,7 +473,7 @@ static int get_pkcs_padding( unsigned char *input, size_t input_len,
for( i = 0; i < input_len; i++ )
bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
}
#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
@ -471,7 +499,7 @@ static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
unsigned char done = 0, prev_done, bad;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
bad = 0xFF;
*data_len = 0;
@ -483,7 +511,7 @@ static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
}
return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
}
#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
@ -510,7 +538,7 @@ static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
unsigned char padding_len, bad = 0;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
padding_len = input[input_len - 1];
*data_len = input_len - padding_len;
@ -524,7 +552,7 @@ static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
for( i = 0; i < input_len - 1; i++ )
bad |= input[i] * ( i >= pad_idx );
return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
}
#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
@ -548,7 +576,7 @@ static int get_zeros_padding( unsigned char *input, size_t input_len,
unsigned char done = 0, prev_done;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
*data_len = 0;
for( i = input_len; i > 0; i-- )
@ -558,7 +586,7 @@ static int get_zeros_padding( unsigned char *input, size_t input_len,
*data_len |= i * ( done != prev_done );
}
return 0;
return( 0 );
}
#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
@ -572,11 +600,11 @@ static int get_no_padding( unsigned char *input, size_t input_len,
size_t *data_len )
{
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
*data_len = input_len;
return 0;
return( 0 );
}
#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
@ -584,7 +612,7 @@ int cipher_finish( cipher_context_t *ctx,
unsigned char *output, size_t *olen )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
*olen = 0;
@ -593,15 +621,15 @@ int cipher_finish( cipher_context_t *ctx,
POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
{
return 0;
return( 0 );
}
if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
{
if( ctx->unprocessed_len != 0 )
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
return 0;
return( 0 );
}
#if defined(POLARSSL_CIPHER_MODE_CBC)
@ -615,24 +643,24 @@ int cipher_finish( cipher_context_t *ctx,
if( NULL == ctx->add_padding )
{
if( 0 != ctx->unprocessed_len )
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
return 0;
return( 0 );
}
ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
ctx->unprocessed_len );
}
else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
{
/*
* For decrypt operations, expect a full block,
* or an empty block if no padding
*/
if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
return 0;
return( 0 );
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
}
/* cipher block */
@ -640,7 +668,7 @@ int cipher_finish( cipher_context_t *ctx,
ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
ctx->unprocessed_data, output ) ) )
{
return ret;
return( ret );
}
/* Set output size for decryption */
@ -650,13 +678,13 @@ int cipher_finish( cipher_context_t *ctx,
/* Set output size for encryption */
*olen = cipher_get_block_size( ctx );
return 0;
return( 0 );
}
#else
((void) output);
#endif /* POLARSSL_CIPHER_MODE_CBC */
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
}
#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
@ -665,7 +693,7 @@ int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
if( NULL == ctx ||
POLARSSL_MODE_CBC != ctx->cipher_info->mode )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
switch( mode )
@ -700,31 +728,29 @@ int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
break;
default:
return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
}
return 0;
return( 0 );
}
#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
#if defined(POLARSSL_CIPHER_MODE_AEAD)
#if defined(POLARSSL_GCM_C)
int cipher_write_tag( cipher_context_t *ctx,
unsigned char *tag, size_t tag_len )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
if( POLARSSL_ENCRYPT != ctx->operation )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
#endif
return 0;
return( 0 );
}
int cipher_check_tag( cipher_context_t *ctx,
const unsigned char *tag, size_t tag_len )
{
@ -733,10 +759,9 @@ int cipher_check_tag( cipher_context_t *ctx,
if( NULL == ctx || NULL == ctx->cipher_info ||
POLARSSL_DECRYPT != ctx->operation )
{
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
}
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
unsigned char check_tag[16];
@ -744,7 +769,7 @@ int cipher_check_tag( cipher_context_t *ctx,
int diff;
if( tag_len > sizeof( check_tag ) )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
check_tag, tag_len ) ) )
@ -761,24 +786,125 @@ int cipher_check_tag( cipher_context_t *ctx,
return( 0 );
}
#endif
return( 0 );
}
#endif /* POLARSSL_GCM_C */
/*
* Packet-oriented wrapper for non-AEAD modes
*/
int cipher_crypt( cipher_context_t *ctx,
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;
if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
return( ret );
if( ( ret = cipher_reset( ctx ) ) != 0 )
return( ret );
if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
return( ret );
if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
return( ret );
*olen += finish_olen;
return( 0 );
}
#if defined(POLARSSL_CIPHER_MODE_AEAD)
/*
* Packet-oriented encryption for AEAD modes
*/
int cipher_auth_encrypt( cipher_context_t *ctx,
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 )
{
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
*olen = ilen;
return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
iv, iv_len, ad, ad_len, input, output,
tag_len, tag ) );
}
#endif /* POLARSSL_GCM_C */
#if defined(POLARSSL_CCM_C)
if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
{
*olen = ilen;
return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len, input, output,
tag, tag_len ) );
}
#endif /* POLARSSL_CCM_C */
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
}
/*
* Packet-oriented decryption for AEAD modes
*/
int cipher_auth_decrypt( cipher_context_t *ctx,
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 )
{
#if defined(POLARSSL_GCM_C)
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
{
int ret;
*olen = ilen;
ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len,
tag, tag_len, input, output );
if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
return( ret );
}
#endif /* POLARSSL_GCM_C */
#if defined(POLARSSL_CCM_C)
if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
{
int ret;
*olen = ilen;
ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
iv, iv_len, ad, ad_len,
input, output, tag, tag_len );
if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
return( ret );
}
#endif /* POLARSSL_CCM_C */
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
}
#endif /* POLARSSL_CIPHER_MODE_AEAD */
#if defined(POLARSSL_SELF_TEST)
#include <stdio.h>
#define ASSERT(x) if (!(x)) { \
printf( "failed with %i at %s\n", value, (#x) ); \
return( 1 ); \
}
/*
* Checkup routine
*/
int cipher_self_test( int verbose )
{
((void) verbose);
@ -786,6 +912,6 @@ int cipher_self_test( int verbose )
return( 0 );
}
#endif
#endif /* POLARSSL_SELF_TEST */
#endif
#endif /* POLARSSL_CIPHER_C */