Skip to content

Commit 7d8d14f

Browse files
Detect crypto library BLAKE2 support
While OpenSSL supports both "b" and "s" variants of the BLAKE2 hash function, other cryptographic libraries may lack support for one or both of the variants. This commit modifies `hashlib`'s C code to detect whether or not the linked libcrypto supports each BLAKE2 variant, and elides references to each variant's NID accordingly. In cases where the underlying libcrypto doesn't fully support BLAKE2, CPython's `./configure` script can be given the following flag to use CPython's interned BLAKE2 implementation: `--with-builtin-hashlib-hashes=blake2`.
1 parent 4abca7e commit 7d8d14f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Modules/_hashopenssl.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
#define PY_OPENSSL_HAS_SCRYPT 1
4848
#define PY_OPENSSL_HAS_SHA3 1
4949
#define PY_OPENSSL_HAS_SHAKE 1
50+
#if defined(NID_blake2s256) || defined(NID_blake2b512)
5051
#define PY_OPENSSL_HAS_BLAKE2 1
52+
#endif
5153

5254
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
5355
#define PY_EVP_MD EVP_MD
@@ -102,8 +104,12 @@ typedef struct {
102104
#define Py_hash_sha3_512 "sha3_512"
103105
#define Py_hash_shake_128 "shake_128"
104106
#define Py_hash_shake_256 "shake_256"
107+
#if defined(NID_blake2s256)
105108
#define Py_hash_blake2s "blake2s"
109+
#endif
110+
#if defined(NID_blake2b512)
106111
#define Py_hash_blake2b "blake2b"
112+
#endif
107113

108114
#define PY_HASH_ENTRY(py_name, py_alias, ossl_name, ossl_nid) \
109115
{py_name, py_alias, ossl_name, ossl_nid, 0, NULL, NULL}
@@ -130,8 +136,12 @@ static const py_hashentry_t py_hashes[] = {
130136
PY_HASH_ENTRY(Py_hash_shake_128, NULL, SN_shake128, NID_shake128),
131137
PY_HASH_ENTRY(Py_hash_shake_256, NULL, SN_shake256, NID_shake256),
132138
/* blake2 digest */
139+
#if defined(NID_blake2s256)
133140
PY_HASH_ENTRY(Py_hash_blake2s, "blake2s256", SN_blake2s256, NID_blake2s256),
141+
#endif
142+
#if defined(NID_blake2s256)
134143
PY_HASH_ENTRY(Py_hash_blake2b, "blake2b512", SN_blake2b512, NID_blake2b512),
144+
#endif
135145
PY_HASH_ENTRY(NULL, NULL, NULL, 0),
136146
};
137147

0 commit comments

Comments
 (0)