aead: Support custom AEAD salt sizes

The salt, or often called implicit nonce, varies between AEAD algorithms and
their use in protocols. For IKE and ESP, GCM uses 4 bytes, while CCM uses
3 bytes. With TLS, however, AEAD mode uses 4 bytes for both GCM and CCM.

Our GCM backends currently support 4 bytes and CCM 3 bytes only. This is fine
until we go for CCM mode support in TLS, which requires 4 byte nonces.
This commit is contained in:
Martin Willi
2014-03-31 15:56:12 +02:00
parent e12eec1008
commit e5d73b0dfa
17 changed files with 131 additions and 43 deletions
+9
View File
@@ -102,6 +102,10 @@ struct aead_t {
/**
* Get the size of the key material (for encryption and authentication).
*
* This includes any additional bytes requires for the implicit nonce part.
* For AEADs based on traditional ciphers, the length is for both
* the integrity and the encryption key in total.
*
* @return key size in bytes
*/
size_t (*get_key_size)(aead_t *this);
@@ -109,6 +113,11 @@ struct aead_t {
/**
* Set the key for encryption and authentication.
*
* If the AEAD uses an implicit nonce, the last part of the key shall
* be the implicit nonce. For AEADs based on traditional ciphers, the
* key shall include both integrity and encryption keys, concatenated
* in that order.
*
* @param key encryption and authentication key
* @return TRUE if key set successfully
*/
+5 -5
View File
@@ -176,7 +176,7 @@ METHOD(crypto_factory_t, create_crypter, crypter_t*,
METHOD(crypto_factory_t, create_aead, aead_t*,
private_crypto_factory_t *this, encryption_algorithm_t algo,
size_t key_size)
size_t key_size, size_t salt_size)
{
enumerator_t *enumerator;
entry_t *entry;
@@ -190,12 +190,12 @@ METHOD(crypto_factory_t, create_aead, aead_t*,
{
if (this->test_on_create &&
!this->tester->test_aead(this->tester, algo, key_size,
entry->create_aead, NULL,
salt_size, entry->create_aead, NULL,
default_plugin_name))
{
continue;
}
aead = entry->create_aead(algo, key_size);
aead = entry->create_aead(algo, key_size, salt_size);
if (aead)
{
break;
@@ -474,7 +474,7 @@ METHOD(crypto_factory_t, add_aead, bool,
u_int speed = 0;
if (!this->test_on_add ||
this->tester->test_aead(this->tester, algo, 0, create,
this->tester->test_aead(this->tester, algo, 0, 0, create,
this->bench ? &speed : NULL, plugin_name))
{
add_entry(this, this->aeads, algo, plugin_name, speed, create);
@@ -1003,7 +1003,7 @@ static u_int verify_registered_algorithms(crypto_factory_t *factory)
this->lock->read_lock(this->lock);
TEST_ALGORITHMS(crypter, 0);
TEST_ALGORITHMS(aead, 0);
TEST_ALGORITHMS(aead, 0, 0);
TEST_ALGORITHMS(signer);
TEST_ALGORITHMS(hasher);
TEST_ALGORITHMS(prf);
+4 -2
View File
@@ -46,7 +46,7 @@ typedef crypter_t* (*crypter_constructor_t)(encryption_algorithm_t algo,
* Constructor function for aead transforms
*/
typedef aead_t* (*aead_constructor_t)(encryption_algorithm_t algo,
size_t key_size);
size_t key_size, size_t salt_size);
/**
* Constructor function for signers
*/
@@ -100,10 +100,12 @@ struct crypto_factory_t {
*
* @param algo encryption algorithm
* @param key_size length of the key in bytes
* @param salt_size size of salt, implicit part of the nonce
* @return aead_t instance, NULL if not supported
*/
aead_t* (*create_aead)(crypto_factory_t *this,
encryption_algorithm_t algo, size_t key_size);
encryption_algorithm_t algo,
size_t key_size, size_t salt_size);
/**
* Create a symmetric signer instance.
+8 -4
View File
@@ -315,7 +315,7 @@ static u_int bench_aead(private_crypto_tester_t *this,
{
aead_t *aead;
aead = create(alg, 0);
aead = create(alg, 0, 0);
if (aead)
{
char iv[aead->get_iv_size(aead)];
@@ -364,7 +364,8 @@ static u_int bench_aead(private_crypto_tester_t *this,
METHOD(crypto_tester_t, test_aead, bool,
private_crypto_tester_t *this, encryption_algorithm_t alg, size_t key_size,
aead_constructor_t create, u_int *speed, const char *plugin_name)
size_t salt_size, aead_constructor_t create,
u_int *speed, const char *plugin_name)
{
enumerator_t *enumerator;
aead_test_vector_t *vector;
@@ -386,10 +387,14 @@ METHOD(crypto_tester_t, test_aead, bool,
{ /* test only vectors with a specific key size, if key size given */
continue;
}
if (salt_size && salt_size != vector->salt_size)
{
continue;
}
tested++;
failed = TRUE;
aead = create(alg, vector->key_size);
aead = create(alg, vector->key_size, vector->salt_size);
if (!aead)
{
DBG1(DBG_LIB, "%N[%s]: %u bit key size not supported",
@@ -1218,4 +1223,3 @@ crypto_tester_t *crypto_tester_create()
return &this->public;
}
+6 -2
View File
@@ -54,6 +54,8 @@ struct aead_test_vector_t {
encryption_algorithm_t alg;
/** key length to use, in bytes */
size_t key_size;
/** salt length to use, in bytes */
size_t salt_size;
/** encryption key of test vector */
u_char *key;
/** initialization vector, using crypters blocksize bytes */
@@ -150,13 +152,15 @@ struct crypto_tester_t {
*
* @param alg algorithm to test
* @param key_size key size to test, 0 for default
* @param salt_size salt length to test, 0 for default
* @param create constructor function for the aead transform
* @param speed speed test result, NULL to omit
* @return TRUE if test passed
*/
bool (*test_aead)(crypto_tester_t *this, encryption_algorithm_t alg,
size_t key_size, aead_constructor_t create,
u_int *speed, const char *plugin_name);
size_t key_size, size_t salt_size,
aead_constructor_t create,
u_int *speed, const char *plugin_name);
/**
* Test a signer algorithm.
*