svn commit: r331312 - in head: sys/conf sys/contrib/libb2 sys/crypto/blake2 sys/modules sys/modules/blake2 sys/modules/crypto sys/opencrypto tests/sys/opencrypto

Conrad Meyer cem at FreeBSD.org
Wed Mar 21 16:18:16 UTC 2018


Author: cem
Date: Wed Mar 21 16:18:14 2018
New Revision: 331312
URL: https://svnweb.freebsd.org/changeset/base/331312

Log:
  Import Blake2 algorithms (blake2b, blake2s) from libb2
  
  The upstream repository is on github BLAKE2/libb2.  Files landed in
  sys/contrib/libb2 are the unmodified upstream files, except for one
  difference:  secure_zero_memory's contents have been replaced with
  explicit_bzero() only because the previous implementation broke powerpc
  link.  Preferential use of explicit_bzero() is in progress upstream, so
  it is anticipated we will be able to drop this diff in the future.
  
  sys/crypto/blake2 contains the source files needed to port libb2 to our
  build system, a wrapped (limited) variant of the algorithm to match the API
  of our auth_transform softcrypto abstraction, incorporation into the Open
  Crypto Framework (OCF) cryptosoft(4) driver, as well as an x86 SSE/AVX
  accelerated OCF driver, blake2(4).
  
  Optimized variants of blake2 are compiled for a number of x86 machines
  (anything from SSE2 to AVX + XOP).  On those machines, FPU context will need
  to be explicitly saved before using blake2(4)-provided algorithms directly.
  Use via cryptodev / OCF saves FPU state automatically, and use via the
  auth_transform softcrypto abstraction does not use FPU.
  
  The intent of the OCF driver is mostly to enable testing in userspace via
  /dev/crypto.  ATF tests are added with published KAT test vectors to
  validate correctness.
  
  Reviewed by:	jhb, markj
  Obtained from:	github BLAKE2/libb2
  Differential Revision:	https://reviews.freebsd.org/D14662

Added:
  head/sys/contrib/libb2/
  head/sys/contrib/libb2/blake2-config.h   (contents, props changed)
  head/sys/contrib/libb2/blake2-dispatch.c   (contents, props changed)
  head/sys/contrib/libb2/blake2-impl.h   (contents, props changed)
  head/sys/contrib/libb2/blake2.h   (contents, props changed)
  head/sys/contrib/libb2/blake2b-load-sse2.h   (contents, props changed)
  head/sys/contrib/libb2/blake2b-load-sse41.h   (contents, props changed)
  head/sys/contrib/libb2/blake2b-ref.c   (contents, props changed)
  head/sys/contrib/libb2/blake2b-round.h   (contents, props changed)
  head/sys/contrib/libb2/blake2b.c   (contents, props changed)
  head/sys/contrib/libb2/blake2bp.c   (contents, props changed)
  head/sys/contrib/libb2/blake2s-load-sse2.h   (contents, props changed)
  head/sys/contrib/libb2/blake2s-load-sse41.h   (contents, props changed)
  head/sys/contrib/libb2/blake2s-load-xop.h   (contents, props changed)
  head/sys/contrib/libb2/blake2s-ref.c   (contents, props changed)
  head/sys/contrib/libb2/blake2s-round.h   (contents, props changed)
  head/sys/contrib/libb2/blake2s.c   (contents, props changed)
  head/sys/contrib/libb2/blake2sp.c   (contents, props changed)
  head/sys/crypto/blake2/
  head/sys/crypto/blake2/blake2-sw.c   (contents, props changed)
  head/sys/crypto/blake2/blake2_cryptodev.c   (contents, props changed)
  head/sys/crypto/blake2/blake2_kfreebsd.h   (contents, props changed)
  head/sys/crypto/blake2/blake2b-avx.c   (contents, props changed)
  head/sys/crypto/blake2/blake2b-sse2.c   (contents, props changed)
  head/sys/crypto/blake2/blake2b-sse41.c   (contents, props changed)
  head/sys/crypto/blake2/blake2b-ssse3.c   (contents, props changed)
  head/sys/crypto/blake2/blake2b-xop.c   (contents, props changed)
  head/sys/crypto/blake2/blake2s-avx.c   (contents, props changed)
  head/sys/crypto/blake2/blake2s-sse2.c   (contents, props changed)
  head/sys/crypto/blake2/blake2s-sse41.c   (contents, props changed)
  head/sys/crypto/blake2/blake2s-ssse3.c   (contents, props changed)
  head/sys/crypto/blake2/blake2s-xop.c   (contents, props changed)
  head/sys/crypto/blake2/config.h   (contents, props changed)
  head/sys/crypto/blake2/stddef.h   (contents, props changed)
  head/sys/crypto/blake2/stdint.h   (contents, props changed)
  head/sys/crypto/blake2/stdio.h   (contents, props changed)
  head/sys/crypto/blake2/stdlib.h   (contents, props changed)
  head/sys/crypto/blake2/string.h   (contents, props changed)
  head/sys/modules/blake2/
  head/sys/modules/blake2/Makefile   (contents, props changed)
  head/tests/sys/opencrypto/blake2-kat.h   (contents, props changed)
  head/tests/sys/opencrypto/blake2_test.c   (contents, props changed)
