summaryrefslogtreecommitdiffstats
path: root/aes.c
diff options
context:
space:
mode:
authorMatteo Brichese <matteo.brichese@wunderbar.com>2017-06-06 22:33:36 +0200
committerMatteo Brichese <matteo.brichese@wunderbar.com>2017-06-06 22:33:36 +0200
commitc1c5fb1974203abf974d6ad359f22dd64b203e45 (patch)
treed9c7994cb0d9c8e04b7c5a362ee9f1dfe8191a29 /aes.c
parentEncrypt CBC works (diff)
downloadtiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar.gz
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar.bz2
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar.lz
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar.xz
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.tar.zst
tiny-AES-c-c1c5fb1974203abf974d6ad359f22dd64b203e45.zip
Diffstat (limited to '')
-rw-r--r--aes.c30
1 files changed, 15 insertions, 15 deletions
diff --git a/aes.c b/aes.c
index 6a2a531..e58498f 100644
--- a/aes.c
+++ b/aes.c
@@ -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
#define Nb 4
+#define BLOCKLEN 16 //Block length in bytes AES is 128b block only
#ifdef AES256
#define Nk 8
@@ -177,7 +178,7 @@ static void KeyExpansion(void)
// All other round keys are found from the previous round keys.
//i == Nk
- for(i = Nk; i < Nb * (Nr + 1); ++i)
+ for(; i < Nb * (Nr + 1); ++i)
{
{
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,
static void XorWithIv(uint8_t* buf)
{
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];
}
@@ -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)
{
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;
// 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
Iv = (uint8_t*)iv;
}
- for(i = 0; i < length; i += 16)
+ for(i = 0; i < length; i += BLOCKLEN)
{
XorWithIv(input);
- memcpy(output, input, 16);
+ memcpy(output, input, BLOCKLEN);
state = (state_t*)output;
Cipher();
Iv = output;
- input += 16;
- output += 16;
+ input += BLOCKLEN;
+ output += BLOCKLEN;
//printf("Step %d - %d", i/16, i);
}
if(extra)
{
- printf("NONO\n");
memcpy(output, input, extra);
state = (state_t*)output;
Cipher();
@@ -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)
{
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;
// 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
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;
InvCipher();
XorWithIv(output);
Iv = input;
- input += 16;
- output += 16;
+ input += BLOCKLEN;
+ output += BLOCKLEN;
}
if(extra)