Skip to content

Commit 47eb2ac

Browse files
ambarusholtmann
authored andcommitted
Bluetooth: move ecdh allocation outside of ecdh_helper
Before this change, a new crypto tfm was allocated, each time, for both key generation and shared secret computation. Allocate a single tfm for both cases. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent 24a3a32 commit 47eb2ac

4 files changed

Lines changed: 87 additions & 50 deletions

File tree

‎net/bluetooth/ecdh_helper.c‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "ecdh_helper.h"
2424

2525
#include <linux/scatterlist.h>
26-
#include <crypto/kpp.h>
2726
#include <crypto/ecdh.h>
2827

2928
struct ecdh_completion {
@@ -50,10 +49,9 @@ static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
5049
out[i] = __swab64(in[ndigits - 1 - i]);
5150
}
5251

53-
bool compute_ecdh_secret(const u8 public_key[64], const u8 private_key[32],
54-
u8 secret[32])
52+
bool compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
53+
const u8 private_key[32], u8 secret[32])
5554
{
56-
struct crypto_kpp *tfm;
5755
struct kpp_request *req;
5856
struct ecdh p;
5957
struct ecdh_completion result;
@@ -66,16 +64,9 @@ bool compute_ecdh_secret(const u8 public_key[64], const u8 private_key[32],
6664
if (!tmp)
6765
return false;
6866

69-
tfm = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
70-
if (IS_ERR(tfm)) {
71-
pr_err("alg: kpp: Failed to load tfm for kpp: %ld\n",
72-
PTR_ERR(tfm));
73-
goto free_tmp;
74-
}
75-
7667
req = kpp_request_alloc(tfm, GFP_KERNEL);
7768
if (!req)
78-
goto free_kpp;
69+
goto free_tmp;
7970

8071
init_completion(&result.completion);
8172

@@ -126,16 +117,14 @@ bool compute_ecdh_secret(const u8 public_key[64], const u8 private_key[32],
126117
kzfree(buf);
127118
free_req:
128119
kpp_request_free(req);
129-
free_kpp:
130-
crypto_free_kpp(tfm);
131120
free_tmp:
132121
kfree(tmp);
133122
return (err == 0);
134123
}
135124

136-
bool generate_ecdh_keys(u8 public_key[64], u8 private_key[32])
125+
bool generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64],
126+
u8 private_key[32])
137127
{
138-
struct crypto_kpp *tfm;
139128
struct kpp_request *req;
140129
struct ecdh p;
141130
struct ecdh_completion result;
@@ -150,16 +139,9 @@ bool generate_ecdh_keys(u8 public_key[64], u8 private_key[32])
150139
if (!tmp)
151140
return false;
152141

153-
tfm = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
154-
if (IS_ERR(tfm)) {
155-
pr_err("alg: kpp: Failed to load tfm for kpp: %ld\n",
156-
PTR_ERR(tfm));
157-
goto free_tmp;
158-
}
159-
160142
req = kpp_request_alloc(tfm, GFP_KERNEL);
161143
if (!req)
162-
goto free_kpp;
144+
goto free_tmp;
163145

164146
init_completion(&result.completion);
165147

@@ -218,8 +200,6 @@ bool generate_ecdh_keys(u8 public_key[64], u8 private_key[32])
218200
kzfree(buf);
219201
free_req:
220202
kpp_request_free(req);
221-
free_kpp:
222-
crypto_free_kpp(tfm);
223203
free_tmp:
224204
kfree(tmp);
225205
return (err == 0);

‎net/bluetooth/ecdh_helper.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
* COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
2121
* SOFTWARE IS DISCLAIMED.
2222
*/
23+
#include <crypto/kpp.h>
2324
#include <linux/types.h>
2425

25-
bool compute_ecdh_secret(const u8 pub_a[64], const u8 priv_b[32],
26-
u8 secret[32]);
27-
bool generate_ecdh_keys(u8 public_key[64], u8 private_key[32]);
26+
bool compute_ecdh_secret(struct crypto_kpp *tfm, const u8 pub_a[64],
27+
const u8 priv_b[32], u8 secret[32]);
28+
bool generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64],
29+
u8 private_key[32]);

‎net/bluetooth/selftest.c‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ static const u8 dhkey_3[32] __initconst = {
138138
0x7c, 0x1c, 0xf9, 0x49, 0xe6, 0xd7, 0xaa, 0x70,
139139
};
140140

141-
static int __init test_ecdh_sample(const u8 priv_a[32], const u8 priv_b[32],
142-
const u8 pub_a[64], const u8 pub_b[64],
143-
const u8 dhkey[32])
141+
static int __init test_ecdh_sample(struct crypto_kpp *tfm, const u8 priv_a[32],
142+
const u8 priv_b[32], const u8 pub_a[64],
143+
const u8 pub_b[64], const u8 dhkey[32])
144144
{
145145
u8 *tmp, *dhkey_a, *dhkey_b;
146146
int ret = 0;
@@ -152,8 +152,8 @@ static int __init test_ecdh_sample(const u8 priv_a[32], const u8 priv_b[32],
152152
dhkey_a = &tmp[0];
153153
dhkey_b = &tmp[32];
154154

155-
compute_ecdh_secret(pub_b, priv_a, dhkey_a);
156-
compute_ecdh_secret(pub_a, priv_b, dhkey_b);
155+
compute_ecdh_secret(tfm, pub_b, priv_a, dhkey_a);
156+
compute_ecdh_secret(tfm, pub_a, priv_b, dhkey_b);
157157

158158
if (memcmp(dhkey_a, dhkey, 32)) {
159159
ret = -EINVAL;
@@ -185,30 +185,43 @@ static const struct file_operations test_ecdh_fops = {
185185

186186
static int __init test_ecdh(void)
187187
{
188+
struct crypto_kpp *tfm;
188189
ktime_t calltime, delta, rettime;
189190
unsigned long long duration;
190191
int err;
191192

192193
calltime = ktime_get();
193194

194-
err = test_ecdh_sample(priv_a_1, priv_b_1, pub_a_1, pub_b_1, dhkey_1);
195+
tfm = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
196+
if (IS_ERR(tfm)) {
197+
BT_ERR("Unable to create ECDH crypto context");
198+
err = PTR_ERR(tfm);
199+
goto done;
200+
}
201+
202+
err = test_ecdh_sample(tfm, priv_a_1, priv_b_1, pub_a_1, pub_b_1,
203+
dhkey_1);
195204
if (err) {
196205
BT_ERR("ECDH sample 1 failed");
197206
goto done;
198207
}
199208

200-
err = test_ecdh_sample(priv_a_2, priv_b_2, pub_a_2, pub_b_2, dhkey_2);
209+
err = test_ecdh_sample(tfm, priv_a_2, priv_b_2, pub_a_2, pub_b_2,
210+
dhkey_2);
201211
if (err) {
202212
BT_ERR("ECDH sample 2 failed");
203213
goto done;
204214
}
205215

206-
err = test_ecdh_sample(priv_a_3, priv_a_3, pub_a_3, pub_a_3, dhkey_3);
216+
err = test_ecdh_sample(tfm, priv_a_3, priv_a_3, pub_a_3, pub_a_3,
217+
dhkey_3);
207218
if (err) {
208219
BT_ERR("ECDH sample 3 failed");
209220
goto done;
210221
}
211222

223+
crypto_free_kpp(tfm);
224+
212225
rettime = ktime_get();
213226
delta = ktime_sub(rettime, calltime);
214227
duration = (unsigned long long) ktime_to_ns(delta) >> 10;

‎net/bluetooth/smp.c‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
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

9799
struct 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

14181434
static 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

37653796
static 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

Comments
 (0)