From 405f6e5eee24aa4247671b4700b3d84df16d9e55 Mon Sep 17 00:00:00 2001 From: Matteo Brichese Date: Tue, 6 Jun 2017 11:36:11 -0700 Subject: Encrypt CBC works --- aes.c | 40 ++++++++++++++++++++-------------------- aes.h | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/aes.c b/aes.c index 3171cdc..6a2a531 100644 --- a/aes.c +++ b/aes.c @@ -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) { 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; // 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 Iv = (uint8_t*)iv; } - for(i = 0; i < length-remainders; i += KEYLEN) + for(i = 0; i < length; i += 16) { XorWithIv(input); - memcpy(output, input, KEYLEN); + memcpy(output, input, 16); state = (state_t*)output; Cipher(); Iv = output; - input += KEYLEN; - output += KEYLEN; + input += 16; + output += 16; + //printf("Step %d - %d", i/16, i); } - if(remainders) + if(extra) { - memcpy(output, input, remainders); - //memset(output + remainders, 0, KEYLEN - remainders); /* add 0-padding */ + printf("NONO\n"); + memcpy(output, input, extra); state = (state_t*)output; Cipher(); } @@ -543,11 +544,11 @@ 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) { uintptr_t i; - uint8_t remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */ - - memcpy(output, input, KEYLEN); - state = (state_t*)output; + uint8_t extra = length % 16; /* Remaining bytes in the last non-full block */ + memcpy(output, input, 16); + state = (state_t*)output; + // Skip the key expansion if key is passed as 0 if(0 != key) { @@ -561,21 +562,20 @@ void AES_CBC_decrypt_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; i += 16) { - memcpy(output, input, KEYLEN); + memcpy(output, input, 16); state = (state_t*)output; InvCipher(); XorWithIv(output); Iv = input; - input += KEYLEN; - output += KEYLEN; + input += 16; + output += 16; } - if(remainders) + if(extra) { - memcpy(output, input, KEYLEN); - memset(output+remainders, 0, KEYLEN - remainders); /* add 0-padding */ + memcpy(output, input, extra); state = (state_t*)output; InvCipher(); } diff --git a/aes.h b/aes.h index 1c8af6c..15b504e 100644 --- a/aes.h +++ b/aes.h @@ -18,7 +18,7 @@ #define ECB 1 #endif -#define AES256 +#define AES128 #if defined(ECB) && ECB -- cgit v1.2.3