linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode
@ 2023-06-13 16:17 Mahmoud Adam
  2023-06-14  9:50 ` Herbert Xu
  2023-06-23  8:22 ` Herbert Xu
  0 siblings, 2 replies; 4+ messages in thread
From: Mahmoud Adam @ 2023-06-13 16:17 UTC (permalink / raw)
  To: herbert; +Cc: davem, linux-crypto, linux-kernel, Mahmoud Adam

check if rsa public exponent is odd and check its value is between
2^16 < e < 2^256.

FIPS 186-5 DSS (page 35)[1] specify that:
1. The public exponent e shall be selected with the following constraints:
  (a) The public verification exponent e shall be selected prior to
  generating the primes, p and q, and the private signature exponent
  d.
  (b) The exponent e shall be an odd positive integer such that:
   2^16 < e < 2^256.

[1] https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf

Signed-off-by: Mahmoud Adam <mngyadam@amazon.com>
---
 crypto/rsa.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/crypto/rsa.c b/crypto/rsa.c
index c50f2d2a4d06..c79613cdce6e 100644
--- a/crypto/rsa.c
+++ b/crypto/rsa.c
@@ -205,6 +205,32 @@ static int rsa_check_key_length(unsigned int len)
 	return -EINVAL;
 }
 
+static int rsa_check_exponent_fips(MPI e)
+{
+	MPI e_max = NULL;
+
+	/* check if odd */
+	if (!mpi_test_bit(e, 0)) {
+		return -EINVAL;
+	}
+
+	/* check if 2^16 < e < 2^256. */
+	if (mpi_cmp_ui(e, 65536) <= 0) {
+		return -EINVAL;
+	}
+
+	e_max = mpi_alloc(0);
+	mpi_set_bit(e_max, 256);
+
+	if (mpi_cmp(e, e_max) >= 0) {
+		mpi_free(e_max);
+		return -EINVAL;
+	}
+
+	mpi_free(e_max);
+	return 0;
+}
+
 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
 			   unsigned int keylen)
 {
@@ -232,6 +258,11 @@ static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
 		return -EINVAL;
 	}
 
+	if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
+		rsa_free_mpi_key(mpi_key);
+		return -EINVAL;
+	}
+
 	return 0;
 
 err:
@@ -290,6 +321,11 @@ static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
 		return -EINVAL;
 	}
 