Modified:
  head/sys/conf/files
  head/sys/modules/Makefile
  head/sys/modules/crypto/Makefile
  head/sys/opencrypto/cryptodev.c
  head/sys/opencrypto/cryptodev.h
  head/sys/opencrypto/cryptosoft.c
  head/sys/opencrypto/xform_auth.h
  head/tests/sys/opencrypto/Makefile

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files	Wed Mar 21 16:12:07 2018	(r331311)
+++ head/sys/conf/files	Wed Mar 21 16:18:14 2018	(r331312)
@@ -648,6 +648,13 @@ contrib/zstd/lib/compress/zstd_ldm.c		standard compile
 contrib/zstd/lib/compress/zstd_opt.c		standard compile-with ${ZSTD_C}
 contrib/zstd/lib/decompress/zstd_decompress.c	standard compile-with ${ZSTD_C}
 contrib/zstd/lib/decompress/huf_decompress.c	standard compile-with ${ZSTD_C}
+# Blake 2
+contrib/libb2/blake2b-ref.c	optional crypto | ipsec | ipsec_support \
+	compile-with "${NORMAL_C} -I$S/crypto/blake2 ${NO_WCAST_QUAL} -DSUFFIX=_ref"
+contrib/libb2/blake2s-ref.c	optional crypto | ipsec | ipsec_support \
+	compile-with "${NORMAL_C} -I$S/crypto/blake2 ${NO_WCAST_QUAL} -DSUFFIX=_ref"
+crypto/blake2/blake2-sw.c	optional crypto | ipsec | ipsec_support \
+	compile-with "${NORMAL_C} -I$S/crypto/blake2 ${NO_WCAST_QUAL}"
 crypto/blowfish/bf_ecb.c	optional ipsec | ipsec_support
 crypto/blowfish/bf_skey.c	optional crypto | ipsec | ipsec_support
 crypto/camellia/camellia.c	optional crypto | ipsec | ipsec_support

Added: head/sys/contrib/libb2/blake2-config.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/contrib/libb2/blake2-config.h	Wed Mar 21 16:18:14 2018	(r331312)
@@ -0,0 +1,71 @@
+/*
+   BLAKE2 reference source code package - optimized C implementations
+
+   Written in 2012 by Samuel Neves <sneves at 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/>.
+*/
+#pragma once
+#ifndef __BLAKE2_CONFIG_H__
+#define __BLAKE2_CONFIG_H__
+
+#if defined(__SSE2__)
+#define HAVE_SSE2
+#endif
+
+#if defined(__SSSE3__)
+#define HAVE_SSSE3
+#endif
+
+#if defined(__SSE4_1__)
+#define HAVE_SSE4_1
+#endif
+
+#if defined(__AVX__)
+#define HAVE_AVX
+#endif
+
+#if defined(__XOP__)
+#define HAVE_XOP
+#endif
+
+
+#ifdef HAVE_AVX2
+#ifndef HAVE_AVX
+#define HAVE_AVX
+#endif
+#endif
+
+#ifdef HAVE_XOP
+#ifndef HAVE_AVX
+#define HAVE_AVX
+#endif
+#endif
+
+#ifdef HAVE_AVX
+#ifndef HAVE_SSE4_1
+#define HAVE_SSE4_1
+#endif
+#endif
+
+#ifdef HAVE_SSE41
+#ifndef HAVE_SSSE3
+#define HAVE_SSSE3
+#endif
+#endif
+
+#ifdef HAVE_SSSE3
+#define HAVE_SSE2
+#endif
+
+#if !defined(HAVE_SSE2)
+#error "This code requires at least SSE2."
+#endif
+
+#endif
+

Added: head/sys/contrib/libb2/blake2-dispatch.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/contrib/libb2/blake2-dispatch.c	Wed Mar 21 16:18:14 2018	(r331312)
@@ -0,0 +1,577 @@
+/*
+   BLAKE2 reference source code package - optimized C implementations
+
+   Written in 2012 by Samuel Neves <sneves at 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 );
+}
+

Added: head/sys/contrib/libb2/blake2-impl.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/contrib/libb2/blake2-impl.h	Wed Mar 21 16:18:14 2018	(r331312)
@@ -0,0 +1,143 @@
+/*
+   BLAKE2 reference source code package - optimized C implementations
+
+   Written in 2012 by Samuel Neves <sneves at 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/>.
+*/
+#pragma once
+#ifndef __BLAKE2_IMPL_H__
+#define __BLAKE2_IMPL_H__
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include "config.h"
+
+#define BLAKE2_IMPL_CAT(x,y) x ## y
+#define BLAKE2_IMPL_EVAL(x,y)  BLAKE2_IMPL_CAT(x,y)
+#define BLAKE2_IMPL_NAME(fun)  BLAKE2_IMPL_EVAL(fun, SUFFIX)
+
+static inline uint32_t load32( const void *src )
+{
+#if defined(NATIVE_LITTLE_ENDIAN) && !defined(HAVE_ALIGNED_ACCESS_REQUIRED)
+  return *( uint32_t * )( src );
+#else
+  const uint8_t *p = ( uint8_t * )src;
+  uint32_t w = *p++;
+  w |= ( uint32_t )( *p++ ) <<  8;
+  w |= ( uint32_t )( *p++ ) << 16;
+  w |= ( uint32_t )( *p++ ) << 24;
+  return w;
+#endif
+}
+
+static inline uint64_t load64( const void *src )
+{
+#if defined(NATIVE_LITTLE_ENDIAN) && !defined(HAVE_ALIGNED_ACCESS_REQUIRED)
+  return *( uint64_t * )( src );
+#else
+  const uint8_t *p = ( uint8_t * )src;
+  uint64_t w = *p++;
+  w |= ( uint64_t )( *p++ ) <<  8;
+  w |= ( uint64_t )( *p++ ) << 16;
+  w |= ( uint64_t )( *p++ ) << 24;
+  w |= ( uint64_t )( *p++ ) << 32;
+  w |= ( uint64_t )( *p++ ) << 40;
+  w |= ( uint64_t )( *p++ ) << 48;
+  w |= ( uint64_t )( *p++ ) << 56;
+  return w;
+#endif
+}
+
+static inline void store32( void *dst, uint32_t w )
+{
+#if defined(NATIVE_LITTLE_ENDIAN) && !defined(HAVE_ALIGNED_ACCESS_REQUIRED)
+  *( uint32_t * )( dst ) = w;
+#else
+  uint8_t *p = ( uint8_t * )dst;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w;
+#endif
+}
+
+static inline void store64( void *dst, uint64_t w )
+{
+#if defined(NATIVE_LITTLE_ENDIAN) && !defined(HAVE_ALIGNED_ACCESS_REQUIRED)
+  *( uint64_t * )( dst ) = w;
+#else
+  uint8_t *p = ( uint8_t * )dst;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w;
+#endif
+}
+
+static inline uint64_t load48( const void *src )
+{
+  const uint8_t *p = ( const uint8_t * )src;
+  uint64_t w = *p++;
+  w |= ( uint64_t )( *p++ ) <<  8;
+  w |= ( uint64_t )( *p++ ) << 16;
+  w |= ( uint64_t )( *p++ ) << 24;
+  w |= ( uint64_t )( *p++ ) << 32;
+  w |= ( uint64_t )( *p++ ) << 40;
+  return w;
+}
+
+static inline void store48( void *dst, uint64_t w )
+{
+  uint8_t *p = ( uint8_t * )dst;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w; w >>= 8;
+  *p++ = ( uint8_t )w;
+}
+
+static inline uint32_t rotl32( const uint32_t w, const unsigned c )
+{
+  return ( w << c ) | ( w >> ( 32 - c ) );
+}
+
+static inline uint64_t rotl64( const uint64_t w, const unsigned c )
+{
+  return ( w << c ) | ( w >> ( 64 - c ) );
+}
+
+static inline uint32_t rotr32( const uint32_t w, const unsigned c )
+{
+  return ( w >> c ) | ( w << ( 32 - c ) );
+}
+
+static inline uint64_t rotr64( const uint64_t w, const unsigned c )
+{
+  return ( w >> c ) | ( w << ( 64 - c ) );
+}
+
+/* prevents compiler optimizing out memset() */
+static inline void secure_zero_memory(void *v, size_t n)
+{
+#ifdef __FreeBSD__
+  explicit_bzero(v, n);
+#else
+  static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
+  memset_v(v, 0, n);
+#endif
+}
+
+#endif
+

