bpo-47095: Use libb2 to provide blake2 implementation (GH-32059)

This commit is contained in:
Christian Heimes 2022-03-26 21:52:24 +02:00 committed by GitHub
parent c23ddf5ec2
commit b16b6bb8da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 149 additions and 17791 deletions

View File

@ -7,6 +7,7 @@ apt-get -yq install \
ccache \
gdb \
lcov \
libb2-dev \
libbz2-dev \
libffi-dev \
libgdbm-dev \

View File

@ -245,6 +245,12 @@ fractions
that an ``isinstance(some_fraction, typing.SupportsInt)`` check passes.
(Contributed by Mark Dickinson in :issue:`44547`.)
hashlib
-------
* :func:`hashlib.blake2b` and :func:`hashlib.blake2s` now prefer `libb2`_
over Python's vendored copy.
(Contributed by Christian Heimes in :issue:`47095`.)
IDLE and idlelib
----------------
@ -1066,6 +1072,9 @@ Porting to Python 3.11
<https://github.com/python/pythoncapi_compat>`__ to get these functions
on old Python functions.
* Distributors are encouraged to build Python with the optimized Blake2
library `libb2`_.
Deprecated
----------
@ -1145,3 +1154,6 @@ Removed
* Remove the ``HAVE_PY_SET_53BIT_PRECISION`` macro (moved to the internal C
API).
(Contributed by Victor Stinner in :issue:`45412`.)
.. _libb2: https://www.blake2.net/

View File

@ -2482,7 +2482,7 @@ MODULE_CMATH_DEPS=$(srcdir)/Modules/_math.h
MODULE_MATH_DEPS=$(srcdir)/Modules/_math.h
MODULE_PYEXPAT_DEPS=$(LIBEXPAT_HEADERS) @LIBEXPAT_INTERNAL@
MODULE_UNICODEDATA_DEPS=$(srcdir)/Modules/unicodedata_db.h $(srcdir)/Modules/unicodename_db.h
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
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
MODULE__CTYPES_DEPS=$(srcdir)/Modules/_ctypes/ctypes.h
MODULE__DECIMAL_DEPS=$(srcdir)/Modules/_decimal/docstrings.h $(LIBMPDEC_HEADERS) @LIBMPDEC_INTERNAL@
MODULE__ELEMENTTREE_DEPS=$(srcdir)/Modules/pyexpat.c $(LIBEXPAT_HEADERS) @LIBEXPAT_INTERNAL@

View File

@ -0,0 +1,2 @@
:mod:`hashlib`'s internal ``_blake2`` module now prefers ``libb2`` from
https://www.blake2.net/ over Python's vendored copy of blake2.

View File

