Skip to content

Commit 01474b7

Browse files
stefanbergerherbertx
authored andcommitted
crypto: ecdh - Initialize ctx->private_key in proper byte order
The private key in ctx->private_key is currently initialized in reverse byte order in ecdh_set_secret and whenever the key is needed in proper byte order the variable priv is introduced and the bytes from ctx->private_key are copied into priv while being byte-swapped (ecc_swap_digits). To get rid of the unnecessary byte swapping initialize ctx->private_key in proper byte order and clean up all functions that were previously using priv or were called with ctx->private_key: - ecc_gen_privkey: Directly initialize the passed ctx->private_key with random bytes filling all the digits of the private key. Get rid of the priv variable. This function only has ecdh_set_secret as a caller to create NIST P192/256/384 private keys. - crypto_ecdh_shared_secret: Called only from ecdh_compute_value with ctx->private_key. Get rid of the priv variable and work with the passed private_key directly. - ecc_make_pub_key: Called only from ecdh_compute_value with ctx->private_key. Get rid of the priv variable and work with the passed private_key directly. Cc: Salvatore Benedetto <[email protected]> Signed-off-by: Stefan Berger <[email protected]> Acked-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent bd955a4 commit 01474b7

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

crypto/ecc.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,10 +1497,10 @@ EXPORT_SYMBOL(ecc_is_key_valid);
14971497
* This method generates a private key uniformly distributed in the range
14981498
* [2, n-3].
14991499
*/
1500-
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
1500+
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
1501+
u64 *private_key)
15011502
{
15021503
const struct ecc_curve *curve = ecc_get_curve(curve_id);
1503-
u64 priv[ECC_MAX_DIGITS];
15041504
unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
15051505
unsigned int nbits = vli_num_bits(curve->n, ndigits);
15061506
int err;
@@ -1509,7 +1509,7 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
15091509
* Step 1 & 2: check that N is included in Table 1 of FIPS 186-5,
15101510
* section 6.1.1.
15111511
*/
1512-
if (nbits < 224 || ndigits > ARRAY_SIZE(priv))
1512+
if (nbits < 224)
15131513
return -EINVAL;
15141514

15151515
/*
@@ -1527,17 +1527,16 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
15271527
return -EFAULT;
15281528

15291529
/* Step 3: obtain N returned_bits from the DRBG. */
1530-
err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
1530+
err = crypto_rng_get_bytes(crypto_default_rng,
1531+
(u8 *)private_key, nbytes);
15311532
crypto_put_default_rng();
15321533
if (err)
15331534
return err;
15341535

15351536
/* Step 4: make sure the private key is in the valid range. */
1536-
if (__ecc_is_key_valid(curve, priv, ndigits))
1537+
if (__ecc_is_key_valid(curve, private_key, ndigits))
15371538
return -EINVAL;
15381539

1539-
ecc_swap_digits(priv, privkey, ndigits);
1540-
15411540
return 0;
15421541
}
15431542
EXPORT_SYMBOL(ecc_gen_privkey);
@@ -1547,23 +1546,20 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
15471546
{
15481547
int ret = 0;
15491548
struct ecc_point *pk;
1550-
u64 priv[ECC_MAX_DIGITS];
15511549
const struct ecc_curve *curve = ecc_get_curve(curve_id);
15521550

1553-
if (!private_key || ndigits > ARRAY_SIZE(priv)) {
1551+
if (!private_key) {
15541552
ret = -EINVAL;
15551553
goto out;
15561554
}
15571555

1558-
ecc_swap_digits(private_key, priv, ndigits);
1559-
15601556
pk = ecc_alloc_point(ndigits);
15611557
if (!pk) {
15621558
ret = -ENOMEM;
15631559
goto out;
15641560
}
15651561

1566-
ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
1562+
ecc_point_mult(pk, &curve->g, private_key, NULL, curve, ndigits);
15671563

15681564
/* SP800-56A rev 3 5.6.2.1.3 key check */
15691565
if (ecc_is_pubkey_valid_full(curve, pk)) {
@@ -1647,13 +1643,11 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
16471643
{
16481644
int ret = 0;
16491645
struct ecc_point *product, *pk;
1650-
u64 priv[ECC_MAX_DIGITS];
16511646
u64 rand_z[ECC_MAX_DIGITS];
16521647
unsigned int nbytes;
16531648
const struct ecc_curve *curve = ecc_get_curve(curve_id);
16541649

1655-
if (!private_key || !public_key ||
1656-
ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
1650+
if (!private_key || !public_key || ndigits > ARRAY_SIZE(rand_z)) {
16571651
ret = -EINVAL;
16581652
goto out;
16591653
}
@@ -1674,15 +1668,13 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
16741668
if (ret)
16751669
goto err_alloc_product;
16761670

1677-
ecc_swap_digits(private_key, priv, ndigits);
1678-
16791671
product = ecc_alloc_point(ndigits);
16801672
if (!product) {
16811673
ret = -ENOMEM;
16821674
goto err_alloc_product;
16831675
}
16841676

1685-
ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
1677+
ecc_point_mult(product, pk, private_key, rand_z, curve, ndigits);
16861678

16871679
if (ecc_point_is_zero(product)) {
16881680
ret = -EFAULT;
@@ -1692,7 +1684,6 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
16921684
ecc_swap_digits(product->x, secret, ndigits);
16931685

16941686
err_validity:
1695-
memzero_explicit(priv, sizeof(priv));
16961687
memzero_explicit(rand_z, sizeof(rand_z));
16971688
ecc_free_point(product);
16981689
err_alloc_product:

crypto/ecdh.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
2727
unsigned int len)
2828
{
2929
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
30-
u64 priv[ECC_MAX_DIGITS];
3130
struct ecdh params;
3231
int ret = 0;
3332

@@ -41,15 +40,14 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
4140
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
4241
ctx->private_key);
4342

44-
memcpy(ctx->private_key, params.key, params.key_size);
45-
ecc_swap_digits(ctx->private_key, priv, ctx->ndigits);
43+
ecc_digits_from_bytes(params.key, params.key_size,
44+
ctx->private_key, ctx->ndigits);
4645

4746
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
48-
priv, params.key_size) < 0) {
47+
ctx->private_key, params.key_size) < 0) {
4948
memzero_explicit(ctx->private_key, params.key_size);
5049
ret = -EINVAL;
5150
}
52-
memzero_explicit(priv, sizeof(priv));
5351

5452
return ret;
5553
}

include/crypto/internal/ecc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
103103
* Returns 0 if the private key was generated successfully, a negative value
104104
* if an error occurred.
105105
*/
106-
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
106+
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
107+
u64 *private_key);
107108

108109
/**
109110
* ecc_make_pub_key() - Compute an ECC public key

0 commit comments

Comments
 (0)