Skip to content

Commit eb2b0c6

Browse files
authored
bpo-38153: detect shake independently from sha3 (GH-16143)
XOF digests (SHAKE) are not available in OpenSSL 1.1.0 but SHA3 fixed-length digests are. Signed-off-by: Christian Heimes <[email protected]>
1 parent f919054 commit eb2b0c6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Modules/_hashopenssl.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@
3434

3535
#define MUNCH_SIZE INT_MAX
3636

37-
#if defined(NID_sha3_224) && defined(EVP_MD_FLAG_XOF)
37+
#ifdef NID_sha3_224
3838
#define PY_OPENSSL_HAS_SHA3 1
3939
#endif
4040

41+
#if defined(EVP_MD_FLAG_XOF) && defined(NID_shake128)
42+
#define PY_OPENSSL_HAS_SHAKE 1
43+
#endif
44+
4145
#ifdef NID_blake2b512
4246
#define PY_OPENSSL_HAS_BLAKE2 1
4347
#endif
@@ -139,6 +143,8 @@ py_digest_name(const EVP_MD *md)
139143
case NID_sha3_512:
140144
name ="sha3_512";
141145
break;
146+
#endif
147+
#ifdef PY_OPENSSL_HAS_SHAKE
142148
case NID_shake128:
143149
name ="shake_128";
144150
break;
@@ -177,8 +183,9 @@ py_digest_by_name(const char *name)
177183
/* OpenSSL uses dash instead of underscore in names of some algorithms
178184
* like SHA3 and SHAKE. Detect different spellings. */
179185
if (digest == NULL) {
186+
if (0) {}
180187
#ifdef NID_sha512_224
181-
if (!strcmp(name, "sha512_224") || !strcmp(name, "SHA512_224")) {
188+
else if (!strcmp(name, "sha512_224") || !strcmp(name, "SHA512_224")) {
182189
digest = EVP_sha512_224();
183190
}
184191
else if (!strcmp(name, "sha512_256") || !strcmp(name, "SHA512_256")) {
@@ -199,6 +206,8 @@ py_digest_by_name(const char *name)
199206
else if (!strcmp(name, "sha3_512")) {
200207
digest = EVP_sha3_512();
201208
}
209+
#endif
210+
#ifdef PY_OPENSSL_HAS_SHAKE
202211
else if (!strcmp(name, "shake_128")) {
203212
digest = EVP_shake128();
204213
}

0 commit comments

Comments
 (0)