Skip to content

Commit 58cba25

Browse files
author
Cruz Monrreal
authored
Merge pull request #9372 from pan-/fix-cryptotoolbox
BLE - Nordic: Release crypto cell when not in use.
2 parents f04d51b + a10a10a commit 58cba25

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/nRF5xPalSecurityManager.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,17 @@ nRF5xSecurityManager::~nRF5xSecurityManager()
107107
ble_error_t nRF5xSecurityManager::initialize()
108108
{
109109
#if defined(MBEDTLS_ECDH_C)
110-
if (_crypto.generate_keys(
110+
// Note: we do not use the object on the stack as the CryptoToolbox is quite large
111+
// Please do not change or we risk a stack overflow.
112+
CryptoToolbox* crypto = new CryptoToolbox();
113+
bool success = crypto->generate_keys(
111114
make_ArrayView(X),
112115
make_ArrayView(Y),
113116
make_ArrayView(secret)
114-
)) {
115-
return BLE_ERROR_NONE;
116-
}
117+
);
118+
delete crypto;
117119

118-
return BLE_ERROR_INTERNAL_STACK_FAILURE;
120+
return success ? BLE_ERROR_NONE : BLE_ERROR_INTERNAL_STACK_FAILURE;
119121
#endif
120122
return BLE_ERROR_NONE;
121123
}
@@ -943,12 +945,16 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
943945
static const size_t key_size = public_key_coord_t::size_;
944946
ble_gap_lesc_dhkey_t shared_secret;
945947

946-
_crypto.generate_shared_secret(
948+
// Allocated on the heap to reduce stack pressure.
949+
// Risk stack overflows if allocated on stack.
950+
CryptoToolbox* crypto = new CryptoToolbox();
951+
crypto->generate_shared_secret(
947952
make_const_ArrayView<key_size>(dhkey_request.p_pk_peer->pk),
948953
make_const_ArrayView<key_size>(dhkey_request.p_pk_peer->pk + key_size),
949954
make_const_ArrayView(secret),
950955
shared_secret.key
951956
);
957+
delete crypto;
952958

953959
sd_ble_gap_lesc_dhkey_reply(connection, &shared_secret);
954960

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/nRF5xPalSecurityManager.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ class nRF5xSecurityManager : public ::ble::pal::SecurityManager {
360360

361361
pairing_control_block_t* _control_blocks;
362362
#if defined(MBEDTLS_ECDH_C)
363-
CryptoToolbox _crypto;
364363
ble::public_key_coord_t X;
365364
ble::public_key_coord_t Y;
366365
ble::public_key_coord_t secret;

0 commit comments

Comments
 (0)