Skip to content

Commit 6763f5e

Browse files
yumeng0117herbertx
authored andcommitted
crypto: ecdh - move curve_id of ECDH from the key to algorithm name
1. crypto and crypto/atmel-ecc: Move curve id of ECDH from the key into the algorithm name instead in crypto and atmel-ecc, so ECDH algorithm name change form 'ecdh' to 'ecdh-nist-pxxx', and we cannot use 'curve_id' in 'struct ecdh'; 2. crypto/testmgr and net/bluetooth: Modify 'testmgr.c', 'testmgr.h' and 'net/bluetooth' to adapt the modification. Signed-off-by: Meng Yu <[email protected]> Reviewed-by: Zaibo Xu <[email protected]> Reported-by: kernel test robot <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 9b94ae7 commit 6763f5e

File tree

9 files changed

+89
-74
lines changed

9 files changed

+89
-74
lines changed

crypto/ecdh.c

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,16 @@ static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
2323
return kpp_tfm_ctx(tfm);
2424
}
2525

26-
static unsigned int ecdh_supported_curve(unsigned int curve_id)
27-
{
28-
switch (curve_id) {
29-
case ECC_CURVE_NIST_P192: return ECC_CURVE_NIST_P192_DIGITS;
30-
case ECC_CURVE_NIST_P256: return ECC_CURVE_NIST_P256_DIGITS;
31-
default: return 0;
32-
}
33-
}
34-
3526
static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
3627
unsigned int len)
3728
{
3829
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
3930
struct ecdh params;
40-
unsigned int ndigits;
4131

4232
if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
43-
params.key_size > sizeof(ctx->private_key))
33+
params.key_size > sizeof(u64) * ctx->ndigits)
4434
return -EINVAL;
4535

46-
ndigits = ecdh_supported_curve(params.curve_id);
47-
if (!ndigits)
48-
return -EINVAL;
49-
50-
ctx->curve_id = params.curve_id;
51-
ctx->ndigits = ndigits;
52-
5336
if (!params.key || !params.key_size)
5437
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
5538
ctx->private_key);
@@ -140,28 +123,73 @@ static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
140123
return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
141124
}
142125

143-
static struct kpp_alg ecdh = {
126+
static int ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm)
127+
{
128+
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
129+
130+
ctx->curve_id = ECC_CURVE_NIST_P192;
131+
ctx->ndigits = ECC_CURVE_NIST_P192_DIGITS;
132+
133+
return 0;
134+
}
135+
136+
static struct kpp_alg ecdh_nist_p192 = {
144137
.set_secret = ecdh_set_secret,
145138
.generate_public_key = ecdh_compute_value,
146139
.compute_shared_secret = ecdh_compute_value,
147140
.max_size = ecdh_max_size,
141+
.init = ecdh_nist_p192_init_tfm,
148142
.base = {
149-
.cra_name = "ecdh",
143+
.cra_name = "ecdh-nist-p192",
150144
.cra_driver_name = "ecdh-generic",
151145
.cra_priority = 100,
152146
.cra_module = THIS_MODULE,
153147
.cra_ctxsize = sizeof(struct ecdh_ctx),
154148
},
155149
};
156150

151+
static int ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm)
152+
{
153+
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
154+
155+
ctx->curve_id = ECC_CURVE_NIST_P256;
156+
ctx->ndigits = ECC_CURVE_NIST_P256_DIGITS;
157+
158+
return 0;
159+
}
160+
161+
static struct kpp_alg ecdh_nist_p256 = {
162+
.set_secret = ecdh_set_secret,
163+
.generate_public_key = ecdh_compute_value,
164+
.compute_shared_secret = ecdh_compute_value,
165+
.max_size = ecdh_max_size,
166+
.init = ecdh_nist_p256_init_tfm,
167+
.base = {
168+
.cra_name = "ecdh-nist-p256",
169+
.cra_driver_name = "ecdh-generic",
170+
.cra_priority = 100,
171+
.cra_module = THIS_MODULE,
172+
.cra_ctxsize = sizeof(struct ecdh_ctx),
173+
},
174+
};
175+
176+
static bool ecdh_nist_p192_registered;
177+
157178
static int ecdh_init(void)
158179
{
159-
return crypto_register_kpp(&ecdh);
180+
int ret;
181+
182+
ret = crypto_register_kpp(&ecdh_nist_p192);
183+
ecdh_nist_p192_registered = ret == 0;
184+
185+
return crypto_register_kpp(&ecdh_nist_p256);
160186
}
161187