@ -21,14 +21,9 @@
#include "pycore_strhex.h" // _Py_strhex()
#include "../hashlib.h"
#include "blake2ns.h"
#define HAVE_BLAKE2B 1
#define BLAKE2_LOCAL_INLINE(type) Py_LOCAL_INLINE(type)
#include "impl/blake2.h"
#include "impl/blake2-impl.h" /* for secure_zero_memory() and store48() */
#include "blake2module.h"
#ifndef HAVE_LIBB2
/* pure SSE2 implementation is very slow, so only use the more optimized SSSE3+
* https://bugs.python.org/issue31834 */
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__AVX__) || defined(__XOP__)
@ -36,10 +31,13 @@
#else
#include "impl/blake2b-ref.c"
#endif
#endif // !HAVE_LIBB2
#define HAVE_BLAKE2B 1
extern PyType_Spec blake2b_type_spec;
typedef struct {
PyObject_HEAD
blake2b_param param;

View File

@ -13,8 +13,7 @@
#endif
#include "Python.h"
#include "impl/blake2.h"
#include "blake2module.h"
extern PyType_Spec blake2b_type_spec;
extern PyType_Spec blake2s_type_spec;

View File

@ -1,9 +1,13 @@
/* Prefix all public blake2 symbols with PyBlake2_
*/
#ifndef Py_BLAKE2MODULE_H
#define Py_BLAKE2MODULE_H
#ifndef Py_BLAKE2_NS
#define Py_BLAKE2_NS
#ifdef HAVE_LIBB2
#include <blake2.h>
#else
// use vendored copy of blake2
// Prefix all public blake2 symbols with PyBlake2_
#define blake2b PyBlake2_blake2b
#define blake2b_compress PyBlake2_blake2b_compress
#define blake2b_final PyBlake2_blake2b_final
@ -29,4 +33,11 @@
#define blake2sp_init_key PyBlake2_blake2sp_init_key
#define blake2sp_update PyBlake2_blake2sp_update
#endif /* Py_BLAKE2_NS */
#include "impl/blake2.h"
#endif // HAVE_LIBB2
// for secure_zero_memory(), store32(), store48(), and store64()
#include "impl/blake2-impl.h"
#endif // Py_BLAKE2MODULE_H

View File

@ -21,14 +21,9 @@
#include "pycore_strhex.h" // _Py_strhex()
#include "../hashlib.h"
#include "blake2ns.h"
#define HAVE_BLAKE2S 1
#define BLAKE2_LOCAL_INLINE(type) Py_LOCAL_INLINE(type)
#include "impl/blake2.h"
#include "impl/blake2-impl.h" /* for secure_zero_memory() and store48() */
#include "blake2module.h"
#ifndef HAVE_LIBB2
/* pure SSE2 implementation is very slow, so only use the more optimized SSSE3+
* https://bugs.python.org/issue31834 */
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__AVX__) || defined(__XOP__)
@ -36,10 +31,13 @@
#else
#include "impl/blake2s-ref.c"
#endif
#endif // !HAVE_LIBB2
#define HAVE_BLAKE2S 1
extern PyType_Spec blake2s_type_spec;
typedef struct {
PyObject_HEAD
blake2s_param param;

View File

@ -1,577 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#if defined(WIN32)
#include <windows.h>
#endif
#include "blake2.h"
#if defined(__x86_64__) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
#define HAVE_X86
#endif
typedef enum
{
NONE = 0,
#if defined(HAVE_X86)
SSE2 = 1,
SSSE3 = 2,
SSE41 = 3,
AVX = 4,
XOP = 5,
/* AVX2 = 6, */
#endif
} cpu_feature_t;
static const char feature_names[][8] =
{
"none",
#if defined(HAVE_X86)
"sse2",
"ssse3",
"sse41",
"avx",
"xop",
/* "avx2" */
#endif
};
#if defined(HAVE_X86)
#if defined(__GNUC__)
static inline void cpuid( uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx )
{
__asm__ __volatile__(
#if defined(__i386__) /* This is needed for -fPIC to work on i386 */
"movl %%ebx, %%esi\n\t"
#endif
"cpuid\n\t"
#if defined(__i386__)
"xchgl %%ebx, %%esi\n\t"
: "=a"( *eax ), "=S"( *ebx ), "=c"( *ecx ), "=d"( *edx ) : "a"( *eax ) );
#else
: "=a"( *eax ), "=b"( *ebx ), "=c"( *ecx ), "=d"( *edx ) : "a"( *eax ) );
#endif
}
static inline uint64_t xgetbv(uint32_t xcr)
{
uint32_t a, d;
__asm__ __volatile__(
"xgetbv"
: "=a"(a),"=d"(d)
: "c"(xcr)
);
return ((uint64_t)d << 32) | a;
}
#elif defined(_MSC_VER)
#include <intrin.h>
static inline void cpuid( uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx )
{
int regs[4];
__cpuid( regs, *eax );
*eax = regs[0];
*ebx = regs[1];
*ecx = regs[2];
*edx = regs[3];
}
#else
#error "Don't know how to call cpuid on this compiler!"
#endif
#endif /* HAVE_X86 */
static inline cpu_feature_t get_cpu_features( void )
{
#if defined(HAVE_X86)
static volatile int initialized = 0;
static cpu_feature_t feature = NONE; // Safe default
uint32_t eax, ecx, edx, ebx;
if( initialized )
return feature;
eax = 1;
cpuid( &eax, &ebx, &ecx, &edx );
if( 1 & ( edx >> 26 ) )
feature = SSE2;
if( 1 & ( ecx >> 9 ) )
feature = SSSE3;
if( 1 & ( ecx >> 19 ) )
feature = SSE41;
#if defined(WIN32) /* Work around the fact that Windows <7 does NOT support AVX... */
if( IsProcessorFeaturePresent(17) ) /* Some environments don't know about PF_XSAVE_ENABLED */
#endif
{
/* check for AVX and OSXSAVE bits */
if( 1 & ( ecx >> 28 ) & (ecx >> 27) ) {
#if !defined(WIN32) /* Already checked for this in WIN32 */
if( (xgetbv(0) & 6) == 6 ) /* XCR0 */
#endif
feature = AVX;
}
eax = 0x80000001;
cpuid( &eax, &ebx, &ecx, &edx );
if( 1 & ( ecx >> 11 ) )
feature = XOP;
}
/* For future architectures */
/*
eax = 7; ecx = 0;
cpuid(&eax, &ebx, &ecx, &edx);
if(1&(ebx >> 5))
feature = AVX2;
*/
/* fprintf( stderr, "Using %s engine\n", feature_names[feature] ); */
initialized = 1;
return feature;
#else
return NONE;
#endif
}
#if defined(__cplusplus)
extern "C" {
#endif
int blake2b_init_ref( blake2b_state *S, size_t outlen );
int blake2b_init_key_ref( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_ref( blake2b_state *S, const blake2b_param *P );
int blake2b_update_ref( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_ref( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_ref( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
#if defined(HAVE_X86)
int blake2b_init_sse2( blake2b_state *S, size_t outlen );
int blake2b_init_key_sse2( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_sse2( blake2b_state *S, const blake2b_param *P );
int blake2b_update_sse2( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_sse2( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_sse2( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2b_init_ssse3( blake2b_state *S, size_t outlen );
int blake2b_init_key_ssse3( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_ssse3( blake2b_state *S, const blake2b_param *P );
int blake2b_update_ssse3( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_ssse3( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_ssse3( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2b_init_sse41( blake2b_state *S, size_t outlen );
int blake2b_init_key_sse41( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_sse41( blake2b_state *S, const blake2b_param *P );
int blake2b_update_sse41( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_sse41( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_sse41( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2b_init_avx( blake2b_state *S, size_t outlen );
int blake2b_init_key_avx( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_avx( blake2b_state *S, const blake2b_param *P );
int blake2b_update_avx( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_avx( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_avx( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2b_init_xop( blake2b_state *S, size_t outlen );
int blake2b_init_key_xop( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_xop( blake2b_state *S, const blake2b_param *P );
int blake2b_update_xop( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_xop( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_xop( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
#endif /* HAVE_X86 */
int blake2s_init_ref( blake2s_state *S, size_t outlen );
int blake2s_init_key_ref( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_ref( blake2s_state *S, const blake2s_param *P );
int blake2s_update_ref( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_ref( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_ref( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
#if defined(HAVE_X86)
int blake2s_init_sse2( blake2s_state *S, size_t outlen );
int blake2s_init_key_sse2( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_sse2( blake2s_state *S, const blake2s_param *P );
int blake2s_update_sse2( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_sse2( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_sse2( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2s_init_ssse3( blake2s_state *S, size_t outlen );
int blake2s_init_key_ssse3( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_ssse3( blake2s_state *S, const blake2s_param *P );
int blake2s_update_ssse3( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_ssse3( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_ssse3( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2s_init_sse41( blake2s_state *S, size_t outlen );
int blake2s_init_key_sse41( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_sse41( blake2s_state *S, const blake2s_param *P );
int blake2s_update_sse41( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_sse41( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_sse41( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2s_init_avx( blake2s_state *S, size_t outlen );
int blake2s_init_key_avx( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_avx( blake2s_state *S, const blake2s_param *P );
int blake2s_update_avx( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_avx( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_avx( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2s_init_xop( blake2s_state *S, size_t outlen );
int blake2s_init_key_xop( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_xop( blake2s_state *S, const blake2s_param *P );
int blake2s_update_xop( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_xop( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_xop( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
#endif /* HAVE_X86 */
#if defined(__cplusplus)
}
#endif
typedef int ( *blake2b_init_fn )( blake2b_state *, size_t );
typedef int ( *blake2b_init_key_fn )( blake2b_state *, size_t, const void *, size_t );
typedef int ( *blake2b_init_param_fn )( blake2b_state *, const blake2b_param * );
typedef int ( *blake2b_update_fn )( blake2b_state *, const uint8_t *, size_t );
typedef int ( *blake2b_final_fn )( blake2b_state *, uint8_t *, size_t );
typedef int ( *blake2b_fn )( uint8_t *, const void *, const void *, size_t, size_t, size_t );
typedef int ( *blake2s_init_fn )( blake2s_state *, size_t );
typedef int ( *blake2s_init_key_fn )( blake2s_state *, size_t, const void *, size_t );
typedef int ( *blake2s_init_param_fn )( blake2s_state *, const blake2s_param * );
typedef int ( *blake2s_update_fn )( blake2s_state *, const uint8_t *, size_t );
typedef int ( *blake2s_final_fn )( blake2s_state *, uint8_t *, size_t );
typedef int ( *blake2s_fn )( uint8_t *, const void *, const void *, size_t, size_t, size_t );
static const blake2b_init_fn blake2b_init_table[] =
{
blake2b_init_ref,
#if defined(HAVE_X86)
blake2b_init_sse2,
blake2b_init_ssse3,
blake2b_init_sse41,
blake2b_init_avx,
blake2b_init_xop
#endif
};
static const blake2b_init_key_fn blake2b_init_key_table[] =
{
blake2b_init_key_ref,
#if defined(HAVE_X86)
blake2b_init_key_sse2,
blake2b_init_key_ssse3,
blake2b_init_key_sse41,
blake2b_init_key_avx,
blake2b_init_key_xop
#endif
};
static const blake2b_init_param_fn blake2b_init_param_table[] =
{
blake2b_init_param_ref,
#if defined(HAVE_X86)
blake2b_init_param_sse2,
blake2b_init_param_ssse3,
blake2b_init_param_sse41,
blake2b_init_param_avx,
blake2b_init_param_xop
#endif
};
static const blake2b_update_fn blake2b_update_table[] =
{
blake2b_update_ref,
#if defined(HAVE_X86)
blake2b_update_sse2,
blake2b_update_ssse3,
blake2b_update_sse41,
blake2b_update_avx,
blake2b_update_xop
#endif
};
static const blake2b_final_fn blake2b_final_table[] =
{
blake2b_final_ref,
#if defined(HAVE_X86)
blake2b_final_sse2,
blake2b_final_ssse3,
blake2b_final_sse41,
blake2b_final_avx,
blake2b_final_xop
#endif
};
static const blake2b_fn blake2b_table[] =
{
blake2b_ref,
#if defined(HAVE_X86)
blake2b_sse2,
blake2b_ssse3,
blake2b_sse41,
blake2b_avx,
blake2b_xop
#endif
};
static const blake2s_init_fn blake2s_init_table[] =
{
blake2s_init_ref,
#if defined(HAVE_X86)
blake2s_init_sse2,
blake2s_init_ssse3,
blake2s_init_sse41,
blake2s_init_avx,
blake2s_init_xop
#endif
};
static const blake2s_init_key_fn blake2s_init_key_table[] =
{
blake2s_init_key_ref,
#if defined(HAVE_X86)
blake2s_init_key_sse2,
blake2s_init_key_ssse3,
blake2s_init_key_sse41,
blake2s_init_key_avx,
blake2s_init_key_xop
#endif
};
static const blake2s_init_param_fn blake2s_init_param_table[] =
{
blake2s_init_param_ref,
#if defined(HAVE_X86)
blake2s_init_param_sse2,
blake2s_init_param_ssse3,
blake2s_init_param_sse41,
blake2s_init_param_avx,
blake2s_init_param_xop
#endif
};
static const blake2s_update_fn blake2s_update_table[] =
{
blake2s_update_ref,
#if defined(HAVE_X86)
blake2s_update_sse2,
blake2s_update_ssse3,
blake2s_update_sse41,
blake2s_update_avx,
blake2s_update_xop
#endif
};
static const blake2s_final_fn blake2s_final_table[] =
{
blake2s_final_ref,
#if defined(HAVE_X86)
blake2s_final_sse2,
blake2s_final_ssse3,
blake2s_final_sse41,
blake2s_final_avx,
blake2s_final_xop
#endif
};
static const blake2s_fn blake2s_table[] =
{
blake2s_ref,
#if defined(HAVE_X86)
blake2s_sse2,
blake2s_ssse3,
blake2s_sse41,
blake2s_avx,
blake2s_xop
#endif
};
#if defined(__cplusplus)
extern "C" {
#endif
int blake2b_init_dispatch( blake2b_state *S, size_t outlen );
int blake2b_init_key_dispatch( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
int blake2b_init_param_dispatch( blake2b_state *S, const blake2b_param *P );
int blake2b_update_dispatch( blake2b_state *S, const uint8_t *in, size_t inlen );
int blake2b_final_dispatch( blake2b_state *S, uint8_t *out, size_t outlen );
int blake2b_dispatch( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
int blake2s_init_dispatch( blake2s_state *S, size_t outlen );
int blake2s_init_key_dispatch( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
int blake2s_init_param_dispatch( blake2s_state *S, const blake2s_param *P );
int blake2s_update_dispatch( blake2s_state *S, const uint8_t *in, size_t inlen );
int blake2s_final_dispatch( blake2s_state *S, uint8_t *out, size_t outlen );
int blake2s_dispatch( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
#if defined(__cplusplus)
}
#endif
static blake2b_init_fn blake2b_init_ptr = blake2b_init_dispatch;
static blake2b_init_key_fn blake2b_init_key_ptr = blake2b_init_key_dispatch;
static blake2b_init_param_fn blake2b_init_param_ptr = blake2b_init_param_dispatch;
static blake2b_update_fn blake2b_update_ptr = blake2b_update_dispatch;
static blake2b_final_fn blake2b_final_ptr = blake2b_final_dispatch;
static blake2b_fn blake2b_ptr = blake2b_dispatch;
static blake2s_init_fn blake2s_init_ptr = blake2s_init_dispatch;
static blake2s_init_key_fn blake2s_init_key_ptr = blake2s_init_key_dispatch;
static blake2s_init_param_fn blake2s_init_param_ptr = blake2s_init_param_dispatch;
static blake2s_update_fn blake2s_update_ptr = blake2s_update_dispatch;
static blake2s_final_fn blake2s_final_ptr = blake2s_final_dispatch;
static blake2s_fn blake2s_ptr = blake2s_dispatch;
int blake2b_init_dispatch( blake2b_state *S, size_t outlen )
{
blake2b_init_ptr = blake2b_init_table[get_cpu_features()];
return blake2b_init_ptr( S, outlen );
}
int blake2b_init_key_dispatch( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
{
blake2b_init_key_ptr = blake2b_init_key_table[get_cpu_features()];
return blake2b_init_key_ptr( S, outlen, key, keylen );
}
int blake2b_init_param_dispatch( blake2b_state *S, const blake2b_param *P )
{
blake2b_init_param_ptr = blake2b_init_param_table[get_cpu_features()];
return blake2b_init_param_ptr( S, P );
}
int blake2b_update_dispatch( blake2b_state *S, const uint8_t *in, size_t inlen )
{
blake2b_update_ptr = blake2b_update_table[get_cpu_features()];
return blake2b_update_ptr( S, in, inlen );
}
int blake2b_final_dispatch( blake2b_state *S, uint8_t *out, size_t outlen )
{
blake2b_final_ptr = blake2b_final_table[get_cpu_features()];
return blake2b_final_ptr( S, out, outlen );
}
int blake2b_dispatch( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
blake2b_ptr = blake2b_table[get_cpu_features()];
return blake2b_ptr( out, in, key, outlen, inlen, keylen );
}
BLAKE2_API int blake2b_init( blake2b_state *S, size_t outlen )
{
return blake2b_init_ptr( S, outlen );
}
BLAKE2_API int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen )
{
return blake2b_init_key_ptr( S, outlen, key, keylen );
}
BLAKE2_API int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
{
return blake2b_init_param_ptr( S, P );
}
BLAKE2_API int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen )
{
return blake2b_update_ptr( S, in, inlen );
}
BLAKE2_API int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen )
{
return blake2b_final_ptr( S, out, outlen );
}
BLAKE2_API int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
return blake2b_ptr( out, in, key, outlen, inlen, keylen );
}
int blake2s_init_dispatch( blake2s_state *S, size_t outlen )
{
blake2s_init_ptr = blake2s_init_table[get_cpu_features()];
return blake2s_init_ptr( S, outlen );
}
int blake2s_init_key_dispatch( blake2s_state *S, size_t outlen, const void *key, size_t keylen )
{
blake2s_init_key_ptr = blake2s_init_key_table[get_cpu_features()];
return blake2s_init_key_ptr( S, outlen, key, keylen );
}
int blake2s_init_param_dispatch( blake2s_state *S, const blake2s_param *P )
{
blake2s_init_param_ptr = blake2s_init_param_table[get_cpu_features()];
return blake2s_init_param_ptr( S, P );
}
int blake2s_update_dispatch( blake2s_state *S, const uint8_t *in, size_t inlen )
{
blake2s_update_ptr = blake2s_update_table[get_cpu_features()];
return blake2s_update_ptr( S, in, inlen );
}
int blake2s_final_dispatch( blake2s_state *S, uint8_t *out, size_t outlen )
{
blake2s_final_ptr = blake2s_final_table[get_cpu_features()];
return blake2s_final_ptr( S, out, outlen );
}
int blake2s_dispatch( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
blake2s_ptr = blake2s_table[get_cpu_features()];
return blake2s_ptr( out, in, key, outlen, inlen, keylen );
}
BLAKE2_API int blake2s_init( blake2s_state *S, size_t outlen )
{
return blake2s_init_ptr( S, outlen );
}
BLAKE2_API int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen )
{
return blake2s_init_key_ptr( S, outlen, key, keylen );
}
BLAKE2_API int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
{
return blake2s_init_param_ptr( S, P );
}
BLAKE2_API int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen )
{
return blake2s_update_ptr( S, in, inlen );
}
BLAKE2_API int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen )
{
return blake2s_final_ptr( S, out, outlen );
}
BLAKE2_API int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
return blake2s_ptr( out, in, key, outlen, inlen, keylen );
}

File diff suppressed because it is too large Load Diff

View File

@ -1,43 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <string.h>
#include "blake2.h"
#include "blake2-kat.h"
int main( int argc, char **argv )
{
uint8_t key[BLAKE2B_KEYBYTES];
uint8_t buf[KAT_LENGTH];
for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i )
key[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
buf[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
{
uint8_t hash[BLAKE2B_OUTBYTES];
if( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 ||
0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
{
puts( "error" );
return -1;
}
}
puts( "ok" );
return 0;
}

View File

@ -1,44 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <string.h>
#include "blake2.h"
#include "blake2-kat.h"
int main( int argc, char **argv )
{
uint8_t key[BLAKE2B_KEYBYTES];
uint8_t buf[KAT_LENGTH];
for( size_t i = 0; i < BLAKE2B_KEYBYTES; ++i )
key[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
buf[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
{
uint8_t hash[BLAKE2B_OUTBYTES];
if( blake2bp( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 ||
0 != memcmp( hash, blake2bp_keyed_kat[i], BLAKE2B_OUTBYTES ) )
{
puts( "error" );
return -1;
}
}
puts( "ok" );
return 0;
}

View File

@ -1,274 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include "blake2.h"
#include "blake2-impl.h"
#define PARALLELISM_DEGREE 4
static int blake2bp_init_leaf( blake2b_state *S, uint8_t outlen, uint8_t keylen, uint64_t offset )
{
blake2b_param P[1];
P->digest_length = outlen;
P->key_length = keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32(&P->leaf_length, 0);
store64(&P->node_offset, offset);
P->node_depth = 0;
P->inner_length = BLAKE2B_OUTBYTES;
memset( P->reserved, 0, sizeof( P->reserved ) );
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
blake2b_init_param( S, P );
S->outlen = P->inner_length;
return 0;
}
static int blake2bp_init_root( blake2b_state *S, uint8_t outlen, uint8_t keylen )
{
blake2b_param P[1];
P->digest_length = outlen;
P->key_length = keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
store32(&P->leaf_length, 0);
store64(&P->node_offset, 0);
P->node_depth = 1;
P->inner_length = BLAKE2B_OUTBYTES;
memset( P->reserved, 0, sizeof( P->reserved ) );
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
blake2b_init_param( S, P );
S->outlen = P->digest_length;
return 0;
}
int blake2bp_init( blake2bp_state *S, size_t outlen )
{
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
memset( S->buf, 0, sizeof( S->buf ) );
S->buflen = 0;
if( blake2bp_init_root( S->R, ( uint8_t ) outlen, 0 ) < 0 )
return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2bp_init_leaf( S->S[i], ( uint8_t ) outlen, 0, i ) < 0 ) return -1;
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
S->outlen = ( uint8_t ) outlen;
return 0;
}
int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen )
{
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
if( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
memset( S->buf, 0, sizeof( S->buf ) );
S->buflen = 0;
if( blake2bp_init_root( S->R, ( uint8_t ) outlen, ( uint8_t ) keylen ) < 0 )
return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2bp_init_leaf( S->S[i], ( uint8_t ) outlen, ( uint8_t ) keylen, i ) < 0 )
return -1;
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
S->outlen = ( uint8_t ) outlen;
{
uint8_t block[BLAKE2B_BLOCKBYTES];
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2b_update( S->S[i], block, BLAKE2B_BLOCKBYTES );
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
}
return 0;
}
int blake2bp_update( blake2bp_state *S, const uint8_t *in, size_t inlen )
{
size_t left = S->buflen;
size_t fill = sizeof( S->buf ) - left;
if( left && inlen >= fill )
{
memcpy( S->buf + left, in, fill );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2b_update( S->S[i], S->buf + i * BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
in += fill;
inlen -= fill;
left = 0;
}
#if defined(_OPENMP)
omp_set_num_threads(PARALLELISM_DEGREE);
#pragma omp parallel shared(S)
#else
for( size_t id__ = 0; id__ < PARALLELISM_DEGREE; ++id__ )
#endif
{
#if defined(_OPENMP)
size_t id__ = ( size_t ) omp_get_thread_num();
#endif
size_t inlen__ = inlen;
const uint8_t *in__ = ( const uint8_t * )in;
in__ += id__ * BLAKE2B_BLOCKBYTES;
while( inlen__ >= PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES )
{
blake2b_update( S->S[id__], in__, BLAKE2B_BLOCKBYTES );
in__ += PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
inlen__ -= PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
}
}
in += inlen - inlen % ( PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES );
inlen %= PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
if( inlen > 0 )
memcpy( S->buf + left, in, inlen );
S->buflen = ( uint32_t ) left + ( uint32_t ) inlen;
return 0;
}
int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen )
{
uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
if(S->outlen != outlen) return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
{
if( S->buflen > i * BLAKE2B_BLOCKBYTES )
{
size_t left = S->buflen - i * BLAKE2B_BLOCKBYTES;
if( left > BLAKE2B_BLOCKBYTES ) left = BLAKE2B_BLOCKBYTES;
blake2b_update( S->S[i], S->buf + i * BLAKE2B_BLOCKBYTES, left );
}
blake2b_final( S->S[i], hash[i], BLAKE2B_OUTBYTES );
}
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2b_update( S->R, hash[i], BLAKE2B_OUTBYTES );
return blake2b_final( S->R, out, outlen );
}
int blake2bp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
uint8_t hash[PARALLELISM_DEGREE][BLAKE2B_OUTBYTES];
blake2b_state S[PARALLELISM_DEGREE][1];
blake2b_state FS[1];
/* Verify parameters */
if ( NULL == in && inlen > 0 ) return -1;
if ( NULL == out ) return -1;
if ( NULL == key && keylen > 0) return -1;
if( !outlen || outlen > BLAKE2B_OUTBYTES ) return -1;
if( keylen > BLAKE2B_KEYBYTES ) return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2bp_init_leaf( S[i], ( uint8_t ) outlen, ( uint8_t ) keylen, i ) < 0 )
return -1;
S[PARALLELISM_DEGREE - 1]->last_node = 1; // mark last node
if( keylen > 0 )
{
uint8_t block[BLAKE2B_BLOCKBYTES];
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2b_update( S[i], block, BLAKE2B_BLOCKBYTES );
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
}
#if defined(_OPENMP)
omp_set_num_threads(PARALLELISM_DEGREE);
#pragma omp parallel shared(S,hash)
#else
for( size_t id__ = 0; id__ < PARALLELISM_DEGREE; ++id__ )
#endif
{
#if defined(_OPENMP)
size_t id__ = ( size_t ) omp_get_thread_num();
#endif
size_t inlen__ = inlen;
const uint8_t *in__ = ( const uint8_t * )in;
in__ += id__ * BLAKE2B_BLOCKBYTES;
while( inlen__ >= PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES )
{
blake2b_update( S[id__], in__, BLAKE2B_BLOCKBYTES );
in__ += PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
inlen__ -= PARALLELISM_DEGREE * BLAKE2B_BLOCKBYTES;
}
if( inlen__ > id__ * BLAKE2B_BLOCKBYTES )
{
const size_t left = inlen__ - id__ * BLAKE2B_BLOCKBYTES;
const size_t len = left <= BLAKE2B_BLOCKBYTES ? left : BLAKE2B_BLOCKBYTES;
blake2b_update( S[id__], in__, len );
}
blake2b_final( S[id__], hash[id__], BLAKE2B_OUTBYTES );
}
if( blake2bp_init_root( FS, ( uint8_t ) outlen, ( uint8_t ) keylen ) < 0 )
return -1;
FS->last_node = 1; // Mark as last node
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2b_update( FS, hash[i], BLAKE2B_OUTBYTES );
return blake2b_final( FS, out, outlen );
}

View File

@ -1,43 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <string.h>
#include "blake2.h"
#include "blake2-kat.h"
int main( int argc, char **argv )
{
uint8_t key[BLAKE2S_KEYBYTES];
uint8_t buf[KAT_LENGTH];
for( size_t i = 0; i < BLAKE2S_KEYBYTES; ++i )
key[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
buf[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
{
uint8_t hash[BLAKE2S_OUTBYTES];
if( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 ||
0 != memcmp( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
{
puts( "error" );
return -1;
}
}
puts( "ok" );
return 0;
}

View File

@ -1,43 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <string.h>
#include "blake2.h"
#include "blake2-kat.h"
int main( int argc, char **argv )
{
uint8_t key[BLAKE2S_KEYBYTES];
uint8_t buf[KAT_LENGTH];
for( size_t i = 0; i < BLAKE2S_KEYBYTES; ++i )
key[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
buf[i] = ( uint8_t )i;
for( size_t i = 0; i < KAT_LENGTH; ++i )
{
uint8_t hash[BLAKE2S_OUTBYTES];
if( blake2sp( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 ||
0 != memcmp( hash, blake2sp_keyed_kat[i], BLAKE2S_OUTBYTES ) )
{
puts( "error" );
return -1;
}
}
puts( "ok" );
return 0;
}

View File

@ -1,274 +0,0 @@
/*
BLAKE2 reference source code package - optimized C implementations
Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include "blake2.h"
#include "blake2-impl.h"
#define PARALLELISM_DEGREE 8
static int blake2sp_init_leaf( blake2s_state *S, uint8_t outlen, uint8_t keylen, uint64_t offset )
{
blake2s_param P[1];
P->digest_length = outlen;
P->key_length = keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
P->leaf_length = 0;
store48( P->node_offset, offset );
P->node_depth = 0;
P->inner_length = BLAKE2S_OUTBYTES;
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
blake2s_init_param( S, P );
S->outlen = P->inner_length;
return 0;
}
static int blake2sp_init_root( blake2s_state *S, uint8_t outlen, uint8_t keylen )
{
blake2s_param P[1];
P->digest_length = outlen;
P->key_length = keylen;
P->fanout = PARALLELISM_DEGREE;
P->depth = 2;
P->leaf_length = 0;
store48( P->node_offset, 0ULL );
P->node_depth = 1;
P->inner_length = BLAKE2S_OUTBYTES;
memset( P->salt, 0, sizeof( P->salt ) );
memset( P->personal, 0, sizeof( P->personal ) );
blake2s_init_param( S, P );
S->outlen = P->digest_length;
return 0;
}
int blake2sp_init( blake2sp_state *S, size_t outlen )
{
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
memset( S->buf, 0, sizeof( S->buf ) );
S->buflen = 0;
if( blake2sp_init_root( S->R, ( uint8_t ) outlen, 0 ) < 0 )
return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2sp_init_leaf( S->S[i], ( uint8_t ) outlen, 0, i ) < 0 ) return -1;
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
S->outlen = ( uint8_t ) outlen;
return 0;
}
int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen )
{
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
if( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
memset( S->buf, 0, sizeof( S->buf ) );
S->buflen = 0;
if( blake2sp_init_root( S->R, ( uint8_t ) outlen, ( uint8_t ) keylen ) < 0 )
return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2sp_init_leaf( S->S[i], ( uint8_t ) outlen, ( uint8_t ) keylen, i ) < 0 )
return -1;
S->R->last_node = 1;
S->S[PARALLELISM_DEGREE - 1]->last_node = 1;
S->outlen = ( uint8_t ) outlen;
{
uint8_t block[BLAKE2S_BLOCKBYTES];
memset( block, 0, BLAKE2S_BLOCKBYTES );
memcpy( block, key, keylen );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2s_update( S->S[i], block, BLAKE2S_BLOCKBYTES );
secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
}
return 0;
}
int blake2sp_update( blake2sp_state *S, const uint8_t *in, size_t inlen )
{
size_t left = S->buflen;
size_t fill = sizeof( S->buf ) - left;
if( left && inlen >= fill )
{
memcpy( S->buf + left, in, fill );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2s_update( S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
in += fill;
inlen -= fill;
left = 0;
}
#if defined(_OPENMP)
omp_set_num_threads(PARALLELISM_DEGREE);
#pragma omp parallel shared(S)
#else
for( size_t id__ = 0; id__ < PARALLELISM_DEGREE; ++id__ )
#endif
{
#if defined(_OPENMP)
size_t id__ = ( size_t ) omp_get_thread_num();
#endif
size_t inlen__ = inlen;
const uint8_t *in__ = ( const uint8_t * )in;
in__ += id__ * BLAKE2S_BLOCKBYTES;
while( inlen__ >= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES )
{
blake2s_update( S->S[id__], in__, BLAKE2S_BLOCKBYTES );
in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
}
}
in += inlen - inlen % ( PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES );
inlen %= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
if( inlen > 0 )
memcpy( S->buf + left, in, inlen );
S->buflen = ( uint32_t ) left + ( uint32_t ) inlen;
return 0;
}
int blake2sp_final( blake2sp_state *S, uint8_t *out, size_t outlen )
{
uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
if(S->outlen != outlen) return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
{
if( S->buflen > i * BLAKE2S_BLOCKBYTES )
{
size_t left = S->buflen - i * BLAKE2S_BLOCKBYTES;
if( left > BLAKE2S_BLOCKBYTES ) left = BLAKE2S_BLOCKBYTES;
blake2s_update( S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, left );
}
blake2s_final( S->S[i], hash[i], BLAKE2S_OUTBYTES );
}
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2s_update( S->R, hash[i], BLAKE2S_OUTBYTES );
blake2s_final( S->R, out, outlen );
return 0;
}
int blake2sp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
{
uint8_t hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
blake2s_state S[PARALLELISM_DEGREE][1];
blake2s_state FS[1];
/* Verify parameters */
if ( NULL == in && inlen > 0 ) return -1;
if ( NULL == out ) return -1;
if ( NULL == key && keylen > 0 ) return -1;
if( !outlen || outlen > BLAKE2S_OUTBYTES ) return -1;
if( keylen > BLAKE2S_KEYBYTES ) return -1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
if( blake2sp_init_leaf( S[i], ( uint8_t ) outlen, ( uint8_t ) keylen, i ) < 0 )
return -1;
S[PARALLELISM_DEGREE - 1]->last_node = 1; // mark last node
if( keylen > 0 )
{
uint8_t block[BLAKE2S_BLOCKBYTES];
memset( block, 0, BLAKE2S_BLOCKBYTES );
memcpy( block, key, keylen );
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2s_update( S[i], block, BLAKE2S_BLOCKBYTES );
secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from stack */
}
#if defined(_OPENMP)
omp_set_num_threads(PARALLELISM_DEGREE);
#pragma omp parallel shared(S,hash)
#else
for( size_t id__ = 0; id__ < PARALLELISM_DEGREE; ++id__ )
#endif
{
#if defined(_OPENMP)
size_t id__ = ( size_t ) omp_get_thread_num();
#endif
size_t inlen__ = inlen;
const uint8_t *in__ = ( const uint8_t * )in;
in__ += id__ * BLAKE2S_BLOCKBYTES;
while( inlen__ >= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES )
{
blake2s_update( S[id__], in__, BLAKE2S_BLOCKBYTES );
in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
}
if( inlen__ > id__ * BLAKE2S_BLOCKBYTES )
{
const size_t left = inlen__ - id__ * BLAKE2S_BLOCKBYTES;
const size_t len = left <= BLAKE2S_BLOCKBYTES ? left : BLAKE2S_BLOCKBYTES;
blake2s_update( S[id__], in__, len );
}
blake2s_final( S[id__], hash[id__], BLAKE2S_OUTBYTES );
}
if( blake2sp_init_root( FS, ( uint8_t ) outlen, ( uint8_t ) keylen ) < 0 )
return -1;
FS->last_node = 1;
for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
blake2s_update( FS, hash[i], BLAKE2S_OUTBYTES );
return blake2s_final( FS, out, outlen );
}

94
configure generated vendored
View File

@ -777,6 +777,8 @@ MODULE__IO_TRUE
MODULES_SETUP_STDLIB
MODULE_BUILDTYPE
TEST_MODULES
LIBB2_LIBS
LIBB2_CFLAGS
OPENSSL_RPATH
OPENSSL_LDFLAGS
OPENSSL_LIBS
@ -1084,7 +1086,9 @@ BZIP2_LIBS
LIBLZMA_CFLAGS
LIBLZMA_LIBS
LIBCRYPT_CFLAGS
LIBCRYPT_LIBS'
LIBCRYPT_LIBS
LIBB2_CFLAGS
LIBB2_LIBS'
# Initialize some variables set by options.
@ -1891,6 +1895,9 @@ Some influential environment variables:
C compiler flags for LIBCRYPT, overriding pkg-config
LIBCRYPT_LIBS
linker flags for LIBCRYPT, overriding pkg-config
LIBB2_CFLAGS
C compiler flags for LIBB2, overriding pkg-config
LIBB2_LIBS linker flags for LIBB2, overriding pkg-config
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
@ -21340,6 +21347,87 @@ esac
done
IFS=$as_save_IFS
if test "x$with_builtin_blake2" = xyes; then :
pkg_failed=no
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for LIBB2" >&5
$as_echo_n "checking for LIBB2... " >&6; }
if test -n "$LIBB2_CFLAGS"; then
pkg_cv_LIBB2_CFLAGS="$LIBB2_CFLAGS"
elif test -n "$PKG_CONFIG"; then
if test -n "$PKG_CONFIG" && \
{ { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libb2\""; } >&5
($PKG_CONFIG --exists --print-errors "libb2") 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; then
pkg_cv_LIBB2_CFLAGS=`$PKG_CONFIG --cflags "libb2" 2>/dev/null`
test "x$?" != "x0" && pkg_failed=yes
else
pkg_failed=yes
fi
else
pkg_failed=untried
fi
if test -n "$LIBB2_LIBS"; then
pkg_cv_LIBB2_LIBS="$LIBB2_LIBS"
elif test -n "$PKG_CONFIG"; then
if test -n "$PKG_CONFIG" && \
{ { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libb2\""; } >&5
($PKG_CONFIG --exists --print-errors "libb2") 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; then
pkg_cv_LIBB2_LIBS=`$PKG_CONFIG --libs "libb2" 2>/dev/null`
test "x$?" != "x0" && pkg_failed=yes
else
pkg_failed=yes
fi
else
pkg_failed=untried
fi
if test $pkg_failed = yes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
_pkg_short_errors_supported=yes
else
_pkg_short_errors_supported=no
fi
if test $_pkg_short_errors_supported = yes; then
LIBB2_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libb2" 2>&1`
else
LIBB2_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libb2" 2>&1`
fi
# Put the nasty error message in config.log where it belongs
echo "$LIBB2_PKG_ERRORS" >&5
have_libb2=no
elif test $pkg_failed = untried; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
have_libb2=no
else
LIBB2_CFLAGS=$pkg_cv_LIBB2_CFLAGS
LIBB2_LIBS=$pkg_cv_LIBB2_LIBS
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
have_libb2=yes
$as_echo "#define HAVE_LIBB2 1" >>confdefs.h
fi
fi
# --with-experimental-isolated-subinterpreters
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for --with-experimental-isolated-subinterpreters" >&5
@ -22915,8 +23003,8 @@ fi
as_fn_append MODULE_BLOCK "MODULE__BLAKE2=$py_cv_module__blake2$as_nl"
if test "x$py_cv_module__blake2" = xyes; then :
as_fn_append MODULE_BLOCK "MODULE__BLAKE2_CFLAGS=$LIBB2_CFLAGS$as_nl"
as_fn_append MODULE_BLOCK "MODULE__BLAKE2_LDFLAGS=$LIBB2_LIBS$as_nl"
fi
if test "$py_cv_module__blake2" = yes; then

View File

@ -6391,6 +6391,15 @@ for builtin_hash in $with_builtin_hashlib_hashes; do
done
IFS=$as_save_IFS
dnl libb2 for blake2. _blake2 module falls back to vendored copy.
AS_VAR_IF([with_builtin_blake2], [yes], [
PKG_CHECK_MODULES([LIBB2], [libb2], [
have_libb2=yes
AC_DEFINE([HAVE_LIBB2], [1],
[Define to 1 if you want to build _blake2 module with libb2])
], [have_libb2=no])
])
# --with-experimental-isolated-subinterpreters
AH_TEMPLATE(EXPERIMENTAL_ISOLATED_SUBINTERPRETERS,
[Better isolate subinterpreters, experimental build mode.])
@ -6668,7 +6677,9 @@ PY_STDLIB_MOD([_sha1], [test "$with_builtin_sha1" = yes])
PY_STDLIB_MOD([_sha256], [test "$with_builtin_sha256" = yes])
PY_STDLIB_MOD([_sha512], [test "$with_builtin_sha512" = yes])
PY_STDLIB_MOD([_sha3], [test "$with_builtin_sha3" = yes])
PY_STDLIB_MOD([_blake2], [test "$with_builtin_blake2" = yes])
PY_STDLIB_MOD([_blake2],
[test "$with_builtin_blake2" = yes], [],
[$LIBB2_CFLAGS], [$LIBB2_LIBS])
PY_STDLIB_MOD([_crypt],
[], [test "$ac_cv_crypt_crypt" = yes],

View File

@ -622,6 +622,9 @@
/* Define to 1 if you have the `lchown' function. */
#undef HAVE_LCHOWN
/* Define to 1 if you want to build _blake2 module with libb2 */
#undef HAVE_LIBB2
/* Define to 1 if you have the `db' library (-ldb). */
#undef HAVE_LIBDB