mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-07 06:42:56 +03:00
Merged GCM refactoring into development
GCM is now independent of AES and can be used as a mode for any cipher-layer supported 128-bit based block cipher
This commit is contained in:
239
library/cipher.c
239
library/cipher.c
@@ -51,6 +51,9 @@
|
||||
static const int supported_ciphers[] = {
|
||||
|
||||
#if defined(POLARSSL_AES_C)
|
||||
POLARSSL_CIPHER_AES_128_ECB,
|
||||
POLARSSL_CIPHER_AES_192_ECB,
|
||||
POLARSSL_CIPHER_AES_256_ECB,
|
||||
POLARSSL_CIPHER_AES_128_CBC,
|
||||
POLARSSL_CIPHER_AES_192_CBC,
|
||||
POLARSSL_CIPHER_AES_256_CBC,
|
||||
@@ -80,6 +83,9 @@ static const int supported_ciphers[] = {
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CAMELLIA_C)
|
||||
POLARSSL_CIPHER_CAMELLIA_128_ECB,
|
||||
POLARSSL_CIPHER_CAMELLIA_192_ECB,
|
||||
POLARSSL_CIPHER_CAMELLIA_256_ECB,
|
||||
POLARSSL_CIPHER_CAMELLIA_128_CBC,
|
||||
POLARSSL_CIPHER_CAMELLIA_192_CBC,
|
||||
POLARSSL_CIPHER_CAMELLIA_256_CBC,
|
||||
@@ -99,12 +105,16 @@ static const int supported_ciphers[] = {
|
||||
#endif /* defined(POLARSSL_CAMELLIA_C) */
|
||||
|
||||
#if defined(POLARSSL_DES_C)
|
||||
POLARSSL_CIPHER_DES_ECB,
|
||||
POLARSSL_CIPHER_DES_EDE_ECB,
|
||||
POLARSSL_CIPHER_DES_EDE3_ECB,
|
||||
POLARSSL_CIPHER_DES_CBC,
|
||||
POLARSSL_CIPHER_DES_EDE_CBC,
|
||||
POLARSSL_CIPHER_DES_EDE3_CBC,
|
||||
#endif /* defined(POLARSSL_DES_C) */
|
||||
|
||||
#if defined(POLARSSL_BLOWFISH_C)
|
||||
POLARSSL_CIPHER_BLOWFISH_ECB,
|
||||
POLARSSL_CIPHER_BLOWFISH_CBC,
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
||||
@@ -135,6 +145,13 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
|
||||
switch ( cipher_type )
|
||||
{
|
||||
#if defined(POLARSSL_AES_C)
|
||||
case POLARSSL_CIPHER_AES_128_ECB:
|
||||
return &aes_128_ecb_info;
|
||||
case POLARSSL_CIPHER_AES_192_ECB:
|
||||
return &aes_192_ecb_info;
|
||||
case POLARSSL_CIPHER_AES_256_ECB:
|
||||
return &aes_256_ecb_info;
|
||||
|
||||
case POLARSSL_CIPHER_AES_128_CBC:
|
||||
return &aes_128_cbc_info;
|
||||
case POLARSSL_CIPHER_AES_192_CBC:
|
||||
@@ -172,6 +189,13 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CAMELLIA_C)
|
||||
case POLARSSL_CIPHER_CAMELLIA_128_ECB:
|
||||
return &camellia_128_ecb_info;
|
||||
case POLARSSL_CIPHER_CAMELLIA_192_ECB:
|
||||
return &camellia_192_ecb_info;
|
||||
case POLARSSL_CIPHER_CAMELLIA_256_ECB:
|
||||
return &camellia_256_ecb_info;
|
||||
|
||||
case POLARSSL_CIPHER_CAMELLIA_128_CBC:
|
||||
return &camellia_128_cbc_info;
|
||||
case POLARSSL_CIPHER_CAMELLIA_192_CBC:
|
||||
@@ -200,6 +224,13 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_DES_C)
|
||||
case POLARSSL_CIPHER_DES_ECB:
|
||||
return &des_ecb_info;
|
||||
case POLARSSL_CIPHER_DES_EDE_ECB:
|
||||
return &des_ede_ecb_info;
|
||||
case POLARSSL_CIPHER_DES_EDE3_ECB:
|
||||
return &des_ede3_ecb_info;
|
||||
|
||||
case POLARSSL_CIPHER_DES_CBC:
|
||||
return &des_cbc_info;
|
||||
case POLARSSL_CIPHER_DES_EDE_CBC:
|
||||
@@ -214,6 +245,9 @@ const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BLOWFISH_C)
|
||||
case POLARSSL_CIPHER_BLOWFISH_ECB:
|
||||
return &blowfish_ecb_info;
|
||||
|
||||
case POLARSSL_CIPHER_BLOWFISH_CBC:
|
||||
return &blowfish_cbc_info;
|
||||
|
||||
@@ -345,6 +379,185 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
|
||||
int key_length,
|
||||
const cipher_mode_t mode )
|
||||
{
|
||||
#if defined(POLARSSL_AES_C)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_AES )
|
||||
{
|
||||
if( mode == POLARSSL_MODE_ECB )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &aes_128_ecb_info;
|
||||
if( key_length == 192 )
|
||||
return &aes_192_ecb_info;
|
||||
if( key_length == 256 )
|
||||
return &aes_256_ecb_info;
|
||||
}
|
||||
|
||||
if( mode == POLARSSL_MODE_CBC )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &aes_128_cbc_info;
|
||||
if( key_length == 192 )
|
||||
return &aes_192_cbc_info;
|
||||
if( key_length == 256 )
|
||||
return &aes_256_cbc_info;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
||||
if( mode == POLARSSL_MODE_CFB )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &aes_128_cfb128_info;
|
||||
if( key_length == 192 )
|
||||
return &aes_192_cfb128_info;
|
||||
if( key_length == 256 )
|
||||
return &aes_256_cfb128_info;
|
||||
}
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CTR)
|
||||
if( mode == POLARSSL_MODE_CTR )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &aes_128_ctr_info;
|
||||
if( key_length == 192 )
|
||||
return &aes_192_ctr_info;
|
||||
if( key_length == 256 )
|
||||
return &aes_256_ctr_info;
|
||||
}
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
|
||||
|
||||
#if defined(POLARSSL_GCM_C)
|
||||
if( mode == POLARSSL_MODE_GCM )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &aes_128_gcm_info;
|
||||
if( key_length == 192 )
|
||||
return &aes_192_gcm_info;
|
||||
if( key_length == 256 )
|
||||
return &aes_256_gcm_info;
|
||||
}
|
||||
#endif /* defined(POLARSSL_GCM_C) */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CAMELLIA_C)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_CAMELLIA )
|
||||
{
|
||||
if( mode == POLARSSL_MODE_ECB )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &camellia_128_ecb_info;
|
||||
if( key_length == 192 )
|
||||
return &camellia_192_ecb_info;
|
||||
if( key_length == 256 )
|
||||
return &camellia_256_ecb_info;
|
||||
}
|
||||
|
||||
if( mode == POLARSSL_MODE_CBC )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &camellia_128_cbc_info;
|
||||
if( key_length == 192 )
|
||||
return &camellia_192_cbc_info;
|
||||
if( key_length == 256 )
|
||||
return &camellia_256_cbc_info;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
||||
if( mode == POLARSSL_MODE_CFB )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &camellia_128_cfb128_info;
|
||||
if( key_length == 192 )
|
||||
return &camellia_192_cfb128_info;
|
||||
if( key_length == 256 )
|
||||
return &camellia_256_cfb128_info;
|
||||
}
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CTR)
|
||||
if( mode == POLARSSL_MODE_CTR )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &camellia_128_ctr_info;
|
||||
if( key_length == 192 )
|
||||
return &camellia_192_ctr_info;
|
||||
if( key_length == 256 )
|
||||
return &camellia_256_ctr_info;
|
||||
}
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_DES_C)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_DES && key_length == 64 )
|
||||
{
|
||||
if( mode == POLARSSL_MODE_ECB )
|
||||
return &des_ecb_info;
|
||||
|
||||
if( mode == POLARSSL_MODE_CBC )
|
||||
return &des_cbc_info;
|
||||
}
|
||||
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_3DES )
|
||||
{
|
||||
if( mode == POLARSSL_MODE_ECB )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &des_ede_ecb_info;
|
||||
if( key_length == 192 )
|
||||
return &des_ede3_ecb_info;
|
||||
}
|
||||
|
||||
if( mode == POLARSSL_MODE_CBC )
|
||||
{
|
||||
if( key_length == 128 )
|
||||
return &des_ede_cbc_info;
|
||||
if( key_length == 192 )
|
||||
return &des_ede3_cbc_info;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ARC4_C)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_ARC4 &&
|
||||
key_length == 128 && mode == POLARSSL_MODE_STREAM )
|
||||
return &arc4_128_info;
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BLOWFISH_C)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_BLOWFISH && key_length == 128 )
|
||||
{
|
||||
if( mode == POLARSSL_MODE_ECB )
|
||||
return &blowfish_ecb_info;
|
||||
|
||||
if( mode == POLARSSL_MODE_CBC )
|
||||
return &blowfish_cbc_info;
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
||||
if( mode == POLARSSL_MODE_CFB )
|
||||
return &blowfish_cfb64_info;
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CTR)
|
||||
if( mode == POLARSSL_MODE_CTR )
|
||||
return &blowfish_ctr_info;
|
||||
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
|
||||
if( cipher_id == POLARSSL_CIPHER_ID_NULL )
|
||||
return &null_cipher_info;
|
||||
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
|
||||
{
|
||||
if( NULL == cipher_info || NULL == ctx )
|
||||
@@ -467,8 +680,24 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
|
||||
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
|
||||
{
|
||||
if( ilen != cipher_get_block_size( ctx ) )
|
||||
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 0;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_GCM_C)
|
||||
if( ctx->cipher_info->mode == POLARSSL_MODE_GCM)
|
||||
if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
|
||||
{
|
||||
*olen = ilen;
|
||||
return gcm_update( ctx->cipher_ctx, ilen, input, output );
|
||||
@@ -780,6 +1009,14 @@ int cipher_finish( cipher_context_t *ctx,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
|
||||
{
|
||||
if( ctx->unprocessed_len != 0 )
|
||||
return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
|
||||
{
|
||||
if( POLARSSL_ENCRYPT == ctx->operation )
|
||||
|
@@ -68,6 +68,12 @@
|
||||
|
||||
#if defined(POLARSSL_AES_C)
|
||||
|
||||
static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
|
||||
const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
|
||||
}
|
||||
|
||||
static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
@@ -134,6 +140,7 @@ static void aes_ctx_free( void *ctx )
|
||||
|
||||
const cipher_base_t aes_info = {
|
||||
POLARSSL_CIPHER_ID_AES,
|
||||
aes_crypt_ecb_wrap,
|
||||
aes_crypt_cbc_wrap,
|
||||
aes_crypt_cfb128_wrap,
|
||||
aes_crypt_ctr_wrap,
|
||||
@@ -144,6 +151,39 @@ const cipher_base_t aes_info = {
|
||||
aes_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t aes_128_ecb_info = {
|
||||
POLARSSL_CIPHER_AES_128_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
128,
|
||||
"AES-128-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&aes_info
|
||||
};
|
||||
|
||||
const cipher_info_t aes_192_ecb_info = {
|
||||
POLARSSL_CIPHER_AES_192_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
192,
|
||||
"AES-192-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&aes_info
|
||||
};
|
||||
|
||||
const cipher_info_t aes_256_ecb_info = {
|
||||
POLARSSL_CIPHER_AES_256_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
256,
|
||||
"AES-256-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&aes_info
|
||||
};
|
||||
|
||||
const cipher_info_t aes_128_cbc_info = {
|
||||
POLARSSL_CIPHER_AES_128_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -258,9 +298,10 @@ static void gcm_ctx_free( void *ctx )
|
||||
polarssl_free( ctx );
|
||||
}
|
||||
|
||||
static int gcm_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
|
||||
static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key, unsigned int key_length )
|
||||
{
|
||||
return gcm_init( (gcm_context *) ctx, key, key_length );
|
||||
return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
|
||||
key, key_length );
|
||||
}
|
||||
|
||||
const cipher_base_t gcm_aes_info = {
|
||||
@@ -269,8 +310,9 @@ const cipher_base_t gcm_aes_info = {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
gcm_setkey_wrap,
|
||||
gcm_setkey_wrap,
|
||||
NULL,
|
||||
gcm_aes_setkey_wrap,
|
||||
gcm_aes_setkey_wrap,
|
||||
gcm_ctx_alloc,
|
||||
gcm_ctx_free,
|
||||
};
|
||||
@@ -313,6 +355,12 @@ const cipher_info_t aes_256_gcm_info = {
|
||||
|
||||
#if defined(POLARSSL_CAMELLIA_C)
|
||||
|
||||
static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
|
||||
const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
return camellia_crypt_ecb( (camellia_context *) ctx, operation, input, output );
|
||||
}
|
||||
|
||||
static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
@@ -379,6 +427,7 @@ static void camellia_ctx_free( void *ctx )
|
||||
|
||||
const cipher_base_t camellia_info = {
|
||||
POLARSSL_CIPHER_ID_CAMELLIA,
|
||||
camellia_crypt_ecb_wrap,
|
||||
camellia_crypt_cbc_wrap,
|
||||
camellia_crypt_cfb128_wrap,
|
||||
camellia_crypt_ctr_wrap,
|
||||
@@ -389,6 +438,39 @@ const cipher_base_t camellia_info = {
|
||||
camellia_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t camellia_128_ecb_info = {
|
||||
POLARSSL_CIPHER_CAMELLIA_128_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
128,
|
||||
"CAMELLIA-128-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&camellia_info
|
||||
};
|
||||
|
||||
const cipher_info_t camellia_192_ecb_info = {
|
||||
POLARSSL_CIPHER_CAMELLIA_192_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
192,
|
||||
"CAMELLIA-192-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&camellia_info
|
||||
};
|
||||
|
||||
const cipher_info_t camellia_256_ecb_info = {
|
||||
POLARSSL_CIPHER_CAMELLIA_256_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
256,
|
||||
"CAMELLIA-256-ECB",
|
||||
16,
|
||||
0,
|
||||
16,
|
||||
&camellia_info
|
||||
};
|
||||
|
||||
const cipher_info_t camellia_128_cbc_info = {
|
||||
POLARSSL_CIPHER_CAMELLIA_128_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -496,6 +578,20 @@ const cipher_info_t camellia_256_ctr_info = {
|
||||
|
||||
#if defined(POLARSSL_DES_C)
|
||||
|
||||
static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
|
||||
const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
((void) operation);
|
||||
return des_crypt_ecb( (des_context *) ctx, input, output );
|
||||
}
|
||||
|
||||
static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
|
||||
const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
((void) operation);
|
||||
return des3_crypt_ecb( (des3_context *) ctx, input, output );
|
||||
}
|
||||
|
||||
static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
@@ -596,6 +692,7 @@ static void des_ctx_free( void *ctx )
|
||||
|
||||
const cipher_base_t des_info = {
|
||||
POLARSSL_CIPHER_ID_DES,
|
||||
des_crypt_ecb_wrap,
|
||||
des_crypt_cbc_wrap,
|
||||
des_crypt_cfb128_wrap,
|
||||
des_crypt_ctr_wrap,
|
||||
@@ -606,6 +703,17 @@ const cipher_base_t des_info = {
|
||||
des_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t des_ecb_info = {
|
||||
POLARSSL_CIPHER_DES_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
POLARSSL_KEY_LENGTH_DES,
|
||||
"DES-ECB",
|
||||
8,
|
||||
0,
|
||||
8,
|
||||
&des_info
|
||||
};
|
||||
|
||||
const cipher_info_t des_cbc_info = {
|
||||
POLARSSL_CIPHER_DES_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -619,6 +727,7 @@ const cipher_info_t des_cbc_info = {
|
||||
|
||||
const cipher_base_t des_ede_info = {
|
||||
POLARSSL_CIPHER_ID_DES,
|
||||
des3_crypt_ecb_wrap,
|
||||
des3_crypt_cbc_wrap,
|
||||
des_crypt_cfb128_wrap,
|
||||
des_crypt_ctr_wrap,
|
||||
@@ -629,6 +738,17 @@ const cipher_base_t des_ede_info = {
|
||||
des_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t des_ede_ecb_info = {
|
||||
POLARSSL_CIPHER_DES_EDE_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
POLARSSL_KEY_LENGTH_DES_EDE,
|
||||
"DES-EDE-ECB",
|
||||
8,
|
||||
0,
|
||||
8,
|
||||
&des_ede_info
|
||||
};
|
||||
|
||||
const cipher_info_t des_ede_cbc_info = {
|
||||
POLARSSL_CIPHER_DES_EDE_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -642,6 +762,7 @@ const cipher_info_t des_ede_cbc_info = {
|
||||
|
||||
const cipher_base_t des_ede3_info = {
|
||||
POLARSSL_CIPHER_ID_DES,
|
||||
des3_crypt_ecb_wrap,
|
||||
des3_crypt_cbc_wrap,
|
||||
des_crypt_cfb128_wrap,
|
||||
des_crypt_ctr_wrap,
|
||||
@@ -652,6 +773,16 @@ const cipher_base_t des_ede3_info = {
|
||||
des_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t des_ede3_ecb_info = {
|
||||
POLARSSL_CIPHER_DES_EDE3_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
POLARSSL_KEY_LENGTH_DES_EDE3,
|
||||
"DES-EDE3-ECB",
|
||||
8,
|
||||
0,
|
||||
8,
|
||||
&des_ede3_info
|
||||
};
|
||||
const cipher_info_t des_ede3_cbc_info = {
|
||||
POLARSSL_CIPHER_DES_EDE3_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -666,6 +797,12 @@ const cipher_info_t des_ede3_cbc_info = {
|
||||
|
||||
#if defined(POLARSSL_BLOWFISH_C)
|
||||
|
||||
static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
|
||||
const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input, output );
|
||||
}
|
||||
|
||||
static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output )
|
||||
{
|
||||
@@ -727,6 +864,7 @@ static void blowfish_ctx_free( void *ctx )
|
||||
|
||||
const cipher_base_t blowfish_info = {
|
||||
POLARSSL_CIPHER_ID_BLOWFISH,
|
||||
blowfish_crypt_ecb_wrap,
|
||||
blowfish_crypt_cbc_wrap,
|
||||
blowfish_crypt_cfb64_wrap,
|
||||
blowfish_crypt_ctr_wrap,
|
||||
@@ -737,6 +875,17 @@ const cipher_base_t blowfish_info = {
|
||||
blowfish_ctx_free
|
||||
};
|
||||
|
||||
const cipher_info_t blowfish_ecb_info = {
|
||||
POLARSSL_CIPHER_BLOWFISH_ECB,
|
||||
POLARSSL_MODE_ECB,
|
||||
128,
|
||||
"BLOWFISH-ECB",
|
||||
8,
|
||||
0,
|
||||
8,
|
||||
&blowfish_info
|
||||
};
|
||||
|
||||
const cipher_info_t blowfish_cbc_info = {
|
||||
POLARSSL_CIPHER_BLOWFISH_CBC,
|
||||
POLARSSL_MODE_CBC,
|
||||
@@ -809,6 +958,7 @@ const cipher_base_t arc4_base_info = {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
arc4_crypt_stream_wrap,
|
||||
arc4_setkey_wrap,
|
||||
arc4_setkey_wrap,
|
||||
@@ -863,6 +1013,7 @@ const cipher_base_t null_base_info = {
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
null_crypt_stream,
|
||||
null_setkey,
|
||||
null_setkey,
|
||||
|
@@ -54,15 +54,17 @@
|
||||
}
|
||||
#endif
|
||||
|
||||
static void gcm_gen_table( gcm_context *ctx )
|
||||
static int gcm_gen_table( gcm_context *ctx )
|
||||
{
|
||||
int i, j;
|
||||
int ret, i, j;
|
||||
uint64_t hi, lo;
|
||||
uint64_t vl, vh;
|
||||
unsigned char h[16];
|
||||
size_t olen = 0;
|
||||
|
||||
memset( h, 0, 16 );
|
||||
aes_crypt_ecb( &ctx->aes_ctx, AES_ENCRYPT, h, h );
|
||||
if( ( ret = cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
ctx->HH[0] = 0;
|
||||
ctx->HL[0] = 0;
|
||||
@@ -99,18 +101,36 @@ static void gcm_gen_table( gcm_context *ctx )
|
||||
HiL[j] = vl ^ ctx->HL[j];
|
||||
}
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize )
|
||||
int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key,
|
||||
unsigned int keysize )
|
||||
{
|
||||
int ret;
|
||||
const cipher_info_t *cipher_info;
|
||||
|
||||
memset( ctx, 0, sizeof(gcm_context) );
|
||||
|
||||
if( ( ret = aes_setkey_enc( &ctx->aes_ctx, key, keysize ) ) != 0 )
|
||||
cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
|
||||
if( cipher_info == NULL )
|
||||
return( POLARSSL_ERR_GCM_BAD_INPUT );
|
||||
|
||||
if( cipher_info->block_size != 16 )
|
||||
return( POLARSSL_ERR_GCM_BAD_INPUT );
|
||||
|
||||
if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
gcm_gen_table( ctx );
|
||||
if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
|
||||
POLARSSL_ENCRYPT ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
if( ( ret = gcm_gen_table( ctx ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@@ -176,10 +196,11 @@ int gcm_starts( gcm_context *ctx,
|
||||
const unsigned char *add,
|
||||
size_t add_len )
|
||||
{
|
||||
int ret;
|
||||
unsigned char work_buf[16];
|
||||
size_t i;
|
||||
const unsigned char *p;
|
||||
size_t use_len;
|
||||
size_t use_len, olen = 0;
|
||||
|
||||
memset( ctx->y, 0x00, sizeof(ctx->y) );
|
||||
memset( ctx->buf, 0x00, sizeof(ctx->buf) );
|
||||
@@ -218,7 +239,11 @@ int gcm_starts( gcm_context *ctx,
|
||||
gcm_mult( ctx, ctx->y, ctx->y );
|
||||
}
|
||||
|
||||
aes_crypt_ecb( &ctx->aes_ctx, AES_ENCRYPT, ctx->y, ctx->base_ectr );
|
||||
if( ( ret = cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr,
|
||||
&olen ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
ctx->add_len = add_len;
|
||||
p = add;
|
||||
@@ -243,11 +268,12 @@ int gcm_update( gcm_context *ctx,
|
||||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
int ret;
|
||||
unsigned char ectr[16];
|
||||
size_t i;
|
||||
const unsigned char *p;
|
||||
unsigned char *out_p = output;
|
||||
size_t use_len;
|
||||
size_t use_len, olen = 0;
|
||||
|
||||
if( output > input && (size_t) ( output - input ) < length )
|
||||
return( POLARSSL_ERR_GCM_BAD_INPUT );
|
||||
@@ -263,7 +289,11 @@ int gcm_update( gcm_context *ctx,
|
||||
if( ++ctx->y[i - 1] != 0 )
|
||||
break;
|
||||
|
||||
aes_crypt_ecb( &ctx->aes_ctx, AES_ENCRYPT, ctx->y, ectr );
|
||||
if( ( ret = cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr,
|
||||
&olen ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
for( i = 0; i < use_len; i++ )
|
||||
{
|
||||
@@ -613,6 +643,7 @@ int gcm_self_test( int verbose )
|
||||
unsigned char buf[64];
|
||||
unsigned char tag_buf[16];
|
||||
int i, j, ret;
|
||||
cipher_id_t cipher = POLARSSL_CIPHER_ID_AES;
|
||||
|
||||
for( j = 0; j < 3; j++ )
|
||||
{
|
||||
@@ -623,7 +654,7 @@ int gcm_self_test( int verbose )
|
||||
if( verbose != 0 )
|
||||
printf( " AES-GCM-%3d #%d (%s): ", key_len, i, "enc" );
|
||||
|
||||
gcm_init( &ctx, key[key_index[i]], key_len );
|
||||
gcm_init( &ctx, cipher, key[key_index[i]], key_len );
|
||||
|
||||
ret = gcm_crypt_and_tag( &ctx, GCM_ENCRYPT,
|
||||
pt_len[i],
|
||||
@@ -647,7 +678,7 @@ int gcm_self_test( int verbose )
|
||||
if( verbose != 0 )
|
||||
printf( " AES-GCM-%3d #%d (%s): ", key_len, i, "dec" );
|
||||
|
||||
gcm_init( &ctx, key[key_index[i]], key_len );
|
||||
gcm_init( &ctx, cipher, key[key_index[i]], key_len );
|
||||
|
||||
ret = gcm_crypt_and_tag( &ctx, GCM_DECRYPT,
|
||||
pt_len[i],
|
||||
@@ -671,7 +702,7 @@ int gcm_self_test( int verbose )
|
||||
if( verbose != 0 )
|
||||
printf( " AES-GCM-%3d #%d split (%s): ", key_len, i, "enc" );
|
||||
|
||||
gcm_init( &ctx, key[key_index[i]], key_len );
|
||||
gcm_init( &ctx, cipher, key[key_index[i]], key_len );
|
||||
|
||||
ret = gcm_starts( &ctx, GCM_ENCRYPT,
|
||||
iv[iv_index[i]], iv_len[i],
|
||||
@@ -734,7 +765,7 @@ int gcm_self_test( int verbose )
|
||||
if( verbose != 0 )
|
||||
printf( " AES-GCM-%3d #%d split (%s): ", key_len, i, "dec" );
|
||||
|
||||
gcm_init( &ctx, key[key_index[i]], key_len );
|
||||
gcm_init( &ctx, cipher, key[key_index[i]], key_len );
|
||||
|
||||
ret = gcm_starts( &ctx, GCM_DECRYPT,
|
||||
iv[iv_index[i]], iv_len[i],
|
||||
|
Reference in New Issue
Block a user