Skip to content

Commit 46ead0d

Browse files
committed
nimble/mesh: Use Mbed-TLS for crypto
So far TinyCRYPT library was used but it is no longer being maintained. This converts code to use Mbed TLS instead.
1 parent a46410c commit 46ead0d

5 files changed

Lines changed: 98 additions & 35 deletions

File tree

nimble/host/mesh/include/mesh/glue.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
#include "../src/ble_sm_priv.h"
3636
#include "../src/ble_hs_hci_priv.h"
3737

38-
#include "tinycrypt/aes.h"
39-
#include "tinycrypt/constants.h"
40-
#include "tinycrypt/utils.h"
41-
#include "tinycrypt/cmac_mode.h"
42-
#include "tinycrypt/ecc_dh.h"
43-
4438
#if MYNEWT_VAL(BLE_MESH_SETTINGS)
4539
#include "config/config.h"
4640
#endif

nimble/host/mesh/pkg.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pkg.keywords:
2929
pkg.deps:
3030
- "@apache-mynewt-core/kernel/os"
3131
- "@apache-mynewt-core/util/mem"
32-
- "@apache-mynewt-core/crypto/tinycrypt"
32+
- "@apache-mynewt-core/crypto/mbedtls"
3333
- nimble
3434
- nimble/host
3535

nimble/host/mesh/src/crypto.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
#include <stdbool.h>
1616
#include <errno.h>
1717

18-
#include <tinycrypt/constants.h>
19-
#include <tinycrypt/utils.h>
20-
#include <tinycrypt/aes.h>
21-
#include <tinycrypt/cmac_mode.h>
22-
#include <tinycrypt/ccm_mode.h>
18+
#include <mbedtls/cmac.h>
2319

2420
#include "crypto.h"
2521

