13
13
#include <zephyr/logging/log.h>
14
14
#include <zephyr/sys/byteorder.h>
15
15
#include <zephyr/sys/util.h>
16
+ #include <zephyr/sys/printk.h>
16
17
17
18
#include <bluetooth/peer_manager/nrf_ble_lesc.h>
18
19
@@ -63,6 +64,8 @@ static nrf_ble_lesc_peer_oob_data_handler m_lesc_oobd_peer_handler;
63
64
#define ECC_PUB_KEY_UNCOMPRESSED_FORMAT_MARKER 0x04
64
65
#define ECC_PUB_KEY_EXPORT_SIZE \
65
66
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)
66
69
#define COORD_SIZE (BLE_GAP_LESC_P256_PK_LEN / 2)
67
70
68
71
/* 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)
126
129
127
130
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT ;
128
131
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
129
135
psa_set_key_usage_flags (& key_attributes , PSA_KEY_USAGE_DERIVE );
136
+ #endif
130
137
psa_set_key_lifetime (& key_attributes , PSA_KEY_LIFETIME_VOLATILE );
131
138
psa_set_key_algorithm (& key_attributes , PSA_ALG_ECDH );
132
139
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)
144
151
LOG_ERR ("psa_export_public_key() returned status %d" , status );
145
152
return NRF_ERROR_INTERNAL ;
146
153
}
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
+
147
174
/* Convert from big-endian to little-endian.
148
175
* Drop the first byte indicating the serialization format.
149
176
*/
0 commit comments