Skip to content

Commit 768e0d7

Browse files
alxelaxcarlescufi
authored andcommitted
Bluetooth: Mesh: encapsulate tinycrypt dependency
Bluetooth Mesh uses tinycrypt library for security related algorithms. This PR encapsulates tinycrypt dependency within one file to make the current implementation more portable. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent 7c5cc99 commit 768e0d7

File tree

3 files changed

+67
-59
lines changed

3 files changed

+67
-59
lines changed

subsys/bluetooth/mesh/crypto.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2017 Intel Corporation
3+
* Copyright (c) 2022 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -17,12 +18,14 @@
1718
#include <tinycrypt/aes.h>
1819
#include <tinycrypt/cmac_mode.h>
1920
#include <tinycrypt/ccm_mode.h>
21+
#include <tinycrypt/ecc.h>
22+
#include <tinycrypt/ecc_dh.h>
2023

2124
#include <bluetooth/mesh.h>
2225
#include <bluetooth/crypto.h>
2326

2427
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_CRYPTO)
25-
#define LOG_MODULE_NAME bt_mesh_crypto
28+
#define LOG_MODULE_NAME bt_mesh_tc_crypto
2629
#include "common/log.h"
2730

2831
#include "mesh.h"
@@ -31,8 +34,13 @@
3134
#define NET_MIC_LEN(pdu) (((pdu)[1] & 0x80) ? 8 : 4)
3235
#define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4)
3336

34-
int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
35-
size_t sg_len, uint8_t mac[16])
37+
struct bt_mesh_sg {
38+
const void *data;
39+
size_t len;
40+
};
41+
42+
static int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
43+
size_t sg_len, uint8_t mac[16])
3644
{
3745
struct tc_aes_key_sched_struct sched;
3846
struct tc_cmac_struct state;
@@ -55,6 +63,21 @@ int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
5563
return 0;
5664
}
5765

66+
static int bt_mesh_aes_cmac_one(const uint8_t key[16], const void *m,
67+
size_t len, uint8_t mac[16])
68+
{
69+
struct bt_mesh_sg sg = { m, len };
70+
71+
return bt_mesh_aes_cmac(key, &sg, 1, mac);
72+
}
73+
74+
int bt_mesh_s1(const char *m, uint8_t salt[16])
75+
{
76+
const uint8_t zero[16] = { 0 };
77+
78+
return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt);
79+
}
80+
5881
int bt_mesh_k1(const uint8_t *ikm, size_t ikm_len, const uint8_t salt[16],
5982
const char *info, uint8_t okm[16])
6083
{
@@ -491,6 +514,19 @@ int bt_mesh_virtual_addr(const uint8_t virtual_label[16], uint16_t *addr)
491514
return 0;
492515
}
493516

517+
int bt_mesh_prov_salt(const uint8_t conf_salt[16], const uint8_t prov_rand[16],
518+
const uint8_t dev_rand[16], uint8_t prov_salt[16])
519+
{
520+
const uint8_t prov_salt_key[16] = { 0 };
521+
struct bt_mesh_sg sg[] = {
522+
{ conf_salt, 16 },
523+
{ prov_rand, 16 },
524+
{ dev_rand, 16 },
525+
};
526+
527+
return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt);
528+
}
529+
494530
int bt_mesh_prov_conf_salt(const uint8_t conf_inputs[145], uint8_t salt[16])
495531
{
496532
const uint8_t conf_salt_key[16] = { 0 };
@@ -552,3 +588,17 @@ int bt_mesh_beacon_auth(const uint8_t beacon_key[16], uint8_t flags,
552588

553589
return err;
554590
}
591+
592+
int bt_mesh_dhkey_gen(const uint8_t *pub_key, const uint8_t *priv_key, uint8_t *dhkey)
593+
{
594+
if (uECC_valid_public_key(pub_key, &curve_secp256r1)) {
595+
BT_ERR("Public key is not valid");
596+
return -EIO;
597+
} else if (uECC_shared_secret(pub_key, priv_key, dhkey,
598+
&curve_secp256r1) != TC_CRYPTO_SUCCESS) {
599+
BT_ERR("DHKey generation failed");
600+
return -EIO;
601+
}
602+
603+
return 0;
604+
}

subsys/bluetooth/mesh/crypto.h

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
11
/*
22
* Copyright (c) 2017 Intel Corporation
3+
* Copyright (c) 2022 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

7-
struct bt_mesh_sg {
8-
const void *data;
9-
size_t len;
10-
};
11-
12-
int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
13-
size_t sg_len, uint8_t mac[16]);
14-
15-
static inline int bt_mesh_aes_cmac_one(const uint8_t key[16], const void *m,
16-
size_t len, uint8_t mac[16])
17-
{
18-
struct bt_mesh_sg sg = { m, len };
19-
20-
return bt_mesh_aes_cmac(key, &sg, 1, mac);
21-
}
22-
23-
static inline bool bt_mesh_s1(const char *m, uint8_t salt[16])
24-
{
25-
const uint8_t zero[16] = { 0 };
26-
27-
return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt);
28-
}
8+
int bt_mesh_s1(const char *m, uint8_t salt[16]);
299

3010
int bt_mesh_k1(const uint8_t *ikm, size_t ikm_len, const uint8_t salt[16],
3111
const char *info, uint8_t okm[16]);
3212

33-
#define bt_mesh_k1_str(ikm, ikm_len, salt_str, info, okm) \
34-
({ \
35-
const uint8_t salt[16] = salt_str; \
36-
bt_mesh_k1(ikm, ikm_len, salt, info, okm); \
37-
})
38-
3913
int bt_mesh_k2(const uint8_t n[16], const uint8_t *p, size_t p_len,
4014
uint8_t net_id[1], uint8_t enc_key[16], uint8_t priv_key[16]);
4115

@@ -48,7 +22,9 @@ int bt_mesh_id128(const uint8_t n[16], const char *s, uint8_t out[16]);
4822
static inline int bt_mesh_id_resolving_key(const uint8_t net_key[16],
4923
uint8_t resolving_key[16])
5024
{
51-
return bt_mesh_k1_str(net_key, 16, "smbt", "smbi", resolving_key);
25+
const uint8_t salt[16] = "smbt";
26+
27+
return bt_mesh_k1(net_key, 16, salt, "smbi", resolving_key);
5228
}
5329

5430
static inline int bt_mesh_identity_key(const uint8_t net_key[16],
@@ -101,20 +77,8 @@ static inline int bt_mesh_dev_key(const uint8_t dhkey[32],
10177
return bt_mesh_k1(dhkey, 32, prov_salt, "prdk", dev_key);
10278
}
10379

104-
static inline int bt_mesh_prov_salt(const uint8_t conf_salt[16],
105-
const uint8_t prov_rand[16],
106-
const uint8_t dev_rand[16],
107-
uint8_t prov_salt[16])
108-
{
109-
const uint8_t prov_salt_key[16] = { 0 };
110-
struct bt_mesh_sg sg[] = {
111-
{ conf_salt, 16 },
112-
{ prov_rand, 16 },
113-
{ dev_rand, 16 },
114-
};
115-
116-
return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt);
117-
}
80+
int bt_mesh_prov_salt(const uint8_t conf_salt[16], const uint8_t prov_rand[16],
81+
const uint8_t dev_rand[16], uint8_t prov_salt[16]);
11882

11983
int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index,
12084
const uint8_t privacy_key[16]);
@@ -163,3 +127,5 @@ int bt_mesh_prov_decrypt(const uint8_t key[16], uint8_t nonce[13],
163127

164128
int bt_mesh_prov_encrypt(const uint8_t key[16], uint8_t nonce[13],
165129
const uint8_t data[25], uint8_t out[25 + 8]);
130+
131+
int bt_mesh_dhkey_gen(const uint8_t *pub_key, const uint8_t *priv_key, uint8_t *dhkey);

subsys/bluetooth/mesh/prov_device.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include <sys/util.h>
1212
#include <sys/byteorder.h>
1313

14-
#include <tinycrypt/constants.h>
15-
#include <tinycrypt/ecc.h>
16-
#include <tinycrypt/ecc_dh.h>
17-
1814
#include <net/buf.h>
1915
#include <bluetooth/bluetooth.h>
2016
#include <bluetooth/conn.h>
@@ -321,18 +317,14 @@ static void prov_dh_key_gen(void)
321317

322318
if (IS_ENABLED(CONFIG_BT_MESH_PROV_OOB_PUBLIC_KEY) &&
323319
atomic_test_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY)) {
324-
if (uECC_valid_public_key(remote_pk, &curve_secp256r1)) {
325-
BT_ERR("Public key is not valid");
326-
} else if (uECC_shared_secret(remote_pk, bt_mesh_prov->private_key_be,
327-
bt_mesh_prov_link.dhkey,
328-
&curve_secp256r1) != TC_CRYPTO_SUCCESS) {
329-
BT_ERR("DHKey generation failed");
330-
} else {
331-
dh_key_gen_complete();
320+
321+
if (bt_mesh_dhkey_gen(remote_pk, bt_mesh_prov->private_key_be,
322+
bt_mesh_prov_link.dhkey)) {
323+
prov_fail(PROV_ERR_UNEXP_ERR);
332324
return;
333325
}
334326

335-
prov_fail(PROV_ERR_UNEXP_ERR);
327+
dh_key_gen_complete();
336328
return;
337329
}
338330

0 commit comments

Comments
 (0)