@@ -29,25 +25,35 @@
2925
int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
3026
size_t sg_len, uint8_t mac[16])
3127
{
32-
struct tc_aes_key_sched_struct sched;
33-
struct tc_cmac_struct state;
28+
mbedtls_cipher_context_t ctx;
29+
int err = -EIO;
3430

35-
if (tc_cmac_setup(&state, key, &sched) == TC_CRYPTO_FAIL) {
36-
return -EIO;
31+
mbedtls_cipher_init(&ctx);
32+
33+
if (mbedtls_cipher_setup(&ctx,
34+
mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB))) {
35+
goto done;
36+
}
37+
38+
if (mbedtls_cipher_cmac_starts(&ctx, key, 128)) {
39+
goto done;
3740
}
3841

3942
for (; sg_len; sg_len--, sg++) {
40-
if (tc_cmac_update(&state, sg->data,
41-
sg->len) == TC_CRYPTO_FAIL) {
42-
return -EIO;
43+
if (mbedtls_cipher_cmac_update(&ctx, sg->data, sg->len)) {
44+
goto done;
4345
}
4446
}
4547

46-
if (tc_cmac_final(mac, &state) == TC_CRYPTO_FAIL) {
47-
return -EIO;
48+
if (mbedtls_cipher_cmac_finish(&ctx, mac)) {
49+
goto done;
4850
}
4951

50-
return 0;
52+
err = 0;
53+
54+
done:
55+
mbedtls_cipher_free(&ctx);
56+
return err;
5157
}
5258

5359
int bt_mesh_k1(const uint8_t *ikm, size_t ikm_len, const uint8_t salt[16],

nimble/host/mesh/src/glue.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "base64/base64.h"
3434
#endif
3535

36+
#include <mbedtls/aes.h>
37+
3638
extern uint8_t g_mesh_addr_type;
3739

3840
#if MYNEWT_VAL(BLE_EXT_ADV)
@@ -130,17 +132,21 @@ void net_buf_simple_clone(const struct os_mbuf *original,
130132
int
131133
bt_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data)
132134
{
133-
struct tc_aes_key_sched_struct s;
135+
mbedtls_aes_context ctx;
136+
int err;
134137

135-
if (tc_aes128_set_encrypt_key(&s, key) == TC_CRYPTO_FAIL) {
136-
return BLE_HS_EUNKNOWN;
137-
}
138+
mbedtls_aes_init(&ctx);
139+
mbedtls_aes_setkey_enc(&ctx, key, 128);
138140

139-
if (tc_aes_encrypt(enc_data, plaintext, &s) == TC_CRYPTO_FAIL) {
140-
return BLE_HS_EUNKNOWN;
141-
}
141+
err = mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, plaintext, enc_data);
142142

143-
return 0;
143+
mbedtls_aes_free(&ctx);
144+
145+
if (err) {
146+
return BLE_HS_EUNKNOWN;
147+
}
148+
149+
return 0;
144150
}
145151

146152
uint16_t

nimble/host/mesh/src/prov_device.c

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "settings.h"
2525
#include "pb_gatt_srv.h"
2626

27+
#include "mbedtls/ecdh.h"
28+
2729
static void send_pub_key(void);
2830
static void pub_key_ready(const uint8_t *pkey);
2931

@@ -295,6 +297,63 @@ static void prov_dh_key_cb(const uint8_t dhkey[BT_DH_KEY_LEN])
295297
dh_key_gen_complete();
296298
}
297299

300+
301+
static int
302+
mbedtls_rand(void *arg, unsigned char *buf, size_t size)
303+
{
304+
return bt_rand(arg, size);
305+
}
306+
307+
int bt_mesh_dhkey_gen(const uint8_t *remote_pk, const uint8_t *private_key_be,
308+
uint8_t *dhkey)
309+
{
310+
int ret;
311+
mbedtls_ecp_group group;
312+
mbedtls_ecp_point pub_key;
313+
mbedtls_mpi priv_key;
314+
mbedtls_mpi z;
315+
uint8_t uncompressed_pk[65];
316+
317+
mbedtls_ecp_group_init(&group);
318+
mbedtls_ecp_point_init(&pub_key);
319+
mbedtls_mpi_init(&priv_key);
320+
mbedtls_mpi_init(&z);
321+
322+
ret = mbedtls_ecp_group_load(&group, MBEDTLS_ECP_DP_SECP256R1);
323+
if (ret != 0) {
324+
goto done;
325+
}
326+
327+
uncompressed_pk[0] = 0x04;
328+
memcpy(&uncompressed_pk[1], remote_pk, 64);
329+
330+
ret = mbedtls_ecp_point_read_binary(&group, &pub_key, uncompressed_pk, 65);
331+
if (ret != 0) {
332+
goto done;
333+
}
334+
335+
ret = mbedtls_mpi_read_binary(&priv_key, private_key_be, 32);
336+
if (ret != 0) {
337+
goto done;
338+
}
339+
340+
ret = mbedtls_ecdh_compute_shared(&group, &z, &pub_key, &priv_key,
341+
mbedtls_rand, NULL);
342+
if (ret != 0) {
343+
goto done;
344+
}
345+
346+
ret = mbedtls_mpi_write_binary(&z, dhkey, 32);
347+
348+
done:
349+
mbedtls_mpi_free(&z);
350+
mbedtls_mpi_free(&priv_key);
351+
mbedtls_ecp_point_free(&pub_key);
352+
mbedtls_ecp_group_free(&group);
353+
354+
return ret;
355+
}
356+
298357
static void prov_dh_key_gen(void)
299358
{
300359
const uint8_t *remote_pk;
@@ -303,11 +362,9 @@ static void prov_dh_key_gen(void)
303362
remote_pk = bt_mesh_prov_link.conf_inputs.pub_key_provisioner;
304363
if (MYNEWT_VAL(BLE_MESH_PROV_OOB_PUBLIC_KEY) &&
305364
atomic_test_bit(bt_mesh_prov_link.flags, OOB_PUB_KEY)) {
306-
if (uECC_valid_public_key(remote_pk, &curve_secp256r1)) {
307-
BT_ERR("Public key is not valid");
308-
} else if (uECC_shared_secret(remote_pk, bt_mesh_prov->private_key_be,
309-
bt_mesh_prov_link.dhkey,
310-
&curve_secp256r1) != TC_CRYPTO_SUCCESS) {
365+
366+
if (bt_mesh_dhkey_gen(remote_pk, bt_mesh_prov->private_key_be,
367+
bt_mesh_prov_link.dhkey)) {
311368
BT_ERR("DHKey generation failed");
312369
} else {
313370
dh_key_gen_complete();

0 commit comments

Comments
 (0)