Skip to content

Commit d1eabc2

Browse files
committed
Keystore module for OTP in FLASH
1 parent 62a5b9f commit d1eabc2

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ ifeq ($(SIGN),NONE)
3535
PRIVATE_KEY=
3636
else
3737
PRIVATE_KEY=wolfboot_signing_private_key.der
38-
OBJS+=./src/keystore.o
38+
ifeq ($(FLASH_OTP_ROT),1)
39+
OBJS+=./src/flash_otp_keystore.o
40+
else
41+
OBJS+=./src/keystore.o
42+
endif
3943
endif
4044

4145
WOLFCRYPT_OBJS:=

include/keystore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct keystore_slot {
4343
uint8_t pubkey[KEYSTORE_PUBKEY_SIZE];
4444
};
4545

46+
#define SIZEOF_KEYSTORE_SLOT (32 + KEYSTORE_PUBKEY_SIZE)
47+
4648
/* KeyStore API */
4749
int keystore_num_pubkeys(void);
4850
#if defined(WOLFBOOT_RENESAS_SCEPROTECT) ||\

src/flash_otp_keystore.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 */

tools/keytools/keygen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ const char Keystore_API[] =
233233
"uint32_t keystore_get_mask(int id)\n"
234234
"{\n"
235235
" if (id >= keystore_num_pubkeys())\n"
236-
" return -1;\n"
236+
" return 0;\n"
237237
" return (int)PubKeys[id].part_id_mask;\n"
238238
"}\n\n"
239239
"uint32_t keystore_get_key_type(int id)\n"

0 commit comments

Comments
 (0)