Merge tag 'block-6.5-2023-07-03' of git://git.kernel.dk/linux
[linux-2.6-microblaze.git] / crypto / rsa.c
index c50f2d2..c79613c 100644 (file)
@@ -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: