Skip to content

Commit c8fec85

Browse files
committed
OTP fixes to include UDS, added tests
1 parent ea75226 commit c8fec85

9 files changed

Lines changed: 119 additions & 29 deletions

File tree

.github/workflows/trustzone-emulator-tests.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,20 @@ jobs:
4848
working-directory: test-app/emu-test-apps
4949
run: |
5050
TARGET=stm32l5 ./test.sh
51+
52+
- name: Clean and build test with DICE attestation (stm32h5)
53+
run: |
54+
make clean distclean
55+
cp config/examples/stm32h5-tz-psa.config .config
56+
make
57+
m33mu wolfboot.bin test-app/image_v1_signed.bin:0x60000 --uart-stdout --expect-bkpt 0x7f --timeout 180
58+
59+
- name: Clean and build test with DICE attestation + OTP (stm32h5)
60+
run: |
61+
make clean distclean
62+
cp config/examples/stm32h5-tz-psa-otp.config .config
63+
make
64+
make -C tools/keytools/otp TARGET=stm32h5 otp-keystore-primer.bin otp-keystore-gen
65+
./tools/keytools/otp/otp-keystore-gen
66+
m33mu tools/keytools/otp/otp-keystore-primer.bin --persist --timeout 10 || true
67+
m33mu wolfboot.bin test-app/image_v1_signed.bin:0x60000 --uart-stdout --expect-bkpt 0x7f --timeout 180 --persist

