Skip to content

Commit fb159a7

Browse files
committed
Use libb2 to provide blake2 implementation
1 parent 8a0a9e5 commit fb159a7

File tree

10 files changed

+228
-53
lines changed

10 files changed

+228
-53
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 \

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-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/_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@

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/blake2module.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#ifndef Py_BLAKE2MODULE_H
2+
#define Py_BLAKE2MODULE_H
3+
4+
#ifdef HAVE_LIBB2
5+
#include <blake2.h>
6+
7+
// copied from blake2-impl.h
8+
static inline void
9+
store32( void *dst, uint32_t w )
10+
{
11+
#if defined(NATIVE_LITTLE_ENDIAN)
12+
memcpy( dst, &w, sizeof( w ) );
13+
#else
14+
uint8_t *p = ( uint8_t * )dst;
15+
*p++ = ( uint8_t )w; w >>= 8;
16+
*p++ = ( uint8_t )w; w >>= 8;
17+
*p++ = ( uint8_t )w; w >>= 8;
18+
*p++ = ( uint8_t )w;
19+
#endif
20+
}
21+
22+
static inline void
23+
store48( void *dst, uint64_t w )
24+
{
25+
uint8_t *p = ( uint8_t * )dst;
26+
*p++ = ( uint8_t )w; w >>= 8;
27+
*p++ = ( uint8_t )w; w >>= 8;
28+
*p++ = ( uint8_t )w; w >>= 8;
29+
*p++ = ( uint8_t )w; w >>= 8;
30+
*p++ = ( uint8_t )w; w >>= 8;
31+
*p++ = ( uint8_t )w;
32+
}
33+
34+
static inline void
35+
store64( void *dst, uint64_t w )
36+
{
37+
#if defined(NATIVE_LITTLE_ENDIAN)
38+
memcpy( dst, &w, sizeof( w ) );
39+
#else
40+
uint8_t *p = ( uint8_t * )dst;
41+
*p++ = ( uint8_t )w; w >>= 8;
42+
*p++ = ( uint8_t )w; w >>= 8;
43+
*p++ = ( uint8_t )w; w >>= 8;
44+
*p++ = ( uint8_t )w; w >>= 8;
45+
*p++ = ( uint8_t )w; w >>= 8;
46+
*p++ = ( uint8_t )w; w >>= 8;
47+
*p++ = ( uint8_t )w; w >>= 8;
48+
*p++ = ( uint8_t )w;
49+
#endif
50+
}
51+
52+
static inline void
53+
secure_zero_memory(void *v, size_t n)
54+
{
55+
#if defined(_WIN32) || defined(WIN32)
56+
SecureZeroMemory(v, n);
57+
#elif defined(__hpux)
58+
static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
59+
memset_v(v, 0, n);
60+
#else
61+
// prioritize first the general C11 call
62+
#if defined(HAVE_MEMSET_S)
63+
memset_s(v, n, 0, n);
64+
#elif defined(HAVE_EXPLICIT_BZERO)
65+
explicit_bzero(v, n);
66+
#elif defined(HAVE_EXPLICIT_MEMSET)
67+
explicit_memset(v, 0, n);
68+
#else
69+
memset(v, 0, n);
70+
__asm__ __volatile__("" :: "r"(v) : "memory");
71+
#endif
72+
#endif
73+
}
74+
75+
#else
76+
// use vendored copy of blake2
77+
78+
// Prefix all public blake2 symbols with PyBlake2_
79+
#define blake2b PyBlake2_blake2b
80+
#define blake2b_compress PyBlake2_blake2b_compress
81+
#define blake2b_final PyBlake2_blake2b_final
82+
#define blake2b_init PyBlake2_blake2b_init
83+
#define blake2b_init_key PyBlake2_blake2b_init_key
84+
#define blake2b_init_param PyBlake2_blake2b_init_param
85+
#define blake2b_update PyBlake2_blake2b_update
86+
#define blake2bp PyBlake2_blake2bp
87+
#define blake2bp_final PyBlake2_blake2bp_final
88+
#define blake2bp_init PyBlake2_blake2bp_init
89+
#define blake2bp_init_key PyBlake2_blake2bp_init_key
90+
#define blake2bp_update PyBlake2_blake2bp_update
91+
#define blake2s PyBlake2_blake2s
92+
#define blake2s_compress PyBlake2_blake2s_compress
93+
#define blake2s_final PyBlake2_blake2s_final
94+
#define blake2s_init PyBlake2_blake2s_init
95+
#define blake2s_init_key PyBlake2_blake2s_init_key
96+
#define blake2s_init_param PyBlake2_blake2s_init_param
97+
#define blake2s_update PyBlake2_blake2s_update
98+
#define blake2sp PyBlake2_blake2sp
99+
#define blake2sp_final PyBlake2_blake2sp_final
100+
#define blake2sp_init PyBlake2_blake2sp_init
101+
#define blake2sp_init_key PyBlake2_blake2sp_init_key
102+
#define blake2sp_update PyBlake2_blake2sp_update
103+
104+
#include "impl/blake2.h"
105+
#include "impl/blake2-impl.h" /* for secure_zero_memory() and store48() */
106+
107+
#endif // HAVE_LIBB2
108+
109+
#endif // Py_BLAKE2MODULE_H

Modules/_blake2/blake2ns.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

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;

configure

Lines changed: 91 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6391,6 +6391,15 @@ for builtin_hash in $with_builtin_hashlib_hashes; do
63916391
done
63926392
IFS=$as_save_IFS
63936393

6394+
dnl libb2 for blake2
6395+
dnl _blake2 module falls back to vendored copy
6396+
AS_VAR_IF([with_builtin_blake2], [yes], [
6397+
PKG_CHECK_MODULES([LIBB2], [libb2], [
6398+
have_libb2=yes
6399+
AC_DEFINE([HAVE_LIBB2], [1], [Define to 1 if you want to build _blake2 with libb2])
6400+
], [have_libb2=no])
6401+
])
6402+
63946403
# --with-experimental-isolated-subinterpreters
63956404
AH_TEMPLATE(EXPERIMENTAL_ISOLATED_SUBINTERPRETERS,
63966405
[Better isolate subinterpreters, experimental build mode.])
@@ -6668,7 +6677,9 @@ PY_STDLIB_MOD([_sha1], [test "$with_builtin_sha1" = yes])
66686677
PY_STDLIB_MOD([_sha256], [test "$with_builtin_sha256" = yes])
66696678
PY_STDLIB_MOD([_sha512], [test "$with_builtin_sha512" = yes])
66706679
PY_STDLIB_MOD([_sha3], [test "$with_builtin_sha3" = yes])
6671-
PY_STDLIB_MOD([_blake2], [test "$with_builtin_blake2" = yes])
6680+
PY_STDLIB_MOD([_blake2],
6681+
[test "$with_builtin_blake2" = yes], [],
6682+
[$LIBB2_CFLAGS], [$LIBB2_LIBS])
66726683

66736684
PY_STDLIB_MOD([_crypt],
66746685
[], [test "$ac_cv_crypt_crypt" = yes],

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,9 @@
622622
/* Define to 1 if you have the `lchown' function. */
623623
#undef HAVE_LCHOWN
624624

625+
/* Define to 1 if you want to build _blake2 with libb2 */
626+
#undef HAVE_LIBB2
627+
625628
/* Define to 1 if you have the `db' library (-ldb). */
626629
#undef HAVE_LIBDB
627630

0 commit comments

Comments
 (0)