Skip to content

Commit 5eff00f

Browse files
kevin-whisperNahal Farhi
authored andcommitted
[MFI] Adds decryption of mode 2 packets (zephyrproject-rtos#11)
Adds in detection of entering apple's MFI mode 2 state and then decrypts mode 2 packets and passes them along to higher levels of the stack. Mode 2 is entered by the iphone first pausing encryption and then re-starting it. After this, all encrypted packets coming from the iphone use mode 2 regardless of whether they're standard BLE packets or audio packets.
1 parent f481b44 commit 5eff00f

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

subsys/bluetooth/controller/hal/ccm.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ struct ccm {
1212
uint8_t resv1:7;
1313
uint8_t iv[8];
1414
} __packed;
15+
16+
// Whisper added for MFI
17+
struct __attribute__ ((packed, aligned(1))) ccm_mode2_nonce {
18+
uint16_t counter;
19+
uint8_t resv1;
20+
uint8_t resv2;
21+
uint8_t resv3:7;
22+
uint8_t direction:1;
23+
uint8_t iv[8];
24+
};

subsys/bluetooth/controller/ll_sw/lll_conn.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ struct lll_conn {
130130

131131
struct ccm ccm_rx;
132132
struct ccm ccm_tx;
133+
134+
// Whisper added for MFI
135+
struct ccm_mode2_nonce ccm_mode2_nonce_rx;
136+
struct ccm_mode2_nonce ccm_mode2_nonce_tx;
137+
uint8_t mode2_rx_enabled;
138+
uint8_t mode2_tx_enabled;
139+
uint8_t has_paused;
133140
#endif /* CONFIG_BT_CTLR_LE_ENC */
134141

135142
#if defined(CONFIG_BT_CTLR_CONN_RSSI)

subsys/bluetooth/controller/ll_sw/nordic/lll/lll_conn.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#include "common/log.h"
4343
#include "hal/debug.h"
4444

45+
// Whisper added for MFI
46+
#include "hal/nrf5/ccm_mode2_soft.h"
47+
4548
static int init_reset(void);
4649
static void isr_done(void *param);
4750
static inline int isr_rx_pdu(struct lll_conn *lll, struct pdu_data *pdu_data_rx,
@@ -970,6 +973,37 @@ static inline int isr_rx_pdu(struct lll_conn *lll, struct pdu_data *pdu_data_rx,
970973

971974
bool mic_failure = !radio_ccm_mic_is_valid();
972975

976+
// Whisper added for MFI
977+
if (lll->mode2_rx_enabled) {
978+
// decrypt apple's mode 2 encryption packet via a soft decrypt routine
979+
// first get the encrypted packet
980+
struct pdu_data *scratch_pkt = radio_pkt_scratch_get();
981+
982+
// next set up the structure for the soft decryption. NOTE: The event counter
983+
// has already been incremented elsewhere in the stack prior to this code running
984+
// so the event_counter we actually want is (event_counter - 1)
985+
ccm_soft_data_t ccm_params;
986+
lll->ccm_mode2_nonce_rx.counter = lll->event_counter - 1;
987+
ccm_params.p_nonce = (uint8_t *)&lll->ccm_mode2_nonce_rx;
988+
ccm_params.p_m = scratch_pkt->lldata;
989+
ccm_params.m_len = scratch_pkt->len;
990+
ccm_params.p_out = pdu_data_rx->lldata;
991+
ccm_params.p_key = lll->ccm_rx.key;
992+
993+
ccm_mode2_soft_decrypt(&ccm_params);
994+
995+
// finally finish setting up pdu_data_rx
996+
pdu_data_rx->ll_id = scratch_pkt->ll_id;
997+
pdu_data_rx->nesn = scratch_pkt->nesn;
998+
pdu_data_rx->sn = scratch_pkt->sn;
999+
pdu_data_rx->md = scratch_pkt->md;
1000+
pdu_data_rx->rfu = scratch_pkt->rfu;
1001+
pdu_data_rx->len = scratch_pkt->len;
1002+
1003+
// there's no MIC with mode 2 encryption so set this to false
1004+
mic_failure = false;
1005+
}
1006+
9731007
if (mic_failure &&
9741008
lll->ccm_rx.counter == 0 &&
9751009
(pdu_data_rx->ll_id ==

subsys/bluetooth/controller/ll_sw/ull_adv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,11 @@ uint8_t ll_adv_enable(uint8_t enable)
10811081
conn->llcp_enc.pause_tx = conn->llcp_enc.pause_rx = 0U;
10821082
conn->llcp_enc.refresh = 0U;
10831083
conn->periph.llcp_type = 0U;
1084+
1085+
// Whisper added for MFI
1086+
conn_lll->mode2_rx_enabled = 0;
1087+
conn_lll->mode2_tx_enabled = 0;
1088+
conn_lll->has_paused = 0;
10841089
#endif /* CONFIG_BT_CTLR_LE_ENC */
10851090

10861091
#if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)

subsys/bluetooth/controller/ll_sw/ull_conn.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,6 +3584,19 @@ static inline void event_enc_prep(struct ll_conn *conn)
35843584
*/
35853585
lll->enc_rx = 1U;
35863586

3587+
// Whisper added for MFI
3588+
if(lll->has_paused) {
3589+
lll->mode2_rx_enabled = 1;
3590+
3591+
// set up the Rx nonce
3592+
lll->ccm_mode2_nonce_rx.counter = lll->event_counter;
3593+
lll->ccm_mode2_nonce_rx.resv1 = 0;
3594+
lll->ccm_mode2_nonce_rx.resv2 = 0;
3595+
lll->ccm_mode2_nonce_rx.resv3 = 0;
3596+
lll->ccm_mode2_nonce_rx.direction = lll->ccm_rx.direction;
3597+
memcpy(lll->ccm_mode2_nonce_rx.iv, lll->ccm_rx.iv, sizeof(lll->ccm_mode2_nonce_rx.iv));
3598+
}
3599+
35873600
/* prepare the start enc req */
35883601
pdu_ctrl_tx->ll_id = PDU_DATA_LLID_CTRL;
35893602
pdu_ctrl_tx->len = offsetof(struct pdu_data_llctrl,
@@ -4957,6 +4970,19 @@ static int start_enc_rsp_send(struct ll_conn *conn,
49574970

49584971
/* enable transmit encryption */
49594972
conn->lll.enc_tx = 1;
4973+
4974+
// Whisper added for MFI
4975+
if(conn->lll.has_paused) {
4976+
conn->lll.mode2_tx_enabled = 1;
4977+
4978+
// set up the Tx nonce
4979+
conn->lll.ccm_mode2_nonce_tx.counter = conn->lll.event_counter;
4980+
conn->lll.ccm_mode2_nonce_tx.resv1 = 0;
4981+
conn->lll.ccm_mode2_nonce_tx.resv2 = 0;
4982+
conn->lll.ccm_mode2_nonce_tx.resv3 = 0;
4983+
conn->lll.ccm_mode2_nonce_tx.direction = conn->lll.ccm_tx.direction;
4984+
memcpy(conn->lll.ccm_mode2_nonce_tx.iv, conn->lll.ccm_tx.iv, sizeof(conn->lll.ccm_mode2_nonce_tx.iv));
4985+
}
49604986

49614987
ull_pdu_data_init(pdu_ctrl_tx);
49624988

@@ -6361,6 +6387,9 @@ static inline void ctrl_tx_ack(struct ll_conn *conn, struct node_tx **tx,
63616387
{
63626388
/* pause data packet tx */
63636389
conn->llcp_enc.pause_tx = 1U;
6390+
6391+
// Whisper added for MFI:
6392+
conn->lll.has_paused = 1U;
63646393
}
63656394
break;
63666395

0 commit comments

Comments
 (0)