Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE - Nordic: Release crypto cell when not in use. #9372

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BLE - Nordic: Release crypto cell when not in use.
Previously, the CryptoToolbox was allocated once as part of the security manager.
This was inneficient memory wise as it is only use to prepare key at initialization
and when we need to compute shared keys.
This was also inneficient power consumption wise as the Crypto cell was kept enabled even
when it wasn't used.

This fix creates a CryptoToolbox whenever it is needed and release it once it has fulfilled its
purpose. Note that CryptoToolbox allocation happens on the heap as mbed tls data structure are huge
and there's an high risk of crushing the stack.
pan- committed Jan 14, 2019
commit a10a10a3d72baf58bef91eff4767710c9bb3395f
Original file line number Diff line number Diff line change
@@ -111,15 +111,17 @@ nRF5xSecurityManager::~nRF5xSecurityManager()
ble_error_t nRF5xSecurityManager::initialize()
{
#if defined(MBEDTLS_ECDH_C)
if (_crypto.generate_keys(
// Note: we do not use the object on the stack as the CryptoToolbox is quite large
// Please do not change or we risk a stack overflow.
CryptoToolbox* crypto = new CryptoToolbox();
bool success = crypto->generate_keys(
make_ArrayView(X),
make_ArrayView(Y),
make_ArrayView(secret)
)) {
return BLE_ERROR_NONE;
}
);
delete crypto;

return BLE_ERROR_INTERNAL_STACK_FAILURE;
return success ? BLE_ERROR_NONE : BLE_ERROR_INTERNAL_STACK_FAILURE;
#endif
return BLE_ERROR_NONE;
}
@@ -934,12 +936,16 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
static const size_t key_size = public_key_coord_t::size_;
ble_gap_lesc_dhkey_t shared_secret;

_crypto.generate_shared_secret(
// Allocated on the heap to reduce stack pressure.
// Risk stack overflows if allocated on stack.
CryptoToolbox* crypto = new CryptoToolbox();
crypto->generate_shared_secret(
make_const_ArrayView<key_size>(dhkey_request.p_pk_peer->pk),
make_const_ArrayView<key_size>(dhkey_request.p_pk_peer->pk + key_size),
make_const_ArrayView(secret),
shared_secret.key
);
delete crypto;

sd_ble_gap_lesc_dhkey_reply(connection, &shared_secret);

Original file line number Diff line number Diff line change
@@ -360,7 +360,6 @@ class nRF5xSecurityManager : public ::ble::pal::SecurityManager {

pairing_control_block_t* _control_blocks;
#if defined(MBEDTLS_ECDH_C)
CryptoToolbox _crypto;
ble::public_key_coord_t X;
ble::public_key_coord_t Y;
ble::public_key_coord_t secret;