Skip to content

Commit b16b6bb

Browse files
authored
bpo-47095: Use libb2 to provide blake2 implementation (pythonGH-32059)
1 parent c23ddf5 commit b16b6bb

19 files changed

+149
-17791
lines changed

.github/workflows/posix-deps-apt.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ apt-get -yq install \
77
ccache \
88
gdb \
99
lcov \
10+
libb2-dev \
1011
libbz2-dev \
1112
libffi-dev \
1213
libgdbm-dev \

Doc/whatsnew/3.11.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ fractions
245245
that an ``isinstance(some_fraction, typing.SupportsInt)`` check passes.
246246
(Contributed by Mark Dickinson in :issue:`44547`.)
247247

248+
hashlib
249+
-------
250+
251+
* :func:`hashlib.blake2b` and :func:`hashlib.blake2s` now prefer `libb2`_
252+
over Python's vendored copy.
253+
(Contributed by Christian Heimes in :issue:`47095`.)
248254

249255
IDLE and idlelib
250256
----------------
@@ -1066,6 +1072,9 @@ Porting to Python 3.11
10661072
<https://github.com/python/pythoncapi_compat>`__ to get these functions
10671073
on old Python functions.
10681074

1075+
* Distributors are encouraged to build Python with the optimized Blake2
1076+
library `libb2`_.
1077+
10691078

10701079
Deprecated
10711080
----------
@@ -1145,3 +1154,6 @@ Removed
11451154
* Remove the ``HAVE_PY_SET_53BIT_PRECISION`` macro (moved to the internal C
11461155
API).
11471156
(Contributed by Victor Stinner in :issue:`45412`.)
1157+
1158+
1159+
.. _libb2: https://www.blake2.net/

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,7 @@ MODULE_CMATH_DEPS=$(srcdir)/Modules/_math.h
24822482
MODULE_MATH_DEPS=$(srcdir)/Modules/_math.h
24832483
MODULE_PYEXPAT_DEPS=$(LIBEXPAT_HEADERS) @LIBEXPAT_INTERNAL@
24842484
MODULE_UNICODEDATA_DEPS=$(srcdir)/Modules/unicodedata_db.h $(srcdir)/Modules/unicodename_db.h
2485-
MODULE__BLAKE2_DEPS=$(srcdir)/Modules/_blake2/impl/blake2-config.h $(srcdir)/Modules/_blake2/impl/blake2-dispatch.c $(srcdir)/Modules/_blake2/impl/blake2-impl.h $(srcdir)/Modules/_blake2/impl/blake2-kat.h $(srcdir)/Modules/_blake2/impl/blake2.h $(srcdir)/Modules/_blake2/impl/blake2b-load-sse2.h $(srcdir)/Modules/_blake2/impl/blake2b-load-sse41.h $(srcdir)/Modules/_blake2/impl/blake2b-ref.c $(srcdir)/Modules/_blake2/impl/blake2b-round.h $(srcdir)/Modules/_blake2/impl/blake2b-test.c $(srcdir)/Modules/_blake2/impl/blake2b.c $(srcdir)/Modules/_blake2/impl/blake2bp-test.c $(srcdir)/Modules/_blake2/impl/blake2bp.c $(srcdir)/Modules/_blake2/impl/blake2s-load-sse2.h $(srcdir)/Modules/_blake2/impl/blake2s-load-sse41.h $(srcdir)/Modules/_blake2/impl/blake2s-load-xop.h $(srcdir)/Modules/_blake2/impl/blake2s-ref.c $(srcdir)/Modules/_blake2/impl/blake2s-round.h $(srcdir)/Modules/_blake2/impl/blake2s-test.c $(srcdir)/Modules/_blake2/impl/blake2s.c $(srcdir)/Modules/_blake2/impl/blake2sp-test.c $(srcdir)/Modules/_blake2/impl/blake2sp.c $(srcdir)/Modules/hashlib.h
2485+
MODULE__BLAKE2_DEPS=$(srcdir)/Modules/_blake2/impl/blake2-config.h $(srcdir)/Modules/_blake2/impl/blake2-impl.h $(srcdir)/Modules/_blake2/impl/blake2.h $(srcdir)/Modules/_blake2/impl/blake2b-load-sse2.h $(srcdir)/Modules/_blake2/impl/blake2b-load-sse41.h $(srcdir)/Modules/_blake2/impl/blake2b-ref.c $(srcdir)/Modules/_blake2/impl/blake2b-round.h $(srcdir)/Modules/_blake2/impl/blake2b.c $(srcdir)/Modules/_blake2/impl/blake2s-load-sse2.h $(srcdir)/Modules/_blake2/impl/blake2s-load-sse41.h $(srcdir)/Modules/_blake2/impl/blake2s-load-xop.h $(srcdir)/Modules/_blake2/impl/blake2s-ref.c $(srcdir)/Modules/_blake2/impl/blake2s-round.h $(srcdir)/Modules/_blake2/impl/blake2s.c $(srcdir)/Modules/_blake2/blake2module.h $(srcdir)/Modules/hashlib.h
24862486
MODULE__CTYPES_DEPS=$(srcdir)/Modules/_ctypes/ctypes.h
24872487
MODULE__DECIMAL_DEPS=$(srcdir)/Modules/_decimal/docstrings.h $(LIBMPDEC_HEADERS) @LIBMPDEC_INTERNAL@
24882488
MODULE__ELEMENTTREE_DEPS=$(srcdir)/Modules/pyexpat.c $(LIBEXPAT_HEADERS) @LIBEXPAT_INTERNAL@
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`hashlib`'s internal ``_blake2`` module now prefers ``libb2`` from
2+
https://www.blake2.net/ over Python's vendored copy of blake2.

