From eb481f1cda0a6eae1aa0486acb637f985cddcc2f Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 14 Nov 2018 22:47:04 +0900 Subject: known device/sram data persistence working --- src/demo.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index ad73cff..05875b8 100644 --- a/src/demo.c +++ b/src/demo.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -56,7 +57,9 @@ static struct NoiseState noise_state; -static uint8_t remote_key_reference[CURVE25519_KEY_LEN]; +static uint8_t remote_key_reference[BLAKE2S_HASH_SIZE] __attribute__((section(".backup_sram"))); +static uint8_t local_key[CURVE25519_KEY_LEN] __attribute__((section(".backup_sram"))); +static uint8_t identity_key_valid __attribute__((section(".backup_sram"))) = 0; void _fini(void); @@ -81,6 +84,9 @@ static void clock_setup(void) { rcc_periph_clock_enable(RCC_DMA2); rcc_periph_clock_enable(RCC_DMA1); + rcc_periph_clock_enable(RCC_PWR); + rcc_periph_clock_enable(RCC_BKPSRAM); + rcc_periph_clock_enable(RCC_RNG); } @@ -413,6 +419,8 @@ int main(void) { clock_setup(); gpio_setup(); + pwr_disable_backup_domain_write_protect(); + PWR_CSR |= PWR_CSR_BRE; /* Enable backup SRAM battery power regulator */ /* provides time_curr_us to usbh_poll function */ tim6_setup(); @@ -447,12 +455,17 @@ int main(void) LOG_PRINTF("Initializing RNG...\n"); rand_init(); - noise_state_init(&noise_state, remote_key_reference); + noise_state_init(&noise_state, remote_key_reference, local_key); /* FIXME load remote key from backup memory */ /* FIXME only run this on first boot and persist key in backup sram. Allow reset via jumper-triggered factory reset function. */ - LOG_PRINTF("Generating identity key...\n"); - if (generate_identity_key(&noise_state)) - LOG_PRINTF("Error generating identiy key\n"); + if (!identity_key_valid) { + LOG_PRINTF("Generating identity key...\n"); + if (generate_identity_key(&noise_state)) { + LOG_PRINTF("Error generating identiy key\n"); + } else { + identity_key_valid = 1; + } + } int poll_ctr = 0; while (23) { -- cgit