1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-02 20:48:30 +00:00
trezor-firmware/micropython/bootloader/crypto.c

104 lines
3.0 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
#include "blake2s.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
2017-02-19 16:48:28 +00:00
bool parse_header(const uint8_t *data, uint32_t *codelen, uint8_t *sigidx, uint8_t *sig)
2017-02-11 15:47:36 +00:00
{
uint32_t magic;
memcpy(&magic, data, 4);
if (magic != 0x4C5A5254) return false; // TRZL
2017-02-11 15:47:36 +00:00
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;
2017-02-19 16:48:28 +00:00
uint32_t clen;
memcpy(&clen, data + 12, 4);
// stage 2 (+header) must fit into sectors 4...11 - see docs/memory.md for more info
2017-02-19 16:48:28 +00:00
if (clen + hdrlen < 4 * 1024) return false;
if (clen + hdrlen > 64 * 1024 + 7 * 128 * 1024) return false;
if ((clen + hdrlen) % 512 != 0) return false;
if (codelen) {
*codelen = clen;
}
2017-02-11 15:47:36 +00:00
uint32_t version;
memcpy(&version, data + 16, 4);
// uint8_t reserved[171];
2017-02-19 16:48:28 +00:00
if (sigidx) {
memcpy(sigidx, data + 0x00BF, 1);
}
2017-02-11 15:47:36 +00:00
2017-02-19 16:48:28 +00:00
if (sig) {
memcpy(sig, data + 0x00C0, 64);
}
2017-02-11 15:47:36 +00:00
return true;
}
2017-02-16 12:48:28 +00:00
2017-03-06 17:05:21 +00:00
#define KEYMASK(A, B, C) ((1 << (A - 1)) | (1 << (B - 1)) | (1 << (C - 1)))
2017-02-19 16:48:28 +00:00
const uint8_t *get_pubkey(uint8_t index)
2017-02-16 12:48:28 +00:00
{
2017-02-19 16:48:28 +00:00
switch (index) {
2017-03-06 17:05:21 +00:00
case KEYMASK(1, 2, 3):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(1, 2, 4):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(1, 2, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(1, 3, 4):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(1, 3, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(1, 4, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(2, 3, 4):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(2, 3, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(2, 4, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
case KEYMASK(3, 4, 5):
return (const uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
2017-02-19 16:48:28 +00:00
default:
return NULL;
}
}
bool check_signature(const uint8_t *start)
{
uint32_t codelen;
uint8_t sigidx;
uint8_t sig[64];
if (!parse_header(start, &codelen, &sigidx, sig)) {
return false;
}
2017-02-16 12:48:28 +00:00
uint8_t hash[BLAKE2S_DIGEST_LENGTH];
BLAKE2S_CTX ctx;
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
blake2s_Update(&ctx, start, 256 - 65);
2017-02-19 16:48:28 +00:00
for (int i = 0; i < 65; i++) {
blake2s_Update(&ctx, (const uint8_t *)"\x00", 1);
2017-02-19 16:48:28 +00:00
}
blake2s_Update(&ctx, start + 256, codelen);
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
2017-02-19 16:48:28 +00:00
const uint8_t *pub = get_pubkey(sigidx);
return pub && (0 == ed25519_sign_open(hash, BLAKE2S_DIGEST_LENGTH, *(const ed25519_public_key *)pub, *(const ed25519_signature *)sig));
2017-02-16 12:48:28 +00:00
}