2626#include <crypto/algapi.h>
2727#include <crypto/b128ops.h>
2828#include <crypto/hash.h>
29+ #include <crypto/kpp.h>
2930
3031#include <net/bluetooth/bluetooth.h>
3132#include <net/bluetooth/hci_core.h>
@@ -92,6 +93,7 @@ struct smp_dev {
9293
9394 struct crypto_cipher * tfm_aes ;
9495 struct crypto_shash * tfm_cmac ;
96+ struct crypto_kpp * tfm_ecdh ;
9597};
9698
9799struct smp_chan {
@@ -131,6 +133,7 @@ struct smp_chan {
131133
132134 struct crypto_cipher * tfm_aes ;
133135 struct crypto_shash * tfm_cmac ;
136+ struct crypto_kpp * tfm_ecdh ;
134137};
135138
136139/* These debug key values are defined in the SMP section of the core
@@ -574,7 +577,8 @@ int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
574577 get_random_bytes (smp -> local_sk , 32 );
575578
576579 /* Generate local key pair for Secure Connections */
577- if (!generate_ecdh_keys (smp -> local_pk , smp -> local_sk ))
580+ if (!generate_ecdh_keys (smp -> tfm_ecdh , smp -> local_pk ,
581+ smp -> local_sk ))
578582 return - EIO ;
579583
580584 /* This is unlikely, but we need to check that
@@ -771,6 +775,7 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
771775
772776 crypto_free_cipher (smp -> tfm_aes );
773777 crypto_free_shash (smp -> tfm_cmac );
778+ crypto_free_kpp (smp -> tfm_ecdh );
774779
775780 /* Ensure that we don't leave any debug key around if debug key
776781 * support hasn't been explicitly enabled.
@@ -1391,16 +1396,19 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
13911396 smp -> tfm_aes = crypto_alloc_cipher ("aes" , 0 , CRYPTO_ALG_ASYNC );
13921397 if (IS_ERR (smp -> tfm_aes )) {
13931398 BT_ERR ("Unable to create AES crypto context" );
1394- kzfree (smp );
1395- return NULL ;
1399+ goto zfree_smp ;
13961400 }
13971401
13981402 smp -> tfm_cmac = crypto_alloc_shash ("cmac(aes)" , 0 , 0 );
13991403 if (IS_ERR (smp -> tfm_cmac )) {
14001404 BT_ERR ("Unable to create CMAC crypto context" );
1401- crypto_free_cipher (smp -> tfm_aes );
1402- kzfree (smp );
1403- return NULL ;
1405+ goto free_cipher ;
1406+ }
1407+
1408+ smp -> tfm_ecdh = crypto_alloc_kpp ("ecdh" , CRYPTO_ALG_INTERNAL , 0 );
1409+ if (IS_ERR (smp -> tfm_ecdh )) {
1410+ BT_ERR ("Unable to create ECDH crypto context" );
1411+ goto free_shash ;
14041412 }
14051413
14061414 smp -> conn = conn ;
@@ -1413,6 +1421,14 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
14131421 hci_conn_hold (conn -> hcon );
14141422
14151423 return smp ;
1424+
1425+ free_shash :
1426+ crypto_free_shash (smp -> tfm_cmac );
1427+ free_cipher :
1428+ crypto_free_cipher (smp -> tfm_aes );
1429+ zfree_smp :
1430+ kzfree (smp );
1431+ return NULL ;
14161432}
14171433
14181434static int sc_mackey_and_ltk (struct smp_chan * smp , u8 mackey [16 ], u8 ltk [16 ])
@@ -1903,7 +1919,8 @@ static u8 sc_send_public_key(struct smp_chan *smp)
19031919 get_random_bytes (smp -> local_sk , 32 );
19041920
19051921 /* Generate local key pair for Secure Connections */
1906- if (!generate_ecdh_keys (smp -> local_pk , smp -> local_sk ))
1922+ if (!generate_ecdh_keys (smp -> tfm_ecdh , smp -> local_pk ,
1923+ smp -> local_sk ))
19071924 return SMP_UNSPECIFIED ;
19081925
19091926 /* This is unlikely, but we need to check that
@@ -2677,7 +2694,8 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
26772694 SMP_DBG ("Remote Public Key X: %32phN" , smp -> remote_pk );
26782695 SMP_DBG ("Remote Public Key Y: %32phN" , smp -> remote_pk + 32 );
26792696
2680- if (!compute_ecdh_secret (smp -> remote_pk , smp -> local_sk , smp -> dhkey ))
2697+ if (!compute_ecdh_secret (smp -> tfm_ecdh , smp -> remote_pk , smp -> local_sk ,
2698+ smp -> dhkey ))
26812699 return SMP_UNSPECIFIED ;
26822700
26832701 SMP_DBG ("DHKey %32phN" , smp -> dhkey );
@@ -3169,6 +3187,7 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
31693187 struct smp_dev * smp ;
31703188 struct crypto_cipher * tfm_aes ;
31713189 struct crypto_shash * tfm_cmac ;
3190+ struct crypto_kpp * tfm_ecdh ;
31723191
31733192 if (cid == L2CAP_CID_SMP_BREDR ) {
31743193 smp = NULL ;
@@ -3194,8 +3213,18 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
31943213 return ERR_CAST (tfm_cmac );
31953214 }
31963215
3216+ tfm_ecdh = crypto_alloc_kpp ("ecdh" , CRYPTO_ALG_INTERNAL , 0 );
3217+ if (IS_ERR (tfm_ecdh )) {
3218+ BT_ERR ("Unable to create ECDH crypto context" );
3219+ crypto_free_shash (tfm_cmac );
3220+ crypto_free_cipher (tfm_aes );
3221+ kzfree (smp );
3222+ return ERR_CAST (tfm_ecdh );
3223+ }
3224+
31973225 smp -> tfm_aes = tfm_aes ;
31983226 smp -> tfm_cmac = tfm_cmac ;
3227+ smp -> tfm_ecdh = tfm_ecdh ;
31993228 smp -> min_key_size = SMP_MIN_ENC_KEY_SIZE ;
32003229 smp -> max_key_size = SMP_MAX_ENC_KEY_SIZE ;
32013230
@@ -3205,6 +3234,7 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
32053234 if (smp ) {
32063235 crypto_free_cipher (smp -> tfm_aes );
32073236 crypto_free_shash (smp -> tfm_cmac );
3237+ crypto_free_kpp (smp -> tfm_ecdh );
32083238 kzfree (smp );
32093239 }
32103240 return ERR_PTR (- ENOMEM );
@@ -3252,6 +3282,7 @@ static void smp_del_chan(struct l2cap_chan *chan)
32523282 chan -> data = NULL ;
32533283 crypto_free_cipher (smp -> tfm_aes );
32543284 crypto_free_shash (smp -> tfm_cmac );
3285+ crypto_free_kpp (smp -> tfm_ecdh );
32553286 kzfree (smp );
32563287 }
32573288
@@ -3498,13 +3529,13 @@ static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
34983529 out [i ] = __swab64 (in [ndigits - 1 - i ]);
34993530}
35003531
3501- static int __init test_debug_key (void )
3532+ static int __init test_debug_key (struct crypto_kpp * tfm_ecdh )
35023533{
35033534 u8 pk [64 ], sk [32 ];
35043535
35053536 swap_digits ((u64 * )debug_sk , (u64 * )sk , 4 );
35063537
3507- if (!generate_ecdh_keys (pk , sk ))
3538+ if (!generate_ecdh_keys (tfm_ecdh , pk , sk ))
35083539 return - EINVAL ;
35093540
35103541 if (crypto_memneq (sk , debug_sk , 32 ))
@@ -3763,15 +3794,16 @@ static const struct file_operations test_smp_fops = {
37633794};
37643795
37653796static int __init run_selftests (struct crypto_cipher * tfm_aes ,
3766- struct crypto_shash * tfm_cmac )
3797+ struct crypto_shash * tfm_cmac ,
3798+ struct crypto_kpp * tfm_ecdh )
37673799{
37683800 ktime_t calltime , delta , rettime ;
37693801 unsigned long long duration ;
37703802 int err ;
37713803
37723804 calltime = ktime_get ();
37733805
3774- err = test_debug_key ();
3806+ err = test_debug_key (tfm_ecdh );
37753807 if (err ) {
37763808 BT_ERR ("debug_key test failed" );
37773809 goto done ;
@@ -3848,6 +3880,7 @@ int __init bt_selftest_smp(void)
38483880{
38493881 struct crypto_cipher * tfm_aes ;
38503882 struct crypto_shash * tfm_cmac ;
3883+ struct crypto_kpp * tfm_ecdh ;
38513884 int err ;
38523885
38533886 tfm_aes = crypto_alloc_cipher ("aes" , 0 , CRYPTO_ALG_ASYNC );
@@ -3863,10 +3896,19 @@ int __init bt_selftest_smp(void)
38633896 return PTR_ERR (tfm_cmac );
38643897 }
38653898
3866- err = run_selftests (tfm_aes , tfm_cmac );
3899+ tfm_ecdh = crypto_alloc_kpp ("ecdh" , CRYPTO_ALG_INTERNAL , 0 );
3900+ if (IS_ERR (tfm_ecdh )) {
3901+ BT_ERR ("Unable to create ECDH crypto context" );
3902+ crypto_free_shash (tfm_cmac );
3903+ crypto_free_cipher (tfm_aes );
3904+ return PTR_ERR (tfm_ecdh );
3905+ }
3906+
3907+ err = run_selftests (tfm_aes , tfm_cmac , tfm_ecdh );
38673908
38683909 crypto_free_shash (tfm_cmac );
38693910 crypto_free_cipher (tfm_aes );
3911+ crypto_free_kpp (tfm_ecdh );
38703912
38713913 return err ;
38723914}
0 commit comments