Added: head/sys/contrib/libb2/blake2.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/contrib/libb2/blake2.h	Wed Mar 21 16:18:14 2018	(r331312)
@@ -0,0 +1,182 @@
+/*
+   BLAKE2 reference source code package - optimized C implementations
+
+   Written in 2012 by Samuel Neves <sneves at 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/>.
+*/
+#pragma once
+#ifndef __BLAKE2_H__
+#define __BLAKE2_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+    #define BLAKE2_DLL_IMPORT __declspec(dllimport)
+    #define BLAKE2_DLL_EXPORT __declspec(dllexport)
+    #define BLAKE2_DLL_PRIVATE
+#elif __GNUC__ >= 4
+  #define BLAKE2_DLL_IMPORT   __attribute__ ((visibility ("default")))
+  #define BLAKE2_DLL_EXPORT   __attribute__ ((visibility ("default")))
+  #define BLAKE2_DLL_PRIVATE  __attribute__ ((visibility ("hidden")))
+#else
+  #define BLAKE2_DLL_IMPORT
+  #define BLAKE2_DLL_EXPORT
+  #define BLAKE2_DLL_PRIVATE
+#endif
+
+#if defined(BLAKE2_DLL)
+  #if defined(BLAKE2_DLL_EXPORTS) // defined if we are building the DLL
+    #define BLAKE2_API BLAKE2_DLL_EXPORT
+  #else
+    #define BLAKE2_API BLAKE2_DLL_IMPORT
+  #endif
+  #define BLAKE2_PRIVATE BLAKE2_DLL_PRIVATE // must only be used by hidden logic
+#else
+  #define BLAKE2_API
+  #define BLAKE2_PRIVATE
+#endif
+
+#if defined(__cplusplus)
+extern "C" {
+#elif defined(_MSC_VER) && !defined(inline)
+#define inline __inline
+#endif
+
+  enum blake2s_constant
+  {
+    BLAKE2S_BLOCKBYTES = 64,
+    BLAKE2S_OUTBYTES   = 32,
+    BLAKE2S_KEYBYTES   = 32,
+    BLAKE2S_SALTBYTES  = 8,
+    BLAKE2S_PERSONALBYTES = 8
+  };
+
+  enum blake2b_constant
+  {
+    BLAKE2B_BLOCKBYTES = 128,
+    BLAKE2B_OUTBYTES   = 64,
+    BLAKE2B_KEYBYTES   = 64,
+    BLAKE2B_SALTBYTES  = 16,
+    BLAKE2B_PERSONALBYTES = 16
+  };
+
+#pragma pack(push, 1)
+  typedef struct __blake2s_param
+  {
+    uint8_t  digest_length; // 1
+    uint8_t  key_length;    // 2
+    uint8_t  fanout;        // 3
+    uint8_t  depth;         // 4
+    uint32_t leaf_length;   // 8
+    uint8_t  node_offset[6];// 14
+    uint8_t  node_depth;    // 15
+    uint8_t  inner_length;  // 16
+    // uint8_t  reserved[0];
+    uint8_t  salt[BLAKE2S_SALTBYTES]; // 24
+    uint8_t  personal[BLAKE2S_PERSONALBYTES];  // 32
+  } blake2s_param;
+
+  typedef struct __blake2s_state
+  {
+    uint32_t h[8];
+    uint32_t t[2];
+    uint32_t f[2];
+    uint8_t  buf[2 * BLAKE2S_BLOCKBYTES];
+    uint32_t buflen;
+    uint8_t  outlen;
+    uint8_t  last_node;
+  } blake2s_state;
+
+  typedef struct __blake2b_param
+  {
+    uint8_t  digest_length; // 1
+    uint8_t  key_length;    // 2
+    uint8_t  fanout;        // 3
+    uint8_t  depth;         // 4
+    uint32_t leaf_length;   // 8
+    uint64_t node_offset;   // 16
+    uint8_t  node_depth;    // 17
+    uint8_t  inner_length;  // 18
+    uint8_t  reserved[14];  // 32
+    uint8_t  salt[BLAKE2B_SALTBYTES]; // 48
+    uint8_t  personal[BLAKE2B_PERSONALBYTES];  // 64
+  } blake2b_param;
+
+  typedef struct __blake2b_state
+  {
+    uint64_t h[8];
+    uint64_t t[2];
+    uint64_t f[2];
+    uint8_t  buf[2 * BLAKE2B_BLOCKBYTES];
+    uint32_t buflen;
+    uint8_t  outlen;
+    uint8_t  last_node;
+  } blake2b_state;
+
+  typedef struct __blake2sp_state
+  {
+    blake2s_state S[8][1];
+    blake2s_state R[1];
+    uint8_t  buf[8 * BLAKE2S_BLOCKBYTES];
+    uint32_t buflen;
+    uint8_t  outlen;
+  } blake2sp_state;
+
+  typedef struct __blake2bp_state
+  {
+    blake2b_state S[4][1];
+    blake2b_state R[1];
+    uint8_t  buf[4 * BLAKE2B_BLOCKBYTES];
+    uint32_t buflen;
+    uint8_t  outlen;
+  } blake2bp_state;
+#pragma pack(pop)
+
+  // Streaming API
+  BLAKE2_API int blake2s_init( blake2s_state *S, size_t outlen );
+  BLAKE2_API int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
+  BLAKE2_API int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
+  BLAKE2_API int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen );
+  BLAKE2_API int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen );
+
+  BLAKE2_API int blake2b_init( blake2b_state *S, size_t outlen );
+  BLAKE2_API int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
+  BLAKE2_API int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
+  BLAKE2_API int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen );
+  BLAKE2_API int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen );
+
+  BLAKE2_API int blake2sp_init( blake2sp_state *S, size_t outlen );
+  BLAKE2_API int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
+  BLAKE2_API int blake2sp_update( blake2sp_state *S, const uint8_t *in, size_t inlen );
+  BLAKE2_API int blake2sp_final( blake2sp_state *S, uint8_t *out, size_t outlen );
+
+  BLAKE2_API int blake2bp_init( blake2bp_state *S, size_t outlen );
+  BLAKE2_API int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
+  BLAKE2_API int blake2bp_update( blake2bp_state *S, const uint8_t *in, size_t inlen );
+  BLAKE2_API int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen );
+
+  // Simple API
+  BLAKE2_API int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
+  BLAKE2_API int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
+
+  BLAKE2_API int blake2sp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
+  BLAKE2_API int blake2bp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
+
+  static inline int blake2( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
+  {
+    return blake2b( out, in, key, outlen, inlen, keylen );
+  }
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-all mailing list