summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/sdc_crypto.c
blob: c094468e5a67a67703460c089e8e972880888e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}