Skip to content

Commit

Permalink
crypto: rsa - allow only odd e and restrict value in FIPS mode
Browse files Browse the repository at this point in the history
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>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
Mahmoud Adam authored and Herbert Xu committed Jun 23, 2023
1 parent ba51738 commit 6637e11
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions crypto/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6637e11

Please sign in to comment.