Skip to content

Commit 530dc9d

Browse files
Add support for creating self-decrypting binaries, and use 4-way AES key shares instead of just the AES key (#207)
Note: this is not the final commit for this functionality, so use with caution for now * Use 4-way key shares for AES private keys The privateaes.bin key file is now 4x256bit numbers (A,B,C,D), and the AES key X is A^B^C^D * Remove check that ELF segments are between metadata blocks This is not required, as you can still load data outside of the region between the metadata blocks which contain the binary - for example, loading code into scratch memory. * Add enc_bootloader binary You can now use `picotool encrypt --embed ...` to create a self-decrypting binary, using enc_bootloader * Specify file types where useful for untyped files (json, pem, bin) * Implement FIB workaround by storing inverse of row n in row n+32 of each OTP page * Only delete existing load_maps when encrypting These only cause issues when encrypting, as the old block needs to be included in the new load_map When signing, the old load_map can be used again without issue * Throw clearer error when using picotool >2.1.1 with SDK <=2.1.1 This is required due to 2.1.0 and 2.1.1 SDK releases pointing at picotool develop branch rather than the respective picotool releases (raspberrypi/pico-sdk#2401) --------- Co-authored-by: Graham Sanderson <graham.sanderson@raspberrypi.com>
1 parent c56c005 commit 530dc9d

25 files changed

+3983
-247
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ jobs:
7373
picotool info -a flash_nuke.uf2
7474
7575
test-examples:
76+
# Prevent running twice for PRs from same repo
77+
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
78+
name: Test Build Examples
7679
runs-on: ubuntu-latest
7780
steps:
7881
- name: Checkout

BUILD.bazel

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ picotool_binary_data_header(
1717
out = "xip_ram_perms_elf.h",
1818
)
1919

20+
# TODO: Make it possible to build the prebuilt from source.
21+
picotool_binary_data_header(
22+
name = "enc_bootloader_elf",
23+
src = "//enc_bootloader:enc_bootloader_prebuilt",
24+
out = "enc_bootloader_elf.h",
25+
)
26+
27+
# TODO: Make it possible to build the prebuilt from source.
28+
picotool_binary_data_header(
29+
name = "enc_bootloader_mbedtls_elf",
30+
src = "//enc_bootloader:enc_bootloader_mbedtls_prebuilt",
31+
out = "enc_bootloader_mbedtls_elf.h",
32+
)
33+
2034
# TODO: Make it possible to build the prebuilt from source.
2135
picotool_binary_data_header(
2236
name = "flash_id_bin",
@@ -26,9 +40,9 @@ picotool_binary_data_header(
2640

2741
cc_library(
2842
name = "xip_ram_perms",
29-
srcs = ["xip_ram_perms.cpp"],
43+
srcs = ["get_xip_ram_perms.cpp"],
3044
hdrs = [
31-
"xip_ram_perms.h",
45+
"get_xip_ram_perms.h",
3246
"xip_ram_perms_elf.h",
3347
],
3448
deps = [
@@ -37,6 +51,20 @@ cc_library(
3751
],
3852
)
3953

54+
cc_library(
55+
name = "enc_bootloader",
56+
srcs = ["get_enc_bootloader.cpp"],
57+
hdrs = [
58+
"get_enc_bootloader.h",
59+
"enc_bootloader_elf.h",
60+
"enc_bootloader_mbedtls_elf.h",
61+
],
62+
deps = [
63+
"//bazel:data_locs",
64+
"//lib/whereami",
65+
],
66+
)
67+
4068
filegroup(
4169
name = "data_locs_header",
4270
srcs = ["data_locs.h"],
@@ -61,7 +89,8 @@ cc_binary(
6189
"otp.cpp",
6290
"otp.h",
6391
"rp2350.rom.h",
64-
"xip_ram_perms.cpp",
92+
"get_xip_ram_perms.cpp",
93+
"get_enc_bootloader.cpp",
6594
] + select({
6695
# MSVC can't handle long strings, so use this manually generated
6796
# header instead.
@@ -97,6 +126,7 @@ cc_binary(
97126
}),
98127
deps = [
99128
":xip_ram_perms",
129+
":enc_bootloader",
100130
"//bazel:data_locs",
101131
"//bintool",
102132
"//elf",

CMakeLists.txt

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ endif()
4242

4343
# todo better install paths for this
4444
set(DATA_LOCS "./" "${CMAKE_INSTALL_PREFIX}/${INSTALL_DATADIR}/")
45-
message(${DATA_LOCS})
4645
string(REGEX REPLACE ";" "\",\"" DATA_LOCS_VEC "${DATA_LOCS}")
4746
configure_file(data_locs.template.cpp ${CMAKE_CURRENT_BINARY_DIR}/data_locs.cpp)
4847

@@ -57,11 +56,49 @@ endif()
5756

5857
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
5958

59+
add_subdirectory(lib)
60+
61+
if (NOT DEFINED USE_PRECOMPILED)
62+
set(USE_PRECOMPILED true)
63+
endif()
64+
65+
# compile enc_bootloader.elf
66+
ExternalProject_Add(enc_bootloader
67+
PREFIX enc_bootloader
68+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/enc_bootloader
69+
BINARY_DIR ${CMAKE_BINARY_DIR}/enc_bootloader
70+
CMAKE_ARGS
71+
"-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}"
72+
"-DPICO_SDK_PATH:FILEPATH=${PICO_SDK_PATH}"
73+
"-DUSE_PRECOMPILED:BOOL=${USE_PRECOMPILED}"
74+
"-DUSE_MBEDTLS=0"
75+
"-DPICO_DEBUG_INFO_IN_RELEASE=OFF"
76+
BUILD_ALWAYS 1 # todo remove this
77+
INSTALL_COMMAND ""
78+
)
79+
80+
set(ENC_BOOTLOADER_ELF ${CMAKE_BINARY_DIR}/enc_bootloader/enc_bootloader.elf)
81+
82+
if (TARGET mbedtls)
83+
ExternalProject_Add(enc_bootloader_mbedtls
84+
PREFIX enc_bootloader_mbedtls
85+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/enc_bootloader
86+
BINARY_DIR ${CMAKE_BINARY_DIR}/enc_bootloader_mbedtls
87+
CMAKE_ARGS
88+
"-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}"
89+
"-DPICO_SDK_PATH:FILEPATH=${PICO_SDK_PATH}"
90+
"-DUSE_PRECOMPILED:BOOL=${USE_PRECOMPILED}"
91+
"-DUSE_MBEDTLS=1"
92+
"-DPICO_DEBUG_INFO_IN_RELEASE=OFF"
93+
BUILD_ALWAYS 1 # todo remove this
94+
INSTALL_COMMAND ""
95+
)
96+
97+
set(ENC_BOOTLOADER_MBEDTLS_ELF ${CMAKE_BINARY_DIR}/enc_bootloader_mbedtls/enc_bootloader.elf)
98+
endif()
99+
60100
if (NOT PICOTOOL_NO_LIBUSB)
61101
# compile xip_ram_perms.elf
62-
if (NOT DEFINED USE_PRECOMPILED)
63-
set(USE_PRECOMPILED true)
64-
endif()
65102
ExternalProject_Add(xip_ram_perms
66103
PREFIX xip_ram_perms
67104
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/xip_ram_perms
@@ -76,14 +113,6 @@ if (NOT PICOTOOL_NO_LIBUSB)
76113
)
77114

78115
set(XIP_RAM_PERMS_ELF ${CMAKE_BINARY_DIR}/xip_ram_perms/xip_ram_perms.elf)
79-
add_executable(xip_ram_perms_elf IMPORTED)
80-
add_dependencies(xip_ram_perms_elf xip_ram_perms)
81-
set_property(TARGET xip_ram_perms_elf PROPERTY IMPORTED_LOCATION ${XIP_RAM_PERMS_ELF})
82-
# copy xip_ram_perms.elf into build directory
83-
add_custom_command(TARGET xip_ram_perms
84-
COMMAND ${CMAKE_COMMAND} -E copy ${XIP_RAM_PERMS_ELF} ${CMAKE_BINARY_DIR}/xip_ram_perms.elf
85-
DEPENDS xip_ram_perms
86-
)
87116

88117
# compile flash_id
89118
ExternalProject_Add(flash_id
@@ -100,14 +129,6 @@ if (NOT PICOTOOL_NO_LIBUSB)
100129
)
101130

102131
set(FLASH_ID_BIN ${CMAKE_BINARY_DIR}/picoboot_flash_id/flash_id.bin)
103-
add_executable(flash_id_bin IMPORTED)
104-
add_dependencies(flash_id_bin flash_id)
105-
set_property(TARGET flash_id_bin PROPERTY IMPORTED_LOCATION ${FLASH_ID_BIN})
106-
# copy flash_id.bin into build directory
107-
add_custom_command(TARGET flash_id
108-
COMMAND ${CMAKE_COMMAND} -E copy ${FLASH_ID_BIN} ${CMAKE_BINARY_DIR}/flash_id.bin
109-
DEPENDS flash_id
110-
)
111132

112133
# We want to generate headers from WELCOME.HTM etc.
113134
ExternalProject_Add(otp_header_parser
@@ -169,7 +190,16 @@ if (NOT PICOTOOL_NO_LIBUSB)
169190
endif()
170191
endif()
171192

172-
add_custom_target(binary_data DEPENDS
193+
if (TARGET mbedtls)
194+
add_custom_target(embedded_data_no_libusb DEPENDS
195+
${CMAKE_CURRENT_BINARY_DIR}/enc_bootloader_elf.h
196+
${CMAKE_CURRENT_BINARY_DIR}/enc_bootloader_mbedtls_elf.h)
197+
else()
198+
add_custom_target(embedded_data_no_libusb DEPENDS
199+
${CMAKE_CURRENT_BINARY_DIR}/enc_bootloader_elf.h)
200+
endif()
201+
202+
add_custom_target(embedded_data DEPENDS
173203
${CMAKE_CURRENT_BINARY_DIR}/rp2350.rom.h
174204
${CMAKE_CURRENT_BINARY_DIR}/xip_ram_perms_elf.h
175205
${CMAKE_CURRENT_BINARY_DIR}/flash_id_bin.h)
@@ -188,6 +218,22 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xip_ram_perms_elf.h
188218
DEPENDS xip_ram_perms
189219
COMMENT "Configuring xip_ram_perms_elf.h"
190220
VERBATIM)
221+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/enc_bootloader_elf.h
222+
COMMAND ${CMAKE_COMMAND}
223+
-D BINARY_FILE=${ENC_BOOTLOADER_ELF}
224+
-D OUTPUT_NAME=enc_bootloader_elf
225+
-P ${CMAKE_CURRENT_LIST_DIR}/cmake/binh.cmake
226+
DEPENDS enc_bootloader
227+
COMMENT "Configuring enc_bootloader_elf.h"
228+
VERBATIM)
229+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/enc_bootloader_mbedtls_elf.h
230+
COMMAND ${CMAKE_COMMAND}
231+
-D BINARY_FILE=${ENC_BOOTLOADER_MBEDTLS_ELF}
232+
-D OUTPUT_NAME=enc_bootloader_mbedtls_elf
233+
-P ${CMAKE_CURRENT_LIST_DIR}/cmake/binh.cmake
234+
DEPENDS enc_bootloader_mbedtls
235+
COMMENT "Configuring enc_bootloader_mbedtls_elf.h"
236+
VERBATIM)
191237
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/flash_id_bin.h
192238
COMMAND ${CMAKE_COMMAND}
193239
-D BINARY_FILE=${FLASH_ID_BIN}
@@ -203,8 +249,6 @@ add_subdirectory(picoboot_connection)
203249
add_subdirectory(elf)
204250
add_subdirectory(elf2uf2)
205251

206-
add_subdirectory(lib)
207-
208252
add_subdirectory(bintool)
209253

210254
if (NOT PICOTOOL_NO_LIBUSB)
@@ -228,11 +272,13 @@ target_include_directories(regs_headers INTERFACE ${PICO_SDK_PATH}/src/rp2350/ha
228272
# Main picotool executable
229273
add_executable(picotool
230274
data_locs.cpp
275+
get_enc_bootloader.cpp
231276
${OTP_EXE}
232277
main.cpp)
278+
add_dependencies(picotool embedded_data_no_libusb)
233279
if (NOT PICOTOOL_NO_LIBUSB)
234-
target_sources(picotool PRIVATE xip_ram_perms.cpp)
235-
add_dependencies(picotool generate_otp_header xip_ram_perms_elf binary_data)
280+
target_sources(picotool PRIVATE get_xip_ram_perms.cpp)
281+
add_dependencies(picotool generate_otp_header embedded_data)
236282
endif()
237283
set(PROJECT_VERSION 2.1.2-develop)
238284
set(PICOTOOL_VERSION 2.1.2-develop)
@@ -327,6 +373,21 @@ install(FILES
327373
DESTINATION ${INSTALL_CONFIGDIR}
328374
)
329375

376+
#Install enc_bootloader.elf
377+
install(FILES
378+
${ENC_BOOTLOADER_ELF}
379+
DESTINATION ${INSTALL_DATADIR}
380+
)
381+
382+
if (TARGET mbedtls)
383+
#Install enc_bootloader_mbedtls.elf
384+
install(FILES
385+
${ENC_BOOTLOADER_MBEDTLS_ELF}
386+
DESTINATION ${INSTALL_DATADIR}
387+
RENAME enc_bootloader_mbedtls.elf
388+
)
389+
endif()
390+
330391
if (NOT PICOTOOL_NO_LIBUSB)
331392
if (NOT PICOTOOL_CODE_OTP)
332393
#Install the otp json

0 commit comments

Comments
 (0)