mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
bpo-47095: Use libb2 to provide blake2 implementation (GH-32059)
This commit is contained in:
parent
c23ddf5ec2
commit
b16b6bb8da
19 changed files with 149 additions and 17791 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue