Skip to content

Commit 6bd58da

Browse files
mngyadamPlaidCat
authored andcommitted
crypto: rsa - allow only odd e and restrict value in FIPS mode
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>
1 parent 0ce2cad commit 6bd58da

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

crypto/rsa.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ static int rsa_check_key_length(unsigned int len)
157157
return -EINVAL;
158158
}
159159

160+
static int rsa_check_exponent_fips(MPI e)
161+
{
162+
MPI e_max = NULL;
163+
164+
/* check if odd */
165+
if (!mpi_test_bit(e, 0)) {
166+
return -EINVAL;
167+
}
168+
169+
/* check if 2^16 < e < 2^256. */
170+
if (mpi_cmp_ui(e, 65536) <= 0) {
171+
return -EINVAL;
172+
}
173+
174+
e_max = mpi_alloc(0);
175+
mpi_set_bit(e_max, 256);
176+
177+
if (mpi_cmp(e, e_max) >= 0) {
178+
mpi_free(e_max);
179+
return -EINVAL;
180+
}
181+
182+
mpi_free(e_max);
183+
return 0;
184+
}
185+
160186
static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
161187
unsigned int keylen)
162188
{
@@ -184,6 +210,11 @@ static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
184210
return -EINVAL;
185211
}
186212

213+
if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
214+
rsa_free_mpi_key(mpi_key);
215+
return -EINVAL;
216+
}
217+
187218
return 0;
188219

189220
err:
@@ -222,6 +253,11 @@ static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
222253
return -EINVAL;
223254
}
224255

256+
if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
257+
rsa_free_mpi_key(mpi_key);
258+
return -EINVAL;
259+
}
260+
225261
return 0;
226262

227263
err:

0 commit comments

Comments
 (0)