summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/sdc_crypto.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-05-13 12:45:53 +0200
committerUros Majstorovic <majstor@majstor.org>2022-05-13 12:45:53 +0200
commit412a8f99928beff605805807b0f07f6bf8d0a965 (patch)
tree366f1768ddfd32960c5d26ca3fba14e82c3b571e /fw/fe310/eos/dev/sdc_crypto.c
parent9ccb4db8d59ec9dab33ee8617d462f21a8bb4fa8 (diff)
code rename
Diffstat (limited to 'fw/fe310/eos/dev/sdc_crypto.c')
-rw-r--r--fw/fe310/eos/dev/sdc_crypto.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/fw/fe310/eos/dev/sdc_crypto.c b/fw/fe310/eos/dev/sdc_crypto.c
new file mode 100644
index 0000000..f0e935d
--- /dev/null
+++ b/fw/fe310/eos/dev/sdc_crypto.c
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#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, &sect, 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, &sect, sizeof(sect));
+ sdc_crypto->enc_essiv(sdc_crypto->ctx_essiv, iv);
+ sdc_crypto->dec(sdc_crypto->ctx, iv, buffer, 512);
+}