Skip to content

Commit 0c3d9e2

Browse files
authored
Merge pull request #47 from pan-/privacy-sd-4
Nordic: Port privacy to softdevice v4.
2 parents 6c44a78 + 37c036c commit 0c3d9e2

File tree

4 files changed

+89
-104
lines changed

4 files changed

+89
-104
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xGap.cpp

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "nRF5xPalSecurityManager.h"
2929

3030
using ble::pal::vendor::nordic::nRF5xSecurityManager;
31-
typedef nRF5xSecurityManager::resolving_list_entry_t resolving_list_entry_t;
3231
using ble::ArrayView;
3332
using ble::pal::advertising_peer_address_type_t;
3433

@@ -316,6 +315,20 @@ ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
316315
}
317316
}
318317
adv_para.p_whitelist = &whitelist;
318+
#else
319+
if (_privacy_enabled) {
320+
bool enable_resolution =
321+
_peripheral_privacy_configuration.resolution_strategy != PeripheralPrivacyConfiguration_t::DO_NOT_RESOLVE;
322+
update_identities_list(enable_resolution);
323+
324+
if (_peripheral_privacy_configuration.use_non_resolvable_random_address &&
325+
is_advertising_non_connectable(params)
326+
) {
327+
set_private_non_resolvable_address();
328+
} else {
329+
set_private_resolvable_address();
330+
}
331+
}
319332
#endif
320333
/* For NRF_SD_BLE_API_VERSION >= 3 nRF5xGap::setWhitelist setups the whitelist. */
321334

@@ -384,7 +397,13 @@ ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams)
384397
scanParams.interval = scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
385398
scanParams.window = scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
386399
scanParams.timeout = scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
400+
387401
if (_privacy_enabled) {
402+
bool enable_resolution =
403+
_central_privacy_configuration.resolution_strategy != CentralPrivacyConfiguration_t::DO_NOT_RESOLVE;
404+
405+
update_identities_list(enable_resolution);
406+
388407
if (_central_privacy_configuration.use_non_resolvable_random_address) {
389408
set_private_non_resolvable_address();
390409
} else {
@@ -524,7 +543,15 @@ ble_error_t nRF5xGap::connect(const Address_t peerAddr,
524543
} else {
525544
addr.addr_id_peer = 0;
526545
}
527-
546+
547+
if (_privacy_enabled) {
548+
bool enable_resolution =
549+
_central_privacy_configuration.resolution_strategy != CentralPrivacyConfiguration_t::DO_NOT_RESOLVE;
550+
551+
update_identities_list(enable_resolution);
552+
set_private_resolvable_address();
553+
}
554+
528555
#endif
529556

530557
if (scanParamsIn != NULL) {
@@ -1201,6 +1228,37 @@ void nRF5xGap::processDisconnectionEvent(
12011228
);
12021229
}
12031230

1231+
ble_error_t nRF5xGap::update_identities_list(bool resolution_enabled)
1232+
{
1233+
uint32_t err;
1234+
1235+
if (resolution_enabled) {
1236+
ArrayView<ble_gap_id_key_t> entries = get_sm().get_resolving_list();
1237+
size_t limit = std::min(
1238+
entries.size(), (size_t) YOTTA_CFG_IRK_TABLE_MAX_SIZE
1239+
);
1240+
ble_gap_id_key_t* id_keys_pp[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
1241+
1242+
for (size_t i = 0; i < limit; ++i) {
1243+
id_keys_pp[i] = &entries[i];
1244+
}
1245+
1246+
err = sd_ble_gap_device_identities_set(
1247+
limit ? id_keys_pp : NULL,
1248+
/* use the local IRK for all devices */ NULL,
1249+
limit
1250+
);
1251+
} else {
1252+
err = sd_ble_gap_device_identities_set(
1253+
NULL,
1254+
/* use the local IRK for all devices */ NULL,
1255+
0
1256+
);
1257+
}
1258+
1259+
return err ? BLE_ERROR_INVALID_STATE : BLE_ERROR_NONE;
1260+
}
1261+
12041262
void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t& evt) {
12051263
using BLEProtocol::AddressType;
12061264

@@ -1246,14 +1304,8 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
12461304

12471305

12481306
if (private_peer_known) {
1249-
// FIXME: Is this correct for SD > 2 ?
1250-
const resolving_list_entry_t* entry = get_sm().resolve_address(
1251-
evt.peer_addr.addr
1252-
);
1253-
MBED_ASSERT(entry == NULL);
1254-
1255-
peer_addr_type = convert_identity_address(entry->peer_identity_address_type);
1256-
peer_address = entry->peer_identity_address.data();
1307+
peer_addr_type = convert_nordic_address(evt.peer_addr.addr_type);;
1308+
peer_address = evt.peer_addr.addr;
12571309
peer_resolvable_address = evt.peer_addr.addr;
12581310
} else {
12591311
if (_privacy_enabled &&
@@ -1294,7 +1346,7 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
12941346
) {
12951347
switch (_peripheral_privacy_configuration.resolution_strategy) {
12961348
case PeripheralPrivacyConfiguration_t::PERFORM_PAIRING_PROCEDURE:
1297-
nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getSecurityManager().requestPairing(handle);
1349+
nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getSecurityManager().requestAuthentication(handle);
12981350
break;
12991351

13001352
case PeripheralPrivacyConfiguration_t::PERFORM_AUTHENTICATION_PROCEDURE:
@@ -1322,32 +1374,16 @@ void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t
13221374
void nRF5xGap::on_advertising_packet(const ble_gap_evt_adv_report_t &evt) {
13231375
using BLEProtocol::AddressType;
13241376

1325-
AddressType_t peer_addr_type;
1326-
const uint8_t* peer_address = evt.peer_addr.addr;
1327-
13281377
if (_privacy_enabled &&
1329-
evt.peer_addr.addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE &&
1330-
_central_privacy_configuration.resolution_strategy != CentralPrivacyConfiguration_t::DO_NOT_RESOLVE
1378+
evt.peer_addr.addr_id_peer == 0 &&
1379+
_central_privacy_configuration.resolution_strategy == CentralPrivacyConfiguration_t::RESOLVE_AND_FILTER
13311380
) {
1332-
using ble::pal::vendor::nordic::nRF5xSecurityManager;
1333-
1334-
const resolving_list_entry_t* entry = get_sm().resolve_address(
1335-
peer_address
1336-
);
1337-
1338-
if (entry) {
1339-
peer_address = entry->peer_identity_address.data();
1340-
peer_addr_type = convert_identity_address(entry->peer_identity_address_type);
1341-
} else if (_central_privacy_configuration.resolution_strategy != CentralPrivacyConfiguration_t::RESOLVE_AND_FORWARD) {
1342-
peer_addr_type = convert_nordic_address(evt.peer_addr.addr_type);
1343-
} else {
1344-
// filter out the packet.
1345-
return;
1346-
}
1347-
} else {
1348-
peer_addr_type = convert_nordic_address(evt.peer_addr.addr_type);
1381+
return;
13491382
}
13501383

1384+
AddressType_t peer_addr_type = convert_nordic_address(evt.peer_addr.addr_type);
1385+
const uint8_t* peer_address = evt.peer_addr.addr;
1386+
13511387
processAdvertisementReport(
13521388
peer_address,
13531389
evt.rssi,

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/nRF5xGap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ class nRF5xGap : public ::Gap, public ble::pal::ConnectionEventMonitor {
267267
friend void btle_handler(const ble_evt_t *p_ble_evt);
268268
friend void btle_handler(const ble_evt_t *p_ble_evt, void *p_context);
269269

270+
ble_error_t update_identities_list(bool resolution_enabled);
270271
void on_connection(Handle_t handle, const ble_gap_evt_connected_t& evt);
271272
void on_advertising_packet(const ble_gap_evt_adv_report_t &evt);
272273
uint16_t m_connectionHandle;

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,18 @@ ble_error_t nRF5xSecurityManager::add_device_to_resolving_list(
158158
return BLE_ERROR_INVALID_STATE;
159159
}
160160

161-
resolving_list_entry_t& entry = resolving_list[resolving_list_entry_count];
162-
entry.peer_identity_address_type = peer_identity_address_type;
163-
entry.peer_identity_address = peer_identity_address;
164-
entry.peer_irk = peer_irk;
161+
ble_gap_id_key_t& entry = resolving_list[resolving_list_entry_count];
162+
entry.id_addr_info.addr_type = peer_identity_address_type.value();
163+
memcpy(
164+
entry.id_addr_info.addr,
165+
peer_identity_address.data(),
166+
peer_identity_address.size()
167+
);
168+
memcpy(
169+
entry.id_info.irk,
170+
peer_irk.data(),
171+
peer_irk.size()
172+
);
165173

166174
++resolving_list_entry_count;
167175

@@ -176,9 +184,9 @@ ble_error_t nRF5xSecurityManager::remove_device_from_resolving_list(
176184

177185
// first the index needs to be found
178186
for (entry_index = 0; entry_index < resolving_list_entry_count; ++entry_index) {
179-
resolving_list_entry_t& entry = resolving_list[entry_index];
180-
if (entry.peer_identity_address_type == peer_identity_address_type &&
181-
entry.peer_identity_address == peer_identity_address
187+
ble_gap_id_key_t& entry = resolving_list[entry_index];
188+
if (entry.id_addr_info.addr_type == peer_identity_address_type.value() &&
189+
entry.id_addr_info.addr == peer_identity_address
182190
) {
183191
break;
184192
}
@@ -204,46 +212,13 @@ ble_error_t nRF5xSecurityManager::clear_resolving_list()
204212
return BLE_ERROR_NONE;
205213
}
206214

207-
ArrayView<nRF5xSecurityManager::resolving_list_entry_t>
208-
nRF5xSecurityManager::get_resolving_list() {
209-
return ArrayView<nRF5xSecurityManager::resolving_list_entry_t>(
215+
ArrayView<ble_gap_id_key_t> nRF5xSecurityManager::get_resolving_list() {
216+
return ArrayView<ble_gap_id_key_t>(
210217
resolving_list,
211218
resolving_list_entry_count
212219
);
213220
}
214221

215-
const nRF5xSecurityManager::resolving_list_entry_t*
216-
nRF5xSecurityManager::resolve_address(const address_t& resolvable_address) {
217-
#if defined(MBEDTLS_ECDH_C)
218-
typedef byte_array_t<CryptoToolbox::hash_size_> hash_t;
219-
220-
for (size_t i = 0; i < resolving_list_entry_count; ++i) {
221-
resolving_list_entry_t& entry = resolving_list[i];
222-
hash_t hash_generated;
223-
224-
// Compute the hash part from the random address part when the irk of
225-
// the entry is used
226-
_crypto.ah(
227-
make_const_ArrayView<CryptoToolbox::irk_size_>(entry.peer_irk),
228-
make_const_ArrayView<CryptoToolbox::prand_size_>(
229-
resolvable_address.data() + CryptoToolbox::hash_size_
230-
),
231-
make_ArrayView(hash_generated)
232-
);
233-
234-
// Compare hash generated with the hash present in the address passed as
235-
// parameter. If they are equal then the IRK of the entry has been used
236-
// to generate the resolvable address.
237-
if (memcmp(hash_generated.data(), resolvable_address.data(), CryptoToolbox::hash_size_) == 0) {
238-
return &entry;
239-
}
240-
}
241-
#endif
242-
return NULL;
243-
}
244-
245-
246-
247222
////////////////////////////////////////////////////////////////////////////
248223
// Pairing
249224
//

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,39 +84,12 @@ class nRF5xSecurityManager : public ::ble::pal::SecurityManager {
8484
*/
8585
virtual ble_error_t clear_resolving_list();
8686

87-
/**
88-
* An entry of the resolving list stored in the SecurityManager.
89-
*/
90-
struct resolving_list_entry_t {
91-
resolving_list_entry_t() :
92-
peer_identity_address_type(
93-
advertising_peer_address_type_t::PUBLIC_ADDRESS
94-
)
95-
{ }
96-
97-
irk_t peer_irk;
98-
address_t peer_identity_address;
99-
advertising_peer_address_type_t peer_identity_address_type;
100-
};
101-
10287
/**
10388
* Return the IRKs present in the resolving list
10489
* @param count The number of entries present in the resolving list.
10590
* @param pointer to the first entry of the resolving list.
10691
*/
107-
ArrayView<resolving_list_entry_t> get_resolving_list();
108-
109-
/**
110-
* Try to resolve a private resolvable address.
111-
*
112-
* @param resolvable_address The address to resolve.
113-
*
114-
* @return Pointer to the entry found if any.
115-
*/
116-
const resolving_list_entry_t* resolve_address(
117-
const address_t& resolvable_address
118-
);
119-
92+
ArrayView<ble_gap_id_key_t> get_resolving_list();
12093

12194
////////////////////////////////////////////////////////////////////////////
12295
// Pairing
@@ -396,7 +369,7 @@ class nRF5xSecurityManager : public ::ble::pal::SecurityManager {
396369
static const size_t MAX_RESOLVING_LIST_ENTRIES = BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT;
397370

398371
size_t resolving_list_entry_count;
399-
resolving_list_entry_t resolving_list[MAX_RESOLVING_LIST_ENTRIES];
372+
ble_gap_id_key_t resolving_list[MAX_RESOLVING_LIST_ENTRIES];
400373
};
401374

402375
} // nordic

0 commit comments

Comments
 (0)