1 |
jpp |
1.1 |
From 52b347703ba2b98a0efee86c1a483c2f0f9f73d6 Mon Sep 17 00:00:00 2001 |
2 |
|
|
From: Clemens Lang <cllang@redhat.com> |
3 |
|
|
Date: Wed, 11 Jan 2023 12:52:59 +0100 |
4 |
|
|
Subject: [PATCH] rsa: Disallow SHAKE in OAEP and PSS in FIPS prov |
5 |
|
|
|
6 |
|
|
According to FIPS 140-3 IG, section C.C, the SHAKE digest algorithms |
7 |
|
|
must not be used in higher-level algorithms (such as RSA-OAEP and |
8 |
|
|
RSASSA-PSS): |
9 |
|
|
|
10 |
|
|
"To be used in an approved mode of operation, the SHA-3 hash functions |
11 |
|
|
may be implemented either as part of an approved higher-level algorithm, |
12 |
|
|
for example, a digital signature algorithm, or as the standalone |
13 |
|
|
functions. The SHAKE128 and SHAKE256 extendable-output functions may |
14 |
|
|
only be used as the standalone algorithms." |
15 |
|
|
|
16 |
|
|
Add a check to prevent their use as message digest in PSS signatures and |
17 |
|
|
as MGF1 hash function in both OAEP and PSS. |
18 |
|
|
|
19 |
|
|
Signed-off-by: Clemens Lang <cllang@redhat.com> |
20 |
|
|
--- |
21 |
|
|
crypto/rsa/rsa_oaep.c | 28 ++++++++++++++++++++++++++++ |
22 |
|
|
crypto/rsa/rsa_pss.c | 16 ++++++++++++++++ |
23 |
|
|
2 files changed, 44 insertions(+) |
24 |
|
|
|
25 |
|
|
diff --git a/crypto/rsa/rsa_oaep.c b/crypto/rsa/rsa_oaep.c |
26 |
|
|
index d9be1a4f98..dfe9c9f0e8 100644 |
27 |
|
|
--- a/crypto/rsa/rsa_oaep.c |
28 |
|
|
+++ b/crypto/rsa/rsa_oaep.c |
29 |
|
|
@@ -73,9 +73,23 @@ int ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(OSSL_LIB_CTX *libctx, |
30 |
|
|
return 0; |
31 |
|
|
#endif |
32 |
|
|
} |
33 |
|
|
+ |
34 |
|
|
+#ifdef FIPS_MODULE |
35 |
|
|
+ if (EVP_MD_is_a(md, "SHAKE-128") || EVP_MD_is_a(md, "SHAKE-256")) { |
36 |
|
|
+ ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED); |
37 |
|
|
+ return 0; |
38 |
|
|
+ } |
39 |
|
|
+#endif |
40 |
|
|
if (mgf1md == NULL) |
41 |
|
|
mgf1md = md; |
42 |
|
|
|
43 |
|
|
+#ifdef FIPS_MODULE |
44 |
|
|
+ if (EVP_MD_is_a(mgf1md, "SHAKE-128") || EVP_MD_is_a(mgf1md, "SHAKE-256")) { |
45 |
|
|
+ ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED); |
46 |
|
|
+ return 0; |
47 |
|
|
+ } |
48 |
|
|
+#endif |
49 |
|
|
+ |
50 |
|
|
mdlen = EVP_MD_get_size(md); |
51 |
|
|
if (mdlen <= 0) { |
52 |
|
|
ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_LENGTH); |
53 |
|
|
@@ -181,9 +195,23 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, |
54 |
|
|
#endif |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
+#ifdef FIPS_MODULE |
58 |
|
|
+ if (EVP_MD_is_a(md, "SHAKE-128") || EVP_MD_is_a(md, "SHAKE-256")) { |
59 |
|
|
+ ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED); |
60 |
|
|
+ return -1; |
61 |
|
|
+ } |
62 |
|
|
+#endif |
63 |
|
|
+ |
64 |
|
|
if (mgf1md == NULL) |
65 |
|
|
mgf1md = md; |
66 |
|
|
|
67 |
|
|
+#ifdef FIPS_MODULE |
68 |
|
|
+ if (EVP_MD_is_a(mgf1md, "SHAKE-128") || EVP_MD_is_a(mgf1md, "SHAKE-256")) { |
69 |
|
|
+ ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED); |
70 |
|
|
+ return -1; |
71 |
|
|
+ } |
72 |
|
|
+#endif |
73 |
|
|
+ |
74 |
|
|
mdlen = EVP_MD_get_size(md); |
75 |
|
|
|
76 |
|
|
if (tlen <= 0 || flen <= 0) |
77 |
|
|
diff --git a/crypto/rsa/rsa_pss.c b/crypto/rsa/rsa_pss.c |
78 |
|
|
index 33874bfef8..e8681b0351 100644 |
79 |
|
|
--- a/crypto/rsa/rsa_pss.c |
80 |
|
|
+++ b/crypto/rsa/rsa_pss.c |
81 |
|
|
@@ -53,6 +53,14 @@ int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, |
82 |
|
|
if (mgf1Hash == NULL) |
83 |
|
|
mgf1Hash = Hash; |
84 |
|
|
|
85 |
|
|
+#ifdef FIPS_MODULE |
86 |
|
|
+ if (EVP_MD_is_a(Hash, "SHAKE-128") || EVP_MD_is_a(Hash, "SHAKE-256")) |
87 |
|
|
+ goto err; |
88 |
|
|
+ |
89 |
|
|
+ if (EVP_MD_is_a(mgf1Hash, "SHAKE-128") || EVP_MD_is_a(mgf1Hash, "SHAKE-256")) |
90 |
|
|
+ goto err; |
91 |
|
|
+#endif |
92 |
|
|
+ |
93 |
|
|
hLen = EVP_MD_get_size(Hash); |
94 |
|
|
if (hLen < 0) |
95 |
|
|
goto err; |
96 |
|
|
@@ -164,6 +172,14 @@ int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, |
97 |
|
|
if (mgf1Hash == NULL) |
98 |
|
|
mgf1Hash = Hash; |
99 |
|
|
|
100 |
|
|
+#ifdef FIPS_MODULE |
101 |
|
|
+ if (EVP_MD_is_a(Hash, "SHAKE-128") || EVP_MD_is_a(Hash, "SHAKE-256")) |
102 |
|
|
+ goto err; |
103 |
|
|
+ |
104 |
|
|
+ if (EVP_MD_is_a(mgf1Hash, "SHAKE-128") || EVP_MD_is_a(mgf1Hash, "SHAKE-256")) |
105 |
|
|
+ goto err; |
106 |
|
|
+#endif |
107 |
|
|
+ |
108 |
|
|
hLen = EVP_MD_get_size(Hash); |
109 |
|
|
if (hLen < 0) |
110 |
|
|
goto err; |
111 |
|
|
-- |
112 |
|
|
2.39.0 |
113 |
|
|
|