Introduce ecc_digits_to_bytes and clean up ecdh.c
Subsystems (3)
Stefan Berger
reviewercover Introduce ecc_digits_to_bytes and clean up ecdh.c
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(-)
Signature · 1 line
2.43.0
Stefan Berger
reviewerRFC PATCH 1/3 crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array
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.
--- crypto/ecc.c | 22 ++++++++++++++++++++++ include/crypto/internal/ecc.h | 13 +++++++++++++ 2 files changed, 35 insertions(+)
crypto/ecc.c +22 -0
@@ -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);
include/crypto/internal/ecc.h +13 -0
@@ -70,6 +70,19 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitvoid 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 *
Signature · 1 line
2.43.0
Stefan Berger
reviewerRFC PATCH 2/3 crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes
In reply to Stefan BergerImplement 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.
--- crypto/ecc.c | 4 +--- crypto/ecdh.c | 15 ++++++++++----- include/crypto/internal/ecc.h | 9 +++++++++ 3 files changed, 20 insertions(+), 8 deletions(-)
crypto/ecc.c +1 -3
@@ -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 ;
crypto/ecdh.c +10 -5
@@ -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)
include/crypto/internal/ecc.h +9 -0
@@ -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 *
Signature · 1 line
2.43.0
Stefan Berger
reviewerRFC PATCH 3/3 crypto: ecdh - Use functions to copy digits from and to byte arrays
In reply to Stefan BergerIn 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.
--- crypto/ecc.c | 21 ++++++++++----------- crypto/ecdh.c | 9 +++++---- include/crypto/internal/ecc.h | 15 +++++++++------ 3 files changed, 24 insertions(+), 21 deletions(-)
crypto/ecc.c +10 -11
@@ -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));
crypto/ecdh.c +5 -4
@@ -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; }
include/crypto/internal/ecc.h +9 -6
@@ -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
Signature · 1 line
2.43.0