162188
static void ecdh_exit(void)
163189
{
164-
crypto_unregister_kpp(&ecdh);
190+
if (ecdh_nist_p192_registered)
191+
crypto_unregister_kpp(&ecdh_nist_p192);
192+
crypto_unregister_kpp(&ecdh_nist_p256);
165193
}
166194

167195
subsys_initcall(ecdh_init);

crypto/ecdh_helper.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <crypto/ecdh.h>
1111
#include <crypto/kpp.h>
1212

13-
#define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 2 * sizeof(short))
13+
#define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + sizeof(short))
1414

1515
static inline u8 *ecdh_pack_data(void *dst, const void *src, size_t sz)
1616
{
@@ -46,7 +46,6 @@ int crypto_ecdh_encode_key(char *buf, unsigned int len,
4646
return -EINVAL;
4747

4848
ptr = ecdh_pack_data(ptr, &secret, sizeof(secret));
49-
ptr = ecdh_pack_data(ptr, &params->curve_id, sizeof(params->curve_id));
5049
ptr = ecdh_pack_data(ptr, &params->key_size, sizeof(params->key_size));
5150
ecdh_pack_data(ptr, params->key, params->key_size);
5251

@@ -70,7 +69,6 @@ int crypto_ecdh_decode_key(const char *buf, unsigned int len,
7069
if (unlikely(len < secret.len))
7170
return -EINVAL;
7271

73-
ptr = ecdh_unpack_data(&params->curve_id, ptr, sizeof(params->curve_id));
7472
ptr = ecdh_unpack_data(&params->key_size, ptr, sizeof(params->key_size));
7573
if (secret.len != crypto_ecdh_key_len(params))
7674
return -EINVAL;

crypto/testmgr.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,11 +4899,20 @@ static const struct alg_test_desc alg_test_descs[] = {
48994899
}
49004900
}, {
49014901
#endif
4902-
.alg = "ecdh",
4902+
#ifndef CONFIG_CRYPTO_FIPS
4903+
.alg = "ecdh-nist-p192",
49034904
.test = alg_test_kpp,
49044905
.fips_allowed = 1,
49054906
.suite = {
4906-
.kpp = __VECS(ecdh_tv_template)
4907+
.kpp = __VECS(ecdh_p192_tv_template)
4908+
}
4909+
}, {
4910+
#endif
4911+
.alg = "ecdh-nist-p256",
4912+
.test = alg_test_kpp,
4913+
.fips_allowed = 1,
4914+
.suite = {
4915+
.kpp = __VECS(ecdh_p256_tv_template)
49074916
}
49084917
}, {
49094918
.alg = "ecrdsa",

crypto/testmgr.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,19 +2261,17 @@ static const struct kpp_testvec curve25519_tv_template[] = {
22612261
}
22622262
};
22632263

2264-
static const struct kpp_testvec ecdh_tv_template[] = {
2265-
{
22662264
#ifndef CONFIG_CRYPTO_FIPS
2265+
static const struct kpp_testvec ecdh_p192_tv_template[] = {
2266+
{
22672267
.secret =
22682268
#ifdef __LITTLE_ENDIAN
22692269
"\x02\x00" /* type */
2270-
"\x20\x00" /* len */
2271-
"\x01\x00" /* curve_id */
2270+
"\x1e\x00" /* len */
22722271
"\x18\x00" /* key_size */
22732272
#else
22742273
"\x00\x02" /* type */
2275-
"\x00\x20" /* len */
2276-
"\x00\x01" /* curve_id */
2274+
"\x00\x1e" /* len */
22772275
"\x00\x18" /* key_size */
22782276
#endif
22792277
"\xb5\x05\xb1\x71\x1e\xbf\x8c\xda"
@@ -2301,18 +2299,20 @@ static const struct kpp_testvec ecdh_tv_template[] = {
23012299
.b_public_size = 48,
23022300
.expected_a_public_size = 48,
23032301
.expected_ss_size = 24
2304-
}, {
2302+
}
2303+
};
23052304
#endif
2305+
2306+
static const struct kpp_testvec ecdh_p256_tv_template[] = {
2307+
{
23062308
.secret =
23072309
#ifdef __LITTLE_ENDIAN
23082310
"\x02\x00" /* type */
2309-
"\x28\x00" /* len */
2310-
"\x02\x00" /* curve_id */
2311+
"\x26\x00" /* len */
23112312
"\x20\x00" /* key_size */
23122313
#else
23132314
"\x00\x02" /* type */
2314-
"\x00\x28" /* len */
2315-
"\x00\x02" /* curve_id */
2315+
"\x00\x26" /* len */
23162316
"\x00\x20" /* key_size */
23172317
#endif
23182318
"\x24\xd1\x21\xeb\xe5\xcf\x2d\x83"
@@ -2350,25 +2350,21 @@ static const struct kpp_testvec ecdh_tv_template[] = {
23502350
.secret =
23512351
#ifdef __LITTLE_ENDIAN
23522352
"\x02\x00" /* type */
2353-
"\x08\x00" /* len */
2354-
"\x02\x00" /* curve_id */
2353+
"\x06\x00" /* len */
23552354
"\x00\x00", /* key_size */
23562355
#else
23572356
"\x00\x02" /* type */
2358-
"\x00\x08" /* len */
2359-
"\x00\x02" /* curve_id */
2357+
"\x00\x06" /* len */
23602358
"\x00\x00", /* key_size */
23612359
#endif
23622360
.b_secret =
23632361
#ifdef __LITTLE_ENDIAN
23642362
"\x02\x00" /* type */
2365-
"\x28\x00" /* len */
2366-
"\x02\x00" /* curve_id */
2363+
"\x26\x00" /* len */
23672364
"\x20\x00" /* key_size */
23682365
#else
23692366
"\x00\x02" /* type */
2370-
"\x00\x28" /* len */
2371-
"\x00\x02" /* curve_id */
2367+
"\x00\x26" /* len */
23722368
"\x00\x20" /* key_size */
23732369
#endif
23742370
"\x24\xd1\x21\xeb\xe5\xcf\x2d\x83"

drivers/crypto/atmel-ecc.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ static struct atmel_ecc_driver_data driver_data;
3434
* of the user to not call set_secret() while
3535
* generate_public_key() or compute_shared_secret() are in flight.
3636
* @curve_id : elliptic curve id
37-
* @n_sz : size in bytes of the n prime
3837
* @do_fallback: true when the device doesn't support the curve or when the user
3938
* wants to use its own private key.
4039
*/
@@ -43,23 +42,21 @@ struct atmel_ecdh_ctx {
4342
struct crypto_kpp *fallback;
4443
const u8 *public_key;
4544
unsigned int curve_id;
46-
size_t n_sz;
4745
bool do_fallback;
4846
};
4947

5048
static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq,
5149
int status)
5250
{
5351
struct kpp_request *req = areq;
54-
struct atmel_ecdh_ctx *ctx = work_data->ctx;
5552
struct atmel_i2c_cmd *cmd = &work_data->cmd;
5653
size_t copied, n_sz;
5754

5855
if (status)
5956
goto free_work_data;
6057

6158
/* might want less than we've got */
62-
n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
59+
n_sz = min_t(size_t, ATMEL_ECC_NIST_P256_N_SIZE, req->dst_len);
6360

6461
/* copy the shared secret */
6562
copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
@@ -73,14 +70,6 @@ static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq,
7370
kpp_request_complete(req, status);
7471
}
7572

76-
static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id)
77-
{
78-
if (curve_id == ECC_CURVE_NIST_P256)
79-
return ATMEL_ECC_NIST_P256_N_SIZE;
80-
81-
return 0;
82-
}
83-
8473
/*
8574
* A random private key is generated and stored in the device. The device
8675
* returns the pair public key.
@@ -104,8 +93,7 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
10493
return -EINVAL;
10594
}
10695

107-
ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id);
108-
if (!ctx->n_sz || params.key_size) {
96+
if (params.key_size) {
10997
/* fallback to ecdh software implementation */
11098
ctx->do_fallback = true;
11199
return crypto_kpp_set_secret(ctx->fallback, buf, len);
@@ -125,7 +113,6 @@ static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
125113
goto free_cmd;
126114

127115
ctx->do_fallback = false;
128-
ctx->curve_id = params.curve_id;
129116

130117
atmel_i2c_init_genkey_cmd(cmd, DATA_SLOT_2);
131118

@@ -263,6 +250,7 @@ static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
263250
struct crypto_kpp *fallback;
264251
struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
265252

253+
ctx->curve_id = ECC_CURVE_NIST_P256;
266254
ctx->client = atmel_ecc_i2c_client_alloc();
267255
if (IS_ERR(ctx->client)) {
268256
pr_err("tfm - i2c_client binding failed\n");
@@ -306,7 +294,7 @@ static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
306294
return ATMEL_ECC_PUBKEY_SIZE;
307295
}
308296

309-
static struct kpp_alg atmel_ecdh = {
297+
static struct kpp_alg atmel_ecdh_nist_p256 = {
310298
.set_secret = atmel_ecdh_set_secret,
311299
.generate_public_key = atmel_ecdh_generate_public_key,
312300
.compute_shared_secret = atmel_ecdh_compute_shared_secret,
@@ -315,7 +303,7 @@ static struct kpp_alg atmel_ecdh = {
315303
.max_size = atmel_ecdh_max_size,
316304
.base = {
317305
.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
318-
.cra_name = "ecdh",
306+
.cra_name = "ecdh-nist-p256",
319307
.cra_driver_name = "atmel-ecdh",
320308
.cra_priority = ATMEL_ECC_PRIORITY,
321309
.cra_module = THIS_MODULE,
@@ -340,14 +328,14 @@ static int atmel_ecc_probe(struct i2c_client *client,
340328
&driver_data.i2c_client_list);
341329
spin_unlock(&driver_data.i2c_list_lock);
342330

343-
ret = crypto_register_kpp(&atmel_ecdh);
331+
ret = crypto_register_kpp(&atmel_ecdh_nist_p256);
344332
if (ret) {
345333
spin_lock(&driver_data.i2c_list_lock);
346334
list_del(&i2c_priv->i2c_client_list_node);
347335
spin_unlock(&driver_data.i2c_list_lock);
348336

349337
dev_err(&client->dev, "%s alg registration failed\n",
350-
atmel_ecdh.base.cra_driver_name);
338+
atmel_ecdh_nist_p256.base.cra_driver_name);
351339
} else {
352340
dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
353341
}
@@ -365,7 +353,7 @@ static int atmel_ecc_remove(struct i2c_client *client)
365353
return -EBUSY;
366354
}
367355

368-
crypto_unregister_kpp(&atmel_ecdh);
356+
crypto_unregister_kpp(&atmel_ecdh_nist_p256);
369357

370358
spin_lock(&driver_data.i2c_list_lock);
371359
list_del(&i2c_priv->i2c_client_list_node);

include/crypto/ecdh.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
/**
3030
* struct ecdh - define an ECDH private key
3131
*
32-
* @curve_id: ECC curve the key is based on.
3332
* @key: Private ECDH key
3433
* @key_size: Size of the private ECDH key
3534
*/
3635
struct ecdh {
37-
unsigned short curve_id;
3836
char *key;
3937
unsigned short key_size;
4038
};

net/bluetooth/ecdh_helper.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
126126
int err;
127127
struct ecdh p = {0};
128128

129-
p.curve_id = ECC_CURVE_NIST_P256;
130-
131129
if (private_key) {
132130
tmp = kmalloc(32, GFP_KERNEL);
133131
if (!tmp)

net/bluetooth/selftest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int __init test_ecdh(void)
205205

206206
calltime = ktime_get();
207207

208-
tfm = crypto_alloc_kpp("ecdh", 0, 0);
208+
tfm = crypto_alloc_kpp("ecdh-nist-p256", 0, 0);
209209
if (IS_ERR(tfm)) {
210210
BT_ERR("Unable to create ECDH crypto context");
211211
err = PTR_ERR(tfm);

0 commit comments

Comments
 (0)