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/a8e3fe08
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/a8e3fe08
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/a8e3fe08

Branch: refs/heads/develop
Commit: a8e3fe08fd85b78064a8be07f906caa98d525471
Parents: 813d4dc
Author: Szymon Janc <[email protected]>
Authored: Wed Jan 18 14:24:44 2017 +0100
Committer: Christopher Collins <[email protected]>
Committed: Wed Jan 18 11:24:13 2017 -0800

----------------------------------------------------------------------
 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/a8e3fe08/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/a8e3fe08/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;
     }
 

Reply via email to