Commit c1c5fb19 authored by Matteo Brichese's avatar Matteo Brichese
Browse files

added AES192 and 256

parent 405f6e5e
...@@ -42,6 +42,7 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) ...@@ -42,6 +42,7 @@ NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
/*****************************************************************************/ /*****************************************************************************/
// The number of columns comprising a state in AES. This is a constant in AES. Value=4 // The number of columns comprising a state in AES. This is a constant in AES. Value=4
#define Nb 4 #define Nb 4
#define BLOCKLEN 16 //Block length in bytes AES is 128b block only
#ifdef AES256 #ifdef AES256
#define Nk 8 #define Nk 8
...@@ -177,7 +178,7 @@ static void KeyExpansion(void) ...@@ -177,7 +178,7 @@ static void KeyExpansion(void)
// All other round keys are found from the previous round keys. // All other round keys are found from the previous round keys.
//i == Nk //i == Nk
for(i = Nk; i < Nb * (Nr + 1); ++i) for(; i < Nb * (Nr + 1); ++i)
{ {
{ {
tempa[0]=RoundKey[(i-1) * 4 + 0]; tempa[0]=RoundKey[(i-1) * 4 + 0];
...@@ -494,7 +495,7 @@ void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, ...@@ -494,7 +495,7 @@ void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output,
static void XorWithIv(uint8_t* buf) static void XorWithIv(uint8_t* buf)
{ {
uint8_t i; uint8_t 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! for(i = 0; i < BLOCKLEN; ++i) //WAS for(i = 0; i < KEYLEN; ++i) but the block in AES is always 128bit so 16 bytes!
{ {
buf[i] ^= Iv[i]; buf[i] ^= Iv[i];
} }
...@@ -503,9 +504,9 @@ static void XorWithIv(uint8_t* buf) ...@@ -503,9 +504,9 @@ static void XorWithIv(uint8_t* buf)
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv) void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
{ {
uintptr_t i; uintptr_t i;
uint8_t extra = length % 16; /* Remaining bytes in the last non-full block */ uint8_t extra = length % BLOCKLEN; /* Remaining bytes in the last non-full block */
memcpy(output, input, 16); memcpy(output, input, BLOCKLEN);
state = (state_t*)output; state = (state_t*)output;
// Skip the key expansion if key is passed as 0 // Skip the key expansion if key is passed as 0
...@@ -520,21 +521,20 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -520,21 +521,20 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
Iv = (uint8_t*)iv; Iv = (uint8_t*)iv;
} }
for(i = 0; i < length; i += 16) for(i = 0; i < length; i += BLOCKLEN)
{ {
XorWithIv(input); XorWithIv(input);
memcpy(output, input, 16); memcpy(output, input, BLOCKLEN);
state = (state_t*)output; state = (state_t*)output;
Cipher(); Cipher();
Iv = output; Iv = output;
input += 16; input += BLOCKLEN;
output += 16; output += BLOCKLEN;
//printf("Step %d - %d", i/16, i); //printf("Step %d - %d", i/16, i);
} }
if(extra) if(extra)
{ {
printf("NONO\n");
memcpy(output, input, extra); memcpy(output, input, extra);
state = (state_t*)output; state = (state_t*)output;
Cipher(); Cipher();
...@@ -544,9 +544,9 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -544,9 +544,9 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv) void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
{ {
uintptr_t i; uintptr_t i;
uint8_t extra = length % 16; /* Remaining bytes in the last non-full block */ uint8_t extra = length % BLOCKLEN; /* Remaining bytes in the last non-full block */
memcpy(output, input, 16); memcpy(output, input, BLOCKLEN);
state = (state_t*)output; state = (state_t*)output;
// Skip the key expansion if key is passed as 0 // Skip the key expansion if key is passed as 0
...@@ -562,15 +562,15 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -562,15 +562,15 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
Iv = (uint8_t*)iv; Iv = (uint8_t*)iv;
} }
for(i = 0; i < length; i += 16) for(i = 0; i < length; i += BLOCKLEN)
{ {
memcpy(output, input, 16); memcpy(output, input, BLOCKLEN);
state = (state_t*)output; state = (state_t*)output;
InvCipher(); InvCipher();
XorWithIv(output); XorWithIv(output);
Iv = input; Iv = input;
input += 16; input += BLOCKLEN;
output += 16; output += BLOCKLEN;
} }
if(extra) if(extra)
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// #define the macros below to 1/0 to enable/disable the mode of operation. // #define the macros below to 1/0 to enable/disable the mode of operation.
// //
// CBC enables AES128 encryption in CBC-mode of operation and handles 0-padding. // CBC enables AES encryption in CBC-mode of operation.
// ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously. // ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously.
// The #ifndef-guard allows it to be configured before #include'ing or at compile time. // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
......
...@@ -20,13 +20,18 @@ static void test_decrypt_cbc(void); ...@@ -20,13 +20,18 @@ static void test_decrypt_cbc(void);
int main(void) int main(void)
{ {
#ifdef AES128 #ifdef AES128
printf("\nAES128\n\n"); printf("\nTesting AES128\n\n");
#elif defined(AES192) #elif defined(AES192)
printf("\nAES192\n\n"); printf("\nTesting AES192\n\n");
#elif defined(AES256) #elif defined(AES256)
printf("\nAES256\n\n"); printf("\nTesting AES256\n\n");
#else
printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
return 0;
#endif #endif
test_encrypt_cbc(); test_encrypt_cbc();
test_decrypt_cbc(); test_decrypt_cbc();
test_decrypt_ecb(); test_decrypt_ecb();
...@@ -157,6 +162,7 @@ static void test_decrypt_cbc(void) ...@@ -157,6 +162,7 @@ static void test_decrypt_cbc(void)
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
uint8_t buffer[64]; uint8_t buffer[64];
uint8_t buffer2[64];
AES_CBC_decrypt_buffer(buffer, in, 64, key, iv); AES_CBC_decrypt_buffer(buffer, in, 64, key, iv);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment