hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/net/bluetooth/smp.c
....@@ -23,6 +23,7 @@
2323 #include <linux/debugfs.h>
2424 #include <linux/scatterlist.h>
2525 #include <linux/crypto.h>
26
+#include <crypto/aes.h>
2627 #include <crypto/algapi.h>
2728 #include <crypto/b128ops.h>
2829 #include <crypto/hash.h>
....@@ -88,7 +89,6 @@
8889 u8 local_rand[16];
8990 bool debug_key;
9091
91
- struct crypto_cipher *tfm_aes;
9292 struct crypto_shash *tfm_cmac;
9393 struct crypto_kpp *tfm_ecdh;
9494 };
....@@ -127,7 +127,6 @@
127127 u8 dhkey[32];
128128 u8 mackey[16];
129129
130
- struct crypto_cipher *tfm_aes;
131130 struct crypto_shash *tfm_cmac;
132131 struct crypto_kpp *tfm_ecdh;
133132 };
....@@ -171,7 +170,6 @@
171170 size_t len, u8 mac[16])
172171 {
173172 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
174
- SHASH_DESC_ON_STACK(desc, tfm);
175173 int err;
176174
177175 if (len > CMAC_MSG_MAX)
....@@ -181,9 +179,6 @@
181179 BT_ERR("tfm %p", tfm);
182180 return -EINVAL;
183181 }
184
-
185
- desc->tfm = tfm;
186
- desc->flags = 0;
187182
188183 /* Swap key and message from LSB to MSB */
189184 swap_buf(k, tmp, 16);
....@@ -198,8 +193,7 @@
198193 return err;
199194 }
200195
201
- err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
202
- shash_desc_zero(desc);
196
+ err = crypto_shash_tfm_digest(tfm, msg_msb, len, mac_msb);
203197 if (err) {
204198 BT_ERR("Hash computation error %d", err);
205199 return err;
....@@ -378,22 +372,18 @@
378372 * s1 and ah.
379373 */
380374
381
-static int smp_e(struct crypto_cipher *tfm, const u8 *k, u8 *r)
375
+static int smp_e(const u8 *k, u8 *r)
382376 {
377
+ struct crypto_aes_ctx ctx;
383378 uint8_t tmp[16], data[16];
384379 int err;
385380
386381 SMP_DBG("k %16phN r %16phN", k, r);
387382
388
- if (!tfm) {
389
- BT_ERR("tfm %p", tfm);
390
- return -EINVAL;
391
- }
392
-
393383 /* The most significant octet of key corresponds to k[0] */
394384 swap_buf(k, tmp, 16);
395385
396
- err = crypto_cipher_setkey(tfm, tmp, 16);
386
+ err = aes_expandkey(&ctx, tmp, 16);
397387 if (err) {
398388 BT_ERR("cipher setkey failed: %d", err);
399389 return err;
....@@ -402,17 +392,18 @@
402392 /* Most significant octet of plaintextData corresponds to data[0] */
403393 swap_buf(r, data, 16);
404394
405
- crypto_cipher_encrypt_one(tfm, data, data);
395
+ aes_encrypt(&ctx, data, data);
406396
407397 /* Most significant octet of encryptedData corresponds to data[0] */
408398 swap_buf(data, r, 16);
409399
410400 SMP_DBG("r %16phN", r);
411401
402
+ memzero_explicit(&ctx, sizeof (ctx));
412403 return err;
413404 }
414405
415
-static int smp_c1(struct crypto_cipher *tfm_aes, const u8 k[16],
406
+static int smp_c1(const u8 k[16],
416407 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
417408 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
418409 {
....@@ -437,7 +428,7 @@
437428 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
438429
439430 /* res = e(k, res) */
440
- err = smp_e(tfm_aes, k, res);
431
+ err = smp_e(k, res);
441432 if (err) {
442433 BT_ERR("Encrypt data error");
443434 return err;
....@@ -454,14 +445,14 @@
454445 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
455446
456447 /* res = e(k, res) */
457
- err = smp_e(tfm_aes, k, res);
448
+ err = smp_e(k, res);
458449 if (err)
459450 BT_ERR("Encrypt data error");
460451
461452 return err;
462453 }
463454
464
-static int smp_s1(struct crypto_cipher *tfm_aes, const u8 k[16],
455
+static int smp_s1(const u8 k[16],
465456 const u8 r1[16], const u8 r2[16], u8 _r[16])
466457 {
467458 int err;
....@@ -470,15 +461,14 @@
470461 memcpy(_r, r2, 8);
471462 memcpy(_r + 8, r1, 8);
472463
473
- err = smp_e(tfm_aes, k, _r);
464
+ err = smp_e(k, _r);
474465 if (err)
475466 BT_ERR("Encrypt data error");
476467
477468 return err;
478469 }
479470
480
-static int smp_ah(struct crypto_cipher *tfm, const u8 irk[16],
481
- const u8 r[3], u8 res[3])
471
+static int smp_ah(const u8 irk[16], const u8 r[3], u8 res[3])
482472 {
483473 u8 _res[16];
484474 int err;
....@@ -487,7 +477,7 @@
487477 memcpy(_res, r, 3);
488478 memset(_res + 3, 0, 13);
489479
490
- err = smp_e(tfm, irk, _res);
480
+ err = smp_e(irk, _res);
491481 if (err) {
492482 BT_ERR("Encrypt error");
493483 return err;
....@@ -508,18 +498,15 @@
508498 const bdaddr_t *bdaddr)
509499 {
510500 struct l2cap_chan *chan = hdev->smp_data;
511
- struct smp_dev *smp;
512501 u8 hash[3];
513502 int err;
514503
515504 if (!chan || !chan->data)
516505 return false;
517506
518
- smp = chan->data;
507
+ bt_dev_dbg(hdev, "RPA %pMR IRK %*phN", bdaddr, 16, irk);
519508
520
- BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
521
-
522
- err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
509
+ err = smp_ah(irk, &bdaddr->b[3], hash);
523510 if (err)
524511 return false;
525512
....@@ -529,24 +516,21 @@
529516 int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
530517 {
531518 struct l2cap_chan *chan = hdev->smp_data;
532
- struct smp_dev *smp;
533519 int err;
534520
535521 if (!chan || !chan->data)
536522 return -EOPNOTSUPP;
537
-
538
- smp = chan->data;
539523
540524 get_random_bytes(&rpa->b[3], 3);
541525
542526 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
543527 rpa->b[5] |= 0x40; /* Set second most significant bit */
544528
545
- err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
529
+ err = smp_ah(irk, &rpa->b[3], rpa->b);
546530 if (err < 0)
547531 return err;
548532
549
- BT_DBG("RPA %pMR", rpa);
533
+ bt_dev_dbg(hdev, "RPA %pMR", rpa);
550534
551535 return 0;
552536 }
....@@ -563,7 +547,7 @@
563547 smp = chan->data;
564548
565549 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
566
- BT_DBG("Using debug keys");
550
+ bt_dev_dbg(hdev, "Using debug keys");
567551 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk);
568552 if (err)
569553 return err;
....@@ -622,7 +606,7 @@
622606
623607 memset(&msg, 0, sizeof(msg));
624608
625
- iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
609
+ iov_iter_kvec(&msg.msg_iter, WRITE, iv, 2, 1 + len);
626610
627611 l2cap_chan_send(chan, &msg, 1 + len);
628612
....@@ -742,6 +726,10 @@
742726 struct hci_dev *hdev = conn->hcon->hdev;
743727 struct smp_chan *smp = chan->data;
744728
729
+ if (conn->hcon->pending_sec_level == BT_SECURITY_FIPS &&
730
+ max_key_size != SMP_MAX_ENC_KEY_SIZE)
731
+ return SMP_ENC_KEY_SIZE;
732
+
745733 if (max_key_size > hdev->le_max_key_size ||
746734 max_key_size < SMP_MIN_ENC_KEY_SIZE)
747735 return SMP_ENC_KEY_SIZE;
....@@ -765,11 +753,10 @@
765753 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
766754 mgmt_smp_complete(hcon, complete);
767755
768
- kzfree(smp->csrk);
769
- kzfree(smp->slave_csrk);
770
- kzfree(smp->link_key);
756
+ kfree_sensitive(smp->csrk);
757
+ kfree_sensitive(smp->slave_csrk);
758
+ kfree_sensitive(smp->link_key);
771759
772
- crypto_free_cipher(smp->tfm_aes);
773760 crypto_free_shash(smp->tfm_cmac);
774761 crypto_free_kpp(smp->tfm_ecdh);
775762
....@@ -802,7 +789,7 @@
802789 }
803790
804791 chan->data = NULL;
805
- kzfree(smp);
792
+ kfree_sensitive(smp);
806793 hci_conn_drop(hcon);
807794 }
808795
....@@ -867,7 +854,7 @@
867854 struct l2cap_chan *chan = conn->smp;
868855 struct smp_chan *smp = chan->data;
869856 u32 passkey = 0;
870
- int ret = 0;
857
+ int ret;
871858
872859 /* Initialize key for JUST WORKS */
873860 memset(smp->tk, 0, sizeof(smp->tk));
....@@ -896,9 +883,16 @@
896883 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
897884 smp->method = JUST_WORKS;
898885
899
- /* If Just Works, Continue with Zero TK */
886
+ /* If Just Works, Continue with Zero TK and ask user-space for
887
+ * confirmation */
900888 if (smp->method == JUST_WORKS) {
901
- set_bit(SMP_FLAG_TK_VALID, &smp->flags);
889
+ ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
890
+ hcon->type,
891
+ hcon->dst_type,
892
+ passkey, 1);
893
+ if (ret)
894
+ return ret;
895
+ set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
902896 return 0;
903897 }
904898
....@@ -915,8 +909,8 @@
915909 hcon->pending_sec_level = BT_SECURITY_HIGH;
916910 }
917911
918
- /* If both devices have Keyoard-Display I/O, the master
919
- * Confirms and the slave Enters the passkey.
912
+ /* If both devices have Keyboard-Display I/O, the initiator
913
+ * Confirms and the responder Enters the passkey.
920914 */
921915 if (smp->method == OVERLAP) {
922916 if (hcon->role == HCI_ROLE_MASTER)
....@@ -958,7 +952,7 @@
958952
959953 BT_DBG("conn %p", conn);
960954
961
- ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
955
+ ret = smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp,
962956 conn->hcon->init_addr_type, &conn->hcon->init_addr,
963957 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
964958 cp.confirm_val);
....@@ -984,12 +978,9 @@
984978 u8 confirm[16];
985979 int ret;
986980
987
- if (IS_ERR_OR_NULL(smp->tfm_aes))
988
- return SMP_UNSPECIFIED;
989
-
990981 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
991982
992
- ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
983
+ ret = smp_c1(smp->tk, smp->rrnd, smp->preq, smp->prsp,
993984 hcon->init_addr_type, &hcon->init_addr,
994985 hcon->resp_addr_type, &hcon->resp_addr, confirm);
995986 if (ret)
....@@ -1006,7 +997,7 @@
1006997 __le64 rand = 0;
1007998 __le16 ediv = 0;
1008999
1009
- smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
1000
+ smp_s1(smp->tk, smp->rrnd, smp->prnd, stk);
10101001
10111002 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
10121003 return SMP_UNSPECIFIED;
....@@ -1022,7 +1013,7 @@
10221013 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
10231014 smp->prnd);
10241015
1025
- smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
1016
+ smp_s1(smp->tk, smp->prnd, smp->rrnd, stk);
10261017
10271018 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
10281019 auth = 1;
....@@ -1161,11 +1152,11 @@
11611152 return;
11621153
11631154 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1164
- /* SALT = 0x00000000000000000000000000000000746D7031 */
1155
+ /* SALT = 0x000000000000000000000000746D7031 */
11651156 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 };
11661157
11671158 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) {
1168
- kzfree(smp->link_key);
1159
+ kfree_sensitive(smp->link_key);
11691160 smp->link_key = NULL;
11701161 return;
11711162 }
....@@ -1174,14 +1165,14 @@
11741165 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
11751166
11761167 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1177
- kzfree(smp->link_key);
1168
+ kfree_sensitive(smp->link_key);
11781169 smp->link_key = NULL;
11791170 return;
11801171 }
11811172 }
11821173
11831174 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1184
- kzfree(smp->link_key);
1175
+ kfree_sensitive(smp->link_key);
11851176 smp->link_key = NULL;
11861177 return;
11871178 }
....@@ -1219,7 +1210,7 @@
12191210 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
12201211
12211212 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1222
- /* SALT = 0x00000000000000000000000000000000746D7032 */
1213
+ /* SALT = 0x000000000000000000000000746D7032 */
12231214 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 };
12241215
12251216 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk))
....@@ -1390,19 +1381,13 @@
13901381 if (!smp)
13911382 return NULL;
13921383
1393
- smp->tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
1394
- if (IS_ERR(smp->tfm_aes)) {
1395
- BT_ERR("Unable to create AES crypto context");
1396
- goto zfree_smp;
1397
- }
1398
-
13991384 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
14001385 if (IS_ERR(smp->tfm_cmac)) {
14011386 BT_ERR("Unable to create CMAC crypto context");
1402
- goto free_cipher;
1387
+ goto zfree_smp;
14031388 }
14041389
1405
- smp->tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
1390
+ smp->tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
14061391 if (IS_ERR(smp->tfm_ecdh)) {
14071392 BT_ERR("Unable to create ECDH crypto context");
14081393 goto free_shash;
....@@ -1421,10 +1406,8 @@
14211406
14221407 free_shash:
14231408 crypto_free_shash(smp->tfm_cmac);
1424
-free_cipher:
1425
- crypto_free_cipher(smp->tfm_aes);
14261409 zfree_smp:
1427
- kzfree(smp);
1410
+ kfree_sensitive(smp);
14281411 return NULL;
14291412 }
14301413
....@@ -1671,7 +1654,7 @@
16711654 memset(smp->tk, 0, sizeof(smp->tk));
16721655 BT_DBG("PassKey: %d", value);
16731656 put_unaligned_le32(value, smp->tk);
1674
- /* Fall Through */
1657
+ fallthrough;
16751658 case MGMT_OP_USER_CONFIRM_REPLY:
16761659 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
16771660 break;
....@@ -1884,7 +1867,7 @@
18841867 {
18851868 struct hci_dev *hdev = smp->conn->hcon->hdev;
18861869
1887
- BT_DBG("");
1870
+ bt_dev_dbg(hdev, "");
18881871
18891872 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
18901873 struct l2cap_chan *chan = hdev->smp_data;
....@@ -2139,7 +2122,7 @@
21392122 struct l2cap_chan *chan = conn->smp;
21402123 struct smp_chan *smp = chan->data;
21412124 struct hci_conn *hcon = conn->hcon;
2142
- u8 *pkax, *pkbx, *na, *nb;
2125
+ u8 *pkax, *pkbx, *na, *nb, confirm_hint;
21432126 u32 passkey;
21442127 int err;
21452128
....@@ -2192,6 +2175,24 @@
21922175 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
21932176 smp->prnd);
21942177 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2178
+
2179
+ /* Only Just-Works pairing requires extra checks */
2180
+ if (smp->method != JUST_WORKS)
2181
+ goto mackey_and_ltk;
2182
+
2183
+ /* If there already exists long term key in local host, leave
2184
+ * the decision to user space since the remote device could
2185
+ * be legitimate or malicious.
2186
+ */
2187
+ if (hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2188
+ hcon->role)) {
2189
+ /* Set passkey to 0. The value can be any number since
2190
+ * it'll be ignored anyway.
2191
+ */
2192
+ passkey = 0;
2193
+ confirm_hint = 1;
2194
+ goto confirm;
2195
+ }
21952196 }
21962197
21972198 mackey_and_ltk:
....@@ -2200,7 +2201,7 @@
22002201 if (err)
22012202 return SMP_UNSPECIFIED;
22022203
2203
- if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
2204
+ if (smp->method == REQ_OOB) {
22042205 if (hcon->out) {
22052206 sc_dhkey_check(smp);
22062207 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
....@@ -2212,8 +2213,14 @@
22122213 if (err)
22132214 return SMP_UNSPECIFIED;
22142215
2216
+ confirm_hint = 0;
2217
+
2218
+confirm:
2219
+ if (smp->method == JUST_WORKS)
2220
+ confirm_hint = 1;
2221
+
22152222 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2216
- hcon->dst_type, passkey, 0);
2223
+ hcon->dst_type, passkey, confirm_hint);
22172224 if (err)
22182225 return SMP_UNSPECIFIED;
22192226
....@@ -2388,12 +2395,17 @@
23882395 authreq |= SMP_AUTH_CT2;
23892396 }
23902397
2391
- /* Require MITM if IO Capability allows or the security level
2392
- * requires it.
2398
+ /* Don't attempt to set MITM if setting is overridden by debugfs
2399
+ * Needed to pass certification test SM/MAS/PKE/BV-01-C
23932400 */
2394
- if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
2395
- hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2396
- authreq |= SMP_AUTH_MITM;
2401
+ if (!hci_dev_test_flag(hcon->hdev, HCI_FORCE_NO_MITM)) {
2402
+ /* Require MITM if IO Capability allows or the security level
2403
+ * requires it.
2404
+ */
2405
+ if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
2406
+ hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2407
+ authreq |= SMP_AUTH_MITM;
2408
+ }
23972409
23982410 if (hcon->role == HCI_ROLE_MASTER) {
23992411 struct smp_cmd_pairing cp;
....@@ -2477,6 +2489,15 @@
24772489 if (skb->len < sizeof(*rp))
24782490 return SMP_INVALID_PARAMS;
24792491
2492
+ /* Pairing is aborted if any blocked keys are distributed */
2493
+ if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_LTK,
2494
+ rp->ltk)) {
2495
+ bt_dev_warn_ratelimited(conn->hcon->hdev,
2496
+ "LTK blocked for %pMR",
2497
+ &conn->hcon->dst);
2498
+ return SMP_INVALID_PARAMS;
2499
+ }
2500
+
24802501 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
24812502
24822503 skb_pull(skb, sizeof(*rp));
....@@ -2532,6 +2553,15 @@
25322553
25332554 if (skb->len < sizeof(*info))
25342555 return SMP_INVALID_PARAMS;
2556
+
2557
+ /* Pairing is aborted if any blocked keys are distributed */
2558
+ if (hci_is_blocked_key(conn->hcon->hdev, HCI_BLOCKED_KEY_TYPE_IRK,
2559
+ info->irk)) {
2560
+ bt_dev_warn_ratelimited(conn->hcon->hdev,
2561
+ "Identity key blocked for %pMR",
2562
+ &conn->hcon->dst);
2563
+ return SMP_INVALID_PARAMS;
2564
+ }
25352565
25362566 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
25372567
....@@ -3046,7 +3076,7 @@
30463076 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
30473077 return;
30483078
3049
- /* Only master may initiate SMP over BR/EDR */
3079
+ /* Only initiator may initiate SMP over BR/EDR */
30503080 if (hcon->role != HCI_ROLE_MASTER)
30513081 return;
30523082
....@@ -3242,7 +3272,6 @@
32423272 {
32433273 struct l2cap_chan *chan;
32443274 struct smp_dev *smp;
3245
- struct crypto_cipher *tfm_aes;
32463275 struct crypto_shash *tfm_cmac;
32473276 struct crypto_kpp *tfm_ecdh;
32483277
....@@ -3255,32 +3284,22 @@
32553284 if (!smp)
32563285 return ERR_PTR(-ENOMEM);
32573286
3258
- tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
3259
- if (IS_ERR(tfm_aes)) {
3260
- BT_ERR("Unable to create AES crypto context");
3261
- kzfree(smp);
3262
- return ERR_CAST(tfm_aes);
3263
- }
3264
-
32653287 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
32663288 if (IS_ERR(tfm_cmac)) {
32673289 BT_ERR("Unable to create CMAC crypto context");
3268
- crypto_free_cipher(tfm_aes);
3269
- kzfree(smp);
3290
+ kfree_sensitive(smp);
32703291 return ERR_CAST(tfm_cmac);
32713292 }
32723293
3273
- tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3294
+ tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
32743295 if (IS_ERR(tfm_ecdh)) {
32753296 BT_ERR("Unable to create ECDH crypto context");
32763297 crypto_free_shash(tfm_cmac);
3277
- crypto_free_cipher(tfm_aes);
3278
- kzfree(smp);
3298
+ kfree_sensitive(smp);
32793299 return ERR_CAST(tfm_ecdh);
32803300 }
32813301
32823302 smp->local_oob = false;
3283
- smp->tfm_aes = tfm_aes;
32843303 smp->tfm_cmac = tfm_cmac;
32853304 smp->tfm_ecdh = tfm_ecdh;
32863305
....@@ -3288,10 +3307,9 @@
32883307 chan = l2cap_chan_create();
32893308 if (!chan) {
32903309 if (smp) {
3291
- crypto_free_cipher(smp->tfm_aes);
32923310 crypto_free_shash(smp->tfm_cmac);
32933311 crypto_free_kpp(smp->tfm_ecdh);
3294
- kzfree(smp);
3312
+ kfree_sensitive(smp);
32953313 }
32963314 return ERR_PTR(-ENOMEM);
32973315 }
....@@ -3336,10 +3354,9 @@
33363354 smp = chan->data;
33373355 if (smp) {
33383356 chan->data = NULL;
3339
- crypto_free_cipher(smp->tfm_aes);
33403357 crypto_free_shash(smp->tfm_cmac);
33413358 crypto_free_kpp(smp->tfm_ecdh);
3342
- kzfree(smp);
3359
+ kfree_sensitive(smp);
33433360 }
33443361
33453362 l2cap_chan_put(chan);
....@@ -3401,94 +3418,6 @@
34013418 .llseek = default_llseek,
34023419 };
34033420
3404
-static ssize_t le_min_key_size_read(struct file *file,
3405
- char __user *user_buf,
3406
- size_t count, loff_t *ppos)
3407
-{
3408
- struct hci_dev *hdev = file->private_data;
3409
- char buf[4];
3410
-
3411
- snprintf(buf, sizeof(buf), "%2u\n", hdev->le_min_key_size);
3412
-
3413
- return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3414
-}
3415
-
3416
-static ssize_t le_min_key_size_write(struct file *file,
3417
- const char __user *user_buf,
3418
- size_t count, loff_t *ppos)
3419
-{
3420
- struct hci_dev *hdev = file->private_data;
3421
- char buf[32];
3422
- size_t buf_size = min(count, (sizeof(buf) - 1));
3423
- u8 key_size;
3424
-
3425
- if (copy_from_user(buf, user_buf, buf_size))
3426
- return -EFAULT;
3427
-
3428
- buf[buf_size] = '\0';
3429
-
3430
- sscanf(buf, "%hhu", &key_size);
3431
-
3432
- if (key_size > hdev->le_max_key_size ||
3433
- key_size < SMP_MIN_ENC_KEY_SIZE)
3434
- return -EINVAL;
3435
-
3436
- hdev->le_min_key_size = key_size;
3437
-
3438
- return count;
3439
-}
3440
-
3441
-static const struct file_operations le_min_key_size_fops = {
3442
- .open = simple_open,
3443
- .read = le_min_key_size_read,
3444
- .write = le_min_key_size_write,
3445
- .llseek = default_llseek,
3446
-};
3447
-
3448
-static ssize_t le_max_key_size_read(struct file *file,
3449
- char __user *user_buf,
3450
- size_t count, loff_t *ppos)
3451
-{
3452
- struct hci_dev *hdev = file->private_data;
3453
- char buf[4];
3454
-
3455
- snprintf(buf, sizeof(buf), "%2u\n", hdev->le_max_key_size);
3456
-
3457
- return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3458
-}
3459
-
3460
-static ssize_t le_max_key_size_write(struct file *file,
3461
- const char __user *user_buf,
3462
- size_t count, loff_t *ppos)
3463
-{
3464
- struct hci_dev *hdev = file->private_data;
3465
- char buf[32];
3466
- size_t buf_size = min(count, (sizeof(buf) - 1));
3467
- u8 key_size;
3468
-
3469
- if (copy_from_user(buf, user_buf, buf_size))
3470
- return -EFAULT;
3471
-
3472
- buf[buf_size] = '\0';
3473
-
3474
- sscanf(buf, "%hhu", &key_size);
3475
-
3476
- if (key_size > SMP_MAX_ENC_KEY_SIZE ||
3477
- key_size < hdev->le_min_key_size)
3478
- return -EINVAL;
3479
-
3480
- hdev->le_max_key_size = key_size;
3481
-
3482
- return count;
3483
-}
3484
-
3485
-static const struct file_operations le_max_key_size_fops = {
3486
- .open = simple_open,
3487
- .read = le_max_key_size_read,
3488
- .write = le_max_key_size_write,
3489
- .llseek = default_llseek,
3490
-};
3491
-
34923421 int smp_register(struct hci_dev *hdev)
34933422 {
34943423 struct l2cap_chan *chan;
....@@ -3512,11 +3441,6 @@
35123441 return PTR_ERR(chan);
35133442
35143443 hdev->smp_data = chan;
3515
-
3516
- debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3517
- &le_min_key_size_fops);
3518
- debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3519
- &le_max_key_size_fops);
35203444
35213445 /* If the controller does not support BR/EDR Secure Connections
35223446 * feature, then the BR/EDR SMP channel shall not be present.
....@@ -3592,7 +3516,7 @@
35923516 return 0;
35933517 }
35943518
3595
-static int __init test_ah(struct crypto_cipher *tfm_aes)
3519
+static int __init test_ah(void)
35963520 {
35973521 const u8 irk[16] = {
35983522 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
....@@ -3602,7 +3526,7 @@
36023526 u8 res[3];
36033527 int err;
36043528
3605
- err = smp_ah(tfm_aes, irk, r, res);
3529
+ err = smp_ah(irk, r, res);
36063530 if (err)
36073531 return err;
36083532
....@@ -3612,7 +3536,7 @@
36123536 return 0;
36133537 }
36143538
3615
-static int __init test_c1(struct crypto_cipher *tfm_aes)
3539
+static int __init test_c1(void)
36163540 {
36173541 const u8 k[16] = {
36183542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
....@@ -3632,7 +3556,7 @@
36323556 u8 res[16];
36333557 int err;
36343558
3635
- err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3559
+ err = smp_c1(k, r, preq, pres, _iat, &ia, _rat, &ra, res);
36363560 if (err)
36373561 return err;
36383562
....@@ -3642,7 +3566,7 @@
36423566 return 0;
36433567 }
36443568
3645
-static int __init test_s1(struct crypto_cipher *tfm_aes)
3569
+static int __init test_s1(void)
36463570 {
36473571 const u8 k[16] = {
36483572 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
....@@ -3657,7 +3581,7 @@
36573581 u8 res[16];
36583582 int err;
36593583
3660
- err = smp_s1(tfm_aes, k, r1, r2, res);
3584
+ err = smp_s1(k, r1, r2, res);
36613585 if (err)
36623586 return err;
36633587
....@@ -3838,8 +3762,7 @@
38383762 .llseek = default_llseek,
38393763 };
38403764
3841
-static int __init run_selftests(struct crypto_cipher *tfm_aes,
3842
- struct crypto_shash *tfm_cmac,
3765
+static int __init run_selftests(struct crypto_shash *tfm_cmac,
38433766 struct crypto_kpp *tfm_ecdh)
38443767 {
38453768 ktime_t calltime, delta, rettime;
....@@ -3854,19 +3777,19 @@
38543777 goto done;
38553778 }
38563779
3857
- err = test_ah(tfm_aes);
3780
+ err = test_ah();
38583781 if (err) {
38593782 BT_ERR("smp_ah test failed");
38603783 goto done;
38613784 }
38623785
3863
- err = test_c1(tfm_aes);
3786
+ err = test_c1();
38643787 if (err) {
38653788 BT_ERR("smp_c1 test failed");
38663789 goto done;
38673790 }
38683791
3869
- err = test_s1(tfm_aes);
3792
+ err = test_s1();
38703793 if (err) {
38713794 BT_ERR("smp_s1 test failed");
38723795 goto done;
....@@ -3923,36 +3846,26 @@
39233846
39243847 int __init bt_selftest_smp(void)
39253848 {
3926
- struct crypto_cipher *tfm_aes;
39273849 struct crypto_shash *tfm_cmac;
39283850 struct crypto_kpp *tfm_ecdh;
39293851 int err;
39303852
3931
- tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
3932
- if (IS_ERR(tfm_aes)) {
3933
- BT_ERR("Unable to create AES crypto context");
3934
- return PTR_ERR(tfm_aes);
3935
- }
3936
-
3937
- tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3853
+ tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
39383854 if (IS_ERR(tfm_cmac)) {
39393855 BT_ERR("Unable to create CMAC crypto context");
3940
- crypto_free_cipher(tfm_aes);
39413856 return PTR_ERR(tfm_cmac);
39423857 }
39433858
3944
- tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3859
+ tfm_ecdh = crypto_alloc_kpp("ecdh", 0, 0);
39453860 if (IS_ERR(tfm_ecdh)) {
39463861 BT_ERR("Unable to create ECDH crypto context");
39473862 crypto_free_shash(tfm_cmac);
3948
- crypto_free_cipher(tfm_aes);
39493863 return PTR_ERR(tfm_ecdh);
39503864 }
39513865
3952
- err = run_selftests(tfm_aes, tfm_cmac, tfm_ecdh);
3866
+ err = run_selftests(tfm_cmac, tfm_ecdh);
39533867
39543868 crypto_free_shash(tfm_cmac);
3955
- crypto_free_cipher(tfm_aes);
39563869 crypto_free_kpp(tfm_ecdh);
39573870
39583871 return err;