|
|
|
@ -20,6 +20,8 @@
|
|
|
|
|
#include "memory.h"
|
|
|
|
|
#include <libopencm3/stm32/flash.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include "blake2s.h"
|
|
|
|
|
#include "flash.h"
|
|
|
|
|
#include "sha2.h"
|
|
|
|
|
|
|
|
|
|
#define FLASH_OPTION_BYTES_1 (*(const uint64_t *)0x1FFFC000)
|
|
|
|
@ -82,3 +84,27 @@ int memory_bootloader_hash(uint8_t *hash) {
|
|
|
|
|
sha256_Raw(hash, 32, hash);
|
|
|
|
|
return 32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int memory_firmware_hash(const uint8_t *challenge, uint32_t challenge_size,
|
|
|
|
|
uint8_t hash[BLAKE2S_DIGEST_LENGTH]) {
|
|
|
|
|
BLAKE2S_CTX ctx;
|
|
|
|
|
if (challenge_size != 0) {
|
|
|
|
|
if (blake2s_InitKey(&ctx, BLAKE2S_DIGEST_LENGTH, challenge,
|
|
|
|
|
challenge_size) != 0) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = FLASH_CODE_SECTOR_FIRST; i <= FLASH_CODE_SECTOR_LAST; i++) {
|
|
|
|
|
uint32_t size = flash_sector_size(i);
|
|
|
|
|
const void *data = flash_get_address(i, 0, size);
|
|
|
|
|
if (data == NULL) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
blake2s_Update(&ctx, data, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);
|
|
|
|
|
}
|
|
|
|
|