Add AES-CCM and CAMELLIA-CCM to the cipher layer

This commit is contained in:
Manuel Pégourié-Gonnard 2014-05-13 13:18:17 +02:00
parent de7bb44004
commit 41936957b3
3 changed files with 176 additions and 2 deletions

View file

@ -120,6 +120,12 @@ typedef enum {
POLARSSL_CIPHER_BLOWFISH_CFB64,
POLARSSL_CIPHER_BLOWFISH_CTR,
POLARSSL_CIPHER_ARC4_128,
POLARSSL_CIPHER_AES_128_CCM,
POLARSSL_CIPHER_AES_192_CCM,
POLARSSL_CIPHER_AES_256_CCM,
POLARSSL_CIPHER_CAMELLIA_128_CCM,
POLARSSL_CIPHER_CAMELLIA_192_CCM,
POLARSSL_CIPHER_CAMELLIA_256_CCM,
} cipher_type_t;
typedef enum {
@ -131,6 +137,7 @@ typedef enum {
POLARSSL_MODE_CTR,
POLARSSL_MODE_GCM,
POLARSSL_MODE_STREAM,
POLARSSL_MODE_CCM,
} cipher_mode_t;
typedef enum {

View file

@ -42,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)
@ -818,7 +822,16 @@ int cipher_auth_encrypt( cipher_context_t *ctx,
iv, iv_len, ad, ad_len, input, output,
tag_len, tag ) );
}
#endif
#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 );
}
@ -848,7 +861,23 @@ int cipher_auth_decrypt( cipher_context_t *ctx,
return( ret );
}
#endif
#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 );
}

View file

@ -61,6 +61,10 @@
#include "polarssl/gcm.h"
#endif
#if defined(POLARSSL_CCM_C)
#include "polarssl/ccm.h"
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
@ -84,6 +88,20 @@ static void gcm_ctx_free( void *ctx )
}
#endif /* POLARSSL_GCM_C */
#if defined(POLARSSL_CCM_C)
/* shared by all CCM ciphers */
static void *ccm_ctx_alloc( void )
{
return polarssl_malloc( sizeof( ccm_context ) );
}
static void ccm_ctx_free( void *ctx )
{
ccm_free( ctx );
polarssl_free( ctx );
}
#endif /* POLARSSL_CCM_C */
#if defined(POLARSSL_AES_C)
static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
@ -378,6 +396,61 @@ const cipher_info_t aes_256_gcm_info = {
};
#endif /* POLARSSL_GCM_C */
#if defined(POLARSSL_CCM_C)
static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_AES,
key, key_length );
}
const cipher_base_t ccm_aes_info = {
POLARSSL_CIPHER_ID_AES,
NULL,
NULL,
NULL,
NULL,
NULL,
ccm_aes_setkey_wrap,
ccm_aes_setkey_wrap,
ccm_ctx_alloc,
ccm_ctx_free,
};
const cipher_info_t aes_128_ccm_info = {
POLARSSL_CIPHER_AES_128_CCM,
POLARSSL_MODE_CCM,
128,
"AES-128-CCM",
12,
1,
16,
&ccm_aes_info
};
const cipher_info_t aes_192_ccm_info = {
POLARSSL_CIPHER_AES_192_CCM,
POLARSSL_MODE_CCM,
192,
"AES-192-CCM",
12,
1,
16,
&ccm_aes_info
};
const cipher_info_t aes_256_ccm_info = {
POLARSSL_CIPHER_AES_256_CCM,
POLARSSL_MODE_CCM,
256,
"AES-256-CCM",
12,
1,
16,
&ccm_aes_info
};
#endif /* POLARSSL_CCM_C */
#endif /* POLARSSL_AES_C */
#if defined(POLARSSL_CAMELLIA_C)
@ -676,6 +749,61 @@ const cipher_info_t camellia_256_gcm_info = {
};
#endif /* POLARSSL_GCM_C */
#if defined(POLARSSL_CCM_C)
static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
key, key_length );
}
const cipher_base_t ccm_camellia_info = {
POLARSSL_CIPHER_ID_CAMELLIA,
NULL,
NULL,
NULL,
NULL,
NULL,
ccm_camellia_setkey_wrap,
ccm_camellia_setkey_wrap,
ccm_ctx_alloc,
ccm_ctx_free,
};
const cipher_info_t camellia_128_ccm_info = {
POLARSSL_CIPHER_CAMELLIA_128_CCM,
POLARSSL_MODE_CCM,
128,
"CAMELLIA-128-CCM",
12,
1,
16,
&ccm_camellia_info
};
const cipher_info_t camellia_192_ccm_info = {
POLARSSL_CIPHER_CAMELLIA_192_CCM,
POLARSSL_MODE_CCM,
192,
"CAMELLIA-192-CCM",
12,
1,
16,
&ccm_camellia_info
};
const cipher_info_t camellia_256_ccm_info = {
POLARSSL_CIPHER_CAMELLIA_256_CCM,
POLARSSL_MODE_CCM,
256,
"CAMELLIA-256-CCM",
12,
1,
16,
&ccm_camellia_info
};
#endif /* POLARSSL_CCM_C */
#endif /* POLARSSL_CAMELLIA_C */
#if defined(POLARSSL_DES_C)
@ -1217,6 +1345,11 @@ const cipher_definition_t cipher_definitions[] =
{ POLARSSL_CIPHER_AES_192_GCM, &aes_192_gcm_info },
{ POLARSSL_CIPHER_AES_256_GCM, &aes_256_gcm_info },
#endif
#if defined(POLARSSL_CCM_C)
{ POLARSSL_CIPHER_AES_128_CCM, &aes_128_ccm_info },
{ POLARSSL_CIPHER_AES_192_CCM, &aes_192_ccm_info },
{ POLARSSL_CIPHER_AES_256_CCM, &aes_256_ccm_info },
#endif
#endif /* POLARSSL_AES_C */
#if defined(POLARSSL_ARC4_C)
@ -1260,6 +1393,11 @@ const cipher_definition_t cipher_definitions[] =
{ POLARSSL_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
{ POLARSSL_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
#endif
#if defined(POLARSSL_CCM_C)
{ POLARSSL_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
{ POLARSSL_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
{ POLARSSL_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
#endif
#endif /* POLARSSL_CAMELLIA_C */
#if defined(POLARSSL_DES_C)