Skip to content

Commit ece442a

Browse files
kevin-whisperNahal Farhi
authored andcommitted
Removed all references to MICs since they're unused for mode2
1 parent 9311b66 commit ece442a

File tree

6 files changed

+17
-170
lines changed

6 files changed

+17
-170
lines changed

subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/aes.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
#include <hal/nrf_ecb.h>
4141

42-
#if !AES_USE_SOFTDEVICE_ECB_WRAPPER
4342
void aes_encrypt(aes_data_t * p_aes_data)
4443
{
4544
NRF_ECB->ECBDATAPTR = (uint32_t) p_aes_data;
@@ -54,4 +53,3 @@ void aes_encrypt(aes_data_t * p_aes_data)
5453

5554
NRF_ECB->EVENTS_ENDECB = 0;
5655
}
57-
#endif

subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/aes.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@
3434
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3535
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
37-
#ifndef MESH_AES_H__
38-
#define MESH_AES_H__
37+
#ifndef MODE2_AES_H__
38+
#define MODE2_AES_H__
3939

40-
// #include "nrf_soc.h"
41-
// #include "nrf_mesh_config_core.h"
4240
#include <stdint.h>
4341

4442
typedef struct {
@@ -49,15 +47,6 @@ typedef struct {
4947

5048
typedef nrf_ecb_hal_data_t aes_data_t;
5149

52-
#ifndef AES_USE_SOFTDEVICE_ECB_WRAPPER
53-
#define AES_USE_SOFTDEVICE_ECB_WRAPPER SOFTDEVICE_PRESENT
54-
#endif
55-
56-
#if AES_USE_SOFTDEVICE_ECB_WRAPPER
57-
#define aes_encrypt(data) (void)sd_ecb_block_encrypt((nrf_ecb_hal_data_t *)(data))
58-
#else
5950
void aes_encrypt(aes_data_t *p_aes_data);
6051

61-
#endif
62-
63-
#endif
52+
#endif // MODE2_AES_H__

subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/ccm_soft.c renamed to subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/ccm_mode2_soft.c

Lines changed: 5 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,11 @@
3434
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3535
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
37+
#include "ccm_mode2_soft.h"
38+
3739
#include <string.h>
38-
// #include <nrf_error.h>
3940

40-
// #include "utils.h"
41-
// #include "log.h"
4241
#include "aes.h"
43-
// #include "toolchain.h"
44-
45-
#include "ccm_soft.h"
46-
// #include "nrf_mesh_assert.h"
4742

4843
/* How it all works:
4944
* Generation of the MIC and encryption are two separate procedures.
@@ -93,16 +88,6 @@ typedef struct
9388
uint16_t counter;
9489
} a_block_t;
9590

96-
typedef struct
97-
{
98-
uint8_t flags;
99-
uint8_t nonce[CCM_NONCE_LENGTH];
100-
uint16_t length_field;
101-
} b0_t;
102-
103-
// NRF_MESH_STATIC_ASSERT(sizeof(a_block_t) == CCM_BLOCK_SIZE);
104-
// NRF_MESH_STATIC_ASSERT(sizeof(b0_t) == CCM_BLOCK_SIZE);
105-
10691
/**
10792
* Bytewise XOR for an array.
10893
*
@@ -124,68 +109,6 @@ static inline void utils_xor(uint8_t * p_dst, const uint8_t * p_src1, const uint
124109
}
125110
}
126111

127-
static void ccm_soft_authenticate_blocks(aes_data_t * p_aes_data,
128-
const uint8_t * p_data,
129-
uint16_t data_size,
130-
uint8_t offset_B)
131-
{
132-
uint8_t * p_clear = p_aes_data->cleartext;
133-
uint8_t * p_cipher = p_aes_data->ciphertext;
134-
135-
while (data_size != 0)
136-
{
137-
if (data_size < (CCM_BLOCK_SIZE - offset_B))
138-
{
139-
memcpy(&p_clear[offset_B], p_data, data_size);
140-
memset(&p_clear[offset_B + data_size], 0x00, CCM_BLOCK_SIZE - (offset_B + data_size));
141-
data_size = 0;
142-
}
143-
else
144-
{
145-
memcpy(&p_clear[offset_B], p_data, (CCM_BLOCK_SIZE - offset_B));
146-
data_size -= (CCM_BLOCK_SIZE - offset_B);
147-
p_data += (CCM_BLOCK_SIZE - offset_B);
148-
}
149-
150-
offset_B = 0;
151-
152-
utils_xor(p_clear, p_cipher, p_clear, CCM_BLOCK_SIZE);
153-
154-
aes_encrypt((nrf_ecb_hal_data_t *) p_aes_data);
155-
}
156-
}
157-
158-
static void ccm_soft_authenticate(ccm_soft_data_t * p_data, aes_data_t * p_aes_data, uint8_t * T)
159-
{
160-
b0_t * p_b0 = (b0_t *) &p_aes_data->cleartext[0];
161-
162-
/* construct B0 */
163-
p_b0->flags = (
164-
((p_data->a_len > 0 ? 1 : 0) << 6) |
165-
((((p_data->mic_len - 2)/2) & 0x07) << 3) |
166-
((L_LEN - 1) & 0x07));
167-
168-
memcpy(p_b0->nonce, p_data->p_nonce, CCM_NONCE_LENGTH);
169-
p_b0->length_field = LE2BE16(p_data->m_len);
170-
171-
aes_encrypt((nrf_ecb_hal_data_t *) p_aes_data);
172-
173-
if (p_data->a_len > 0)
174-
{
175-
// NRF_MESH_ASSERT(p_data->a_len < 0xFF00); /* Longer a-data requires different (unsupported) encoding */
176-
*((uint16_t *) &p_aes_data->cleartext[0]) = LE2BE16(p_data->a_len);
177-
178-
ccm_soft_authenticate_blocks(p_aes_data, p_data->p_a, p_data->a_len, 2);
179-
}
180-
181-
if (p_data->m_len > 0)
182-
{
183-
ccm_soft_authenticate_blocks(p_aes_data, p_data->p_m, p_data->m_len, 0);
184-
}
185-
186-
memcpy(T, p_aes_data->ciphertext, p_data->mic_len);
187-
}
188-
189112
/**
190113
* Encrypt all data. Assumes p_aes_data already has key set and cleartext=A[0]
191114
*/
@@ -222,47 +145,20 @@ static inline void build_a_block(const uint8_t * p_nonce, void * A0, uint16_t i)
222145
p_a_block->counter = LE2BE16(i);
223146
}
224147