hal/uart/uart_drv_stm32h5.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ int uart_tx(const uint8_t c)
130130
int uart_rx(uint8_t *c)
131131
{
132132
volatile uint32_t reg;
133-
int i = 0;
134133
reg = UART_ISR(USE_UART);
135134
if (reg & (UART_ENE | UART_EPE | UART_ORE | UART_EFE))
136135
uart_clear_errors(USE_UART);

include/user_settings.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ extern int tolower(int c);
164164
# define HAVE_ECC256
165165
# endif
166166
# if defined(WOLFBOOT_SIGN_ECC384) || \
167-
defined(WOLFBOOT_SIGN_SECONDARY_ECC384)
167+
defined(WOLFBOOT_SIGN_SECONDARY_ECC384) || \
168+
defined(WOLFCRYPT_SECURE_MODE)
168169
# define HAVE_ECC384
169170
# define WOLFSSL_SP_384
170171
# endif
171172
# if defined(WOLFBOOT_SIGN_ECC521) || \
172-
defined(WOLFBOOT_SIGN_SECONDARY_ECC521)
173+
defined(WOLFBOOT_SIGN_SECONDARY_ECC521) || \
174+
defined(WOLFCRYPT_SECURE_MODE)
173175
# define HAVE_ECC521
174176
# define WOLFSSL_SP_521
175177
# endif

options.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,14 @@ endif
616616
ifeq ($(NO_XIP),1)
617617
CFLAGS+=-D"NO_XIP"
618618
endif
619+
620+
ifeq ($(DEBUG_UART),1)
621+
CFLAGS+=-DDEBUG_UART
622+
UART_DRV_OBJ:=hal/uart/uart_drv_$(UART_TARGET).o
623+
ifneq ($(findstring $(UART_DRV_OBJ),$(OBJS)),$(UART_DRV_OBJ))
624+
OBJS+=$(UART_DRV_OBJ)
625+
endif
626+
endif
619627
ifeq ($(NO_QNX),1)
620628
CFLAGS+=-D"NO_QNX"
621629
endif
@@ -767,6 +775,7 @@ endif
767775

768776
ifeq ($(WOLFCRYPT_TZ_PSA),1)
769777
CFLAGS+=-DWOLFCRYPT_TZ_PSA
778+
CFLAGS+=-DWOLFCRYPT_SECURE_MODE
770779
CFLAGS+=-DWOLFSSL_PSA_ENGINE
771780
CFLAGS+=-DWOLFPSA_CUSTOM_STORE
772781
CFLAGS+=-DNO_DES3 -DNO_DES3_TLS_SUITES

src/arm_tee_psa_ipc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <wolfssl/wolfcrypt/types.h>
3232
#include <wolfboot/arm_tee_api.h>
3333
#include <wolfboot/dice.h>
34+
#include "printf.h"
3435

3536
/* Service IDs/handles aligned with ARM TEE defaults. */
3637
#define ARM_TEE_CRYPTO_SID (0x00000080U)
@@ -877,11 +878,15 @@ int32_t arm_tee_psa_call(psa_handle_t handle, int32_t type,
877878
return PSA_ERROR_INVALID_ARGUMENT;
878879
}
879880

881+
wolfBoot_printf("[ATTEST] GET_TOKEN: challenge_len=%u out_len=%u\r\n",
882+
(unsigned)challenge_vec->len, (unsigned)out_vec[0].len);
880883
dice_rc = wolfBoot_dice_get_token((const uint8_t *)challenge_vec->base,
881884
challenge_vec->len,
882885
(uint8_t *)out_vec[0].base,
883886
out_vec[0].len,
884887
&token_len);
888+
wolfBoot_printf("[ATTEST] GET_TOKEN: dice_rc=%d token_len=%u\r\n",
889+
dice_rc, (unsigned)token_len);
885890
status = wolfboot_attest_status(dice_rc);
886891
if (status == PSA_SUCCESS || status == PSA_ERROR_BUFFER_TOO_SMALL) {
887892
out_vec[0].len = token_len;
@@ -906,8 +911,12 @@ int32_t arm_tee_psa_call(psa_handle_t handle, int32_t type,
906911
}
907912

908913
challenge_size = (const rot_size_t *)in_vec[0].base;
914+
wolfBoot_printf("[ATTEST] GET_TOKEN_SIZE: challenge_size=%u\r\n",
915+
(unsigned)*challenge_size);
909916
dice_rc = wolfBoot_dice_get_token_size(*challenge_size,
910917
&token_size_native);
918+
wolfBoot_printf("[ATTEST] GET_TOKEN_SIZE: dice_rc=%d size=%u\r\n",
919+
dice_rc, (unsigned)token_size_native);
911920
status = wolfboot_attest_status(dice_rc);
912921
if (status != PSA_SUCCESS) {
913922
return status;

test-app/app_stm32h5.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,6 @@ static int run_attestation_test(void)
632632
challenge[i] = (uint8_t)i;
633633
}
634634

635-
status = psa_initial_attest_get_token_size(sizeof(challenge), &token_size);
636-
if (status != PSA_SUCCESS) {
637-
printf("attest: token size query failed (%d)\r\n", status);
638-
return -1;
639-
}
640-
printf("attest: token size is %lu bytes\r\n",
641-
(unsigned long)token_size);
642-
643635
status = psa_initial_attest_get_token(challenge, sizeof(challenge),
644636
token, sizeof(token), &token_size);
645637
if (status != PSA_SUCCESS) {
@@ -790,6 +782,13 @@ static int run_psa_boot_attestation(void)
790782
}
791783

792784
printf("PSA boot attestation: %s\r\n", ret == 0 ? "success" : "failed");
785+
786+
if (ret == 0)
787+
asm volatile ("bkpt #0x7f");
788+
else
789+
asm volatile ("bkpt #0x7e");
790+
791+
793792
return ret;
794793
}
795794
#endif

tools/keytools/otp/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ifeq ($(V),0)
88
endif
99

1010
WOLFBOOT_LIB_WOLFSSL?=../../../lib/wolfssl
11+
OTP_GEN_DEFS=
1112

1213
TARGET?=none
1314
ARCH?=ARM
@@ -16,7 +17,7 @@ CFLAGS+=-O0 -ggdb
1617
CFLAGS+=-I. -I../../../ -I../../../include -I$(WOLFBOOT_LIB_WOLFSSL) \
1718
-I$(WOLFBOOT_LIB_WOLFSSL)/wolfssl
1819
CFLAGS+=-I./wcs
19-
CFLAGS+=-DFLASH_OTP_KEYSTORE -D__FLASH_OTP_PRIMER -DWOLFSSL_USER_SETTINGS
20+
CFLAGS+=-DFLASH_OTP_KEYSTORE -D__FLASH_OTP_PRIMER -DWOLFSSL_USER_SETTINGS -DWOLFCRYPT_SECURE_MODE
2021
PRI_KS_OBJS+=startup.o otp-keystore-primer.o ../../../src/keystore.o
2122

2223
ifeq ($(HASH),SHA256)
@@ -35,11 +36,13 @@ ifeq ($(TARGET),stm32h7)
3536
CFLAGS+=-DTARGET_stm32h7
3637
CFLAGS+=-mcpu=cortex-m7 -ffunction-sections -fdata-sections -fno-common -ffreestanding -nostartfiles
3738
PRI_KS_OBJS+=stm32h7.o
39+
OTP_GEN_DEFS+=-DTARGET_stm32h7
3840
endif
3941
ifeq ($(TARGET),stm32h5)
4042
CFLAGS+=-DTARGET_stm32h5
4143
CFLAGS+=-mcpu=cortex-m33 -ffunction-sections -fdata-sections -fno-common -ffreestanding -nostartfiles
42-
PRI_KS_OBJS+=stm32h5.o
44+
PRI_KS_OBJS+=stm32h5.o stm32_tz.o
45+
OTP_GEN_DEFS+=-DTARGET_stm32h5
4346
endif
4447
CC=$(CROSS_COMPILE)gcc
4548
OBJCOPY?=$(CROSS_COMPILE)objcopy
@@ -54,7 +57,7 @@ all: otp-keystore-primer.bin otp-keystore-gen
5457
otp-keystore-gen: otp-keystore-gen.c
5558
gcc -o $@ otp-keystore-gen.c ../../../src/keystore.c -I. -I../../../ \
5659
-I../../../include -I$(WOLFBOOT_LIB_WOLFSSL) \
57-
-I$(WOLFBOOT_LIB_WOLFSSL)/wolfssl -DFLASH_OTP_KEYSTORE
60+
-I$(WOLFBOOT_LIB_WOLFSSL)/wolfssl -DFLASH_OTP_KEYSTORE $(OTP_GEN_DEFS)
5861

5962

6063
otp-keystore-primer.bin: otp-keystore-primer.elf
@@ -74,6 +77,9 @@ stm32h7.o: ../../../hal/stm32h7.c
7477
stm32h5.o: ../../../hal/stm32h5.c
7578
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
7679

80+
stm32_tz.o: ../../../hal/stm32_tz.c
81+
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
82+
7783

7884

7985

tools/keytools/otp/otp-keystore-gen.c

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include <unistd.h>
3131
#include <errno.h>
3232

33-
/* Define a generic max OTP size to appease otp_keystore.h */
34-
#ifndef OTP_SIZE
33+
/* Define a generic max OTP size to appease otp_keystore.h when no target is set. */
34+
#if !defined(OTP_SIZE) && !defined(TARGET_stm32h7) && !defined(TARGET_stm32h5)
3535
#define OTP_SIZE 4096
3636
#endif
3737

@@ -51,6 +51,11 @@ int main(void)
5151
uint32_t tot_len;
5252
int ofd;
5353
int slot_size;
54+
uint8_t *otp_buf = NULL;
55+
uint8_t uds[OTP_UDS_LEN];
56+
size_t offset;
57+
int rand_fd;
58+
ssize_t rlen;
5459

5560
memcpy(hdr.keystore_hdr_magic, KEYSTORE_HDR_MAGIC, 8);
5661
hdr.item_count = n_keys;
@@ -68,31 +73,65 @@ int main(void)
6873
slot_size += KEYSTORE_HDR_SIZE;
6974
fprintf(stderr, "Slot size: %d\n", slot_size);
7075
fprintf(stderr, "Number of slots: %d\n", n_keys);
71-
fprintf(stderr, "%s size: %d\n", outfile, (slot_size * n_keys) +
72-
(int)sizeof(struct wolfBoot_otp_hdr));
76+
tot_len = (uint32_t)sizeof(struct wolfBoot_otp_hdr) +
77+
(uint32_t)(slot_size * n_keys);
78+
fprintf(stderr, "%s keystore size: %u\n", outfile, tot_len);
79+
if (tot_len > OTP_UDS_OFFSET) {
80+
fprintf(stderr,
81+
"Error: keystore size %u exceeds OTP UDS offset %u\n",
82+
tot_len, (unsigned)OTP_UDS_OFFSET);
83+
exit(1);
84+
}
85+
86+
otp_buf = (uint8_t *)malloc(OTP_SIZE);
87+
if (otp_buf == NULL) {
88+
fprintf(stderr, "Error: out of memory allocating OTP buffer\n");
89+
exit(1);
90+
}
91+
memset(otp_buf, 0xFF, OTP_SIZE);
92+
93+
memcpy(otp_buf, &hdr, sizeof(hdr));
7394

7495
ofd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
7596
if (ofd < 0) {
7697
perror("opening output file");
98+
free(otp_buf);
7799
exit(2);
78100
}
79101

80-
/* Write the header to the beginning of the OTP binary file */
81-
if (write(ofd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
82-
fprintf(stderr, "Error writing to %s: %s\n", outfile, strerror(errno));
83-
}
84-
85102
for (i = 0; i < n_keys; i++) {
86103
/* Write each public key to its slot in OTP */
87-
if (write(ofd, &PubKeys[i],
88-
slot_size) < 0) {
89-
fprintf(stderr, "Error adding key %d to %s: %s\n", i, outfile,
90-
strerror(errno));
91-
exit(3);
92-
}
104+
offset = sizeof(hdr) + (size_t)i * (size_t)slot_size;
105+
memcpy(otp_buf + offset, &PubKeys[i], (size_t)slot_size);
106+
}
107+
108+
rand_fd = open("/dev/urandom", O_RDONLY);
109+
if (rand_fd < 0) {
110+
perror("opening /dev/urandom");
111+
close(ofd);
112+
free(otp_buf);
113+
exit(4);
114+
}
115+
rlen = read(rand_fd, uds, sizeof(uds));
116+
close(rand_fd);
117+
if (rlen != (ssize_t)sizeof(uds)) {
118+
fprintf(stderr, "Error: failed to read random UDS (%zd)\n", rlen);
119+
close(ofd);
120+
free(otp_buf);
121+
exit(5);
122+
}
123+
124+
memcpy(otp_buf + OTP_UDS_OFFSET, uds, sizeof(uds));
125+
126+
if (write(ofd, otp_buf, OTP_SIZE) != OTP_SIZE) {
127+
fprintf(stderr, "Error writing to %s: %s\n", outfile, strerror(errno));
128+
close(ofd);
129+
free(otp_buf);
130+
exit(3);
93131
}
94132
fprintf(stderr, "%s successfully created.\nGoodbye.\n", outfile);
95133
close(ofd);
134+
free(otp_buf);
96135

97136
return 0;
98137
}

zephyr/src/arm_tee_attest_api.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2222
*/
2323

24+
#include <stdio.h>
25+
2426
#include "psa/initial_attestation.h"
2527
#include "psa/client.h"
2628
#include "psa_manifest/sid.h"
@@ -42,10 +44,14 @@ psa_initial_attest_get_token(const uint8_t *auth_challenge,
4244
{ token_buf, token_buf_size }
4345
};
4446

47+
printf("[ATTEST-NS] get_token: chal=%u buf=%u\r\n",
48+
(unsigned)challenge_size, (unsigned)token_buf_size);
4549
status = psa_call(ARM_TEE_ATTESTATION_SERVICE_HANDLE,
4650
ARM_TEE_ATTEST_GET_TOKEN,
4751
in_vec, IOVEC_LEN(in_vec),
4852
out_vec, IOVEC_LEN(out_vec));
53+
printf("[ATTEST-NS] get_token: status=%ld len=%u\r\n",
54+
(long)status, (unsigned)out_vec[0].len);
4955

5056
if (status == PSA_SUCCESS && token_size != NULL) {
5157
*token_size = out_vec[0].len;
@@ -78,10 +84,14 @@ psa_initial_attest_get_token_size(size_t challenge_size,
7884
return PSA_ERROR_INVALID_ARGUMENT;
7985
}
8086

87+
printf("[ATTEST-NS] get_token_size: chal=%u\r\n",
88+
(unsigned)challenge_size);
8189
status = psa_call(ARM_TEE_ATTESTATION_SERVICE_HANDLE,
8290
ARM_TEE_ATTEST_GET_TOKEN_SIZE,
8391
in_vec, IOVEC_LEN(in_vec),
8492
out_vec, IOVEC_LEN(out_vec));
93+
printf("[ATTEST-NS] get_token_size: status=%ld size=%u\r\n",
94+
(long)status, (unsigned)token_size_param);
8595

8696
*token_size = token_size_param;
8797

0 commit comments

Comments
 (0)