Skip to content

Commit 774cb88

Browse files
pi-anlclaude
andcommitted
stm32/mpbtstackport: Add ER/IR key generation for BTstack pairing.
Implements mp_bluetooth_btstack_port_set_er_ir_keys() function using STM32 hardware RNG to generate cryptographically secure Encryption Root (ER) and Identity Root (IR) keys required for Bluetooth pairing and bonding. This resolves the undefined reference error when building STM32 boards with MICROPY_BLUETOOTH_BTSTACK=1 and pairing/bonding enabled. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent baea340 commit 774cb88

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

ports/stm32/mpbtstackport.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "extmod/btstack/btstack_hci_uart.h"
3838
#include "extmod/btstack/modbluetooth_btstack.h"
3939
#include "mpbthciport.h"
40+
#include "rng.h"
4041

4142
void mp_bluetooth_hci_poll_in_ms(uint32_t ms);
4243

@@ -159,4 +160,20 @@ void mp_bluetooth_btstack_port_start(void) {
159160
hci_power_control(HCI_POWER_ON);
160161
}
161162

163+
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
164+
void mp_bluetooth_btstack_port_set_er_ir_keys(void) {
165+
// Generate cryptographically secure ER and IR keys using hardware RNG
166+
sm_key_t er_key, ir_key;
167+
168+
// Use STM32 hardware RNG to generate secure keys
169+
for (int i = 0; i < 16; i++) {
170+
er_key[i] = (uint8_t)(rng_get() & 0xFF);
171+
ir_key[i] = (uint8_t)(rng_get() & 0xFF);
172+
}
173+
174+
sm_set_er(er_key);
175+
sm_set_ir(ir_key);
176+
}
177+
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
178+
162179
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK

0 commit comments

Comments
 (0)