adding aes256 aes192 options

This commit is contained in:
Matteo Brichese
2017-06-06 11:10:33 -07:00
parent 8592c76b48
commit 2089462272
3 changed files with 101 additions and 48 deletions

36
aes.c
View File

@@ -448,16 +448,6 @@ static void InvCipher(void)
AddRoundKey(0);
}
static void BlockCopy(uint8_t* output, const uint8_t* input)
{
uint8_t i;
for (i=0;i<KEYLEN;++i)
{
output[i] = input[i];
}
}
/*****************************************************************************/
/* Public functions: */
@@ -465,10 +455,10 @@ static void BlockCopy(uint8_t* output, const uint8_t* input)
#if defined(ECB) && ECB
void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output)
void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output, const uint32_t length)
{
// Copy input to output, and work in-memory on output
BlockCopy(output, input);
memcpy(output, input, length);
state = (state_t*)output;
Key = key;
@@ -478,10 +468,10 @@ void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output)
Cipher();
}
void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output)
void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
{
// Copy input to output, and work in-memory on output
BlockCopy(output, input);
memcpy(output, input, length);
state = (state_t*)output;
// The KeyExpansion routine must be called before encryption.
@@ -504,7 +494,7 @@ void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output)
static void XorWithIv(uint8_t* buf)
{
uint8_t i;
for(i = 0; i < KEYLEN; ++i)
for(i = 0; i < 16; ++i) //WAS for(i = 0; i < KEYLEN; ++i) but the block in AES is always 128bit so 16 bytes!
{
buf[i] ^= Iv[i];
}
@@ -515,7 +505,7 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
uintptr_t i;
uint8_t remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */
BlockCopy(output, input);
memcpy(output, input, KEYLEN);
state = (state_t*)output;
// Skip the key expansion if key is passed as 0
@@ -530,10 +520,10 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
Iv = (uint8_t*)iv;
}
for(i = 0; i < length; i += KEYLEN)
for(i = 0; i < length-remainders; i += KEYLEN)
{
XorWithIv(input);
BlockCopy(output, input);
memcpy(output, input, KEYLEN);
state = (state_t*)output;
Cipher();
Iv = output;
@@ -543,8 +533,8 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
if(remainders)
{
BlockCopy(output, input);
memset(output + remainders, 0, KEYLEN - remainders); /* add 0-padding */
memcpy(output, input, remainders);
//memset(output + remainders, 0, KEYLEN - remainders); /* add 0-padding */
state = (state_t*)output;
Cipher();
}
@@ -555,7 +545,7 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
uintptr_t i;
uint8_t remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */
BlockCopy(output, input);
memcpy(output, input, KEYLEN);
state = (state_t*)output;
// Skip the key expansion if key is passed as 0
@@ -573,7 +563,7 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
for(i = 0; i < length; i += KEYLEN)
{
BlockCopy(output, input);
memcpy(output, input, KEYLEN);
state = (state_t*)output;
InvCipher();
XorWithIv(output);
@@ -584,7 +574,7 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
if(remainders)
{
BlockCopy(output, input);
memcpy(output, input, KEYLEN);
memset(output+remainders, 0, KEYLEN - remainders); /* add 0-padding */
state = (state_t*)output;
InvCipher();