Commit 405f6e5e authored by Matteo Brichese's avatar Matteo Brichese
Browse files

Encrypt CBC works

parent 20894622
...@@ -503,9 +503,9 @@ static void XorWithIv(uint8_t* buf) ...@@ -503,9 +503,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 remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */ uint8_t extra = length % 16; /* Remaining bytes in the last non-full block */
memcpy(output, input, KEYLEN); memcpy(output, input, 16);
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 +520,22 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -520,21 +520,22 @@ 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-remainders; i += KEYLEN) for(i = 0; i < length; i += 16)
{ {
XorWithIv(input); XorWithIv(input);
memcpy(output, input, KEYLEN); memcpy(output, input, 16);
state = (state_t*)output; state = (state_t*)output;
Cipher(); Cipher();
Iv = output; Iv = output;
input += KEYLEN; input += 16;
output += KEYLEN; output += 16;
//printf("Step %d - %d", i/16, i);
} }
if(remainders) if(extra)
{ {
memcpy(output, input, remainders); printf("NONO\n");
//memset(output + remainders, 0, KEYLEN - remainders); /* add 0-padding */ memcpy(output, input, extra);
state = (state_t*)output; state = (state_t*)output;
Cipher(); Cipher();
} }
...@@ -543,9 +544,9 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -543,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 remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */ uint8_t extra = length % 16; /* Remaining bytes in the last non-full block */
memcpy(output, input, KEYLEN); memcpy(output, input, 16);
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
...@@ -561,21 +562,20 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co ...@@ -561,21 +562,20 @@ 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 += KEYLEN) for(i = 0; i < length; i += 16)
{ {
memcpy(output, input, KEYLEN); memcpy(output, input, 16);
state = (state_t*)output; state = (state_t*)output;
InvCipher(); InvCipher();
XorWithIv(output); XorWithIv(output);
Iv = input; Iv = input;
input += KEYLEN; input += 16;
output += KEYLEN; output += 16;
} }
if(remainders) if(extra)
{ {
memcpy(output, input, KEYLEN); memcpy(output, input, extra);
memset(output+remainders, 0, KEYLEN - remainders); /* add 0-padding */
state = (state_t*)output; state = (state_t*)output;
InvCipher(); InvCipher();
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#define ECB 1 #define ECB 1
#endif #endif
#define AES256 #define AES128
#if defined(ECB) && ECB #if defined(ECB) && ECB
......
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