Skip to content

Commit f7b6312

Browse files
authored
Encrypted improvements (#619)
* Use key share for AES file Update CMake tooling to use 128 byte key files (a 4-way share of the 32 byte key). Also temporarily update the enc_bootloader to deshare this key - the actual fix will need to be in aes.S. * Improve checking for malicious flash data Add data_max_size to prevent overwriting the bootloader with data from flash * Incorporate latest changes to aes.S Also shrink the space allocated for the bootloader to 32K (plus 8K scratch) * Encorporated latest encryption code with 4-way shares Also switch to random default key * Apply encrypted-example 6de8084b6eda * Add hello_encrypted example * Use new `enable_interrupts` function * Remove update-key.cmake This is not necessary anymore, now picotool writes the AES key to otp json files Fixes #613 * Add hello_encrypted to readme * Update enc_bootloader with latest aes.S (picotool 333d571c) CK_JITTER is removed as the enc_bootloader runs from XOSC not ROSC * Add IV salts * Update with latest aes.S * Update readmes This includes the changes from #553 * Add secret file to print out This is useful for testing decryption with large files * Add notes about unique AES keys, and not losing keys/salts * Update readmes * Fix enc_bootloader example OTP output * Remove OTP key locking functionality from encrypted examples * Improve TBYB sequence Add self check (1 == 1), which is only performed on first boot * Review fixups Comments and readme * Add MbedTLS self-decrypting example
1 parent 58327e2 commit f7b6312

21 files changed

+1911
-1586
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ add_subdirectory(cmake)
7171
add_subdirectory(dcp)
7272
add_subdirectory(divider)
7373
add_subdirectory(dma)
74+
add_subdirectory(encrypted)
7475
add_subdirectory(flash)
7576
add_subdirectory(gpio)
7677
add_subdirectory(hstx)

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ App|Description
8686
[channel_irq](dma/channel_irq) | Use an IRQ handler to reconfigure a DMA channel, in order to continuously drive data through a PIO state machine.
8787
[sniff_crc](dma/sniff_crc) | Use the DMA engine's 'sniff' capability to calculate a CRC32 on a data buffer.
8888

89+
### Encrypted (RP235x Only)
90+
91+
App|Description
92+
---|---
93+
[hello_encrypted](encrypted/hello_encrypted) | Create a self-decrypting binary.
94+
8995
### HSTX (RP235x Only)
9096

9197
App|Description

bootloaders/encrypted/CMakeLists.txt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ add_executable(enc_bootloader
44
aes.S
55
)
66

7-
# Add command to update otp.json if privateaes.bin changes
8-
add_custom_command(OUTPUT ${CMAKE_CURRENT_LIST_DIR}/otp.json
9-
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_LIST_DIR}/update-key.cmake"
10-
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin)
11-
# Copy that otp.json file to build directory
12-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/otp.json
13-
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_LIST_DIR}/otp.json" "${CMAKE_CURRENT_BINARY_DIR}/otp.json"
14-
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/otp.json)
15-
add_custom_target(otp_json DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
16-
add_dependencies(enc_bootloader otp_json)
17-
187
# pull in common dependencies
198
target_link_libraries(enc_bootloader pico_stdlib pico_rand)
209

@@ -46,11 +35,8 @@ function(add_linker_script target origin length)
4635
pico_set_linker_script(${target} ${CMAKE_CURRENT_BINARY_DIR}/${target}.ld)
4736
endfunction()
4837

49-
# create linker script to run from 0x20070000
50-
add_linker_script(enc_bootloader "0x20070000" "64k")
51-
52-
# configure otp output
53-
pico_set_otp_key_output_file(enc_bootloader ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
38+
# create linker script to run from 0x20078000
39+
add_linker_script(enc_bootloader "0x20078000" "32k")
5440

5541
# sign, hash, and clear SRAM
5642
pico_sign_binary(enc_bootloader ${CMAKE_CURRENT_LIST_DIR}/private.pem)
@@ -86,10 +72,13 @@ pico_set_binary_type(hello_serial_enc no_flash)
8672
# create linker script to ensure it doesn't overwrite the bootloader at 0x20070000
8773
add_linker_script(hello_serial_enc "0x20000000" "448k")
8874

75+
# configure otp output
76+
pico_set_otp_key_output_file(hello_serial_enc ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
77+
8978
# sign, hash, and encrypt
9079
pico_sign_binary(hello_serial_enc ${CMAKE_CURRENT_LIST_DIR}/private.pem)
9180
pico_hash_binary(hello_serial_enc)
92-
pico_encrypt_binary(hello_serial_enc ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin)
81+
pico_encrypt_binary(hello_serial_enc ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin ${CMAKE_CURRENT_LIST_DIR}/ivsalt.bin)
9382

9483
# package uf2 in flash
9584
pico_package_uf2_output(hello_serial_enc 0x10000000)

bootloaders/encrypted/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
Replace private.pem and privateaes.bin with your own keys - your signing key must be for the _secp256k1_ curve, in PEM format. You can create a .PEM file with:
1+
For security you **must** replace private.pem and privateaes.bin with your own keys, and ivsalt.bin with your own per-device salt. Make sure you **don't lose your keys and salts**, else you may not be able to update the code on your device.
2+
3+
Your signing key must be for the _secp256k1_ curve, in PEM format. You can create a .PEM file with:
24

35
```bash
46
openssl ecparam -name secp256k1 -genkey -out private.pem
57
```
68

7-
The AES key is just be a 32 byte binary file - you can create one with
9+
The AES key is stored as a 4-way share in a 128 byte binary file - you can create one with
10+
11+
```bash
12+
dd if=/dev/urandom of=privateaes.bin bs=1 count=128
13+
```
14+
15+
or in Powershell 7
16+
```powershell
17+
[byte[]] $(Get-SecureRandom -Maximum 256 -Count 128) | Set-Content privateaes.bin -AsByteStream
18+
```
19+
20+
The IV salt is just a 16 byte binary file - you can create it the same way, replacing `128` with `16` and `privateaes.bin` with `ivsalt.bin` in the commands above.
821

22+
You will need to program your OTP using the `otp.json` file generated by the build in your build folder
23+
NOTE: This will enable secure boot on your device, so only correctly signed binaries can then run, and will also lock down the OTP pages the AES key and IV salt are stored in.
924
```bash
10-
dd if=/dev/urandom of=privateaes.bin bs=1 count=32
25+
picotool otp load otp.json
1126
```
1227

28+
> For more information on security see chapter 10 of the [RP2350 datasheet](https://datasheets.raspberrypi.com/rp2350/rp2350-datasheet.pdf), and for information on how to sign other binaries to run on a secure chip see section 5.10
29+
1330
Then either drag & drop the UF2 files to the device in order (enc_bootloader first, then hello_serial_enc) waiting for a reboot in-between, or run
1431
```bash
1532
picotool load enc_bootloader.uf2

0 commit comments

Comments
 (0)