2017-04-01 00:32:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "blake2s.h"
|
|
|
|
#include "ed25519-donna/ed25519.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
2017-10-26 17:09:53 +00:00
|
|
|
#include "flash.h"
|
2017-04-01 10:57:14 +00:00
|
|
|
#include "image.h"
|
2017-04-01 00:32:05 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
static secbool compute_pubkey(uint8_t sig_m, uint8_t sig_n, const uint8_t * const *pub, uint8_t sigmask, ed25519_public_key res)
|
2017-04-01 00:32:05 +00:00
|
|
|
{
|
2017-10-26 21:51:39 +00:00
|
|
|
if (!sig_m || !sig_n) return secfalse;
|
|
|
|
if (sig_m > sig_n) return secfalse;
|
2017-04-01 20:30:10 +00:00
|
|
|
|
2017-09-29 15:31:59 +00:00
|
|
|
// discard bits higher than sig_n
|
|
|
|
sigmask &= ((1 << sig_n) - 1);
|
2017-04-01 20:30:10 +00:00
|
|
|
|
2017-09-29 15:31:59 +00:00
|
|
|
// remove if number of set bits in sigmask is not equal to sig_m
|
2017-10-26 21:51:39 +00:00
|
|
|
if (__builtin_popcount(sigmask) != sig_m) return secfalse;
|
2017-04-02 00:25:55 +00:00
|
|
|
|
2017-09-29 15:31:59 +00:00
|
|
|
ed25519_public_key keys[sig_m];
|
2017-04-02 00:25:55 +00:00
|
|
|
int j = 0;
|
2017-09-29 15:31:59 +00:00
|
|
|
for (int i = 0; i < sig_n; i++) {
|
2017-04-02 00:25:55 +00:00
|
|
|
if ((1 << i) & sigmask) {
|
2017-09-29 15:31:59 +00:00
|
|
|
memcpy(keys[j], pub[i], 32);
|
2017-04-02 00:25:55 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 20:30:10 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (0 == ed25519_cosi_combine_publickeys(res, keys, sig_m));
|
2017-04-01 00:32:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool load_image_header(const uint8_t * const data, const uint32_t magic, const uint32_t maxsize, uint8_t key_m, uint8_t key_n, const uint8_t * const *keys, image_header * const hdr)
|
2017-04-01 00:32:05 +00:00
|
|
|
{
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&hdr->magic, data, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (hdr->magic != magic) return secfalse;
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
memcpy(&hdr->hdrlen, data + 4, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (hdr->hdrlen != IMAGE_HEADER_SIZE) return secfalse;
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
memcpy(&hdr->expiry, data + 8, 4);
|
2017-10-19 16:45:57 +00:00
|
|
|
// TODO: expiry mechanism needs to be ironed out before production or those
|
|
|
|
// devices won't accept expiring bootloaders (due to boardloader write protection).
|
2017-10-26 21:51:39 +00:00
|
|
|
if (hdr->expiry != 0) return secfalse;
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
memcpy(&hdr->codelen, data + 12, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (hdr->codelen > (maxsize - hdr->hdrlen)) return secfalse;
|
|
|
|
if ((hdr->hdrlen + hdr->codelen) < 4 * 1024) return secfalse;
|
|
|
|
if ((hdr->hdrlen + hdr->codelen) % 512 != 0) return secfalse;
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
memcpy(&hdr->version, data + 16, 4);
|
|
|
|
|
2017-10-26 13:10:29 +00:00
|
|
|
memcpy(hdr->hashes, data + 32, 512);
|
2017-04-05 13:10:33 +00:00
|
|
|
|
2017-10-26 00:09:06 +00:00
|
|
|
memcpy(&hdr->sigmask, data + IMAGE_HEADER_SIZE - IMAGE_SIG_SIZE, 1);
|
2017-04-05 13:10:33 +00:00
|
|
|
|
2017-10-26 00:09:06 +00:00
|
|
|
memcpy(hdr->sig, data + IMAGE_HEADER_SIZE - IMAGE_SIG_SIZE + 1, IMAGE_SIG_SIZE - 1);
|
2017-04-05 13:10:33 +00:00
|
|
|
|
2017-10-26 13:10:29 +00:00
|
|
|
// check header signature
|
2017-04-05 13:10:33 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
uint8_t hash[BLAKE2S_DIGEST_LENGTH];
|
|
|
|
BLAKE2S_CTX ctx;
|
|
|
|
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
|
2017-10-26 00:09:06 +00:00
|
|
|
blake2s_Update(&ctx, data, IMAGE_HEADER_SIZE - IMAGE_SIG_SIZE);
|
|
|
|
for (int i = 0; i < IMAGE_SIG_SIZE; i++) {
|
2017-04-01 00:32:05 +00:00
|
|
|
blake2s_Update(&ctx, (const uint8_t *)"\x00", 1);
|
|
|
|
}
|
|
|
|
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
|
|
|
|
|
2017-04-02 00:25:55 +00:00
|
|
|
ed25519_public_key pub;
|
2017-10-26 21:51:39 +00:00
|
|
|
if (!compute_pubkey(key_m, key_n, keys, hdr->sigmask, pub)) return secfalse;
|
2017-04-01 00:32:05 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (0 == ed25519_sign_open(hash, BLAKE2S_DIGEST_LENGTH, pub, *(const ed25519_signature *)hdr->sig));
|
2017-04-01 00:32:05 +00:00
|
|
|
}
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool load_vendor_header(const uint8_t * const data, uint8_t key_m, uint8_t key_n, const uint8_t * const *keys, vendor_header * const vhdr)
|
2017-04-01 17:24:41 +00:00
|
|
|
{
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&vhdr->magic, data, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (vhdr->magic != 0x565A5254) return secfalse; // TRZV
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&vhdr->hdrlen, data + 4, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (vhdr->hdrlen > 64 * 1024) return secfalse;
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&vhdr->expiry, data + 8, 4);
|
2017-10-26 21:51:39 +00:00
|
|
|
if (vhdr->expiry != 0) return secfalse;
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&vhdr->version, data + 12, 2);
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-04-05 13:10:33 +00:00
|
|
|
memcpy(&vhdr->vsig_m, data + 14, 1);
|
|
|
|
memcpy(&vhdr->vsig_n, data + 15, 1);
|
2017-10-05 15:31:05 +00:00
|
|
|
memcpy(&vhdr->vtrust, data + 16, 1);
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-04-05 15:41:10 +00:00
|
|
|
if (vhdr->vsig_n > MAX_VENDOR_PUBLIC_KEYS) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-04-05 15:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 13:10:33 +00:00
|
|
|
for (int i = 0; i < vhdr->vsig_n; i++) {
|
2017-10-05 15:31:05 +00:00
|
|
|
vhdr->vpub[i] = data + 32 + i * 32;
|
2017-04-01 17:24:41 +00:00
|
|
|
}
|
2017-04-05 15:41:10 +00:00
|
|
|
for (int i = vhdr->vsig_n; i < MAX_VENDOR_PUBLIC_KEYS; i++) {
|
2017-04-05 13:10:33 +00:00
|
|
|
vhdr->vpub[i] = 0;
|
2017-04-01 17:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:31:05 +00:00
|
|
|
memcpy(&vhdr->vstr_len, data + 32 + vhdr->vsig_n * 32, 1);
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-05 15:31:05 +00:00
|
|
|
vhdr->vstr = data + 32 + vhdr->vsig_n * 32 + 1;
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-05 15:31:05 +00:00
|
|
|
vhdr->vimg = data + 32 + vhdr->vsig_n * 32 + 1 + vhdr->vstr_len;
|
2017-04-01 17:24:41 +00:00
|
|
|
// align to 4 bytes
|
2017-04-05 13:10:33 +00:00
|
|
|
vhdr->vimg += (-(uintptr_t)vhdr->vimg) & 3;
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-26 00:09:06 +00:00
|
|
|
memcpy(&vhdr->sigmask, data + vhdr->hdrlen - IMAGE_SIG_SIZE, 1);
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-26 00:09:06 +00:00
|
|
|
memcpy(vhdr->sig, data + vhdr->hdrlen - IMAGE_SIG_SIZE + 1, IMAGE_SIG_SIZE - 1);
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-26 13:10:29 +00:00
|
|
|
// check header signature
|
2017-04-01 17:24:41 +00:00
|
|
|
|
|
|
|
uint8_t hash[BLAKE2S_DIGEST_LENGTH];
|
|
|
|
BLAKE2S_CTX ctx;
|
|
|
|
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
|
2017-10-26 00:09:06 +00:00
|
|
|
blake2s_Update(&ctx, data, vhdr->hdrlen - IMAGE_SIG_SIZE);
|
|
|
|
for (int i = 0; i < IMAGE_SIG_SIZE; i++) {
|
2017-04-01 17:24:41 +00:00
|
|
|
blake2s_Update(&ctx, (const uint8_t *)"\x00", 1);
|
|
|
|
}
|
|
|
|
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
|
|
|
|
|
2017-04-02 00:25:55 +00:00
|
|
|
ed25519_public_key pub;
|
2017-10-26 21:51:39 +00:00
|
|
|
if (!compute_pubkey(key_m, key_n, keys, vhdr->sigmask, pub)) return secfalse;
|
2017-04-01 17:24:41 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (0 == ed25519_sign_open(hash, BLAKE2S_DIGEST_LENGTH, pub, *(const ed25519_signature *)vhdr->sig));
|
2017-04-01 17:24:41 +00:00
|
|
|
}
|
2017-10-26 15:16:59 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
static secbool check_hash(const uint8_t * const hash, const uint8_t * const data, int len)
|
2017-10-26 15:16:59 +00:00
|
|
|
{
|
|
|
|
uint8_t h[BLAKE2S_DIGEST_LENGTH];
|
|
|
|
blake2s(data, len, h, BLAKE2S_DIGEST_LENGTH);
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue * (0 == memcmp(h, hash, BLAKE2S_DIGEST_LENGTH));
|
2017-10-26 15:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool check_image_contents(const image_header * const hdr, uint32_t firstskip, const uint8_t *sectors, int blocks)
|
2017-10-26 15:16:59 +00:00
|
|
|
{
|
2017-10-26 17:09:53 +00:00
|
|
|
if (!sectors || blocks < 1) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-26 17:09:53 +00:00
|
|
|
}
|
|
|
|
const void *data = (const void *)(FLASH_SECTOR_TABLE[sectors[0]] + firstskip);
|
2017-10-26 15:16:59 +00:00
|
|
|
int remaining = hdr->codelen;
|
2017-10-26 17:09:53 +00:00
|
|
|
if (!check_hash(hdr->hashes, data, MIN(remaining, IMAGE_CHUNK_SIZE - firstskip))) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-26 15:16:59 +00:00
|
|
|
}
|
|
|
|
int block = 1;
|
2017-10-26 16:53:20 +00:00
|
|
|
remaining -= IMAGE_CHUNK_SIZE - firstskip;
|
2017-10-26 15:16:59 +00:00
|
|
|
while (remaining > 0) {
|
2017-10-26 17:09:53 +00:00
|
|
|
if (block >= blocks) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-26 15:16:59 +00:00
|
|
|
}
|
2017-10-26 17:09:53 +00:00
|
|
|
data = (const void *)FLASH_SECTOR_TABLE[sectors[block]];
|
|
|
|
if (!check_hash(hdr->hashes + block * 32, data, MIN(remaining, IMAGE_CHUNK_SIZE))) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-26 15:16:59 +00:00
|
|
|
}
|
|
|
|
block++;
|
|
|
|
remaining -= IMAGE_CHUNK_SIZE;
|
|
|
|
}
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
2017-10-26 15:16:59 +00:00
|
|
|
}
|