Skip to content

Commit 46aed89

Browse files
committed
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.
1 parent b6ac07f commit 46aed89

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
lines changed

bootloaders/encrypted/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ Replace private.pem and privateaes.bin with your own keys - your signing key mus
44
openssl ecparam -name secp256k1 -genkey -out private.pem
55
```
66

7-
The AES key is just be a 32 byte binary file - you can create one with
7+
The AES key is stored as a 4-way share in a 128 byte binary file - you can create one with
88

99
```bash
10-
dd if=/dev/urandom of=privateaes.bin bs=1 count=32
10+
dd if=/dev/urandom of=privateaes.bin bs=1 count=128
11+
```
12+
13+
or in Powershell 7
14+
```powershell
15+
[byte[]] $(Get-SecureRandom -Maximum 256 -Count 128) | Set-Content privateaes.bin -AsByteStream
1116
```
1217

1318
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

bootloaders/encrypted/enc_bootloader.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,19 @@ int main() {
180180
init_lut_map();
181181
// Read key directly from OTP - guarded reads will throw a bus fault if there are any errors
182182
uint16_t* otp_data = (uint16_t*)OTP_DATA_GUARDED_BASE;
183-
init_key(rkey_s, (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x780)]));
183+
184+
// Temporary de-sharing - REMOVE THIS AND MODIFY ASM INSTEAD
185+
uint8_t* shared_key_a = (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x780)]);
186+
uint8_t* shared_key_b = (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x790)]);
187+
uint8_t* shared_key_c = (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x7A0)]);
188+
uint8_t* shared_key_d = (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x7B0)]);
189+
uint8_t deshared_key[32];
190+
for (int i=0; i < sizeof(deshared_key); i++) {
191+
deshared_key[i] = shared_key_a[i] ^ shared_key_b[i] ^ shared_key_c[i] ^ shared_key_d[i];
192+
}
193+
init_key(rkey_s, deshared_key);
194+
195+
// init_key(rkey_s, (uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & 0x780)]));
184196
otp_hw->sw_lock[30] = 0xf;
185197
flush_reg();
186198
ctr_crypt_s(iv, (void*)SRAM_BASE, data_size/16);

bootloaders/encrypted/otp.json

+98-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,108 @@
3535
"0xc0",
3636
"0xd0",
3737
"0xe0",
38-
"0xf0"
38+
"0xf0",
39+
"0x0f",
40+
"0x0e",
41+
"0x0d",
42+
"0x0c",
43+
"0x0b",
44+
"0x0a",
45+
"0x09",
46+
"0x08",
47+
"0x07",
48+
"0x06",
49+
"0x05",
50+
"0x04",
51+
"0x03",
52+
"0x02",
53+
"0x01",
54+
"0x00",
55+
"0xf0",
56+
"0xe0",
57+
"0xd0",
58+
"0xc0",
59+
"0xb0",
60+
"0xa0",
61+
"0x90",
62+
"0x80",
63+
"0x70",
64+
"0x60",
65+
"0x50",
66+
"0x40",
67+
"0x30",
68+
"0x20",
69+
"0x10",
70+
"0x00",
71+
"0x08",
72+
"0x09",
73+
"0x0a",
74+
"0x0b",
75+
"0x0c",
76+
"0x0d",
77+
"0x0e",
78+
"0x0f",
79+
"0x00",
80+
"0x01",
81+
"0x02",
82+
"0x03",
83+
"0x04",
84+
"0x05",
85+
"0x06",
86+
"0x07",
87+
"0x80",
88+
"0x90",
89+
"0xa0",
90+
"0xb0",
91+
"0xc0",
92+
"0xd0",
93+
"0xe0",
94+
"0xf0",
95+
"0x00",
96+
"0x10",
97+
"0x20",
98+
"0x30",
99+
"0x40",
100+
"0x50",
101+
"0x60",
102+
"0x70",
103+
"0x07",
104+
"0x06",
105+
"0x05",
106+
"0x04",
107+
"0x03",
108+
"0x02",
109+
"0x01",
110+
"0x00",
111+
"0x0f",
112+
"0x0e",
113+
"0x0d",
114+
"0x0c",
115+
"0x0b",
116+
"0x0a",
117+
"0x09",
118+
"0x08",
119+
"0x70",
120+
"0x60",
121+
"0x50",
122+
"0x40",
123+
"0x30",
124+
"0x20",
125+
"0x10",
126+
"0x00",
127+
"0xf0",
128+
"0xe0",
129+
"0xd0",
130+
"0xc0",
131+
"0xb0",
132+
"0xa0",
133+
"0x90",
134+
"0x80"
39135
]
40136
},
41137
"OTP_DATA_KEY1" : [ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 ],
42138
"OTP_DATA_KEY1_VALID" : "0x010101",
43139
"OTP_DATA_KEY2" : [ 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0 ],
44140
"OTP_DATA_KEY2_VALID" : "0x010101",
45141
"PAGE30_LOCK0" : "0x4a4a4a"
46-
}
142+
}

bootloaders/encrypted/privateaes.bin

96 Bytes
Binary file not shown.

bootloaders/encrypted/update-key.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (CMAKE_VERSION VERSION_LESS 3.19)
22
# Check if keyfile is not the default, and print warning
33
file(READ ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin key_file HEX)
4-
if (NOT ${key_file} STREQUAL "000102030405060708090a0b0c0d0e0f00102030405060708090a0b0c0d0e0f0")
4+
if (NOT ${key_file} STREQUAL "000102030405060708090a0b0c0d0e0f00102030405060708090a0b0c0d0e0f00f0e0d0c0b0a09080706050403020100f0e0d0c0b0a09080706050403020100008090a0b0c0d0e0f00010203040506078090a0b0c0d0e0f0001020304050607007060504030201000f0e0d0c0b0a09087060504030201000f0e0d0c0b0a09080")
55
message(WARNING
66
"Encrypted bootloader AES key not updated in otp.json file, as CMake version is < 3.19"
77
" - you will need to change the key in otp.json manually and re-run the build"

0 commit comments

Comments
 (0)