Split mbedtls_ccm_init() -> setkey()
diff --git a/ChangeLog b/ChangeLog
index c7d86ad..69d7e19 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,9 @@
Migration helpers scripts/rename.pl and include/mbedlts/compat-1.3.h are
provided.
* Headers are now found in the 'mbedtls' directory (previously 'polarssl').
+ * The following _init() functions that could return errors have
+ been split into an _init() that returns void and another function:
+ mbedtls_ccm_init() -> mbedtls_ccm_setkey()
* In the threading layer, mbedtls_mutex_init() and mbedtls_mutex_free() now
return void.
* ecdsa_write_signature() gained an addtional md_alg argument and
diff --git a/include/mbedtls/ccm.h b/include/mbedtls/ccm.h
index 7ee3a37..894794d 100644
--- a/include/mbedtls/ccm.h
+++ b/include/mbedtls/ccm.h
@@ -42,6 +42,15 @@
mbedtls_ccm_context;
/**
+ * \brief Initialize CCM context (just makes references valid)
+ * Makes the context ready for mbedtls_ccm_setkey() or
+ * mbedtls_ccm_free().
+ *
+ * \param ctx CCM context to initialize
+ */
+void mbedtls_ccm_init( mbedtls_ccm_context *ctx );
+
+/**
* \brief CCM initialization (encryption and decryption)
*
* \param ctx CCM context to be initialized
@@ -51,8 +60,10 @@
*
* \return 0 if successful, or a cipher specific error code
*/
-int mbedtls_ccm_init( mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher,
- const unsigned char *key, unsigned int keysize );
+int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
+ mbedtls_cipher_id_t cipher,
+ const unsigned char *key,
+ unsigned int keysize );
/**
* \brief Free a CCM context and underlying cipher sub-context
diff --git a/library/ccm.c b/library/ccm.c
index 72eed38..957fda9 100644
--- a/library/ccm.c
+++ b/library/ccm.c
@@ -61,8 +61,15 @@
/*
* Initialize context
*/
-int mbedtls_ccm_init( mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher,
- const unsigned char *key, unsigned int keysize )
+void mbedtls_ccm_init( mbedtls_ccm_context *ctx )
+{
+ memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
+}
+
+int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx,
+ mbedtls_cipher_id_t cipher,
+ const unsigned char *key,
+ unsigned int keysize )
{
int ret;
const mbedtls_cipher_info_t *cipher_info;
@@ -398,7 +405,9 @@
size_t i;
int ret;
- if( mbedtls_ccm_init( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
+ mbedtls_ccm_init( &ctx );
+
+ if( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( " CCM: setup failed" );
diff --git a/library/cipher_wrap.c b/library/cipher_wrap.c
index ebc3c4f..eb291b6 100644
--- a/library/cipher_wrap.c
+++ b/library/cipher_wrap.c
@@ -395,7 +395,7 @@
static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
- return mbedtls_ccm_init( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
+ return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
key, key_length );
}
@@ -752,7 +752,7 @@
static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
unsigned int key_length )
{
- return mbedtls_ccm_init( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
+ return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
key, key_length );
}
diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c
index 56e31e0..afb4652 100644
--- a/programs/test/benchmark.c
+++ b/programs/test/benchmark.c
@@ -433,13 +433,15 @@
{
int keysize;
mbedtls_ccm_context ccm;
+
+ mbedtls_ccm_init( &ccm );
for( keysize = 128; keysize <= 256; keysize += 64 )
{
mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
memset( buf, 0, sizeof( buf ) );
memset( tmp, 0, sizeof( tmp ) );
- mbedtls_ccm_init( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
+ mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
TIME_AND_TSC( title,
mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
diff --git a/tests/suites/test_suite_ccm.data b/tests/suites/test_suite_ccm.data
index 9ce19ee..90ba42d 100644
--- a/tests/suites/test_suite_ccm.data
+++ b/tests/suites/test_suite_ccm.data
@@ -3,19 +3,19 @@
CCM init #1 AES-128: OK
depends_on:MBEDTLS_AES_C
-mbedtls_ccm_init:MBEDTLS_CIPHER_ID_AES:128:0
+mbedtls_ccm_setkey:MBEDTLS_CIPHER_ID_AES:128:0
CCM init #2 CAMELLIA-256: OK
depends_on:MBEDTLS_CAMELLIA_C
-mbedtls_ccm_init:MBEDTLS_CIPHER_ID_CAMELLIA:256:0
+mbedtls_ccm_setkey:MBEDTLS_CIPHER_ID_CAMELLIA:256:0
CCM init #3 AES-224: bad key size
depends_on:MBEDTLS_AES_C
-mbedtls_ccm_init:MBEDTLS_CIPHER_ID_AES:224:MBEDTLS_ERR_CCM_BAD_INPUT
+mbedtls_ccm_setkey:MBEDTLS_CIPHER_ID_AES:224:MBEDTLS_ERR_CCM_BAD_INPUT
CCM init #4 BLOWFISH-128: bad block size
depends_on:MBEDTLS_BLOWFISH_C
-mbedtls_ccm_init:MBEDTLS_CIPHER_ID_BLOWFISH:128:MBEDTLS_ERR_CCM_BAD_INPUT
+mbedtls_ccm_setkey:MBEDTLS_CIPHER_ID_BLOWFISH:128:MBEDTLS_ERR_CCM_BAD_INPUT
CCM lengths #1 all OK
ccm_lengths:5:10:5:8:0
diff --git a/tests/suites/test_suite_ccm.function b/tests/suites/test_suite_ccm.function
index 6769ed6..13371eb 100644
--- a/tests/suites/test_suite_ccm.function
+++ b/tests/suites/test_suite_ccm.function
@@ -15,16 +15,18 @@
/* END_CASE */
/* BEGIN_CASE */
-void mbedtls_ccm_init( int cipher_id, int key_size, int result )
+void mbedtls_ccm_setkey( int cipher_id, int key_size, int result )
{
mbedtls_ccm_context ctx;
unsigned char key[32];
int ret;
+ mbedtls_ccm_init( &ctx );
+
memset( key, 0x2A, sizeof( key ) );
TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
- ret = mbedtls_ccm_init( &ctx, cipher_id, key, key_size );
+ ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size );
TEST_ASSERT( ret == result );
exit:
@@ -44,6 +46,8 @@
unsigned char tag[18];
int decrypt_ret;
+ mbedtls_ccm_init( &ctx );
+
memset( key, 0, sizeof( key ) );
memset( msg, 0, sizeof( msg ) );
memset( iv, 0, sizeof( iv ) );
@@ -51,7 +55,7 @@
memset( out, 0, sizeof( out ) );
memset( tag, 0, sizeof( tag ) );
- TEST_ASSERT( mbedtls_ccm_init( &ctx, MBEDTLS_CIPHER_ID_AES,
+ TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
key, 8 * sizeof( key ) ) == 0 );
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
@@ -84,6 +88,8 @@
mbedtls_ccm_context ctx;
size_t key_len, msg_len, iv_len, add_len, tag_len, result_len;
+ mbedtls_ccm_init( &ctx );
+
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( iv, 0x00, sizeof( iv ) );
@@ -97,7 +103,7 @@
result_len = unhexify( result, result_hex );
tag_len = result_len - msg_len;
- TEST_ASSERT( mbedtls_ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 );
+ TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
/* Test with input == output */
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
@@ -129,6 +135,8 @@
size_t key_len, msg_len, iv_len, add_len, result_len;
int ret;
+ mbedtls_ccm_init( &ctx );
+
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( iv, 0x00, sizeof( iv ) );
@@ -154,7 +162,7 @@
result_len = unhexify( result, result_hex );
}
- TEST_ASSERT( mbedtls_ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 );
+ TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
/* Test with input == output */
TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,