Repository: incubator-mynewt-core Updated Branches: refs/heads/sensors_branch 6247b5afa -> 2681044e8
nimble/sm: Use TinyCrypt for AES TinyCrypt is smaller than mbedTLS and is already used for ECDH. Using TC for all crypto in SM results in following code size reductions for bletiny application: Legacy Pairing only from > 250 277 *fill* > 11160 0 crypto_mbedtls.a > 48581 3410 net_nimble_host.a > 144992 2784 15788 163564 27eec apps/bletiny/bletiny.elf to < 252 277 *fill* < 1112 0 crypto_tinycrypt.a < 48563 3130 net_nimble_host.a < 134928 2784 15508 153220 25684 app/apps/bletiny/bletiny.elf Legacy + LE SC from > 264 276 *fill* > 11160 0 crypto_mbedtls.a > 51881 3627 net_nimble_host.a > 152272 2980 16004 171256 29cf8 app/apps/bletiny/bletiny.elf to < 254 276 *fill* < 51863 3347 net_nimble_host.a < 141084 2980 15724 159788 2702c app/apps/bletiny/bletiny.elf Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/2785cad5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/2785cad5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/2785cad5 Branch: refs/heads/sensors_branch Commit: 2785cad50147160d21bef9aef143199f294ed093 Parents: a46fdfe Author: Szymon Janc <szymon.j...@codecoup.pl> Authored: Wed Jan 18 14:24:44 2017 +0100 Committer: Szymon Janc <szymon.j...@codecoup.pl> Committed: Wed Jan 18 14:54:44 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/pkg.yml | 2 +- net/nimble/host/src/ble_sm_alg.c | 21 +++++++-------------- 2 files changed, 8 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2785cad5/net/nimble/host/pkg.yml ---------------------------------------------------------------------- diff --git a/net/nimble/host/pkg.yml b/net/nimble/host/pkg.yml index f7539a4..d025934 100644 --- a/net/nimble/host/pkg.yml +++ b/net/nimble/host/pkg.yml @@ -31,7 +31,7 @@ pkg.deps: - util/mem pkg.deps.BLE_SM_LEGACY: - - crypto/mbedtls + - crypto/tinycrypt pkg.deps.BLE_SM_SC: - crypto/tinycrypt http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2785cad5/net/nimble/host/src/ble_sm_alg.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_alg.c b/net/nimble/host/src/ble_sm_alg.c index 8a5365d..f8208b4 100644 --- a/net/nimble/host/src/ble_sm_alg.c +++ b/net/nimble/host/src/ble_sm_alg.c @@ -28,20 +28,15 @@ #include "nimble/ble.h" #include "nimble/nimble_opt.h" #include "ble_hs_priv.h" -#include "mbedtls/aes.h" - -#if MYNEWT_VAL(BLE_SM_SC) - #include "tinycrypt/aes.h" #include "tinycrypt/constants.h" #include "tinycrypt/utils.h" + +#if MYNEWT_VAL(BLE_SM_SC) #include "tinycrypt/cmac_mode.h" #include "tinycrypt/ecc_dh.h" - #endif -static mbedtls_aes_context ble_sm_alg_ctxt; - static void ble_sm_alg_xor_128(uint8_t *p, uint8_t *q, uint8_t *r) { @@ -55,22 +50,20 @@ ble_sm_alg_xor_128(uint8_t *p, uint8_t *q, uint8_t *r) static int ble_sm_alg_encrypt(uint8_t *key, uint8_t *plaintext, uint8_t *enc_data) { - mbedtls_aes_init(&ble_sm_alg_ctxt); + struct tc_aes_key_sched_struct s; uint8_t tmp[16]; - int rc; swap_buf(tmp, key, 16); - rc = mbedtls_aes_setkey_enc(&ble_sm_alg_ctxt, tmp, 128); - if (rc != 0) { + if (tc_aes128_set_encrypt_key(&s, tmp) == TC_CRYPTO_FAIL) { return BLE_HS_EUNKNOWN; } swap_buf(tmp, plaintext, 16); - rc = mbedtls_aes_crypt_ecb(&ble_sm_alg_ctxt, MBEDTLS_AES_ENCRYPT, - tmp, enc_data); - if (rc != 0) { + + + if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) { return BLE_HS_EUNKNOWN; }