From 3cac09c89fe0081f06188f1e9440de5d863951be Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 20 May 2021 19:06:47 +0200 Subject: fat filesystem with disk encryption for sd card driver --- fw/fe310/eos/sdc_crypto.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 fw/fe310/eos/sdc_crypto.c (limited to 'fw/fe310/eos/sdc_crypto.c') diff --git a/fw/fe310/eos/sdc_crypto.c b/fw/fe310/eos/sdc_crypto.c new file mode 100644 index 0000000..c094468 --- /dev/null +++ b/fw/fe310/eos/sdc_crypto.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "sha/sha1.h" +#include "sdc_crypto.h" + +#define SDC_CRYPTO_KEY_SIZE 16 +#define SDC_CRYPTO_BLK_SIZE 16 +#define SDC_CRYPTO_HASH_SIZE 20 + +EOSSDCCrypto *sdc_crypto; + +void eos_sdcc_init(EOSSDCCrypto *crypto, uint8_t *key, void *ctx, eve_sdcc_init_t init, eve_sdcc_crypt_t enc, eve_sdcc_crypt_t dec, void *ctx_essiv, eve_sdcc_init_t init_essiv, eve_sdcc_essiv_t enc_essiv) { + char key_essiv[SDC_CRYPTO_HASH_SIZE]; + + sdc_crypto = crypto; + + memset(key_essiv, 0, SDC_CRYPTO_HASH_SIZE); + init(ctx, key); + sdc_crypto->ctx = ctx; + sdc_crypto->enc = enc; + sdc_crypto->dec = dec; + + SHA1(key_essiv, key, SDC_CRYPTO_KEY_SIZE); + init_essiv(ctx_essiv, key_essiv); + sdc_crypto->ctx_essiv = ctx_essiv; + sdc_crypto->enc_essiv = enc_essiv; +} + +void eos_sdcc_encrypt(uint32_t sect, uint8_t *buffer) { + uint8_t iv[SDC_CRYPTO_BLK_SIZE]; + + if (sdc_crypto == NULL) return; + + memset(iv, 0, SDC_CRYPTO_BLK_SIZE); + memcpy(iv, §, sizeof(sect)); + sdc_crypto->enc_essiv(sdc_crypto->ctx_essiv, iv); + sdc_crypto->enc(sdc_crypto->ctx, iv, buffer, 512); +} + +void eos_sdcc_decrypt(uint32_t sect, uint8_t *buffer) { + uint8_t iv[SDC_CRYPTO_BLK_SIZE]; + + if (sdc_crypto == NULL) return; + + memset(iv, 0, SDC_CRYPTO_BLK_SIZE); + memcpy(iv, §, sizeof(sect)); + sdc_crypto->enc_essiv(sdc_crypto->ctx_essiv, iv); + sdc_crypto->dec(sdc_crypto->ctx, iv, buffer, 512); +} \ No newline at end of file -- cgit v1.2.3