Key changed to password-based

This commit is contained in:
Daniele Lacamera 2019-10-21 23:04:14 +02:00
parent 1a38e58d6f
commit 3feb3b8a60
3 changed files with 23 additions and 10 deletions

View file

@ -27,15 +27,12 @@ uint8_t crypto_tmp[CRYPTO_BLOCK_SIZE];
uint8_t crypto_iv[CRYPTO_BLOCK_SIZE];
#include <wolfssl/wolfcrypt/chacha.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/sha256.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
@ -87,7 +84,6 @@ static void cache_commit(void)
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);
}
@ -113,7 +109,6 @@ static void cache_load(uint32_t blk)
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);
}
@ -482,11 +477,29 @@ int usecfs_close(int fd)
}
}
int usecfs_init(void)
#ifdef CRYPTO
#define SALT_LEN 32
const uint8_t password_salt[SALT_LEN] = {
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
int usecfs_init(const char *password)
{
blockdev = block_open(BLOCKDEV_OPEN_ARGS);
if (!blockdev)
return -1;
memset(OpenFiles, 0xFF, MAX_OPEN_FILES * sizeof(struct openfile));
#ifdef CRYPTO
{
uint8_t chacha_key[CRYPTO_KEY_SIZE];
int ret = 0;
ret = wc_PBKDF2(chacha_key, password, strlen(password), password_salt, SALT_LEN, 2048, CRYPTO_KEY_SIZE, SHA256);
wc_Chacha_SetKey(&chacha, chacha_key, CRYPTO_KEY_SIZE);
}
#endif
return 0;
}

View file

@ -4,7 +4,7 @@
#define MAX_OPEN_FILES 16
#include <stdint.h>
int usecfs_init(void);
int usecfs_init(const char *password);
int usecfs_open(const char *name);
int usecfs_creat(const char *name);
int usecfs_read(int fd, void *data, uint32_t len);

View file

@ -7,7 +7,7 @@ int main(void)
int fd;
int buf[40] = { };
if (usecfs_init() < 0)
if (usecfs_init("sEcret") < 0)
{
printf("error.\n");
return 1;