* [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c
@ 2024-06-12 15:18 Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array Stefan Berger
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Stefan Berger @ 2024-06-12 15:18 UTC (permalink / raw)
To: keyrings, linux-crypto, herbert, davem
Cc: linux-kernel, saulo.alessandre, ardb, Stefan Berger
This series introduces ecc_digits_to_bytes to convert an array of digits
to bytes and uses this function and ecc_digits_from_bytes in ecdh-related
code to convert between byte arrays and digits. Using these functions is
generally better than using ecc_swap_digits. The latter cannot be used with
curves like NIST P521 since it does not properly handle curves that do not
use all bytes in the most significant digit.
It also introduces ecc_curve_get_nbytes to get the number of bytes (nbytes)
a curve needs for its coordinates. It derives this number from the nbits
parameter of a curve. Using this function is also generally better than
deriving nbytes from ndigits, which does not work correctly vor NIST P521.
None of the converted functions have been used with NIST P521 but only
with NIST P192/256/384 so far, so they work fine as they are.
Due to concerns related to constant time operations when signing with EC
private keys I am not planning to add NIST P521 support to ecdh, so the
changes in this series are primarly 'code improvements' (-> RFC).
Stefan
Stefan Berger (3):
crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte
array
crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's
nbytes
crypto: ecdh - Use functions to copy digits from and to byte arrays
crypto/ecc.c | 47 ++++++++++++++++++++++++-----------
crypto/ecdh.c | 24 +++++++++++-------
include/crypto/internal/ecc.h | 37 ++++++++++++++++++++++-----
3 files changed, 79 insertions(+), 29 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array
2024-06-12 15:18 [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c Stefan Berger
@ 2024-06-12 15:18 ` Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 2/3] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes Stefan Berger
2024-06-12 15:19 ` [RFC PATCH 3/3] crypto: ecdh - Use functions to copy digits from and to byte arrays Stefan Berger
2 siblings, 0 replies; 4+ messages in thread
From: Stefan Berger @ 2024-06-12 15:18 UTC (permalink / raw)
To: keyrings, linux-crypto, herbert, davem
Cc: linux-kernel, saulo.alessandre, ardb, Stefan Berger
Implement ecc_digits_to_bytes to convert an array of digits into an
nbytes-sized byte array. The first byte in the byte array holds the most
significant bits of the large integer.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
crypto/ecc.c | 22 ++++++++++++++++++++++
include/crypto/internal/ecc.h | 13 +++++++++++++
2 files changed, 35 insertions(+)
diff --git a/crypto/ecc.c b/crypto/ecc.c
index af698f8852fb..1cdb5df3aa5d 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -90,6 +90,28 @@ void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
}
EXPORT_SYMBOL(ecc_digits_from_bytes);
+void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits,
+ u8 *out, unsigned int nbytes)
+{
+ unsigned int o = nbytes & 7;
+ __be64 msd;
+ int i;
+
+ if (o) {
+ msd = cpu_to_be64(in[--ndigits]);
+ memcpy(out, (u8 *)&msd + sizeof(msd) - o, o);
+ out += o;
+ nbytes -= o;
+ }
+
+ for (i = ndigits - 1; i >= 0 && nbytes > 0; i--) {
+ put_unaligned_be64(in[i], out);
+ out += sizeof(u64);
+ nbytes -= sizeof(u64);
+ }
+}
+EXPORT_SYMBOL(ecc_digits_to_bytes);
+
static u64 *ecc_alloc_digits_space(unsigned int ndigits)
{
size_t len = ndigits * sizeof(u64);
diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index 0717a53ae732..b18297aaff08 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -70,6 +70,19 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit
void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
u64 *out, unsigned int ndigits);
+/**
+ * ecc_digits_to_bytes() - Copy digits into a byte array of size nbytes
+ * @in: Input digits array
+ * @ndigits: Number of digits in input digits array
+ * @out: Output byte array
+ * @nbytes: Number of bytes to copy into byte array
+ *
+ * The first byte in the byte array will have the most significant bits of the
+ * large integer.
+ */
+void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits,
+ u8 *out, unsigned int nbytes);
+
/**
* ecc_is_key_valid() - Validate a given ECDH private key
*
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [RFC PATCH 2/3] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes
2024-06-12 15:18 [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array Stefan Berger
@ 2024-06-12 15:18 ` Stefan Berger
2024-06-12 15:19 ` [RFC PATCH 3/3] crypto: ecdh - Use functions to copy digits from and to byte arrays Stefan Berger
2 siblings, 0 replies; 4+ messages in thread
From: Stefan Berger @ 2024-06-12 15:18 UTC (permalink / raw)
To: keyrings, linux-crypto, herbert, davem
Cc: linux-kernel, saulo.alessandre, ardb, Stefan Berger
Implement ecc_curve_get_nbytes to get a curve's number of bytes (nbytes)
derived from the nbits field of a curve. This function should be used
where nbytes is currently derived from ndigits since it gives a precise
number for all curves including those that do not use all bytes in the
most significant digit, such as NIST P521.
Neither of the modified functions have so far been used with the NIST P521
curve.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
crypto/ecc.c | 4 +---
crypto/ecdh.c | 15 ++++++++++-----
include/crypto/internal/ecc.h | 9 +++++++++
3 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/crypto/ecc.c b/crypto/ecc.c
index 1cdb5df3aa5d..7cc82c9eacc8 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1522,10 +1522,8 @@ static int __ecc_is_key_valid(const struct ecc_curve *curve,
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
const u64 *private_key, unsigned int private_key_len)
{
- int nbytes;
const struct ecc_curve *curve = ecc_get_curve(curve_id);
-
- nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
+ unsigned int nbytes = ecc_curve_get_nbytes(curve);
if (private_key_len != nbytes)
return -EINVAL;
diff --git a/crypto/ecdh.c b/crypto/ecdh.c
index 72cfd1590156..55d140772da0 100644
--- a/crypto/ecdh.c
+++ b/crypto/ecdh.c
@@ -27,11 +27,13 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
unsigned int len)
{
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
+ const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
+ unsigned int nbytes = ecc_curve_get_nbytes(curve);
struct ecdh params;
int ret = 0;
if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0 ||
- params.key_size > sizeof(u64) * ctx->ndigits)
+ params.key_size > nbytes)
return -EINVAL;
memset(ctx->private_key, 0, sizeof(ctx->private_key));
@@ -56,13 +58,14 @@ static int ecdh_compute_value(struct kpp_request *req)
{
struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
+ const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
+ unsigned int nbytes = ecc_curve_get_nbytes(curve);
u64 *public_key;
u64 *shared_secret = NULL;
void *buf;
- size_t copied, nbytes, public_key_sz;
+ size_t copied, public_key_sz;
int ret = -ENOMEM;
- nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
/* Public part is a point thus it has both coordinates */
public_key_sz = 2 * nbytes;
@@ -123,9 +126,11 @@ static int ecdh_compute_value(struct kpp_request *req)
static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
{
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
+ const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
+ unsigned int nbytes = ecc_curve_get_nbytes(curve);
- /* Public key is made of two coordinates, add one to the left shift */
- return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
+ /* Public key is made of two coordinates */
+ return nbytes * 2;
}
static int ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm)
diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index b18297aaff08..d6e51e45fb3d 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -83,6 +83,15 @@ void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits,
u8 *out, unsigned int nbytes);
+/*
+ * ecc_curve_get_nbytes() - Get the number of bytes the curve requires
+ * @curve: The curve
+ */
+static inline unsigned int ecc_curve_get_nbytes(const struct ecc_curve *curve)
+{
+ return DIV_ROUND_UP(curve->nbits, 8);
+}
+
/**
* ecc_is_key_valid() - Validate a given ECDH private key
*
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [RFC PATCH 3/3] crypto: ecdh - Use functions to copy digits from and to byte arrays
2024-06-12 15:18 [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 2/3] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes Stefan Berger
@ 2024-06-12 15:19 ` Stefan Berger
2 siblings, 0 replies; 4+ messages in thread
From: Stefan Berger @ 2024-06-12 15:19 UTC (permalink / raw)
To: keyrings, linux-crypto, herbert, davem
Cc: linux-kernel, saulo.alessandre, ardb, Stefan Berger
In ecdh_compute_value the public_key and shared_secret variables are not
arrays of digits (u64 *) but byte buffers that are either copied into from
the scatterlist req->src or copied out of to the scatterlist req->dst.
Therefore, convert these variables to 'u8 *' and modify ecc_make_pub_key
and crypto_ecdh_shared_secret to use ecc_digits_from_bytes to convert byte
arrays to digits and ecc_digits_to_bytes to convert digits to byte arrays.
This also prepares the code for usage with curves that do not use all
bytes in the most significant digit, such as NIST P521, since these two
functions can handle conversions between digits and byte arrays related to
such curves.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
crypto/ecc.c | 21 ++++++++++-----------
crypto/ecdh.c | 9 +++++----
include/crypto/internal/ecc.h | 15 +++++++++------
3 files changed, 24 insertions(+), 21 deletions(-)
diff --git a/crypto/ecc.c b/crypto/ecc.c
index 7cc82c9eacc8..3ad2d8ae41b9 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1584,7 +1584,8 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
EXPORT_SYMBOL(ecc_gen_privkey);
int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
- const u64 *private_key, u64 *public_key)
+ const u64 *private_key, u8 *public_key,
+ unsigned int nbytes)
{
int ret = 0;
struct ecc_point *pk;
@@ -1609,8 +1610,8 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
goto err_free_point;
}
- ecc_swap_digits(pk->x, public_key, ndigits);
- ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
+ ecc_digits_to_bytes(pk->x, ndigits, public_key, nbytes);
+ ecc_digits_to_bytes(pk->y, ndigits, &public_key[nbytes], nbytes);
err_free_point:
ecc_free_point(pk);
@@ -1680,13 +1681,12 @@ int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
- const u64 *private_key, const u64 *public_key,
- u64 *secret)
+ const u64 *private_key, const u8 *public_key,
+ unsigned int nbytes, u8 *secret)
{
int ret = 0;
struct ecc_point *product, *pk;
u64 rand_z[ECC_MAX_DIGITS];
- unsigned int nbytes;
const struct ecc_curve *curve = ecc_get_curve(curve_id);
if (!private_key || !public_key || ndigits > ARRAY_SIZE(rand_z)) {
@@ -1694,8 +1694,6 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
goto out;
}
- nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
-
get_random_bytes(rand_z, nbytes);
pk = ecc_alloc_point(ndigits);
@@ -1704,8 +1702,9 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
goto out;
}
- ecc_swap_digits(public_key, pk->x, ndigits);
- ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
+ ecc_digits_from_bytes(public_key, nbytes, pk->x, ndigits);
+ ecc_digits_from_bytes(&public_key[nbytes], nbytes, pk->y, ndigits);
+
ret = ecc_is_pubkey_valid_partial(curve, pk);
if (ret)
goto err_alloc_product;
@@ -1723,7 +1722,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
goto err_validity;
}
- ecc_swap_digits(product->x, secret, ndigits);
+ ecc_digits_to_bytes(product->x, ndigits, secret, nbytes);
err_validity:
memzero_explicit(rand_z, sizeof(rand_z));
diff --git a/crypto/ecdh.c b/crypto/ecdh.c
index 55d140772da0..dfb5fa1a50d2 100644
--- a/crypto/ecdh.c
+++ b/crypto/ecdh.c
@@ -60,8 +60,8 @@ static int ecdh_compute_value(struct kpp_request *req)
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
unsigned int nbytes = ecc_curve_get_nbytes(curve);
- u64 *public_key;
- u64 *shared_secret = NULL;
+ u8 *public_key;
+ u8 *shared_secret = NULL;
void *buf;
size_t copied, public_key_sz;
int ret = -ENOMEM;
@@ -94,12 +94,13 @@ static int ecdh_compute_value(struct kpp_request *req)
ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
ctx->private_key, public_key,
- shared_secret);
+ nbytes, shared_secret);
buf = shared_secret;
} else {
ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
- ctx->private_key, public_key);
+ ctx->private_key, public_key,
+ nbytes);
buf = public_key;
nbytes = public_key_sz;
}
diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index d6e51e45fb3d..19265394be48 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -126,13 +126,15 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
* @curve_id: id representing the curve to use
* @ndigits: curve's number of digits
* @private_key: pregenerated private key for the given curve
- * @public_key: buffer for storing the generated public key
+ * @public_key: 2 * nbytes buffer for storing the generated public key
+ * @nbytes: size of one coordinate of the public key
*
* Returns 0 if the public key was generated successfully, a negative value
* if an error occurred.
*/
int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
- const u64 *private_key, u64 *public_key);
+ const u64 *private_key, u8 *public_key,
+ unsigned int nbytes);
/**
* crypto_ecdh_shared_secret() - Compute a shared secret
@@ -140,8 +142,9 @@ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
* @curve_id: id representing the curve to use
* @ndigits: curve's number of digits
* @private_key: private key of part A
- * @public_key: public key of counterpart B
- * @secret: buffer for storing the calculated shared secret
+ * @public_key: 2 * nbytes buffer with public key of counterpart B
+ * @nbytes: size of one coordinate of the public key
+ * @secret: nbytes buffer for storing the calculated shared secret
*
* Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
* before using it for symmetric encryption or HMAC.
@@ -150,8 +153,8 @@ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
* if an error occurred.
*/
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
- const u64 *private_key, const u64 *public_key,
- u64 *secret);
+ const u64 *private_key, const u8 *public_key,
+ unsigned int nbytes, u8 *secret);
/**
* ecc_is_pubkey_valid_partial() - Partial public key validation
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-06-12 15:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-12 15:18 [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array Stefan Berger
2024-06-12 15:18 ` [RFC PATCH 2/3] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes Stefan Berger
2024-06-12 15:19 ` [RFC PATCH 3/3] crypto: ecdh - Use functions to copy digits from and to byte arrays Stefan Berger
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox