Skip to content

Commit 4b2f32f

Browse files
committed
Fix enc_bootloader example OTP output
1 parent 0fbc465 commit 4b2f32f

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

bootloaders/encrypted/CMakeLists.txt

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ add_executable(enc_bootloader
44
aes.S
55
)
66

7-
# Copy otp.json file to build directory
8-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/otp.json
9-
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_LIST_DIR}/otp.json" "${CMAKE_CURRENT_BINARY_DIR}/otp.json"
10-
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/otp.json)
11-
add_custom_target(otp_json DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
12-
add_dependencies(enc_bootloader otp_json)
13-
147
# pull in common dependencies
158
target_link_libraries(enc_bootloader pico_stdlib pico_rand)
169

@@ -45,9 +38,6 @@ endfunction()
4538
# create linker script to run from 0x20078000
4639
add_linker_script(enc_bootloader "0x20078000" "32k")
4740

48-
# configure otp output
49-
pico_set_otp_key_output_file(enc_bootloader ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
50-
5141
# sign, hash, and clear SRAM
5242
pico_sign_binary(enc_bootloader ${CMAKE_CURRENT_LIST_DIR}/private.pem)
5343
pico_hash_binary(enc_bootloader)
@@ -82,6 +72,16 @@ pico_set_binary_type(hello_serial_enc no_flash)
8272
# create linker script to ensure it doesn't overwrite the bootloader at 0x20070000
8373
add_linker_script(hello_serial_enc "0x20000000" "448k")
8474

75+
# Copy otp.json file to build directory
76+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/otp.json
77+
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_LIST_DIR}/otp.json" "${CMAKE_CURRENT_BINARY_DIR}/otp.json"
78+
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/otp.json)
79+
add_custom_target(otp_json DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
80+
add_dependencies(hello_serial_enc otp_json)
81+
82+
# configure otp output
83+
pico_set_otp_key_output_file(hello_serial_enc ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
84+
8585
# sign, hash, and encrypt
8686
pico_sign_binary(hello_serial_enc ${CMAKE_CURRENT_LIST_DIR}/private.pem)
8787
pico_hash_binary(hello_serial_enc)

bootloaders/encrypted/aes.S

+38-21
Original file line numberDiff line numberDiff line change
@@ -278,31 +278,39 @@ init_rstate:
278278
str r1,[r4,#TRNG_TRNG_CONFIG_OFFSET -TRNG_RNG_IMR_OFFSET] @ turn off rand source and wipe SHA bits left in TRNG config; r1=0
279279
str r1,[r4,#TRNG_RND_SOURCE_ENABLE_OFFSET -TRNG_RNG_IMR_OFFSET]
280280
adds r5,r5,#SHA256_SUM0_OFFSET
281-
ldmia r5!,{r0-r3}
282-
ldr r5,=rstate_sha
283-
stmia r5,{r0-r3}
281+
@ r5=SHA256 SUM0 register (r5+4=SUM1, r4+8=SUM2, etc)
282+
ldmia r5,{r0-r3} @ load first 4 words of the 8 word SHA256 output
283+
ldr r6,=rstate_sha
284+
@ r5=SHA256 SUM0 register (r5+4=SUM1, r4+8=SUM2, etc), r6=rstate_sha
285+
stmia r6,{r0-r3}
284286
CHK_COUNT 26,6
285-
286-
@ r5=rstate_sha
287287
movs r0,#0
288-
strb r0,[r5] @ make sure rstate_sha[0] has byte 0 set to 0, representing "out of data"
289-
@ try to find a non-zero initialiser to create a non-degenerate LFSR
290-
ldr r1,[r5,#4]
291-
cbnz r1,1f @ is word 1 non-zero? then use it
292-
ldr r1,[r5,#8]
293-
cbnz r1,1f @ otherwise, is word 2 non-zero? use it
294-
ldr r1,[r5,#12]
295-
cbnz r1,1f @ otherwise, is word 3 non-zero? use it
296-
mov r1,r5 @ give up and use the address of rstate_sha (which is non-zero); this can't really happen (2^{-96} probability)
288+
strb r0,[r6] @ make sure rstate_sha[0] has byte 0 set to 0, representing "out of data"
289+
290+
@ try to find a non-zero initialiser to create a non-degenerate LFSR random state
291+
ldr r1,[r5,#16] @ SHA SUM4
292+
cbnz r1,1f @ is word 4 non-zero? then use it
293+
ldr r1,[r5,#20] @ SHA SUM5
294+
cbnz r1,1f @ otherwise, is word 5 non-zero? use it
295+
mov r1,r6 @ give up and use the address of rstate_sha (which is non-zero); this can't really happen (2^{-64} probability)
296+
1:
297+
str r1,[r6,#rstate_lfsr-rstate_sha]
298+
299+
@ try to find a non-zero initialiser to create a non-degenerate ROSC random state
300+
ldr r1,[r5,#24] @ SHA SUM6
301+
cbnz r1,1f @ is word 6 non-zero? then use it
302+
ldr r1,[r5,#28] @ SHA SUM7
303+
cbnz r1,1f @ otherwise, is word 7 non-zero? use it
304+
mov r1,r6 @ give up and use the address of rstate_sha (which is non-zero); this can't really happen (2^{-64} probability)
297305
1:
298-
str r1,[r5,#rstate_lfsr-rstate_sha]
299306
ldr r2,=ROSC_RANDOM_OFFSET+ROSC_BASE
300-
str r1,[r2,#0]
307+
str r1,[r2,#0] @ Initialise ROSC LFSR
301308
CHK_COUNT 27,6
309+
302310
.if GEN_RAND_SHA
303311
.if SH_JITTER
304312
movs r2,#0
305-
str r2,[r5,#jstate-rstate_sha]
313+
str r2,[r6,#jstate-rstate_sha]
306314
.endif
307315
.endif
308316

@@ -1655,14 +1663,23 @@ ctr_crypt_s:
16551663
pop {r1}
16561664
ldmia r1,{r8-r11} @ r8-r11 = IVshareB
16571665
clear03 32
1658-
bl gen_rand_sha_nonpres; eors r4,r4,r0; mov r8, r8, ror#16; eor r8, r8, r0,ror#16
1659-
bl gen_rand_sha_nonpres; eors r5,r5,r0; mov r9, r9, ror#16; eor r9, r9, r0,ror#16
1660-
bl gen_rand_sha_nonpres; eors r6,r6,r0; mov r10,r10,ror#16; eor r10,r10,r0,ror#16
1661-
bl gen_rand_sha_nonpres; eors r7,r7,r0; mov r11,r11,ror#16; eor r11,r11,r0,ror#16
1666+
bl gen_rand_sha_nonpres; eors r4,r4,r0; movs r1,#0; mov r8, r8, ror#16; eor r8, r8, r0,ror#16 @ Barriers between shares to prevent implicit r4^r8 etc
1667+
bl gen_rand_sha_nonpres; eors r5,r5,r0; movs r1,#0; mov r9, r9, ror#16; eor r9, r9, r0,ror#16
1668+
bl gen_rand_sha_nonpres; eors r6,r6,r0; movs r1,#0; mov r10,r10,ror#16; eor r10,r10,r0,ror#16
1669+
bl gen_rand_sha_nonpres; eors r7,r7,r0; movs r1,#0; mov r11,r11,ror#16; eor r11,r11,r0,ror#16
16621670
ldr r0,=IV0
16631671
stmia r0,{r4-r7}
16641672
adds r0,r0,#20
16651673
stmia r0,{r8-r11}
1674+
@ "Decommission" IV0 so that it doesn't get stacked
1675+
bl gen_rand_sha_nonpres; movs r4,r0
1676+
bl gen_rand_sha_nonpres; movs r5,r0
1677+
bl gen_rand_sha_nonpres; movs r6,r0
1678+
bl gen_rand_sha_nonpres; movs r7,r0
1679+
bl gen_rand_sha_nonpres; mov r8,r0
1680+
bl gen_rand_sha_nonpres; mov r9,r0
1681+
bl gen_rand_sha_nonpres; mov r10,r0
1682+
bl gen_rand_sha_nonpres; mov r11,r0
16661683
pop {r1,r2}
16671684
@ r1=cipher/plaintext buffer, r2=number of blocks
16681685

bootloaders/encrypted/enc_bootloader.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ int main() {
152152
uint16_t* otp_data = (uint16_t*)OTP_DATA_GUARDED_BASE;
153153

154154
decrypt(
155-
(uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & (OTP_KEY_PAGE * 0x40))]),
156-
(uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & ((OTP_KEY_PAGE + 1) * 0x40))]),
155+
(uint8_t*)&(otp_data[OTP_KEY_PAGE * 0x40]),
156+
(uint8_t*)&(otp_data[(OTP_KEY_PAGE + 1) * 0x40]),
157157
iv, (void*)SRAM_BASE, data_size/16
158158
);
159159

0 commit comments

Comments
 (0)