Replaced cipher with chacha20

This commit is contained in:
Daniele Lacamera 2019-10-21 21:03:04 +02:00
parent 1e0427b324
commit 1a38e58d6f

View file

@ -21,18 +21,20 @@
#ifdef CRYPTO
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/aes.h>
#define CRYPTO_BLOCK_SIZE 16
#define CRYPTO_KEY_SIZE 16 /* Aes128 */
static Aes aes;
uint8_t aes_tmp[CRYPTO_BLOCK_SIZE];
uint8_t aes_iv[CRYPTO_BLOCK_SIZE];
const uint8_t aes_key[] = {
0xc6, 0x38, 0xa6, 0xa5,
0xb4, 0x25, 0x07, 0x7e,
0x97, 0x6a, 0xd2, 0x42,
0xf4, 0x6a, 0x6a, 0xb0
uint8_t crypto_tmp[CRYPTO_BLOCK_SIZE];
uint8_t crypto_iv[CRYPTO_BLOCK_SIZE];
#include <wolfssl/wolfcrypt/chacha.h>
static ChaCha chacha;
#define CRYPTO_KEY_SIZE 32
const uint8_t chacha20_key[] = {
0xe7, 0xa1, 0x9c, 0xb0, 0x48, 0xa8, 0x30, 0xf9, 0x37, 0xda, 0x8e, 0xde,
0xff, 0xb2, 0x62, 0x03, 0x24, 0x55, 0xb8, 0x8b, 0x7b, 0x18, 0x68, 0x57,
0x7d, 0x35, 0xbe, 0xbd, 0xf6, 0x0e, 0xc1, 0x2c
};
#endif
@ -80,14 +82,14 @@ static void cache_commit(void)
#ifdef CRYPTO
if (!is_block_empty()) {
uint32_t i;
memset(aes_iv, 0, CRYPTO_BLOCK_SIZE);
memset(crypto_iv, 0, CRYPTO_BLOCK_SIZE);
for (i = 0; i < BLOCK_SIZE / CRYPTO_BLOCK_SIZE; i++) {
memcpy(&aes_iv[8], &cached_block, sizeof(uint32_t));
memcpy(&aes_iv[12], &i, sizeof(uint32_t));
if (wc_AesSetKey(&aes, aes_key, CRYPTO_KEY_SIZE, aes_iv, AES_ENCRYPTION) != 0)
return;
memcpy(aes_tmp, cache + (i * CRYPTO_BLOCK_SIZE), CRYPTO_BLOCK_SIZE);
wc_AesEncryptDirect(&aes, cache + i * CRYPTO_BLOCK_SIZE, aes_tmp);
memcpy(&crypto_iv[0], &cached_block, sizeof(uint32_t));
memcpy(&crypto_iv[4], &i, sizeof(uint32_t));
memcpy(crypto_tmp, cache + (i * CRYPTO_BLOCK_SIZE), CRYPTO_BLOCK_SIZE);
wc_Chacha_SetKey(&chacha, chacha20_key, CRYPTO_KEY_SIZE);
wc_Chacha_SetIV(&chacha, crypto_iv, CRYPTO_BLOCK_SIZE);
wc_Chacha_Process(&chacha, cache + i * CRYPTO_BLOCK_SIZE, crypto_tmp, CRYPTO_BLOCK_SIZE);
}
}
#endif
@ -106,15 +108,14 @@ static void cache_load(uint32_t blk)
#ifdef CRYPTO
if (!is_block_empty()) {
uint32_t i;
Aes aes;
memset(aes_iv, 0, CRYPTO_BLOCK_SIZE);
memset(crypto_iv, 0, CRYPTO_BLOCK_SIZE);
for (i = 0; i < BLOCK_SIZE / CRYPTO_BLOCK_SIZE; i++) {
memcpy(&aes_iv[8], &blk, sizeof(uint32_t));
memcpy(&aes_iv[12], &i, sizeof(uint32_t));
if (wc_AesSetKey(&aes, aes_key, CRYPTO_KEY_SIZE, aes_iv, AES_DECRYPTION) != 0)
return;
memcpy(aes_tmp, cache + (i * CRYPTO_BLOCK_SIZE), CRYPTO_BLOCK_SIZE);
wc_AesDecryptDirect(&aes, cache + i * CRYPTO_BLOCK_SIZE, aes_tmp);
memcpy(&crypto_iv[0], &blk, sizeof(uint32_t));
memcpy(&crypto_iv[4], &i, sizeof(uint32_t));
memcpy(crypto_tmp, cache + (i * CRYPTO_BLOCK_SIZE), CRYPTO_BLOCK_SIZE);
wc_Chacha_SetKey(&chacha, chacha20_key, CRYPTO_KEY_SIZE);
wc_Chacha_SetIV(&chacha, crypto_iv, CRYPTO_BLOCK_SIZE);
wc_Chacha_Process(&chacha, cache + i * CRYPTO_BLOCK_SIZE, crypto_tmp, CRYPTO_BLOCK_SIZE);
}
}
#endif