From 3feb3b8a605b6f98eb5b3397bcc84348f25d052c Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Mon, 21 Oct 2019 23:04:14 +0200 Subject: [PATCH] Key changed to password-based --- src/usecfs.c | 29 +++++++++++++++++++++-------- src/usecfs.h | 2 +- test/main.c | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/usecfs.c b/src/usecfs.c index 0caaf56..16e06dc 100644 --- a/src/usecfs.c +++ b/src/usecfs.c @@ -27,15 +27,12 @@ uint8_t crypto_tmp[CRYPTO_BLOCK_SIZE]; uint8_t crypto_iv[CRYPTO_BLOCK_SIZE]; #include +#include +#include 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; } diff --git a/src/usecfs.h b/src/usecfs.h index 26b3c91..cfadbde 100644 --- a/src/usecfs.h +++ b/src/usecfs.h @@ -4,7 +4,7 @@ #define MAX_OPEN_FILES 16 #include -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); diff --git a/test/main.c b/test/main.c index e927a71..befc705 100644 --- a/test/main.c +++ b/test/main.c @@ -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;