Skip to content

Commit 6755fd2

Browse files
ambarusherbertx
authored andcommitted
crypto: ecdh - add privkey generation support
Add support for generating ecc private keys. Generation of ecc private keys is helpful in a user-space to kernel ecdh offload because the keys are not revealed to user-space. Private key generation is also helpful to implement forward secrecy. If the user provides a NULL ecc private key, the kernel will generate it and further use it for ecdh. Move ecdh's object files below drbg's. drbg must be present in the kernel at the time of calling. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Reviewed-by: Stephan Müller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent f266387 commit 6755fd2

5 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎crypto/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ config CRYPTO_DH
130130
config CRYPTO_ECDH
131131
tristate "ECDH algorithm"
132132
select CRYTPO_KPP
133+
select CRYPTO_RNG_DEFAULT
133134
help
134135
Generic implementation of the ECDH algorithm
135136

‎crypto/Makefile‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ obj-$(CONFIG_CRYPTO_KPP2) += kpp.o
3333
dh_generic-y := dh.o
3434
dh_generic-y += dh_helper.o
3535
obj-$(CONFIG_CRYPTO_DH) += dh_generic.o
36-
ecdh_generic-y := ecc.o
37-
ecdh_generic-y += ecdh.o
38-
ecdh_generic-y += ecdh_helper.o
39-
obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
4036

4137
$(obj)/rsapubkey-asn1.o: $(obj)/rsapubkey-asn1.c $(obj)/rsapubkey-asn1.h
4238
$(obj)/rsaprivkey-asn1.o: $(obj)/rsaprivkey-asn1.c $(obj)/rsaprivkey-asn1.h
@@ -138,6 +134,11 @@ obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
138134
obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
139135
obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
140136

137+
ecdh_generic-y := ecc.o
138+
ecdh_generic-y += ecdh.o
139+
ecdh_generic-y += ecdh_helper.o
140+
obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
141+
141142
#
142143
# generic algorithms and the async_tx api
143144
#

‎crypto/ecc.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/swab.h>
3030
#include <linux/fips.h>
3131
#include <crypto/ecdh.h>
32+
#include <crypto/rng.h>
3233

3334
#include "ecc.h"
3435
#include "ecc_curve_defs.h"
@@ -927,6 +928,61 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
927928
return 0;
928929
}
929930

931+
/*
932+
* ECC private keys are generated using the method of extra random bits,
933+
* equivalent to that described in FIPS 186-4, Appendix B.4.1.
934+
*
935+
* d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
936+
* than requested
937+
* 0 <= c mod(n-1) <= n-2 and implies that
938+
* 1 <= d <= n-1
939+
*
940+
* This method generates a private key uniformly distributed in the range
941+
* [1, n-1].
942+
*/
943+
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
944+
{
945+
const struct ecc_curve *curve = ecc_get_curve(curve_id);
946+
u64 priv[ndigits];
947+
unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
948+
unsigned int nbits = vli_num_bits(curve->n, ndigits);
949+
int err;
950+
951+
/* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
952+
if (nbits < 160)
953+
return -EINVAL;
954+
955+
/*
956+
* FIPS 186-4 recommends that the private key should be obtained from a
957+
* RBG with a security strength equal to or greater than the security
958+
* strength associated with N.
959+
*
960+
* The maximum security strength identified by NIST SP800-57pt1r4 for
961+
* ECC is 256 (N >= 512).
962+
*
963+
* This condition is met by the default RNG because it selects a favored
964+
* DRBG with a security strength of 256.
965+
*/
966+
if (crypto_get_default_rng())
967+
err = -EFAULT;
968+
969+
err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
970+
crypto_put_default_rng();
971+
if (err)
972+
return err;
973+
974+
if (vli_is_zero(priv, ndigits))
975+
return -EINVAL;
976+
977+
/* Make sure the private key is in the range [1, n-1]. */
978+
if (vli_cmp(curve->n, priv, ndigits) != 1)
979+
return -EINVAL;
980+
981+
ecc_swap_digits(priv, privkey, ndigits);
982+
983+
return 0;
984+
}
985+
930986
int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
931987
const u64 *private_key, u64 *public_key)
932988
{

‎crypto/ecc.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
4444
const u64 *private_key, unsigned int private_key_len);
4545

46+
/**
47+
* ecc_gen_privkey() - Generates an ECC private key.
48+
* The private key is a random integer in the range 0 < random < n, where n is a
49+
* prime that is the order of the cyclic subgroup generated by the distinguished
50+
* point G.
51+
* @curve_id: id representing the curve to use
52+
* @ndigits: curve number of digits
53+
* @private_key: buffer for storing the generated private key
54+
*
55+
* Returns 0 if the private key was generated successfully, a negative value
56+
* if an error occurred.
57+
*/
58+
int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
59+
4660
/**
4761
* ecc_make_pub_key() - Compute an ECC public key
4862
*

‎crypto/ecdh.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
5555
ctx->curve_id = params.curve_id;
5656
ctx->ndigits = ndigits;
5757

58+
if (!params.key || !params.key_size)
59+
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
60+
ctx->private_key);
61+
5862
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
5963
(const u64 *)params.key, params.key_size) < 0)
6064
return -EINVAL;

0 commit comments

Comments
 (0)