aes: Added AES_ECB support

This commit is contained in:
Andreas Steffen
2019-11-28 17:03:09 +01:00
committed by Tobias Brunner
parent 6f44bd6fe8
commit f884ee6497
2 changed files with 62 additions and 30 deletions
+31 -2
View File
@@ -70,6 +70,11 @@ struct private_aes_crypter_t {
* Key size of this AES cypher object. * Key size of this AES cypher object.
*/ */
uint32_t key_size; uint32_t key_size;
/**
* Does AES mode require an IV
*/
bool has_iv;
}; };
/** /**
@@ -809,6 +814,8 @@ METHOD(crypter_t, decrypt, bool,
while (pos >= 0) while (pos >= 0)
{ {
decrypt_block(this, in, out); decrypt_block(this, in, out);
if (this->has_iv)
{
if (pos == 0) if (pos == 0)
{ {
iv_i = (const uint32_t*) (iv.ptr); iv_i = (const uint32_t*) (iv.ptr);
@@ -821,6 +828,7 @@ METHOD(crypter_t, decrypt, bool,
*((uint32_t *)(&out[ 4])) ^= iv_i[1]; *((uint32_t *)(&out[ 4])) ^= iv_i[1];
*((uint32_t *)(&out[ 8])) ^= iv_i[2]; *((uint32_t *)(&out[ 8])) ^= iv_i[2];
*((uint32_t *)(&out[12])) ^= iv_i[3]; *((uint32_t *)(&out[12])) ^= iv_i[3];
}
in-= 16; in-= 16;
out-= 16; out-= 16;
pos-= 16; pos-= 16;
@@ -845,6 +853,8 @@ METHOD(crypter_t, encrypt, bool,
pos = 0; pos = 0;
while (pos < data.len) while (pos < data.len)
{
if (this->has_iv)
{ {
if (pos == 0) if (pos == 0)
{ {
@@ -858,6 +868,15 @@ METHOD(crypter_t, encrypt, bool,
*((uint32_t *)(&out[ 4])) = iv_i[1]^*((const uint32_t *)(&in[ 4])); *((uint32_t *)(&out[ 4])) = iv_i[1]^*((const uint32_t *)(&in[ 4]));
*((uint32_t *)(&out[ 8])) = iv_i[2]^*((const uint32_t *)(&in[ 8])); *((uint32_t *)(&out[ 8])) = iv_i[2]^*((const uint32_t *)(&in[ 8]));
*((uint32_t *)(&out[12])) = iv_i[3]^*((const uint32_t *)(&in[12])); *((uint32_t *)(&out[12])) = iv_i[3]^*((const uint32_t *)(&in[12]));
}
else
{
*((uint32_t *)(&out[ 0])) = *((const uint32_t *)(&in[ 0]));
*((uint32_t *)(&out[ 4])) = *((const uint32_t *)(&in[ 4]));
*((uint32_t *)(&out[ 8])) = *((const uint32_t *)(&in[ 8]));
*((uint32_t *)(&out[12])) = *((const uint32_t *)(&in[12]));
}
encrypt_block(this, out, out); encrypt_block(this, out, out);
in+= 16; in+= 16;
out+= 16; out+= 16;
@@ -875,7 +894,7 @@ METHOD(crypter_t, get_block_size, size_t,
METHOD(crypter_t, get_iv_size, size_t, METHOD(crypter_t, get_iv_size, size_t,
private_aes_crypter_t *this) private_aes_crypter_t *this)
{ {
return AES_BLOCK_SIZE; return this->has_iv ? AES_BLOCK_SIZE : 0;
} }
METHOD(crypter_t, get_key_size, size_t, METHOD(crypter_t, get_key_size, size_t,
@@ -978,11 +997,20 @@ METHOD(crypter_t, destroy, void,
aes_crypter_t *aes_crypter_create(encryption_algorithm_t algo, size_t key_size) aes_crypter_t *aes_crypter_create(encryption_algorithm_t algo, size_t key_size)
{ {
private_aes_crypter_t *this; private_aes_crypter_t *this;
bool has_iv;
if (algo != ENCR_AES_CBC) switch (algo)
{ {
case ENCR_AES_CBC:
has_iv = TRUE;
break;
case ENCR_AES_ECB:
has_iv = FALSE;
break;
default:
return NULL; return NULL;
} }
switch (key_size) switch (key_size)
{ {
case 0: case 0:
@@ -1010,6 +1038,7 @@ aes_crypter_t *aes_crypter_create(encryption_algorithm_t algo, size_t key_size)
}, },
.key_size = key_size, .key_size = key_size,
.aes_Nkey = key_size / 4, .aes_Nkey = key_size / 4,
.has_iv = has_iv,
); );
return &this->public; return &this->public;
@@ -45,6 +45,9 @@ METHOD(plugin_t, get_features, int,
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 16), PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 16),
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 24), PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 24),
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 32), PLUGIN_PROVIDE(CRYPTER, ENCR_AES_CBC, 32),
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_ECB, 16),
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_ECB, 24),
PLUGIN_PROVIDE(CRYPTER, ENCR_AES_ECB, 32),
}; };
*features = f; *features = f;
return countof(f); return countof(f);