Modules/_blake2/blake2b_impl.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,23 @@
2121
#include "pycore_strhex.h" // _Py_strhex()
2222

2323
#include "../hashlib.h"
24-
#include "blake2ns.h"
25-
26-
#define HAVE_BLAKE2B 1
27-
#define BLAKE2_LOCAL_INLINE(type) Py_LOCAL_INLINE(type)
28-
29-
#include "impl/blake2.h"
30-
#include "impl/blake2-impl.h" /* for secure_zero_memory() and store48() */
24+
#include "blake2module.h"
3125

26+
#ifndef HAVE_LIBB2
3227
/* pure SSE2 implementation is very slow, so only use the more optimized SSSE3+
3328
* https://bugs.python.org/issue31834 */
3429
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__AVX__) || defined(__XOP__)
3530
#include "impl/blake2b.c"
3631
#else
3732
#include "impl/blake2b-ref.c"
3833
#endif
34+
#endif // !HAVE_LIBB2
3935

36+
#define HAVE_BLAKE2B 1
4037

4138
extern PyType_Spec blake2b_type_spec;
4239

40+
4341
typedef struct {
4442
PyObject_HEAD
4543
blake2b_param param;

Modules/_blake2/blake2module.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#endif
1414

1515
#include "Python.h"
16-
17-
#include "impl/blake2.h"
16+
#include "blake2module.h"
1817

1918
extern PyType_Spec blake2b_type_spec;
2019
extern PyType_Spec blake2s_type_spec;

Modules/_blake2/blake2ns.h renamed to Modules/_blake2/blake2module.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
/* Prefix all public blake2 symbols with PyBlake2_
2-
*/
1+
#ifndef Py_BLAKE2MODULE_H
2+
#define Py_BLAKE2MODULE_H
33

4-
#ifndef Py_BLAKE2_NS
5-
#define Py_BLAKE2_NS
4+
#ifdef HAVE_LIBB2
5+
#include <blake2.h>
66

7+
#else
8+
// use vendored copy of blake2
9+
10+
// Prefix all public blake2 symbols with PyBlake2_
711
#define blake2b PyBlake2_blake2b
812
#define blake2b_compress PyBlake2_blake2b_compress
913
#define blake2b_final PyBlake2_blake2b_final
@@ -29,4 +33,11 @@
2933
#define blake2sp_init_key PyBlake2_blake2sp_init_key
3034
#define blake2sp_update PyBlake2_blake2sp_update
3135

32-
#endif /* Py_BLAKE2_NS */
36+
#include "impl/blake2.h"
37+
38+
#endif // HAVE_LIBB2
39+
40+
// for secure_zero_memory(), store32(), store48(), and store64()
41+
#include "impl/blake2-impl.h"
42+
43+
#endif // Py_BLAKE2MODULE_H

Modules/_blake2/blake2s_impl.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,23 @@
2121
#include "pycore_strhex.h" // _Py_strhex()
2222

2323
#include "../hashlib.h"
24-
#include "blake2ns.h"
25-
26-
#define HAVE_BLAKE2S 1
27-
#define BLAKE2_LOCAL_INLINE(type) Py_LOCAL_INLINE(type)
28-
29-
#include "impl/blake2.h"
30-
#include "impl/blake2-impl.h" /* for secure_zero_memory() and store48() */
24+
#include "blake2module.h"
3125

26+
#ifndef HAVE_LIBB2
3227
/* pure SSE2 implementation is very slow, so only use the more optimized SSSE3+
3328
* https://bugs.python.org/issue31834 */
3429
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__AVX__) || defined(__XOP__)
3530
#include "impl/blake2s.c"
3631
#else
3732
#include "impl/blake2s-ref.c"
3833
#endif
34+
#endif // !HAVE_LIBB2
3935

36+
#define HAVE_BLAKE2S 1
4037

4138
extern PyType_Spec blake2s_type_spec;
4239

40+
4341
typedef struct {
4442
PyObject_HEAD
4543
blake2s_param param;

0 commit comments

Comments
 (0)