Skip to content

Commit 2dd6816

Browse files
lib: peer_manager: add option to export private LESC key for debugging
Add option to export private LESC key for debugging. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent ff28882 commit 2dd6816

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/peer_manager/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ zephyr_library_sources(peer_manager.c)
1010
zephyr_library_sources(nrf_strerror.c)
1111

1212
add_subdirectory(modules)
13+
14+
if(CONFIG_PM_LESC_PRIVATE_KEY_EXPORT)
15+
message(WARNING "CONFIG_PM_LESC_PRIVATE_KEY_EXPORT must only be used for debugging purposes. "
16+
"Not to be used in production!")
17+
endif()

lib/peer_manager/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ config PM_LESC_GENERATE_NEW_KEYS
8181
help
8282
New LESC keys are generated on the auth status event.
8383

84+
config PM_LESC_PRIVATE_KEY_EXPORT
85+
bool "Export private key for debugging purposes"
86+
help
87+
Export private key.
88+
This is for for debugging purposes only and is not to be used in production!
89+
8490
endif # PM_LESC_ENABLED
8591

8692
config PM_RA_PROTECTION_ENABLED

lib/peer_manager/modules/nrf_ble_lesc.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <zephyr/logging/log.h>
1414
#include <zephyr/sys/byteorder.h>
1515
#include <zephyr/sys/util.h>
16+
#include <zephyr/sys/printk.h>
1617

1718
#include <bluetooth/peer_manager/nrf_ble_lesc.h>
1819

@@ -63,6 +64,8 @@ static nrf_ble_lesc_peer_oob_data_handler m_lesc_oobd_peer_handler;
6364
#define ECC_PUB_KEY_UNCOMPRESSED_FORMAT_MARKER 0x04
6465
#define ECC_PUB_KEY_EXPORT_SIZE \
6566
PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1), 256)
67+
#define ECC_PRIV_KEY_EXPORT_SIZE \
68+
PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1), 256)
6669
#define COORD_SIZE (BLE_GAP_LESC_P256_PK_LEN / 2)
6770

6871
/* Convert an ECC (secp256r1) public key from between big-endian and little-endian.
@@ -126,7 +129,11 @@ uint32_t nrf_ble_lesc_keypair_generate(void)
126129

127130
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
128131

132+
#if defined(CONFIG_PM_LESC_PRIVATE_KEY_EXPORT)
133+
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
134+
#else
129135
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
136+
#endif
130137
psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE);
131138
psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
132139
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
@@ -144,6 +151,26 @@ uint32_t nrf_ble_lesc_keypair_generate(void)
144151
LOG_ERR("psa_export_public_key() returned status %d", status);
145152
return NRF_ERROR_INTERNAL;
146153
}
154+
155+
#if defined(CONFIG_PM_LESC_PRIVATE_KEY_EXPORT)
156+
uint8_t priv_key[ECC_PRIV_KEY_EXPORT_SIZE];
157+
size_t priv_key_len = 0;
158+
159+
LOG_WRN("CONFIG_PM_LESC_PRIVATE_KEY_EXPORT is not to be used in production!");
160+
status = psa_export_key(m_keypair_id, priv_key, sizeof(priv_key), &priv_key_len);
161+
if (status != PSA_SUCCESS) {
162+
LOG_ERR("psa_export_key() returned status %d", status);
163+
} else {
164+
printk("PRIV KEY: 0x");
165+
for (int i = 0; i < sizeof(priv_key); i++) {
166+
printk("%02x", priv_key[i]);
167+
}
168+
169+
printk("\n\n");
170+
}
171+
172+
#endif
173+
147174
/* Convert from big-endian to little-endian.
148175
* Drop the first byte indicating the serialization format.
149176
*/

0 commit comments

Comments
 (0)