+	if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
+		rsa_free_mpi_key(mpi_key);
+		return -EINVAL;
+	}
+
 	return 0;
 
 err:
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode
  2023-06-13 16:17 [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode Mahmoud Adam
@ 2023-06-14  9:50 ` Herbert Xu
  2023-06-14 11:59   ` Stephan Mueller
  2023-06-23  8:22 ` Herbert Xu
  1 sibling, 1 reply; 4+ messages in thread
From: Herbert Xu @ 2023-06-14  9:50 UTC (permalink / raw)
  To: Mahmoud Adam; +Cc: davem, linux-crypto, linux-kernel, Stephan Mueller

On Tue, Jun 13, 2023 at 04:17:31PM +0000, Mahmoud Adam wrote:
> check if rsa public exponent is odd and check its value is between
> 2^16 < e < 2^256.
> 
> FIPS 186-5 DSS (page 35)[1] specify that:
> 1. The public exponent e shall be selected with the following constraints:
>   (a) The public verification exponent e shall be selected prior to
>   generating the primes, p and q, and the private signature exponent
>   d.
>   (b) The exponent e shall be an odd positive integer such that:
>    2^16 < e < 2^256.
> 
> [1] https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf
> 
> Signed-off-by: Mahmoud Adam <mngyadam@amazon.com>
> ---
>  crypto/rsa.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)
> 
> diff --git a/crypto/rsa.c b/crypto/rsa.c
> index c50f2d2a4d06..c79613cdce6e 100644
> --- a/crypto/rsa.c
> +++ b/crypto/rsa.c
> @@ -205,6 +205,32 @@ static int rsa_check_key_length(unsigned int len)
>  	return -EINVAL;
>  }
>  
> +static int rsa_check_exponent_fips(MPI e)
> +{
> +	MPI e_max = NULL;
> +
> +	/* check if odd */
> +	if (!mpi_test_bit(e, 0)) {
> +		return -EINVAL;
> +	}
> +
> +	/* check if 2^16 < e < 2^256. */
> +	if (mpi_cmp_ui(e, 65536) <= 0) {
> +		return -EINVAL;
> +	}
> +
> +	e_max = mpi_alloc(0);
> +	mpi_set_bit(e_max, 256);
> +
> +	if (mpi_cmp(e, e_max) >= 0) {
> +		mpi_free(e_max);
> +		return -EINVAL;
> +	}
> +
> +	mpi_free(e_max);
> +	return 0;
> +}
> +
>  static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
>  			   unsigned int keylen)
>  {
> @@ -232,6 +258,11 @@ static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
>  		return -EINVAL;
>  	}
>  
> +	if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
> +		rsa_free_mpi_key(mpi_key);
> +		return -EINVAL;
> +	}
> +
>  	return 0;
>  
>  err:
> @@ -290,6 +321,11 @@ static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
>  		return -EINVAL;
>  	}
>  
> +	if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
> +		rsa_free_mpi_key(mpi_key);
> +		return -EINVAL;
> +	}
> +
>  	return 0;
>  
>  err:
> -- 
> 2.40.1

Cc Stephan Mueller
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode
  2023-06-14  9:50 ` Herbert Xu
@ 2023-06-14 11:59   ` Stephan Mueller
  0 siblings, 0 replies; 4+ messages in thread
From: Stephan Mueller @ 2023-06-14 11:59 UTC (permalink / raw)
  To: Mahmoud Adam, Herbert Xu; +Cc: davem, linux-crypto, linux-kernel

Am Mittwoch, 14. Juni 2023, 11:50:52 CEST schrieb Herbert Xu:

Hi Herbert,

> On Tue, Jun 13, 2023 at 04:17:31PM +0000, Mahmoud Adam wrote:
> > check if rsa public exponent is odd and check its value is between
> > 2^16 < e < 2^256.
> > 
> > FIPS 186-5 DSS (page 35)[1] specify that:
> > 
> > 1. The public exponent e shall be selected with the following constraints:
> >   (a) The public verification exponent e shall be selected prior to
> >   generating the primes, p and q, and the private signature exponent
> >   d.
> >   
> >   (b) The exponent e shall be an odd positive integer such that:
> >    2^16 < e < 2^256.
> > 
> > [1] https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf
> > 
> > Signed-off-by: Mahmoud Adam <mngyadam@amazon.com>

Reviewed-by: Stephan Mueller <smueller@chronox.de>

Ciao
Stephan



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode
  2023-06-13 16:17 [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode Mahmoud Adam
  2023-06-14  9:50 ` Herbert Xu
@ 2023-06-23  8:22 ` Herbert Xu
  1 sibling, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2023-06-23  8:22 UTC (permalink / raw)
  To: Mahmoud Adam; +Cc: davem, linux-crypto, linux-kernel

On Tue, Jun 13, 2023 at 04:17:31PM +0000, Mahmoud Adam wrote:
> check if rsa public exponent is odd and check its value is between
> 2^16 < e < 2^256.
> 
> FIPS 186-5 DSS (page 35)[1] specify that:
> 1. The public exponent e shall be selected with the following constraints:
>   (a) The public verification exponent e shall be selected prior to
>   generating the primes, p and q, and the private signature exponent
>   d.
>   (b) The exponent e shall be an odd positive integer such that:
>    2^16 < e < 2^256.
> 
> [1] https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf
> 
> Signed-off-by: Mahmoud Adam <mngyadam@amazon.com>
> ---
>  crypto/rsa.c | 36 ++++++++++++++++++++++++++++++++++++
>  1 file changed, 36 insertions(+)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-23  8:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-13 16:17 [PATCH] crypto: rsa - allow only odd e and restrict value in FIPS mode Mahmoud Adam
2023-06-14  9:50 ` Herbert Xu
2023-06-14 11:59   ` Stephan Mueller
2023-06-23  8:22 ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).