-
Notifications
You must be signed in to change notification settings - Fork 399
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
nimble/ll: Add vs hci to set local IRK #1630
Merged
andrzej-kaczmarek
merged 1 commit into
apache:master
from
andrzej-kaczmarek:ll-rpa0-hack
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,16 @@ struct ble_ll_resolv_data g_ble_ll_resolv_data; | |
__attribute__((aligned(4))) | ||
struct ble_ll_resolv_entry g_ble_ll_resolv_list[MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE)]; | ||
|
||
#if MYNEWT_VAL(BLE_LL_HCI_VS_LOCAL_IRK) | ||
struct local_irk_data { | ||
uint8_t is_set; | ||
uint8_t irk[16]; | ||
uint8_t rpa[6]; | ||
}; | ||
/* 0 is for public, 1 is for static address */ | ||
static struct local_irk_data g_local_irk[2]; | ||
#endif | ||
|
||
/** | ||
* Called to determine if a change is allowed to the resolving list at this | ||
* time. We are not allowed to modify the resolving list if address translation | ||
|
@@ -70,6 +80,30 @@ ble_ll_resolv_list_chg_allowed(void) | |
return rc; | ||
} | ||
|
||
static void | ||
generate_rpa(const uint8_t *irk, uint8_t *rpa) | ||
{ | ||
uint8_t *prand; | ||
struct ble_encryption_block ecb; | ||
|
||
/* Get prand */ | ||
prand = rpa + 3; | ||
ble_ll_rand_prand_get(prand); | ||
|
||
/* Calculate hash, hash = ah(local IRK, prand) */ | ||
memcpy(ecb.key, irk, 16); | ||
memset(ecb.plain_text, 0, 13); | ||
ecb.plain_text[13] = prand[2]; | ||
ecb.plain_text[14] = prand[1]; | ||
ecb.plain_text[15] = prand[0]; | ||
|
||
/* Calculate hash */ | ||
ble_hw_encrypt_block(&ecb); | ||
|
||
rpa[0] = ecb.cipher_text[15]; | ||
rpa[1] = ecb.cipher_text[14]; | ||
rpa[2] = ecb.cipher_text[13]; | ||
} | ||
|
||
/** | ||
* Called to generate a resolvable private address in rl structure | ||
|
@@ -81,8 +115,6 @@ static void | |
ble_ll_resolv_gen_priv_addr(struct ble_ll_resolv_entry *rl, int local) | ||
{ | ||
uint8_t *irk; | ||
uint8_t *prand; | ||
struct ble_encryption_block ecb; | ||
uint8_t *addr; | ||
|
||
BLE_LL_ASSERT(rl != NULL); | ||
|
@@ -95,23 +127,7 @@ ble_ll_resolv_gen_priv_addr(struct ble_ll_resolv_entry *rl, int local) | |
irk = rl->rl_peer_irk; | ||
} | ||
|
||
/* Get prand */ | ||
prand = addr + 3; | ||
ble_ll_rand_prand_get(prand); | ||
|
||
/* Calculate hash, hash = ah(local IRK, prand) */ | ||
memcpy(ecb.key, irk, 16); | ||
memset(ecb.plain_text, 0, 13); | ||
ecb.plain_text[13] = prand[2]; | ||
ecb.plain_text[14] = prand[1]; | ||
ecb.plain_text[15] = prand[0]; | ||
|
||
/* Calculate hash */ | ||
ble_hw_encrypt_block(&ecb); | ||
|
||
addr[0] = ecb.cipher_text[15]; | ||
addr[1] = ecb.cipher_text[14]; | ||
addr[2] = ecb.cipher_text[13]; | ||
generate_rpa(irk, addr); | ||
} | ||
|
||
/** | ||
|
@@ -124,6 +140,10 @@ ble_ll_resolv_rpa_timer_cb(struct ble_npl_event *ev) | |
int i; | ||
os_sr_t sr; | ||
struct ble_ll_resolv_entry *rl; | ||
#if MYNEWT_VAL(BLE_LL_HCI_VS_LOCAL_IRK) | ||
struct local_irk_data *irk_data; | ||
uint8_t rpa[6]; | ||
#endif | ||
|
||
rl = &g_ble_ll_resolv_list[0]; | ||
for (i = 0; i < g_ble_ll_resolv_data.rl_cnt; ++i) { | ||
|
@@ -141,6 +161,18 @@ ble_ll_resolv_rpa_timer_cb(struct ble_npl_event *ev) | |
++rl; | ||
} | ||
|
||
#if MYNEWT_VAL(BLE_LL_HCI_VS_LOCAL_IRK) | ||
for (i = 0; i < ARRAY_SIZE(g_local_irk); i++) { | ||
irk_data = &g_local_irk[i]; | ||
if (irk_data->is_set) { | ||
generate_rpa(irk_data->irk, rpa); | ||
OS_ENTER_CRITICAL(sr); | ||
memcpy(irk_data->rpa, rpa, 6); | ||
OS_EXIT_CRITICAL(sr); | ||
} | ||
} | ||
#endif | ||
|
||
ble_npl_callout_reset(&g_ble_ll_resolv_data.rpa_timer, | ||
g_ble_ll_resolv_data.rpa_tmo); | ||
|
||
|
@@ -637,6 +669,58 @@ ble_ll_resolv_gen_rpa(uint8_t *addr, uint8_t addr_type, uint8_t *rpa, int local) | |
return 0; | ||
} | ||
|
||
#if MYNEWT_VAL(BLE_LL_HCI_VS_LOCAL_IRK) | ||
int | ||
ble_ll_resolv_local_irk_set(uint8_t own_addr_type, const uint8_t *irk) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use ble_ll_resolv_irk_nonzero() |
||
struct local_irk_data *irk_data; | ||
int i; | ||
|
||
if (own_addr_type >= 2) { | ||
return -1; | ||
} | ||
|
||
irk_data = &g_local_irk[own_addr_type]; | ||
|
||
memcpy(irk_data->irk, irk, 16); | ||
|
||
irk_data->is_set = 0; | ||
|
||
for (i = 0; i < 16; i++) { | ||
if (irk[i]) { | ||
irk_data->is_set = 1; | ||
break; | ||
} | ||
} | ||
|
||
if (irk_data->is_set) { | ||
generate_rpa(irk_data->irk, irk_data->rpa); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int | ||
ble_ll_resolv_local_rpa_get(uint8_t own_addr_type, uint8_t *rpa) | ||
{ | ||
struct local_irk_data *irk_data; | ||
|
||
if (own_addr_type >= 2) { | ||
return -1; | ||
} | ||
|
||
irk_data = &g_local_irk[own_addr_type]; | ||
|
||
if (!irk_data->is_set) { | ||
return -1; | ||
} | ||
|
||
memcpy(rpa, irk_data->rpa, 6); | ||
|
||
return 0; | ||
} | ||
#endif | ||
|
||
/** | ||
* Resolve a Resolvable Private Address | ||
* | ||
|
@@ -738,6 +822,10 @@ ble_ll_resolv_init(void) | |
&g_ble_ll_data.ll_evq, | ||
ble_ll_resolv_rpa_timer_cb, | ||
NULL); | ||
|
||
#if MYNEWT_VAL(BLE_LL_HCI_VS_LOCAL_IRK) | ||
memset(&g_local_irk, 0, sizeof(g_local_irk)); | ||
#endif | ||
} | ||
|
||
#endif /* if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1208,8 +1208,11 @@ struct ble_hci_vs_set_data_len_rp { | |
struct ble_hci_vs_set_antenna_cp { | ||
uint8_t antenna; | ||
} __attribute__((packed)); | ||
|
||
|
||
#define BLE_HCI_OCF_VS_SET_LOCAL_IRK (MYNEWT_VAL(BLE_HCI_VS_OCF_OFFSET) + (0x000A)) | ||
struct ble_hci_vs_set_local_irk_cp { | ||
uint8_t own_addr_type; | ||
uint8_t irk[16]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a loose thought, maybe this could also have address type? public vs static_random |
||
} __attribute__((packed)); | ||
|
||
/* Command Specific Definitions */ | ||
/* --- Set controller to host flow control (OGF 0x03, OCF 0x0031) --- */ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably disallow this when initiating and advertising (?) (connectable, undirected)