2011-01-06 15:37:30 +00:00
|
|
|
/**
|
|
|
|
* \file cipher.c
|
2014-02-01 21:50:26 +00:00
|
|
|
*
|
2011-01-06 15:37:30 +00:00
|
|
|
* \brief Generic cipher wrapper for PolarSSL
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2014-02-01 21:50:26 +00:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2011-01-06 15:37:30 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2011-01-06 15:37:30 +00:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_CIPHER_C)
|
|
|
|
|
|
|
|
#include "polarssl/cipher.h"
|
|
|
|
#include "polarssl/cipher_wrap.h"
|
|
|
|
|
2013-08-30 16:34:08 +00:00
|
|
|
#if defined(POLARSSL_GCM_C)
|
|
|
|
#include "polarssl/gcm.h"
|
|
|
|
#endif
|
|
|
|
|
2014-05-13 11:18:17 +00:00
|
|
|
#if defined(POLARSSL_CCM_C)
|
|
|
|
#include "polarssl/ccm.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-08-28 14:36:14 +00:00
|
|
|
#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
|
2013-08-28 11:50:42 +00:00
|
|
|
#define POLARSSL_CIPHER_MODE_STREAM
|
|
|
|
#endif
|
|
|
|
|
2013-10-29 14:22:54 +00:00
|
|
|
#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2011-04-18 03:47:52 +00:00
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 15:20:13 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
static int supported_init = 0;
|
2011-01-16 21:27:44 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
const int *cipher_list( void )
|
|
|
|
{
|
|
|
|
const cipher_definition_t *def;
|
|
|
|
int *type;
|
2012-07-04 17:10:40 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
if( ! supported_init )
|
|
|
|
{
|
|
|
|
def = cipher_definitions;
|
|
|
|
type = supported_ciphers;
|
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
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( supported_ciphers );
|
2011-01-16 21:27:44 +00:00
|
|
|
}
|
|
|
|
|
2012-01-14 18:24:43 +00:00
|
|
|
const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
|
2011-01-06 15:37:30 +00:00
|
|
|
{
|
2013-09-18 13:12:07 +00:00
|
|
|
const cipher_definition_t *def;
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
for( def = cipher_definitions; def->info != NULL; def++ )
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const cipher_info_t *cipher_info_from_string( const char *cipher_name )
|
|
|
|
{
|
2013-09-18 13:12:07 +00:00
|
|
|
const cipher_definition_t *def;
|
|
|
|
|
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
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
for( def = cipher_definitions; def->info != NULL; def++ )
|
|
|
|
if( ! strcasecmp( def->info->name, cipher_name ) )
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-08 22:08:26 +00:00
|
|
|
const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
|
|
|
|
int key_length,
|
|
|
|
const cipher_mode_t mode )
|
|
|
|
{
|
2013-09-18 13:12:07 +00:00
|
|
|
const cipher_definition_t *def;
|
2013-09-08 22:08:26 +00:00
|
|
|
|
2013-09-18 13:12:07 +00:00
|
|
|
for( def = cipher_definitions; def->info != NULL; def++ )
|
|
|
|
if( def->info->base->cipher == cipher_id &&
|
|
|
|
def->info->key_length == (unsigned) key_length &&
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
|
|
|
|
{
|
|
|
|
if( NULL == cipher_info || NULL == ctx )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2012-04-26 10:09:35 +00:00
|
|
|
memset( ctx, 0, sizeof( 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() ) )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
ctx->cipher_info = cipher_info;
|
|
|
|
|
2013-09-13 12:41:45 +00:00
|
|
|
#if defined(POLARSSL_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
|
|
|
|
*/
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
|
2013-07-25 10:31:10 +00:00
|
|
|
(void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
|
2013-08-14 10:21:18 +00:00
|
|
|
#else
|
|
|
|
(void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
|
|
|
|
#endif
|
2013-09-13 12:41:45 +00:00
|
|
|
#endif /* POLARSSL_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
|
|
|
}
|
|
|
|
|
|
|
|
int cipher_free_ctx( cipher_context_t *ctx )
|
|
|
|
{
|
|
|
|
if( ctx == NULL || ctx->cipher_info == NULL )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2011-06-09 14:27:58 +00:00
|
|
|
ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
|
2014-06-13 15:20:13 +00:00
|
|
|
polarssl_zeroize( ctx, sizeof(cipher_context_t) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2014-06-23 10:10:59 +00:00
|
|
|
if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
|
|
|
|
(int) ctx->cipher_info->key_length != key_length )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-23 10:10:59 +00:00
|
|
|
}
|
2013-09-16 09:47:43 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->key_length = key_length;
|
|
|
|
ctx->operation = operation;
|
|
|
|
|
2011-06-09 14:27:58 +00:00
|
|
|
/*
|
2012-07-04 17:10:40 +00:00
|
|
|
* For CFB and CTR mode always use the encryption key schedule
|
2011-06-09 14:27:58 +00:00
|
|
|
*/
|
|
|
|
if( POLARSSL_ENCRYPT == operation ||
|
2012-07-04 17:10:40 +00:00
|
|
|
POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
|
2011-06-09 14:27:58 +00:00
|
|
|
POLARSSL_MODE_CTR == ctx->cipher_info->mode )
|
|
|
|
{
|
|
|
|
return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->key_length );
|
2011-06-09 14:27:58 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2011-06-09 14:27:58 +00:00
|
|
|
if( POLARSSL_DECRYPT == operation )
|
|
|
|
return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->key_length );
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 11:04:44 +00:00
|
|
|
int cipher_set_iv( cipher_context_t *ctx,
|
|
|
|
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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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 */
|
|
|
|
if( iv_len > POLARSSL_MAX_IV_LENGTH )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2013-10-24 14:54:25 +00:00
|
|
|
|
2014-06-23 09:33:18 +00:00
|
|
|
if( ( ctx->cipher_info->flags & POLARSSL_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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-10-24 14:54:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-03 11:54:12 +00:00
|
|
|
int cipher_reset( 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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
|
|
|
|
2014-06-24 13:26:28 +00:00
|
|
|
#if defined(POLARSSL_GCM_C)
|
2013-09-03 11:54:12 +00:00
|
|
|
int cipher_update_ad( cipher_context_t *ctx,
|
|
|
|
const unsigned char *ad, size_t ad_len )
|
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2013-08-30 16:34:08 +00:00
|
|
|
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
|
|
|
|
{
|
2013-10-11 16:58:55 +00:00
|
|
|
return gcm_starts( (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
|
|
|
}
|
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( 0 );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif /* POLARSSL_GCM_C */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2014-05-01 12:18:25 +00:00
|
|
|
int cipher_update( cipher_context_t *ctx, const unsigned char *input,
|
|
|
|
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;
|
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
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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;
|
|
|
|
|
2013-09-08 21:04:04 +00:00
|
|
|
if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
|
|
|
|
{
|
|
|
|
if( ilen != cipher_get_block_size( ctx ) )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
|
|
|
|
2013-09-05 11:38:15 +00:00
|
|
|
#if defined(POLARSSL_GCM_C)
|
2013-09-08 21:04:04 +00:00
|
|
|
if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
|
2013-09-05 11:38:15 +00:00
|
|
|
{
|
|
|
|
*olen = ilen;
|
2013-10-11 16:58:55 +00:00
|
|
|
return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
|
|
|
|
output );
|
2013-09-05 11:38:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
if( input == output &&
|
|
|
|
( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-01-07 17:20:04 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-09-13 12:41:45 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_CBC)
|
2013-09-05 11:38:15 +00:00
|
|
|
if( ctx->cipher_info->mode == POLARSSL_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.
|
|
|
|
*/
|
|
|
|
if( ( ctx->operation == POLARSSL_DECRYPT &&
|
|
|
|
ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
|
|
|
|
( ctx->operation == POLARSSL_ENCRYPT &&
|
|
|
|
ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
if( ctx->unprocessed_len != 0 )
|
|
|
|
{
|
|
|
|
copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
|
|
|
|
|
|
|
|
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,
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->operation, 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
|
|
|
}
|
|
|
|
|
|
|
|
*olen += cipher_get_block_size( ctx );
|
|
|
|
output += cipher_get_block_size( ctx );
|
|
|
|
ctx->unprocessed_len = 0;
|
|
|
|
|
|
|
|
input += copy_len;
|
|
|
|
ilen -= copy_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache final, incomplete block
|
|
|
|
*/
|
|
|
|
if( 0 != ilen )
|
|
|
|
{
|
|
|
|
copy_len = ilen % cipher_get_block_size( ctx );
|
|
|
|
if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
|
2014-06-17 14:39:18 +00:00
|
|
|
copy_len = cipher_get_block_size( ctx );
|
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
|
|
|
}
|
2013-09-13 12:41:45 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_CBC */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
2012-07-04 17:10:40 +00:00
|
|
|
if( ctx->cipher_info->mode == POLARSSL_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
|
|
|
}
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_CFB */
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2013-01-07 17:20:04 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_CTR)
|
2011-06-09 14:27:58 +00:00
|
|
|
if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
|
|
|
|
{
|
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
|
|
|
}
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_CTR */
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2013-08-28 11:50:42 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_STREAM)
|
|
|
|
if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_STREAM */
|
2013-08-28 11:50:42 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 12:41:45 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2013-10-27 17:25:03 +00:00
|
|
|
bad = 0xFF;
|
|
|
|
*data_len = 0;
|
|
|
|
for( i = input_len; i > 0; i-- )
|
|
|
|
{
|
|
|
|
prev_done = done;
|
|
|
|
done |= ( input[i-1] != 0 );
|
|
|
|
*data_len |= ( i - 1 ) * ( done != prev_done );
|
|
|
|
bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
|
|
|
|
}
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2013-07-26 10:46:02 +00:00
|
|
|
|
|
|
|
}
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
|
2013-07-26 10:46:02 +00:00
|
|
|
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
2013-07-26 12:55:18 +00:00
|
|
|
}
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
|
2013-07-26 12:55:18 +00:00
|
|
|
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
2013-08-14 10:21:18 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
|
2013-07-26 14:05:14 +00:00
|
|
|
|
2013-07-26 14:50:44 +00:00
|
|
|
/*
|
|
|
|
* No padding: don't pad :)
|
|
|
|
*
|
|
|
|
* There is no add_padding function (check for NULL in cipher_finish)
|
|
|
|
* 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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
2013-09-13 12:41:45 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
|
2013-07-26 14:50:44 +00:00
|
|
|
|
2013-08-31 15:31:03 +00:00
|
|
|
int cipher_finish( 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 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
*olen = 0;
|
|
|
|
|
2012-07-04 17:10:40 +00:00
|
|
|
if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
|
2012-02-06 16:45:10 +00:00
|
|
|
POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
|
2013-09-05 11:38:15 +00:00
|
|
|
POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
|
2013-08-28 14:36:14 +00:00
|
|
|
POLARSSL_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
|
|
|
}
|
|
|
|
|
2013-09-08 21:04:04 +00:00
|
|
|
if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
|
|
|
|
{
|
|
|
|
if( ctx->unprocessed_len != 0 )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
|
|
|
|
2013-09-13 12:41:45 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_CBC)
|
2011-01-06 15:37:30 +00:00
|
|
|
if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
|
|
|
|
{
|
2013-09-13 12:41:45 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
if( POLARSSL_ENCRYPT == ctx->operation )
|
|
|
|
{
|
2013-07-26 14:50:44 +00:00
|
|
|
/* check for 'no padding' mode */
|
|
|
|
if( NULL == ctx->add_padding )
|
|
|
|
{
|
|
|
|
if( 0 != ctx->unprocessed_len )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
|
|
|
|
2013-07-25 10:31:10 +00:00
|
|
|
ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
|
2011-01-06 15:37:30 +00:00
|
|
|
ctx->unprocessed_len );
|
|
|
|
}
|
2014-06-17 14:39:18 +00:00
|
|
|
else if( 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
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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,
|
|
|
|
ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
|
|
|
|
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 */
|
|
|
|
if( POLARSSL_DECRYPT == ctx->operation )
|
2013-07-25 10:31:10 +00:00
|
|
|
return ctx->get_padding( output, cipher_get_block_size( ctx ),
|
|
|
|
olen );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* Set output size for encryption */
|
|
|
|
*olen = 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);
|
|
|
|
#endif /* POLARSSL_CIPHER_MODE_CBC */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
2011-01-06 15:37:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 12:41:45 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
|
2013-07-25 10:31:10 +00:00
|
|
|
int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
|
|
|
|
{
|
|
|
|
if( NULL == ctx ||
|
|
|
|
POLARSSL_MODE_CBC != ctx->cipher_info->mode )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
{
|
2013-08-14 10:21:18 +00:00
|
|
|
#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
|
2013-08-14 10:04:26 +00:00
|
|
|
case POLARSSL_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
|
|
|
|
#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
|
2013-08-14 10:04:26 +00:00
|
|
|
case POLARSSL_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
|
|
|
|
#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
|
2013-08-14 10:04:26 +00:00
|
|
|
case POLARSSL_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
|
|
|
|
#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
|
2013-08-14 10:04:26 +00:00
|
|
|
case POLARSSL_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
|
2013-08-14 10:04:26 +00:00
|
|
|
case POLARSSL_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:
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_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
|
|
|
}
|
2013-09-13 12:41:45 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
|
2013-07-25 10:31:10 +00:00
|
|
|
|
2014-06-24 13:26:28 +00:00
|
|
|
#if defined(POLARSSL_GCM_C)
|
2013-09-03 14:19:22 +00:00
|
|
|
int cipher_write_tag( cipher_context_t *ctx,
|
|
|
|
unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
|
|
|
if( POLARSSL_ENCRYPT != ctx->operation )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
|
2013-10-11 16:58:55 +00:00
|
|
|
return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
|
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
|
|
|
|
2013-09-03 14:19:22 +00:00
|
|
|
int cipher_check_tag( cipher_context_t *ctx,
|
|
|
|
const unsigned char *tag, size_t tag_len )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( NULL == ctx || NULL == ctx->cipher_info ||
|
|
|
|
POLARSSL_DECRYPT != ctx->operation )
|
|
|
|
{
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:35:53 +00:00
|
|
|
}
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
|
|
|
|
{
|
|
|
|
unsigned char check_tag[16];
|
|
|
|
size_t i;
|
|
|
|
int diff;
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( tag_len > sizeof( check_tag ) )
|
2014-06-17 12:06:49 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
|
|
|
|
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" */
|
|
|
|
for( diff = 0, i = 0; i < tag_len; i++ )
|
|
|
|
diff |= tag[i] ^ check_tag[i];
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
if( diff != 0 )
|
2013-09-19 16:09:14 +00:00
|
|
|
return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-03 14:35:53 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2013-09-03 14:19:22 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif /* POLARSSL_GCM_C */
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2014-05-12 11:46:08 +00:00
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:19:29 +00:00
|
|
|
#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 ) );
|
|
|
|
}
|
2014-05-13 11:18:17 +00:00
|
|
|
#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 */
|
2014-05-13 10:19:29 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
2014-05-13 11:18:17 +00:00
|
|
|
#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 */
|
2014-05-13 10:19:29 +00:00
|
|
|
|
|
|
|
return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_CIPHER_MODE_AEAD */
|
|
|
|
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
|
|
|
int cipher_self_test( int verbose )
|
|
|
|
{
|
2011-01-18 16:17:47 +00:00
|
|
|
((void) verbose);
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_SELF_TEST */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_CIPHER_C */
|