Reuse macros

This commit is contained in:
Frank Denis
2017-02-23 09:24:15 +01:00
parent c06418a382
commit a3d7bc065e
12 changed files with 153 additions and 212 deletions
-1
View File
@@ -20,7 +20,6 @@ libsodium_la_SOURCES = \
crypto_core/salsa20/core_salsa20.c \
crypto_generichash/crypto_generichash.c \
crypto_generichash/blake2b/generichash_blake2.c \
crypto_generichash/blake2b/ref/blake2-impl.h \
crypto_generichash/blake2b/ref/blake2.h \
crypto_generichash/blake2b/ref/blake2b-compress-ref.c \
crypto_generichash/blake2b/ref/blake2b-load-sse2.h \
@@ -13,12 +13,6 @@ Public domain.
#define ROUNDS 20
#define U32C(v) (v##U)
static uint32_t
rotate(uint32_t u, int c)
{
return (u << c) | (u >> (32 - c));
}
int
crypto_core_hsalsa20(unsigned char *out,
const unsigned char *in,
@@ -54,38 +48,38 @@ crypto_core_hsalsa20(unsigned char *out,
x9 = LOAD32_LE(in + 12);
for (i = ROUNDS; i > 0; i -= 2) {
x4 ^= rotate(x0 + x12, 7);
x8 ^= rotate(x4 + x0, 9);
x12 ^= rotate(x8 + x4, 13);
x0 ^= rotate(x12 + x8, 18);
x9 ^= rotate(x5 + x1, 7);
x13 ^= rotate(x9 + x5, 9);
x1 ^= rotate(x13 + x9, 13);
x5 ^= rotate(x1 + x13, 18);
x14 ^= rotate(x10 + x6, 7);
x2 ^= rotate(x14 + x10, 9);
x6 ^= rotate(x2 + x14, 13);
x10 ^= rotate(x6 + x2, 18);
x3 ^= rotate(x15 + x11, 7);
x7 ^= rotate(x3 + x15, 9);
x11 ^= rotate(x7 + x3, 13);
x15 ^= rotate(x11 + x7, 18);
x1 ^= rotate(x0 + x3, 7);
x2 ^= rotate(x1 + x0, 9);
x3 ^= rotate(x2 + x1, 13);
x0 ^= rotate(x3 + x2, 18);
x6 ^= rotate(x5 + x4, 7);
x7 ^= rotate(x6 + x5, 9);
x4 ^= rotate(x7 + x6, 13);
x5 ^= rotate(x4 + x7, 18);
x11 ^= rotate(x10 + x9, 7);
x8 ^= rotate(x11 + x10, 9);
x9 ^= rotate(x8 + x11, 13);
x10 ^= rotate(x9 + x8, 18);
x12 ^= rotate(x15 + x14, 7);
x13 ^= rotate(x12 + x15, 9);
x14 ^= rotate(x13 + x12, 13);
x15 ^= rotate(x14 + x13, 18);
x4 ^= ROTL32(x0 + x12, 7);
x8 ^= ROTL32(x4 + x0, 9);
x12 ^= ROTL32(x8 + x4, 13);
x0 ^= ROTL32(x12 + x8, 18);
x9 ^= ROTL32(x5 + x1, 7);
x13 ^= ROTL32(x9 + x5, 9);
x1 ^= ROTL32(x13 + x9, 13);
x5 ^= ROTL32(x1 + x13, 18);
x14 ^= ROTL32(x10 + x6, 7);
x2 ^= ROTL32(x14 + x10, 9);
x6 ^= ROTL32(x2 + x14, 13);
x10 ^= ROTL32(x6 + x2, 18);
x3 ^= ROTL32(x15 + x11, 7);
x7 ^= ROTL32(x3 + x15, 9);
x11 ^= ROTL32(x7 + x3, 13);
x15 ^= ROTL32(x11 + x7, 18);
x1 ^= ROTL32(x0 + x3, 7);
x2 ^= ROTL32(x1 + x0, 9);
x3 ^= ROTL32(x2 + x1, 13);
x0 ^= ROTL32(x3 + x2, 18);
x6 ^= ROTL32(x5 + x4, 7);
x7 ^= ROTL32(x6 + x5, 9);
x4 ^= ROTL32(x7 + x6, 13);
x5 ^= ROTL32(x4 + x7, 18);
x11 ^= ROTL32(x10 + x9, 7);
x8 ^= ROTL32(x11 + x10, 9);
x9 ^= ROTL32(x8 + x11, 13);
x10 ^= ROTL32(x9 + x8, 18);
x12 ^= ROTL32(x15 + x14, 7);
x13 ^= ROTL32(x12 + x15, 9);
x14 ^= ROTL32(x13 + x12, 13);
x15 ^= ROTL32(x14 + x13, 18);
}
STORE32_LE(out + 0, x0);
@@ -13,12 +13,6 @@ Public domain.
#define ROUNDS 20
#define U32C(v) (v##U)
static uint32_t
rotate(uint32_t u, int c)
{
return (u << c) | (u >> (32 - c));
}
int
crypto_core_salsa20(unsigned char *out,
const unsigned char *in,
@@ -56,38 +50,38 @@ crypto_core_salsa20(unsigned char *out,
j14 = x14 = LOAD32_LE(k + 28);
for (i = ROUNDS; i > 0; i -= 2) {
x4 ^= rotate(x0 + x12, 7);
x8 ^= rotate(x4 + x0, 9);
x12 ^= rotate(x8 + x4, 13);
x0 ^= rotate(x12 + x8, 18);
x9 ^= rotate(x5 + x1, 7);
x13 ^= rotate(x9 + x5, 9);
x1 ^= rotate(x13 + x9, 13);
x5 ^= rotate(x1 + x13, 18);
x14 ^= rotate(x10 + x6, 7);
x2 ^= rotate(x14 + x10, 9);
x6 ^= rotate(x2 + x14, 13);
x10 ^= rotate(x6 + x2, 18);
x3 ^= rotate(x15 + x11, 7);
x7 ^= rotate(x3 + x15, 9);
x11 ^= rotate(x7 + x3, 13);
x15 ^= rotate(x11 + x7, 18);
x1 ^= rotate(x0 + x3, 7);
x2 ^= rotate(x1 + x0, 9);
x3 ^= rotate(x2 + x1, 13);
x0 ^= rotate(x3 + x2, 18);
x6 ^= rotate(x5 + x4, 7);
x7 ^= rotate(x6 + x5, 9);
x4 ^= rotate(x7 + x6, 13);
x5 ^= rotate(x4 + x7, 18);
x11 ^= rotate(x10 + x9, 7);
x8 ^= rotate(x11 + x10, 9);
x9 ^= rotate(x8 + x11, 13);
x10 ^= rotate(x9 + x8, 18);
x12 ^= rotate(x15 + x14, 7);
x13 ^= rotate(x12 + x15, 9);
x14 ^= rotate(x13 + x12, 13);
x15 ^= rotate(x14 + x13, 18);
x4 ^= ROTL32(x0 + x12, 7);
x8 ^= ROTL32(x4 + x0, 9);
x12 ^= ROTL32(x8 + x4, 13);
x0 ^= ROTL32(x12 + x8, 18);
x9 ^= ROTL32(x5 + x1, 7);
x13 ^= ROTL32(x9 + x5, 9);
x1 ^= ROTL32(x13 + x9, 13);
x5 ^= ROTL32(x1 + x13, 18);
x14 ^= ROTL32(x10 + x6, 7);
x2 ^= ROTL32(x14 + x10, 9);
x6 ^= ROTL32(x2 + x14, 13);
x10 ^= ROTL32(x6 + x2, 18);
x3 ^= ROTL32(x15 + x11, 7);
x7 ^= ROTL32(x3 + x15, 9);
x11 ^= ROTL32(x7 + x3, 13);
x15 ^= ROTL32(x11 + x7, 18);
x1 ^= ROTL32(x0 + x3, 7);
x2 ^= ROTL32(x1 + x0, 9);
x3 ^= ROTL32(x2 + x1, 13);
x0 ^= ROTL32(x3 + x2, 18);
x6 ^= ROTL32(x5 + x4, 7);
x7 ^= ROTL32(x6 + x5, 9);
x4 ^= ROTL32(x7 + x6, 13);
x5 ^= ROTL32(x4 + x7, 18);
x11 ^= ROTL32(x10 + x9, 7);
x8 ^= ROTL32(x11 + x10, 9);
x9 ^= ROTL32(x8 + x11, 13);
x10 ^= ROTL32(x9 + x8, 18);
x12 ^= ROTL32(x15 + x14, 7);
x13 ^= ROTL32(x12 + x15, 9);
x14 ^= ROTL32(x13 + x12, 13);
x15 ^= ROTL32(x14 + x13, 18);
}
x0 += j0;
@@ -13,12 +13,6 @@ Public domain.
#define ROUNDS 12
#define U32C(v) (v##U)
static uint32_t
rotate(uint32_t u, int c)
{
return (u << c) | (u >> (32 - c));
}
int
crypto_core_salsa2012(unsigned char *out,
const unsigned char *in,
@@ -56,38 +50,38 @@ crypto_core_salsa2012(unsigned char *out,
j14 = x14 = LOAD32_LE(k + 28);
for (i = ROUNDS; i > 0; i -= 2) {
x4 ^= rotate(x0 + x12, 7);
x8 ^= rotate(x4 + x0, 9);
x12 ^= rotate(x8 + x4, 13);
x0 ^= rotate(x12 + x8, 18);
x9 ^= rotate(x5 + x1, 7);
x13 ^= rotate(x9 + x5, 9);
x1 ^= rotate(x13 + x9, 13);
x5 ^= rotate(x1 + x13, 18);
x14 ^= rotate(x10 + x6, 7);
x2 ^= rotate(x14 + x10, 9);
x6 ^= rotate(x2 + x14, 13);
x10 ^= rotate(x6 + x2, 18);
x3 ^= rotate(x15 + x11, 7);
x7 ^= rotate(x3 + x15, 9);
x11 ^= rotate(x7 + x3, 13);
x15 ^= rotate(x11 + x7, 18);
x1 ^= rotate(x0 + x3, 7);
x2 ^= rotate(x1 + x0, 9);
x3 ^= rotate(x2 + x1, 13);
x0 ^= rotate(x3 + x2, 18);
x6 ^= rotate(x5 + x4, 7);
x7 ^= rotate(x6 + x5, 9);
x4 ^= rotate(x7 + x6, 13);
x5 ^= rotate(x4 + x7, 18);
x11 ^= rotate(x10 + x9, 7);
x8 ^= rotate(x11 + x10, 9);
x9 ^= rotate(x8 + x11, 13);
x10 ^= rotate(x9 + x8, 18);
x12 ^= rotate(x15 + x14, 7);
x13 ^= rotate(x12 + x15, 9);
x14 ^= rotate(x13 + x12, 13);
x15 ^= rotate(x14 + x13, 18);
x4 ^= ROTL32(x0 + x12, 7);
x8 ^= ROTL32(x4 + x0, 9);
x12 ^= ROTL32(x8 + x4, 13);
x0 ^= ROTL32(x12 + x8, 18);
x9 ^= ROTL32(x5 + x1, 7);
x13 ^= ROTL32(x9 + x5, 9);
x1 ^= ROTL32(x13 + x9, 13);
x5 ^= ROTL32(x1 + x13, 18);
x14 ^= ROTL32(x10 + x6, 7);
x2 ^= ROTL32(x14 + x10, 9);
x6 ^= ROTL32(x2 + x14, 13);
x10 ^= ROTL32(x6 + x2, 18);
x3 ^= ROTL32(x15 + x11, 7);
x7 ^= ROTL32(x3 + x15, 9);
x11 ^= ROTL32(x7 + x3, 13);
x15 ^= ROTL32(x11 + x7, 18);
x1 ^= ROTL32(x0 + x3, 7);
x2 ^= ROTL32(x1 + x0, 9);
x3 ^= ROTL32(x2 + x1, 13);
x0 ^= ROTL32(x3 + x2, 18);
x6 ^= ROTL32(x5 + x4, 7);
x7 ^= ROTL32(x6 + x5, 9);
x4 ^= ROTL32(x7 + x6, 13);
x5 ^= ROTL32(x4 + x7, 18);
x11 ^= ROTL32(x10 + x9, 7);
x8 ^= ROTL32(x11 + x10, 9);
x9 ^= ROTL32(x8 + x11, 13);
x10 ^= ROTL32(x9 + x8, 18);
x12 ^= ROTL32(x15 + x14, 7);
x13 ^= ROTL32(x12 + x15, 9);
x14 ^= ROTL32(x13 + x12, 13);
x15 ^= ROTL32(x14 + x13, 18);
}
x0 += j0;
@@ -13,12 +13,6 @@ Public domain.
#define ROUNDS 8
#define U32C(v) (v##U)
static uint32_t
rotate(uint32_t u, int c)
{
return (u << c) | (u >> (32 - c));
}
int
crypto_core_salsa208(unsigned char *out,
const unsigned char *in,
@@ -56,38 +50,38 @@ crypto_core_salsa208(unsigned char *out,
j14 = x14 = LOAD32_LE(k + 28);
for (i = ROUNDS; i > 0; i -= 2) {
x4 ^= rotate(x0 + x12, 7);
x8 ^= rotate(x4 + x0, 9);
x12 ^= rotate(x8 + x4, 13);
x0 ^= rotate(x12 + x8, 18);
x9 ^= rotate(x5 + x1, 7);
x13 ^= rotate(x9 + x5, 9);
x1 ^= rotate(x13 + x9, 13);
x5 ^= rotate(x1 + x13, 18);
x14 ^= rotate(x10 + x6, 7);
x2 ^= rotate(x14 + x10, 9);
x6 ^= rotate(x2 + x14, 13);
x10 ^= rotate(x6 + x2, 18);
x3 ^= rotate(x15 + x11, 7);
x7 ^= rotate(x3 + x15, 9);
x11 ^= rotate(x7 + x3, 13);
x15 ^= rotate(x11 + x7, 18);
x1 ^= rotate(x0 + x3, 7);
x2 ^= rotate(x1 + x0, 9);
x3 ^= rotate(x2 + x1, 13);
x0 ^= rotate(x3 + x2, 18);
x6 ^= rotate(x5 + x4, 7);
x7 ^= rotate(x6 + x5, 9);
x4 ^= rotate(x7 + x6, 13);
x5 ^= rotate(x4 + x7, 18);
x11 ^= rotate(x10 + x9, 7);
x8 ^= rotate(x11 + x10, 9);
x9 ^= rotate(x8 + x11, 13);
x10 ^= rotate(x9 + x8, 18);
x12 ^= rotate(x15 + x14, 7);
x13 ^= rotate(x12 + x15, 9);
x14 ^= rotate(x13 + x12, 13);
x15 ^= rotate(x14 + x13, 18);
x4 ^= ROTL32(x0 + x12, 7);
x8 ^= ROTL32(x4 + x0, 9);
x12 ^= ROTL32(x8 + x4, 13);
x0 ^= ROTL32(x12 + x8, 18);
x9 ^= ROTL32(x5 + x1, 7);
x13 ^= ROTL32(x9 + x5, 9);
x1 ^= ROTL32(x13 + x9, 13);
x5 ^= ROTL32(x1 + x13, 18);
x14 ^= ROTL32(x10 + x6, 7);
x2 ^= ROTL32(x14 + x10, 9);
x6 ^= ROTL32(x2 + x14, 13);
x10 ^= ROTL32(x6 + x2, 18);
x3 ^= ROTL32(x15 + x11, 7);
x7 ^= ROTL32(x3 + x15, 9);
x11 ^= ROTL32(x7 + x3, 13);
x15 ^= ROTL32(x11 + x7, 18);
x1 ^= ROTL32(x0 + x3, 7);
x2 ^= ROTL32(x1 + x0, 9);
x3 ^= ROTL32(x2 + x1, 13);
x0 ^= ROTL32(x3 + x2, 18);
x6 ^= ROTL32(x5 + x4, 7);
x7 ^= ROTL32(x6 + x5, 9);
x4 ^= ROTL32(x7 + x6, 13);
x5 ^= ROTL32(x4 + x7, 18);
x11 ^= ROTL32(x10 + x9, 7);
x8 ^= ROTL32(x11 + x10, 9);
x9 ^= ROTL32(x8 + x11, 13);
x10 ^= ROTL32(x9 + x8, 18);
x12 ^= ROTL32(x15 + x14, 7);
x13 ^= ROTL32(x12 + x15, 9);
x14 ^= ROTL32(x13 + x12, 13);
x15 ^= ROTL32(x14 + x13, 18);
}
x0 += j0;
@@ -1,48 +0,0 @@
/*
BLAKE2 reference source code package - reference 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/>.
*/
#ifndef blake2_impl_H
#define blake2_impl_H
#include <stdint.h>
#include <string.h>
#include "utils.h"
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 )
{
sodium_memzero(v, n);
}
#endif
@@ -20,8 +20,8 @@
#include <immintrin.h>
#include "blake2.h"
#include "blake2-impl.h"
#include "blake2b-compress-avx2.h"
#include "private/common.h"
CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] =
{
@@ -3,7 +3,6 @@
#include <string.h>
#include "blake2.h"
#include "blake2-impl.h"
#include "private/common.h"
CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] =
@@ -53,13 +52,13 @@ int blake2b_compress_ref( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYT
#define G(r,i,a,b,c,d) \
do { \
a = a + b + m[blake2b_sigma[r][2*i+0]]; \
d = rotr64(d ^ a, 32); \
d = ROTR64(d ^ a, 32); \
c = c + d; \
b = rotr64(b ^ c, 24); \
b = ROTR64(b ^ c, 24); \
a = a + b + m[blake2b_sigma[r][2*i+1]]; \
d = rotr64(d ^ a, 16); \
d = ROTR64(d ^ a, 16); \
c = c + d; \
b = rotr64(b ^ c, 63); \
b = ROTR64(b ^ c, 63); \
} while(0)
#define ROUND(r) \
do { \
@@ -17,8 +17,8 @@
#include <smmintrin.h>
#include "blake2.h"
#include "blake2-impl.h"
#include "blake2b-compress-sse41.h"
#include "private/common.h"
CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] =
{
@@ -15,8 +15,8 @@
#include <tmmintrin.h>
#include "blake2.h"
#include "blake2-impl.h"
#include "blake2b-compress-ssse3.h"
#include "private/common.h"
CRYPTO_ALIGN(64) static const uint64_t blake2b_IV[8] =
{
@@ -17,8 +17,8 @@
#include <string.h>
#include "blake2.h"
#include "blake2-impl.h"
#include "runtime.h"
#include "utils.h"
#include "private/common.h"
#ifdef HAVE_TI_MODE
@@ -250,7 +250,7 @@ int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
sodium_memzero( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
}
return 0;
}
@@ -291,7 +291,7 @@ int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, cons
memset( block, 0, BLAKE2B_BLOCKBYTES );
memcpy( block, key, keylen );
blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
sodium_memzero( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from stack */
}
return 0;
}
@@ -154,4 +154,19 @@ store32_be(uint8_t dst[4], uint32_t w)
#endif
}
#ifndef __GNUC__
# ifdef __attribute__
# undef __attribute__
# endif
# define __attribute__(a)
#endif
#ifndef CRYPTO_ALIGN
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
# define CRYPTO_ALIGN(x) __declspec(align(x))
# else
# define CRYPTO_ALIGN(x) __attribute__ ((aligned(x)))
# endif
#endif
#endif