mirror of
https://github.com/kokke/tiny-AES-c
synced 2024-11-25 06:49:37 +03:00
Created in-place CBC functions. Also added a couple of consts.
This commit is contained in:
parent
0373207800
commit
43b0a4fa9c
45
aes.c
45
aes.c
@ -73,7 +73,7 @@ static const uint8_t* Key;
|
||||
|
||||
#if defined(CBC) && CBC
|
||||
// Initial Vector used only for CBC mode
|
||||
static uint8_t* Iv;
|
||||
static const uint8_t* Iv;
|
||||
#endif
|
||||
|
||||
// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
|
||||
@ -434,7 +434,7 @@ static void InvCipher(void)
|
||||
AddRoundKey(0);
|
||||
}
|
||||
|
||||
static void BlockCopy(uint8_t* output, uint8_t* input)
|
||||
static void BlockCopy(uint8_t* output, const uint8_t* input)
|
||||
{
|
||||
uint8_t i;
|
||||
for (i=0;i<KEYLEN;++i)
|
||||
@ -577,6 +577,47 @@ void AES128_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length,
|
||||
}
|
||||
}
|
||||
|
||||
void AES128_CBC_encrypt_buffer16_ip(uint8_t* data, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||
{
|
||||
int i;
|
||||
|
||||
state = (state_t*)data;
|
||||
|
||||
Key = key;
|
||||
KeyExpansion();
|
||||
|
||||
Iv = (uint8_t*)iv;
|
||||
|
||||
for(i = 0; i < length; i += 16)
|
||||
{
|
||||
XorWithIv(data);
|
||||
state = (state_t*)data;
|
||||
Cipher();
|
||||
Iv = data;
|
||||
data += 16;
|
||||
}
|
||||
}
|
||||
|
||||
void AES128_CBC_decrypt_buffer16_ip(uint8_t* data, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||
{
|
||||
int i;
|
||||
uint8_t iv_copy[32];
|
||||
|
||||
Key = key;
|
||||
KeyExpansion();
|
||||
|
||||
BlockCopy(iv_copy, iv);
|
||||
|
||||
for(i = 0; i < length; i += 16)
|
||||
{
|
||||
BlockCopy(&iv_copy[(i + 16) % 32], data);
|
||||
state = (state_t*)data;
|
||||
InvCipher();
|
||||
Iv = &iv_copy[i % 32];
|
||||
XorWithIv(data);
|
||||
data += 16;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // #if defined(CBC) && CBC
|
||||
|
||||
|
7
aes.h
7
aes.h
@ -33,6 +33,13 @@ void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output);
|
||||
void AES128_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);
|
||||
void AES128_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);
|
||||
|
||||
/*
|
||||
* In-place encrypt/decrypt functions (whole blocks only)
|
||||
* data[] and length must be a multiple of 16 bytes
|
||||
*/
|
||||
void AES128_CBC_encrypt_buffer16_ip(uint8_t* data, uint32_t length, const uint8_t* key, const uint8_t* iv);
|
||||
void AES128_CBC_decrypt_buffer16_ip(uint8_t* data, uint32_t length, const uint8_t* key, const uint8_t* iv);
|
||||
|
||||
#endif // #if defined(CBC) && CBC
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user