Skip to content

Commit d427fcf

Browse files
committed
Nordic BLE: Remove dependency to mbedtls for CryptoToolbox::ah.
1 parent fb9e0dc commit d427fcf

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/nRF5xCrypto.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "mbedtls/entropy.h"
3333
#include "mbedtls/ecp.h"
3434

35+
#endif
36+
3537
#include "platform/NonCopyable.h"
3638
#include "platform/CriticalSectionLock.h"
3739
#include "ble/BLETypes.h"
@@ -46,6 +48,8 @@ namespace pal {
4648
namespace vendor {
4749
namespace nordic {
4850

51+
#if defined(MBEDTLS_ECDH_C)
52+
4953
CryptoToolbox::CryptoToolbox() : _initialized(false) {
5054
mbedtls_entropy_init(&_entropy_context);
5155
mbedtls_ecp_group_init(&_group);
@@ -131,6 +135,8 @@ bool CryptoToolbox::generate_shared_secret(
131135
return err ? false : true;
132136
}
133137

138+
#endif
139+
134140
bool CryptoToolbox::ah(
135141
const ArrayView<const uint8_t, irk_size_>& irk,
136142
const ArrayView<const uint8_t, prand_size_>& prand,
@@ -161,6 +167,7 @@ bool CryptoToolbox::ah(
161167
return true;
162168
}
163169

170+
#if defined(MBEDTLS_ECDH_C)
164171

165172
void CryptoToolbox::load_mpi(mbedtls_mpi& dest, const ArrayView<const uint8_t, lesc_key_size_>& src) {
166173
ble::public_key_coord_t src_be = src.data();
@@ -173,6 +180,8 @@ void CryptoToolbox::store_mpi(ArrayView<uint8_t, lesc_key_size_>& dest, const mb
173180
swap_endian(dest.data(), dest.size());
174181
}
175182

183+
#endif
184+
176185
void CryptoToolbox::swap_endian(uint8_t* buf, size_t len) {
177186
for(size_t low = 0, high = (len - 1); high > low; --high, ++low) {
178187
std::swap(buf[low], buf[high]);
@@ -183,6 +192,3 @@ void CryptoToolbox::swap_endian(uint8_t* buf, size_t len) {
183192
} // vendor
184193
} // pal
185194
} // ble
186-
187-
#endif //defined(MBEDTLS_ECDH_C)
188-

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/nRF5xCrypto.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "mbedtls/entropy.h"
3232
#include "mbedtls/ecp.h"
3333

34+
#endif
35+
3436
#include "platform/NonCopyable.h"
3537
#include "ble/BLETypes.h"
3638

@@ -65,6 +67,8 @@ class CryptoToolbox : mbed::NonCopyable<CryptoToolbox> {
6567
*/
6668
static const ptrdiff_t prand_size_ = 3;
6769

70+
#if defined(MBEDTLS_ECDH_C)
71+
6872
/**
6973
* Create a new CryptoToolbox.
7074
*/
@@ -105,6 +109,8 @@ class CryptoToolbox : mbed::NonCopyable<CryptoToolbox> {
105109
ArrayView<uint8_t, lesc_key_size_> shared_secret
106110
);
107111

112+
#endif
113+
108114
/**
109115
* Execute the function ah. This function can be used to generate private
110116
* resolvable addresses and resolve them.
@@ -118,29 +124,33 @@ class CryptoToolbox : mbed::NonCopyable<CryptoToolbox> {
118124
*
119125
* @return true in case of success and false otherwise.
120126
*/
121-
bool ah(
127+
static bool ah(
122128
const ArrayView<const uint8_t, irk_size_>& irk,
123129
const ArrayView<const uint8_t, prand_size_>& prand,
124130
ArrayView<uint8_t, hash_size_> hash
125131
);
126132

127133
private:
134+
135+
#if defined(MBEDTLS_ECDH_C)
128136
void load_mpi(mbedtls_mpi& dest, const ArrayView<const uint8_t, lesc_key_size_>& src);
129137

130138
void store_mpi(ArrayView<uint8_t, lesc_key_size_>& dest, const mbedtls_mpi& src);
139+
#endif
131140

132-
void swap_endian(uint8_t* buf, size_t len);
141+
static void swap_endian(uint8_t* buf, size_t len);
133142

143+
#if defined(MBEDTLS_ECDH_C)
134144
bool _initialized;
135145
mbedtls_entropy_context _entropy_context;
136146
mbedtls_ecp_group _group;
147+
#endif
148+
137149
};
138150

139151
} // nordic
140152
} // vendor
141153
} // pal
142154
} // ble
143155

144-
#endif // defined(MBEDTLS_ECDH_C)
145-
146156
#endif // NRF5X_CRYPTO_

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/nRF5xPalSecurityManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ nRF5xSecurityManager::get_resolving_list() {
213213

214214
const nRF5xSecurityManager::resolving_list_entry_t*
215215
nRF5xSecurityManager::resolve_address(const address_t& resolvable_address) {
216-
#if defined(MBEDTLS_ECDH_C)
217216
typedef byte_array_t<CryptoToolbox::hash_size_> hash_t;
218217

219218
for (size_t i = 0; i < resolving_list_entry_count; ++i) {
@@ -222,7 +221,7 @@ nRF5xSecurityManager::resolve_address(const address_t& resolvable_address) {
222221

223222
// Compute the hash part from the random address part when the irk of
224223
// the entry is used
225-
_crypto.ah(
224+
CryptoToolbox::ah(
226225
make_const_ArrayView<CryptoToolbox::irk_size_>(entry.peer_irk),
227226
make_const_ArrayView<CryptoToolbox::prand_size_>(
228227
resolvable_address.data() + CryptoToolbox::hash_size_
@@ -237,7 +236,7 @@ nRF5xSecurityManager::resolve_address(const address_t& resolvable_address) {
237236
return &entry;
238237
}
239238
}
240-
#endif
239+
241240
return NULL;
242241
}
243242

0 commit comments

Comments
 (0)