cleanning and fixes for ecc, (ec)dh
Subsystems (2)
cover cleanning and fixes for ecc, (ec)dh
The first two patches fix some typos and remove unused function arguments.
The next three remove some unnecessary casts for the ecc keys.
The next two fix the max_size() implemtations for (ec)dh. They were missing the check if the key was set.
The last one allows other algorithms to use the function that generates ecc public keys.
Tudor Ambarus (8):
crypto: kpp, (ec)dh - fix typos
crypto: ecc - remove unused function arguments
crypto: ecc - remove casts in ecdh_make_pub_key
crypto: ecc - remove casts in crypto_ecdh_shared_secret
crypto: ecc - remove casts in ecc_is_key_valid
crypto: dh - fix dh_max_size
crypto: ecdh - fix ecdh_max_size
crypto: ecc - don't be selfish on pubkeys
crypto/dh.c | 6 +++--- crypto/dh_helper.c | 4 ++-- crypto/ecc.c | 32 ++++++++++++++------------------ crypto/ecc.h | 25 ++++++++++--------------- crypto/ecdh.c | 24 +++++++++++------------- crypto/ecdh_helper.c | 4 ++-- include/crypto/dh.h | 4 ++-- include/crypto/ecdh.h | 4 ++-- include/crypto/kpp.h | 4 ++-- 9 files changed, 48 insertions(+), 59 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/8 crypto: kpp, (ec)dh - fix typos
While here, add missing argument description (ndigits).
--- crypto/dh.c | 4 ++-- crypto/dh_helper.c | 4 ++-- crypto/ecc.h | 8 +++++--- crypto/ecdh.c | 4 ++-- crypto/ecdh_helper.c | 4 ++-- include/crypto/dh.h | 4 ++-- include/crypto/ecdh.h | 4 ++-- include/crypto/kpp.h | 4 ++-- 8 files changed, 19 insertions(+), 17 deletions(-)
crypto/dh.c +2 -2
@@ -4,9 +4,9 @@* Authors: Salvatore Benedetto <[email protected]> * * This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence +* modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. +* 2 of the License, or (at your option) any later version. */ #include <linux/module.h>
crypto/dh_helper.c +2 -2
@@ -3,9 +3,9 @@* Authors: Salvatore Benedetto <[email protected]> * * This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence +* modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. +* 2 of the License, or (at your option) any later version. */ #include <linux/kernel.h> #include <linux/export.h>
crypto/ecc.h +5 -3
@@ -34,9 +34,9 @@* ecc_is_key_valid() - Validate a given ECDH private key * * @curve_id: id representing the curve to use -* @ndigits: curve number of digits +* @ndigits: curve's number of digits * @private_key: private key to be used for the given curve -* @private_key_len: private key len +* @private_key_len: private key length * * Returns 0 if the key is acceptable, a negative value otherwise */
@@ -47,9 +47,10 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,* ecdh_make_pub_key() - Compute an ECC public key * * @curve_id: id representing the curve to use +* @ndigits: curve's number of digits * @private_key: pregenerated private key for the given curve * @private_key_len: length of private_key -* @public_key: buffer for storing the public key generated +* @public_key: buffer for storing the generated public key * @public_key_len: length of the public_key buffer * * Returns 0 if the public key was generated successfully, a negative value
@@ -63,6 +64,7 @@ int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits,* crypto_ecdh_shared_secret() - Compute a shared secret * * @curve_id: id representing the curve to use +* @ndigits: curve's number of digits * @private_key: private key of part A * @private_key_len: length of private_key * @public_key: public key of counterpart B
crypto/ecdh.c +2 -2
@@ -4,9 +4,9 @@* Authors: Salvator Benedetto <[email protected]> * * This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence +* modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. +* 2 of the License, or (at your option) any later version. */ #include <linux/module.h>
crypto/ecdh_helper.c +2 -2
@@ -3,9 +3,9 @@* Authors: Salvatore Benedetto <[email protected]> * * This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence +* modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. +* 2 of the License, or (at your option) any later version. */ #include <linux/kernel.h> #include <linux/export.h>
include/crypto/dh.h +2 -2
@@ -73,9 +73,9 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params);/** * crypto_dh_decode_key() - decode a private key * @buf: Buffer holding a packet key that should be decoded -* @len: Lenth of the packet private key buffer +* @len: Length of the packet private key buffer * @params: Buffer allocated by the caller that is filled with the -* unpacket DH private key. +* unpacked DH private key. * * The unpacking obtains the private key by pointing @p to the correct location * in @buf. Thus, both pointers refer to the same memory.
include/crypto/ecdh.h +2 -2
@@ -74,9 +74,9 @@ int crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *p);/** * crypto_ecdh_decode_key() - decode a private key * @buf: Buffer holding a packet key that should be decoded -* @len: Lenth of the packet private key buffer +* @len: Length of the packet private key buffer * @p: Buffer allocated by the caller that is filled with the -* unpacket ECDH private key. +* unpacked ECDH private key. * * The unpacking obtains the private key by pointing @p to the correct location * in @buf. Thus, both pointers refer to the same memory.
include/crypto/kpp.h +2 -2
@@ -53,7 +53,7 @@ struct crypto_kpp {* * @set_secret: Function invokes the protocol specific function to * store the secret private key along with parameters. -* The implementation knows how to decode thie buffer +* The implementation knows how to decode the buffer * @generate_public_key: Function generate the public key to be sent to the * counterpart. In case of error, where output is not big * enough req->dst_len will be updated to the size
@@ -102,7 +102,7 @@ struct kpp_alg {* @mask: specifies the mask for the algorithm * * Allocate a handle for kpp algorithm. The returned struct crypto_kpp -* is requeried for any following API invocation +* is required for any following API invocation * * Return: allocated handle in case of success; IS_ERR() is true in case of * an error, PTR_ERR() returns the error code.
Signature · 1 line
2.7.4
PATCH 2/8 crypto: ecc - remove unused function arguments
In reply to Tudor Ambarus--- crypto/ecc.c | 8 +++----- crypto/ecc.h | 13 +++---------- crypto/ecdh.c | 11 +++++------ 3 files changed, 11 insertions(+), 21 deletions(-)
crypto/ecc.c +3 -5
@@ -928,8 +928,7 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, }int ecdh_make_pub_key (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,unsigned int private_key_len, -u8 *public_key,unsigned int public_key_len) +const u8 *private_key,u8 *public_key) {int ret =0 ;struct ecc_point *pk;
@@ -967,9 +966,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits, }int crypto_ecdh_shared_secret (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,unsigned int private_key_len, -const u8 *public_key,unsigned int public_key_len, -u8 *secret,unsigned int secret_len) +const u8 *private_key,const u8 *public_key, +u8 *secret) {int ret =0 ;struct ecc_point *product, *pk;
crypto/ecc.h +3 -10
@@ -49,16 +49,13 @@ int ecc_is_key_valid(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 -* @private_key_len: length of private_key * @public_key: buffer for storing the generated public key -* @public_key_len: length of the public_key buffer * * Returns 0 if the public key was generated successfully, a negative value * if an error occurred. */ int ecdh_make_pub_key (const unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,unsigned int private_key_len, -u8 *public_key,unsigned int public_key_len); +const u8 *private_key,u8 *public_key);/** * crypto_ecdh_shared_secret() - Compute a shared secret
@@ -66,11 +63,8 @@ int ecdh_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 -* @private_key_len: length of private_key * @public_key: public key of counterpart B -* @public_key_len: length of public_key * @secret: buffer for storing the calculated shared secret -* @secret_len: length of the secret buffer * * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret * before using it for symmetric encryption or HMAC.
@@ -79,7 +73,6 @@ int ecdh_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 u8 *private_key,unsigned int private_key_len, -const u8 *public_key,unsigned int public_key_len, -u8 *secret,unsigned int secret_len); +const u8 *private_key,const u8 *public_key, +u8 *secret);#endif
crypto/ecdh.c +5 -6
@@ -81,16 +81,15 @@ static int ecdh_compute_value(struct kpp_request *req)return -EINVAL ; ret =crypto_ecdh_shared_secret (ctx->curve_id, ctx->ndigits, - (const u8 *)ctx->private_key, nbytes, - (const u8 *)ctx->public_key,2 * nbytes, - (u8 *)ctx->shared_secret, nbytes); + (const u8 *)ctx->private_key, + (const u8 *)ctx->public_key, + (u8 *)ctx->shared_secret); buf = ctx->shared_secret; }else { ret =ecdh_make_pub_key (ctx->curve_id, ctx->ndigits, - (const u8 *)ctx->private_key, nbytes, - (u8 *)ctx->public_key, -sizeof (ctx->public_key)); + (const u8 *)ctx->private_key, + (u8 *)ctx->public_key); buf = ctx->public_key;/* Public part is a point thus it has both coordinates */ nbytes *=2 ;
Signature · 1 line
2.7.4
PATCH 3/8 crypto: ecc - remove casts in ecdh_make_pub_key
In reply to Tudor Ambarusecc software implementation works with chunks of u64 data. There were some unnecessary casts to u8 and then back to u64 for the ecc keys. This patch removes the unnecessary casts.
--- crypto/ecc.c | 10 ++++------ crypto/ecc.h | 2 +- crypto/ecdh.c | 3 +-- 3 files changed, 6 insertions(+), 9 deletions(-)
crypto/ecc.c +4 -6
@@ -928,12 +928,11 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, }int ecdh_make_pub_key (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,u8 *public_key) +const u64 *private_key,u64 *public_key) {int ret =0 ;struct ecc_point *pk;u64 priv[ndigits]; -unsigned int nbytes;const struct ecc_curve *curve =ecc_get_curve (curve_id);if (!private_key || !curve) {
@@ -941,7 +940,7 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits, goto out; } -ecc_swap_digits ((const u64 *)private_key, priv, ndigits); +ecc_swap_digits (private_key, priv, ndigits); pk =ecc_alloc_point (ndigits);if (!pk) {
@@ -955,9 +954,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits, goto err_free_point; } - nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; -ecc_swap_digits (pk->x, (u64 *)public_key, ndigits); -ecc_swap_digits (pk->y, (u64 *)&public_key[nbytes], ndigits); +ecc_swap_digits (pk->x, public_key, ndigits); +ecc_swap_digits (pk->y, &public_key[ndigits], ndigits); err_free_point:ecc_free_point (pk);
crypto/ecc.h +1 -1
@@ -55,7 +55,7 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,* if an error occurred. */ int ecdh_make_pub_key (const unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,u8 *public_key); +const u64 *private_key,u64 *public_key);/** * crypto_ecdh_shared_secret() - Compute a shared secret
crypto/ecdh.c +1 -2
@@ -88,8 +88,7 @@ static int ecdh_compute_value(struct kpp_request *req) buf = ctx->shared_secret; }else { ret =ecdh_make_pub_key (ctx->curve_id, ctx->ndigits, - (const u8 *)ctx->private_key, - (u8 *)ctx->public_key); + ctx->private_key, ctx->public_key); buf = ctx->public_key;/* Public part is a point thus it has both coordinates */ nbytes *=2 ;
Signature · 1 line
2.7.4
PATCH 4/8 crypto: ecc - remove casts in crypto_ecdh_shared_secret
In reply to Tudor Ambarusecc software implementation works with chunks of u64 data. There were some unnecessary casts to u8 and then back to u64 for the ecc keys. This patch removes the unnecessary casts.
--- crypto/ecc.c | 12 ++++++------ crypto/ecc.h | 4 ++-- crypto/ecdh.c | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-)
crypto/ecc.c +6 -6
@@ -964,8 +964,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits, }int crypto_ecdh_shared_secret (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,const u8 *public_key, -u8 *secret) +const u64 *private_key,const u64 *public_key, +u64 *secret) {int ret =0 ;struct ecc_point *product, *pk;
@@ -995,13 +995,13 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto err_alloc_product; } -ecc_swap_digits ((const u64 *)public_key, pk->x, ndigits); -ecc_swap_digits ((const u64 *)&public_key[nbytes], pk->y, ndigits); -ecc_swap_digits ((const u64 *)private_key, priv, ndigits); +ecc_swap_digits (public_key, pk->x, ndigits); +ecc_swap_digits (&public_key[ndigits], pk->y, ndigits); +ecc_swap_digits (private_key, priv, ndigits);ecc_point_mult (product, pk, priv, rand_z, curve->p, ndigits); -ecc_swap_digits (product->x, (u64 *)secret, ndigits); +ecc_swap_digits (product->x, secret, ndigits);if (ecc_point_is_zero (product)) ret = -EFAULT ;
crypto/ecc.h +2 -2
@@ -73,6 +73,6 @@ int ecdh_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 u8 *private_key,const u8 *public_key, -u8 *secret); +const u64 *private_key,const u64 *public_key, +u64 *secret);#endif
crypto/ecdh.c +3 -3
@@ -81,9 +81,9 @@ static int ecdh_compute_value(struct kpp_request *req)return -EINVAL ; ret =crypto_ecdh_shared_secret (ctx->curve_id, ctx->ndigits, - (const u8 *)ctx->private_key, - (const u8 *)ctx->public_key, - (u8 *)ctx->shared_secret); + ctx->private_key, + ctx->public_key, + ctx->shared_secret); buf = ctx->shared_secret; }else {
Signature · 1 line
2.7.4
PATCH 5/8 crypto: ecc - remove casts in ecc_is_key_valid
In reply to Tudor Ambarusecc software implementation works with chunks of u64 data. There were some unnecessary casts to u8 and then back to u64 for the ecc keys. This patch removes the unnecessary casts.
--- crypto/ecc.c | 6 +++--- crypto/ecc.h | 2 +- crypto/ecdh.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
crypto/ecc.c +3 -3
@@ -904,7 +904,7 @@ static inline void ecc_swap_digits(const u64 *in, u64 *out, }int ecc_is_key_valid (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,unsigned int private_key_len) +const u64 *private_key,unsigned int private_key_len) {int nbytes;const struct ecc_curve *curve =ecc_get_curve (curve_id);
@@ -917,11 +917,11 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,if (private_key_len != nbytes)return -EINVAL ; -if (vli_is_zero ((const u64 *)&private_key[0 ], ndigits)) +if (vli_is_zero (private_key, ndigits))return -EINVAL ;/* Make sure the private key is in the range [1, n-1]. */ -if (vli_cmp (curve->n, (const u64 *)&private_key[0 ], ndigits) !=1 ) +if (vli_cmp (curve->n, private_key, ndigits) !=1 )return -EINVAL ;return 0 ;
crypto/ecc.h +1 -1
@@ -41,7 +41,7 @@* Returns 0 if the key is acceptable, a negative value otherwise */ int ecc_is_key_valid (unsigned int curve_id,unsigned int ndigits, -const u8 *private_key,unsigned int private_key_len); +const u64 *private_key,unsigned int private_key_len);/** * ecdh_make_pub_key() - Compute an ECC public key
crypto/ecdh.c +1 -1
@@ -56,7 +56,7 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, ctx->ndigits = ndigits;if (ecc_is_key_valid (ctx->curve_id, ctx->ndigits, - (const u8 *)params.key, params.key_size) <0 ) + (const u64 *)params.key, params.key_size) <0 )return -EINVAL ;memcpy (ctx->private_key, params.key, params.key_size);
Signature · 1 line
2.7.4
PATCH 6/8 crypto: dh - fix dh_max_size
In reply to Tudor AmbarusThe function should return minimum size for output buffer or error code if key hasn't been set.
--- crypto/dh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/dh.c +1 -1
@@ -148,7 +148,7 @@ static int dh_max_size(struct crypto_kpp *tfm) {struct dh_ctx *ctx =dh_get_ctx (tfm); -return mpi_get_size (ctx->p); +return ctx->p ?mpi_get_size (ctx->p) : -EINVAL ; }static void dh_exit_tfm (struct crypto_kpp *tfm)
Signature · 1 line
2.7.4
PATCH 7/8 crypto: ecdh - fix ecdh_max_size
In reply to Tudor AmbarusThe function should return minimum size for output buffer or error code if key hasn't been set.
--- crypto/ecdh.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
crypto/ecdh.c +3 -3
@@ -107,10 +107,10 @@ static int ecdh_compute_value(struct kpp_request *req)static int ecdh_max_size (struct crypto_kpp *tfm) {struct ecdh_ctx *ctx =ecdh_get_ctx (tfm); -int nbytes = ctx->ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; -/* Public key is made of two coordinates */ -return 2 * nbytes; +/* Public key is made of two coordinates, add one to the left shift */ +return ctx->ndigits ? ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT +1 ) : + -EINVAL ; }static void no_exit_tfm (struct crypto_kpp *tfm)
Signature · 1 line
2.7.4
PATCH 8/8 crypto: ecc - don't be selfish on pubkeys
In reply to Tudor AmbarusRename ecdh_make_pub_key() to ecc_make_pub_key().
This function might as well be used by ecdsa.
--- crypto/ecc.c | 4 ++-- crypto/ecc.h | 4 ++-- crypto/ecdh.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
crypto/ecc.c +2 -2
@@ -927,8 +927,8 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,return 0 ; } -int ecdh_make_pub_key (unsigned int curve_id,unsigned int ndigits, -const u64 *private_key,u64 *public_key) +int ecc_make_pub_key (unsigned int curve_id,unsigned int ndigits, +const u64 *private_key,u64 *public_key) {int ret =0 ;struct ecc_point *pk;
crypto/ecc.h +2 -2
@@ -54,8 +54,8 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,* Returns 0 if the public key was generated successfully, a negative value * if an error occurred. */ -int ecdh_make_pub_key (const unsigned int curve_id,unsigned int ndigits, -const u64 *private_key,u64 *public_key); +int ecc_make_pub_key (const unsigned int curve_id,unsigned int ndigits, +const u64 *private_key,u64 *public_key);/** * crypto_ecdh_shared_secret() - Compute a shared secret
crypto/ecdh.c +2 -2
@@ -87,8 +87,8 @@ static int ecdh_compute_value(struct kpp_request *req) buf = ctx->shared_secret; }else { - ret =ecdh_make_pub_key (ctx->curve_id, ctx->ndigits, - ctx->private_key, ctx->public_key); + ret =ecc_make_pub_key (ctx->curve_id, ctx->ndigits, + ctx->private_key, ctx->public_key); buf = ctx->public_key;/* Public part is a point thus it has both coordinates */ nbytes *=2 ;
Signature · 1 line
2.7.4
Herbert Xu
maintainerRe: [PATCH 6/8] crypto: dh - fix dh_max_size
In reply to Tudor AmbarusQuoted 13 lines · Tudor Ambarus
The function should return minimum size for output buffer or error code if key hasn't been set.
Signed-off-by Tudor Ambarus <[email protected]>--- crypto/dh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)crypto/dh.c +0
-0
crypto/dh.c static int dh_max_size(struct crypto_kpp *tfm) · line 152
Quoted 6 lines
crypto/dh.c +1
-1@@ -148,7 +148,7 @@ static int dh_max_size(struct crypto_kpp *tfm) {struct dh_ctx *ctx =dh_get_ctx (tfm); -return mpi_get_size (ctx->p); +return ctx->p ?mpi_get_size (ctx->p) : -EINVAL ; }
How about ENOKEY?
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt