|
| 1 | +/* flash_otp_keystore.c |
| 2 | + * |
| 3 | + * Implementation for Flash based OTP keystore used as trust anchor |
| 4 | + * |
| 5 | + * |
| 6 | + * Copyright (C) 2024 wolfSSL Inc. |
| 7 | + * |
| 8 | + * This file is part of wolfBoot. |
| 9 | + * |
| 10 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * wolfBoot is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdint.h> |
| 26 | +#include <string.h> |
| 27 | +#include "wolfboot/wolfboot.h" |
| 28 | +#include "keystore.h" |
| 29 | +#include "hal.h" |
| 30 | + |
| 31 | +#ifdef FLASH_OTP_ROT |
| 32 | + |
| 33 | +#ifdef TARGET_stm32h7 |
| 34 | +#include "hal/stm32h7.h" |
| 35 | +#endif |
| 36 | + |
| 37 | +#define OTP_HDR_SIZE 16 |
| 38 | + |
| 39 | +struct wolfBoot_otp_hdr_size { |
| 40 | + char keystore_hdr_magic[8]; |
| 41 | + uint16_t item_count; |
| 42 | + uint16_t flags; |
| 43 | + uint32_t version; |
| 44 | +}; |
| 45 | + |
| 46 | +static const char KEYSTORE_HDR_MAGIC[8] = "WOLFBOOT"; |
| 47 | + |
| 48 | +#if !defined(KEYSTORE_ANY) && (KEYSTORE_PUBKEY_SIZE != KEYSTORE_PUBKEY_SIZE_ECC256) |
| 49 | + #error Key algorithm mismatch. Remove old keys via 'make keysclean' |
| 50 | +#else |
| 51 | + |
| 52 | +#define KEYSTORE_MAX_PUBKEYS ((OTP_SIZE - OTP_HDR_SIZE) / SIZEOF_KEYSTORE_SLOT) |
| 53 | + |
| 54 | +#if (KEYSTORE_MAX_PUBKEYS < 1) |
| 55 | + #error "No space for keystore in OTP with current algorithm" |
| 56 | +#endif |
| 57 | + |
| 58 | +int keystore_num_pubkeys(void) |
| 59 | +{ |
| 60 | + uint8_t otp_header[OTP_HDR_SIZE]; |
| 61 | + struct wolfBoot_otp_hdr_size *hdr = (struct wolfBoot_otp_hdr_size *)otp_header; |
| 62 | + if (hal_flash_otp_read(FLASH_OTP_BASE, (void *)otp_header, OTP_HDR_SIZE) != 0) |
| 63 | + return 0; |
| 64 | + if (memcmp(hdr->keystore_hdr_magic, KEYSTORE_HDR_MAGIC, 8) != 0) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + if (hdr->item_count > KEYSTORE_MAX_PUBKEYS) |
| 68 | + return 0; |
| 69 | + return hdr->item_count; |
| 70 | +} |
| 71 | + |
| 72 | +static uint16_t otp_slot_item_cache[SIZEOF_KEYSTORE_SLOT/2]; |
| 73 | + |
| 74 | +uint8_t *keystore_get_buffer(int id) |
| 75 | +{ |
| 76 | + struct keystore_slot *slot; |
| 77 | + if (id >= keystore_num_pubkeys()) |
| 78 | + return (uint8_t *)0; |
| 79 | + if (hal_flash_otp_read(FLASH_OTP_BASE + |
| 80 | + OTP_HDR_SIZE + id * SIZEOF_KEYSTORE_SLOT, otp_slot_item_cache, |
| 81 | + SIZEOF_KEYSTORE_SLOT) != 0) |
| 82 | + return (uint8_t *)0; |
| 83 | + slot = (struct keystore_slot *)otp_slot_item_cache; |
| 84 | + return slot->pubkey; |
| 85 | +} |
| 86 | + |
| 87 | +int keystore_get_size(int id) |
| 88 | +{ |
| 89 | + struct keystore_slot *slot; |
| 90 | + if (id >= keystore_num_pubkeys()) |
| 91 | + return -1; |
| 92 | + if (hal_flash_otp_read(FLASH_OTP_BASE + |
| 93 | + OTP_HDR_SIZE + id * SIZEOF_KEYSTORE_SLOT, otp_slot_item_cache, |
| 94 | + SIZEOF_KEYSTORE_SLOT) != 0) |
| 95 | + return -1; |
| 96 | + slot = (struct keystore_slot *)otp_slot_item_cache; |
| 97 | + return slot->pubkey_size; |
| 98 | +} |
| 99 | + |
| 100 | +uint32_t keystore_get_mask(int id) |
| 101 | +{ |
| 102 | + struct keystore_slot *slot; |
| 103 | + if (id >= keystore_num_pubkeys()) |
| 104 | + return 0; |
| 105 | + if (hal_flash_otp_read(FLASH_OTP_BASE + |
| 106 | + OTP_HDR_SIZE + id * SIZEOF_KEYSTORE_SLOT, otp_slot_item_cache, |
| 107 | + SIZEOF_KEYSTORE_SLOT) != 0) |
| 108 | + return 0; |
| 109 | + slot = (struct keystore_slot *)otp_slot_item_cache; |
| 110 | + return slot->part_id_mask; |
| 111 | +} |
| 112 | + |
| 113 | +uint32_t keystore_get_key_type(int id) |
| 114 | +{ |
| 115 | + struct keystore_slot *slot; |
| 116 | + if (id >= keystore_num_pubkeys()) |
| 117 | + return -1; |
| 118 | + if (hal_flash_otp_read(FLASH_OTP_BASE + |
| 119 | + OTP_HDR_SIZE + id * SIZEOF_KEYSTORE_SLOT, otp_slot_item_cache, |
| 120 | + SIZEOF_KEYSTORE_SLOT) != 0) |
| 121 | + return -1; |
| 122 | + slot = (struct keystore_slot *)otp_slot_item_cache; |
| 123 | + return slot->key_type; |
| 124 | +} |
| 125 | + |
| 126 | +#endif /* Keystore public key size check */ |
| 127 | + |
| 128 | +#endif /* FLASH_OTP_ROT */ |
0 commit comments