Skip to content

Commit 6637e11

Browse files
mngyadamherbertx
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 ba51738 commit 6637e11

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
@@ -205,6 +205,32 @@ static int rsa_check_key_length(unsigned int len)
205205
return -EINVAL;
206206
}
207207

208+
static int rsa_check_exponent_fips(MPI e)
209+
{
210+
MPI e_max = NULL;
211+
212+
/* check if odd */
213+
if (!mpi_test_bit(e, 0)) {
214+
return -EINVAL;
215+
}
216+
217+
/* check if 2^16 < e < 2^256. */
218+
if (mpi_cmp_ui(e, 65536) <= 0) {
219+
return -EINVAL;
220+
}
221+
222+
e_max = mpi_alloc(0);
223+
mpi_set_bit(e_max, 256);
224+
225+
if (mpi_cmp(e, e_max) >= 0) {
226+
mpi_free(e_max);
227+
return -EINVAL;
228+
}
229+
230+
mpi_free(e_max);
231+
return 0;
232+
}
233+
208234
static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
209235
unsigned int keylen)
210236
{
@@ -232,6 +258,11 @@ static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
232258
return -EINVAL;
233259
}
234260

261+
if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
262+
rsa_free_mpi_key(mpi_key);
263+
return -EINVAL;
264+
}
265+
235266
return 0;
236267

237268
err:
@@ -290,6 +321,11 @@ static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
290321
return -EINVAL;
291322
}
292323

324+
if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) {
325+
rsa_free_mpi_key(mpi_key);
326+
return -EINVAL;
327+
}
328+
293329
return 0;
294330

295331
err:

0 commit comments

Comments
 (0)