aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hal_internal.h9
-rw-r--r--ks_flash.c20
2 files changed, 15 insertions, 14 deletions
diff --git a/hal_internal.h b/hal_internal.h
index 7de434e..0934718 100644
--- a/hal_internal.h
+++ b/hal_internal.h
@@ -277,16 +277,19 @@ extern const hal_rpc_pkey_dispatch_t hal_rpc_local_pkey_dispatch, hal_rpc_remote
#define HAL_STATIC_PKEY_STATE_BLOCKS 0
#endif
+/* This struct is ordered such that all metadata appears before the
+ * big buffers, in order for all metadata to be loaded with a single
+ * page read from e.g. the ks_flash module.
+ */
typedef struct {
hal_key_type_t type;
hal_curve_name_t curve;
hal_key_flags_t flags;
- uint32_t ks_internal; /* keystorage driver specific */
uint8_t in_use;
- uint8_t name[HAL_RPC_PKEY_NAME_MAX];
size_t name_len;
- uint8_t der[HAL_KS_WRAPPED_KEYSIZE];
size_t der_len;
+ uint8_t name[HAL_RPC_PKEY_NAME_MAX];
+ uint8_t der[HAL_KS_WRAPPED_KEYSIZE];
} hal_ks_key_t;
#ifndef HAL_PIN_SALT_LENGTH
diff --git a/ks_flash.c b/ks_flash.c
index 7fba710..9e78f64 100644
--- a/ks_flash.c
+++ b/ks_flash.c
@@ -64,10 +64,6 @@ extern int keystore_erase_sectors(uint32_t start, uint32_t stop);
*/
static hal_ks_keydb_t db[1];
-volatile uint32_t num_keys = 0;
-
-/* Offsets where we found the entrys */
-
#define FLASH_SECTOR_1_OFFSET (0 * KEYSTORE_SECTOR_SIZE)
#define FLASH_SECTOR_2_OFFSET (1 * KEYSTORE_SECTOR_SIZE)
@@ -114,7 +110,6 @@ const hal_ks_keydb_t *hal_ks_get_keydb(void)
offset = _get_key_offset(i, sizeof(*key));
if (offset > KEYSTORE_SECTOR_SIZE) {
memset(&db->keys[idx], 0, sizeof(*db->keys));
- db->keys[idx].ks_internal = offset;
idx++;
continue;
}
@@ -127,7 +122,6 @@ const hal_ks_keydb_t *hal_ks_get_keydb(void)
if (key->in_use == 0xff) {
/* unprogrammed data */
memset(&db->keys[idx], 0, sizeof(*db->keys));
- db->keys[idx].ks_internal = offset;
idx++;
continue;
}
@@ -148,11 +142,10 @@ const hal_ks_keydb_t *hal_ks_get_keydb(void)
to_read &= PAGE_SIZE_MASK;
if (to_read) {
- /* Partial last sector. We can only read full sectors so load it into page_buf. */
+ /* Partial last page. We can only read full pages so load it into page_buf. */
if (keystore_read_data(offset + sizeof(*key) - to_read, page_buf, sizeof(page_buf)) != 1) return NULL;
memcpy(dst, page_buf, to_read);
}
- key->ks_internal = offset;
}
}
@@ -187,20 +180,22 @@ hal_error_t _write_db_to_flash(const uint32_t sector_offset)
{
hal_error_t status;
uint8_t page_buf[KEYSTORE_PAGE_SIZE];
- uint32_t i, offset = sector_offset;
+ uint32_t i, offset;
if (sizeof(db->wheel_pin) + sizeof(db->so_pin) + sizeof(db->user_pin) > sizeof(page_buf)) {
return HAL_ERROR_BAD_ARGUMENTS;
}
- /* Write PINs into the second of the two reserved pages at the start of the sector. */
- offset += KEYSTORE_PAGE_SIZE;
+ /* Put the three PINs into page_buf */
+ offset = 0;
memcpy(page_buf + offset, &db->wheel_pin, sizeof(db->wheel_pin));
offset += sizeof(db->wheel_pin);
memcpy(page_buf + offset, &db->so_pin, sizeof(db->so_pin));
offset += sizeof(db->so_pin);
memcpy(page_buf + offset, &db->user_pin, sizeof(db->user_pin));
+ /* Write PINs into the second of the two reserved pages at the start of the sector. */
+ offset = sector_offset + KEYSTORE_PAGE_SIZE;
if ((status = _write_data_to_flash(offset, page_buf, sizeof(page_buf))) != HAL_OK) {
return status;
}
@@ -251,6 +246,9 @@ hal_error_t hal_ks_set_keydb(const hal_ks_key_t * const key,
}
tmp_key = (hal_ks_key_t *) page_buf;
+ db->keys[loc] = *key;
+ db->keys[loc].in_use = 1;
+
if (tmp_key->in_use == 0xff) {
/* Key slot was unused in flash. Write the new key there. */
if ((status = _write_data_to_flash(offset, (uint8_t *) key, sizeof(*db->keys))) != HAL_OK) {