225-
static inline void build_mic(ccm_soft_data_t * p_ccm_data, aes_data_t * p_aes_data, uint8_t * T, uint8_t * p_mic_out)
148+
void ccm_mode2_soft_encrypt(ccm_soft_data_t * p_data)
226149
{
227-
build_a_block(p_ccm_data->p_nonce, p_aes_data->cleartext, 0);
228-
229-
/* S0 = AES(A0) */
230-
aes_encrypt((nrf_ecb_hal_data_t *) p_aes_data);
231-
232-
/* MIC = T ^ S0 */
233-
utils_xor(p_mic_out, T, p_aes_data->ciphertext, p_ccm_data->mic_len);
234-
}
235-
236-
void ccm_soft_encrypt(ccm_soft_data_t * p_data)
237-
{
238-
#if CCM_DEBUG_MODE_ENABLED
239-
__LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_encrypt: IN ", p_data->p_m, p_data->m_len);
240-
#endif
241-
242150
aes_data_t aes_data;
243151

244152
memcpy(aes_data.key, p_data->p_key, CCM_BLOCK_SIZE);
245153

246-
ccm_soft_authenticate(p_data, &aes_data, p_data->p_mic);
247-
248-
build_mic(p_data, &aes_data, p_data->p_mic, p_data->p_mic);
154+
build_a_block(p_data->p_nonce, aes_data.cleartext, 0);
249155

250156
/* aes_data.cleartext now contains A0, no need to regenerate it. */
251157
ccm_soft_crypt(p_data, &aes_data);
252-
253-
#if CCM_DEBUG_MODE_ENABLED
254-
__LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_encrypt: OUT", p_data->p_out, p_data->m_len);
255-
__LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_encrypt: MIC", p_data->p_mic, p_data->mic_len);
256-
#endif
257158
}
258159

259-
void ccm_soft_decrypt(ccm_soft_data_t * p_data, bool * p_mic_passed)
160+
void ccm_mode2_soft_decrypt(ccm_soft_data_t * p_data)
260161
{
261-
#if CCM_DEBUG_MODE_ENABLED
262-
__LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_decrypt: IN", p_data->p_m, p_data->m_len);
263-
#endif
264-
// NRF_MESH_ASSERT_DEBUG(p_data->mic_len <= CCM_MIC_LENGTH_MAX);
265-
266162
aes_data_t aes_data;
267163

268164
memcpy(aes_data.key, p_data->p_key, CCM_BLOCK_SIZE);
@@ -273,29 +169,4 @@ void ccm_soft_decrypt(ccm_soft_data_t * p_data, bool * p_mic_passed)
273169
build_a_block(p_data->p_nonce, aes_data.cleartext, 0);
274170
ccm_soft_crypt(p_data, &aes_data);
275171
}
276-
277-
// const uint8_t * p_m = p_data->p_m;
278-
// p_data->p_m = p_data->p_out;
279-
280-
// /* Authenticate data */
281-
// uint8_t mic_out[CCM_MIC_LENGTH_MAX];
282-
283-
// ccm_soft_authenticate(p_data, &aes_data, mic_out);
284-
// build_mic(p_data, &aes_data, mic_out, mic_out);
285-
286-
// p_data->p_m = p_m;
287-
// #if CCM_DEBUG_MODE_ENABLED
288-
// __LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_decrypt: OUT", p_data->p_out, p_data->m_len);
289-
// __LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_decrypt: MIC", mic_out, p_data->mic_len);
290-
// #endif
291-
292-
// *p_mic_passed = memcmp(mic_out, p_data->p_mic, p_data->mic_len) == 0;
293-
// #if CCM_DEBUG_MODE_ENABLED
294-
// if (!*p_mic_passed)
295-
// {
296-
// /* No MIC match. */
297-
// __LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_decrypt: mic_in", p_data->p_mic, p_data->mic_len);
298-
// __LOG_XB(LOG_SRC_CCM, LOG_LEVEL_INFO, "ccm_soft_decrypt: mic_out", mic_out, p_data->mic_len);
299-
// }
300-
// #endif
301172
}

subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/ccm_soft.h renamed to subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/ccm_mode2_soft.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3535
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
37-
#ifndef CCM_SOFT_H__
38-
#define CCM_SOFT_H__
37+
#ifndef CCM_MODE2_SOFT_H__
38+
#define CCM_MODE2_SOFT_H__
3939

4040
#include <stdbool.h>
4141
#include <stdint.h>
@@ -57,9 +57,6 @@
5757
/** Length of nonce. */
5858
#define CCM_NONCE_LENGTH (13)
5959

60-
/** Longest MIC allowed. */
61-
#define CCM_MIC_LENGTH_MAX (16)
62-
6360
/**
6461
* Struct for passing AES-CCM encryption data.
6562
*
@@ -72,11 +69,7 @@ typedef struct
7269
const uint8_t * p_m; /**< Message to authenticate and encrypt/decrypt. Set
7370
* to NULL to skip decryption stage. */
7471
uint16_t m_len; /**< Message size (in octets). */
75-
const uint8_t * p_a; /**< Additional authenticated data. */
76-
uint16_t a_len; /**< Additional data size (in octets). */
7772
uint8_t * p_out; /**< (Out) Encrypted/decrypted output. */
78-
uint8_t * p_mic; /**< (Out) Message Integrety Check value */
79-
uint8_t mic_len; /**< Length of the message integrity check value. */
8073
} ccm_soft_data_t;
8174

8275
/**
@@ -85,16 +78,15 @@ typedef struct
8578
* @param p_data Structure with the needed parameters to encrypt the cleartext
8679
* message. See @ref ccm_soft_data_t.
8780
*/
88-
void ccm_soft_encrypt(ccm_soft_data_t * p_data);
81+
void ccm_mode2_soft_encrypt(ccm_soft_data_t * p_data);
8982

9083
/**
9184
* Decrypts data using the AES-CCM algorithm.
9285
*
9386
* @param p_data Pointer to structure with parameters for decrypting a message.
9487
* See @ref ccm_soft_data_t.
95-
* @param p_mic_passed Pointer to bool for storing result of MIC
9688
*/
97-
void ccm_soft_decrypt(ccm_soft_data_t * p_data, bool * p_mic_passed);
89+
void ccm_mode2_soft_decrypt(ccm_soft_data_t * p_data);
9890

9991
/**
10092
* @}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "lll_tim_internal.h"
3838
#include "lll_prof_internal.h"
3939

40-
#include "hal/nrf5/ccm_soft.h"
40+
#include "hal/nrf5/ccm_mode2_soft.h"
4141

4242
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
4343
#define LOG_MODULE_NAME bt_ctlr_lll_conn
@@ -987,8 +987,6 @@ static inline int isr_rx_pdu(struct lll_conn *lll, struct pdu_data *pdu_data_rx,
987987
static uint8_t decrypted_data[64];
988988
uint8_t nonce[CCM_NONCE_LENGTH];
989989
ccm_soft_data_t ccm_params;
990-
ccm_params.a_len = 0;
991-
ccm_params.p_a = NULL;
992990
//ccm_params.p_nonce = nonce;
993991
ccm_params.p_m = scratch_pkt->lldata;
994992
ccm_params.m_len = scratch_pkt->len;
@@ -1016,11 +1014,8 @@ static inline int isr_rx_pdu(struct lll_conn *lll, struct pdu_data *pdu_data_rx,
10161014
nonce[11] = (uint8_t)lll->ccm_rx.iv[6];
10171015
nonce[12] = (uint8_t)lll->ccm_rx.iv[7];
10181016
ccm_params.p_nonce = nonce;
1019-
ccm_params.mic_len = 0;
1020-
ccm_params.p_mic = NULL;
10211017

1022-
bool mic_passed;
1023-
ccm_soft_decrypt(&ccm_params, &mic_passed);
1018+
ccm_mode2_soft_decrypt(&ccm_params);
10241019

10251020
if (ctrl_pdu_len_check(
10261021
scratch_pkt->len)) {

subsys/bluetooth/controller/ll_sw/nrf.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ zephyr_library_sources(
8989
ll_sw/nordic/hal/nrf5/radio/radio.c
9090
ll_sw/nordic/hal/nrf5/mayfly.c
9191
ll_sw/nordic/hal/nrf5/ticker.c
92+
93+
# WHISPER: Below added for MFI
9294
ll_sw/nordic/hal/nrf5/aes.c
93-
ll_sw/nordic/hal/nrf5/ccm_soft.c
95+
ll_sw/nordic/hal/nrf5/ccm_mode2_soft.c
9496
)
9597

9698
zephyr_library_sources_ifdef(

0 commit comments

Comments
 (0)