1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-11 07:50:57 +00:00
trezor-firmware/micropython/stmhal/bootloader/crypto.c

63 lines
1.5 KiB
C
Raw Normal View History

2017-02-11 15:47:36 +00:00
#include <string.h>
2016-10-04 16:01:48 +00:00
2017-02-11 15:47:36 +00:00
#include "sha2.h"
2016-10-04 16:01:48 +00:00
#include "ed25519-donna/ed25519.h"
2017-02-11 15:47:36 +00:00
#include "crypto.h"
2016-10-04 16:01:48 +00:00
2017-02-16 12:48:28 +00:00
#define FLASH_BASE 0x08000000
2016-10-04 16:01:48 +00:00
void hash_flash(uint8_t hash[SHA256_DIGEST_LENGTH])
{
sha256_Raw((const uint8_t *)FLASH_BASE, 1024*1024, hash);
}
2017-02-16 12:48:28 +00:00
bool ed25519_verify(const uint8_t *msg, uint32_t msglen, const uint8_t *pubkey, const uint8_t *signature)
2016-10-04 16:01:48 +00:00
{
return (0 == ed25519_sign_open(msg, msglen, *(const ed25519_public_key *)pubkey, *(const ed25519_signature *)signature));
}
2017-02-11 15:47:36 +00:00
bool check_header(const uint8_t *data)
{
uint32_t magic;
memcpy(&magic, data, 4);
if (magic != 0x425A5254) return false; // TRZB
uint32_t hdrlen;
memcpy(&hdrlen, data + 4, 4);
if (hdrlen != 256) return false;
uint32_t expiry;
memcpy(&expiry, data + 8, 4);
if (expiry != 0) return false;
uint32_t codelen;
memcpy(&codelen, data + 12, 4);
if (codelen != 64 * 1024) return false;
uint32_t version;
memcpy(&version, data + 16, 4);
// uint8_t reserved[171];
uint8_t sigidx;
memcpy(&sigidx, data + 0x00BF, 1);
uint8_t sig[64];
memcpy(sig, data + 0x00C0, 64);
// TODO: check signature
return true;
}
2017-02-16 12:48:28 +00:00
bool check_signature(void)
{
uint8_t hash[SHA256_DIGEST_LENGTH];
hash_flash(hash);
const uint8_t *pub = (const uint8_t *)"0123456789ABCDEF0123456789ABCDEF";
const uint8_t *sig = (const uint8_t *)"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
return ed25519_verify(hash, SHA256_DIGEST_LENGTH, pub, sig);
}