diff --git a/configure.ac b/configure.ac index f4bbbd63c..b2bcb22d6 100644 --- a/configure.ac +++ b/configure.ac @@ -400,7 +400,7 @@ AC_ARG_ENABLE([blake2], if test "$ENABLED_BLAKE2" = "yes" then - AM_CFLAGS="$AM_CFLAGS -DCYASSL_BLAKE2" + AM_CFLAGS="$AM_CFLAGS -DHAVE_BLAKE2" fi AM_CONDITIONAL([BUILD_BLAKE2], [test "x$ENABLED_BLAKE2" = "xyes"]) diff --git a/ctaocrypt/benchmark/benchmark.c b/ctaocrypt/benchmark/benchmark.c index 256261444..40e9eddaa 100644 --- a/ctaocrypt/benchmark/benchmark.c +++ b/ctaocrypt/benchmark/benchmark.c @@ -55,7 +55,7 @@ #endif -#ifdef CYASSL_BLAKE2 +#ifdef HAVE_BLAKE2 #include void bench_blake2(void); #endif @@ -172,7 +172,7 @@ int main(int argc, char** argv) #ifdef CYASSL_RIPEMD bench_ripemd(); #endif -#ifdef CYASSL_BLAKE2 +#ifdef HAVE_BLAKE2 bench_blake2(); #endif @@ -622,21 +622,21 @@ void bench_ripemd(void) #endif -#ifdef CYASSL_BLAKE2 +#ifdef HAVE_BLAKE2 void bench_blake2(void) { - blake2b_state S[1]; + Blake2 b2; byte digest[32]; double start, total, persec; int i; - - blake2b_init(S, 32); + + InitBlake2(&b2, 32); start = current_time(1); for(i = 0; i < numBlocks; i++) - blake2b_update(S, plain, sizeof(plain)); + Blake2Update(&b2, plain, sizeof(plain)); - blake2b_final(S, digest, 32); + Blake2Final(&b2, digest, 32); total = current_time(0) - start; persec = 1 / total * numBlocks; diff --git a/ctaocrypt/src/blake2b.c b/ctaocrypt/src/blake2b.c index 1896d3430..7258efdeb 100644 --- a/ctaocrypt/src/blake2b.c +++ b/ctaocrypt/src/blake2b.c @@ -36,6 +36,8 @@ #include #endif +#ifdef HAVE_BLAKE2 + #include #include @@ -429,3 +431,35 @@ int main( int argc, char **argv ) } #endif + +/* CTaoCrypt API */ + +/* Init Blake2 digest, track size incase final doesn't want to "remember" */ +int InitBlake2(Blake2* b2, word32 digestSz) +{ + b2->digestSz = digestSz; + + return blake2b_init(b2->S, (byte)digestSz); +} + + +/* Blake2 Update */ +int Blake2Update(Blake2* b2, const byte* data, word32 sz) +{ + return blake2b_update(b2->S, data, sz); +} + + +/* Blake2 Final, if pass in zero size we use init digestSz */ +int Blake2Final(Blake2* b2, byte* final, word32 requestSz) +{ + word32 sz = requestSz ? requestSz : b2->digestSz; + + return blake2b_final(b2->S, final, (byte)sz); +} + + +/* end CTaoCrypt API */ + +#endif /* HAVE_BLAKE2 */ + diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 7da380252..80cb7ced4 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -51,6 +51,9 @@ #ifdef HAVE_ECC #include #endif +#ifdef HAVE_BLAKE2 + #include +#endif #ifdef HAVE_LIBZ #include #endif @@ -142,6 +145,9 @@ int pbkdf2_test(void); #ifdef HAVE_ECC int ecc_test(void); #endif +#ifdef HAVE_BLAKE2 + int blake2_test(void); +#endif #ifdef HAVE_LIBZ int compress_test(void); #endif @@ -240,6 +246,13 @@ void ctaocrypt_test(void* args) printf( "RIPEMD test passed!\n"); #endif +#ifdef HAVE_BLAKE2 + if ( (ret = blake2_test()) ) + err_sys("BLAKE2 test failed!\n", ret); + else + printf( "BLAKE2 test passed!\n"); +#endif + #ifndef NO_HMAC #ifndef NO_MD5 if ( (ret = hmac_md5_test()) ) @@ -781,6 +794,72 @@ int ripemd_test(void) #endif /* CYASSL_RIPEMD */ +#ifdef HAVE_BLAKE2 + + +#define BLAKE2_TESTS 3 + +static const byte blake2b_vec[BLAKE2_TESTS][BLAKE2B_OUTBYTES] = +{ + { + 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03, + 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72, + 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61, + 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19, + 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53, + 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B, + 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55, + 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE + }, + { + 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95, + 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7, + 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44, + 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C, + 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB, + 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4, + 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48, + 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B + }, + { + 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9, + 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72, + 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF, + 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15, + 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB, + 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04, + 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1, + 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5 + } +}; + + + +int blake2_test(void) +{ + Blake2 b2; + byte digest[64]; + byte input[64]; + int i; + + for (i = 0; i < (int)sizeof(input); i++) + input[i] = (byte)i; + + for (i = 0; i < BLAKE2_TESTS; i++) { + InitBlake2(&b2, 64); + Blake2Update(&b2, input, i); + Blake2Final(&b2, digest, 64); + + if (memcmp(digest, blake2b_vec[i], 64) != 0) { + return -300 - i; + } + } + + return 0; +} +#endif /* HAVE_BLAKE2 */ + + #ifndef NO_SHA256 int sha256_test(void) { diff --git a/cyassl/ctaocrypt/blake2-int.h b/cyassl/ctaocrypt/blake2-int.h new file mode 100644 index 000000000..c4be83a5f --- /dev/null +++ b/cyassl/ctaocrypt/blake2-int.h @@ -0,0 +1,183 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Written in 2012 by Samuel Neves + + 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 . +*/ +/* blake2-int.h + * + * Copyright (C) 2006-2013 wolfSSL Inc. + * + * This file is part of CyaSSL. + * + * CyaSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * CyaSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + + + +#ifndef CTAOCRYPT_BLAKE2_INT_H +#define CTAOCRYPT_BLAKE2_INT_H + +#include + + +#if defined(_MSC_VER) + #define ALIGN(x) __declspec(align(x)) +#elif defined(__GNUC__) + #define ALIGN(x) __attribute__((aligned(x))) +#else + #define ALIGN(x) +#endif + + +#if defined(__cplusplus) + extern "C" { +#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 + { + byte digest_length; /* 1 */ + byte key_length; /* 2 */ + byte fanout; /* 3 */ + byte depth; /* 4 */ + word32 leaf_length; /* 8 */ + byte node_offset[6];/* 14 */ + byte node_depth; /* 15 */ + byte inner_length; /* 16 */ + /* byte reserved[0]; */ + byte salt[BLAKE2B_SALTBYTES]; /* 24 */ + byte personal[BLAKE2S_PERSONALBYTES]; /* 32 */ + } blake2s_param; + + ALIGN( 64 ) typedef struct __blake2s_state + { + word32 h[8]; + word32 t[2]; + word32 f[2]; + byte buf[2 * BLAKE2S_BLOCKBYTES]; + word64 buflen; + byte last_node; + } blake2s_state ; + + typedef struct __blake2b_param + { + byte digest_length; /* 1 */ + byte key_length; /* 2 */ + byte fanout; /* 3 */ + byte depth; /* 4 */ + word32 leaf_length; /* 8 */ + word64 node_offset; /* 16 */ + byte node_depth; /* 17 */ + byte inner_length; /* 18 */ + byte reserved[14]; /* 32 */ + byte salt[BLAKE2B_SALTBYTES]; /* 48 */ + byte personal[BLAKE2B_PERSONALBYTES]; /* 64 */ + } blake2b_param; + + ALIGN( 64 ) typedef struct __blake2b_state + { + word64 h[8]; + word64 t[2]; + word64 f[2]; + byte buf[2 * BLAKE2B_BLOCKBYTES]; + word64 buflen; + byte last_node; + } blake2b_state; + + typedef struct __blake2sp_state + { + blake2s_state S[8][1]; + blake2s_state R[1]; + byte buf[8 * BLAKE2S_BLOCKBYTES]; + word64 buflen; + } blake2sp_state; + + typedef struct __blake2bp_state + { + blake2b_state S[4][1]; + blake2b_state R[1]; + byte buf[4 * BLAKE2B_BLOCKBYTES]; + word64 buflen; + } blake2bp_state; +#pragma pack(pop) + + /* Streaming API */ + int blake2s_init( blake2s_state *S, const byte outlen ); + int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, const byte keylen ); + int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); + int blake2s_update( blake2s_state *S, const byte *in, word64 inlen ); + int blake2s_final( blake2s_state *S, byte *out, byte outlen ); + + int blake2b_init( blake2b_state *S, const byte outlen ); + int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen ); + int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); + int blake2b_update( blake2b_state *S, const byte *in, word64 inlen ); + int blake2b_final( blake2b_state *S, byte *out, byte outlen ); + + int blake2sp_init( blake2sp_state *S, const byte outlen ); + int blake2sp_init_key( blake2sp_state *S, const byte outlen, const void *key, const byte keylen ); + int blake2sp_update( blake2sp_state *S, const byte *in, word64 inlen ); + int blake2sp_final( blake2sp_state *S, byte *out, byte outlen ); + + int blake2bp_init( blake2bp_state *S, const byte outlen ); + int blake2bp_init_key( blake2bp_state *S, const byte outlen, const void *key, const byte keylen ); + int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen ); + int blake2bp_final( blake2bp_state *S, byte *out, byte outlen ); + + /* Simple API */ + int blake2s( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); + int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); + + int blake2sp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); + int blake2bp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); + + static inline int blake2( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ) + { + return blake2b( out, in, key, outlen, inlen, keylen ); + } + + + +#if defined(__cplusplus) + } +#endif + +#endif /* CTAOCRYPT_BLAKE2_INT_H */ + diff --git a/cyassl/ctaocrypt/blake2.h b/cyassl/ctaocrypt/blake2.h index a5983bb3a..6127d5346 100644 --- a/cyassl/ctaocrypt/blake2.h +++ b/cyassl/ctaocrypt/blake2.h @@ -1,15 +1,3 @@ -/* - BLAKE2 reference source code package - reference C implementations - - Written in 2012 by Samuel Neves - - 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 . -*/ /* blake2.h * * Copyright (C) 2006-2013 wolfSSL Inc. @@ -31,152 +19,38 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ + +#ifdef HAVE_BLAKE2 + #ifndef CTAOCRYPT_BLAKE2_H #define CTAOCRYPT_BLAKE2_H -#include +#include -#if defined(_MSC_VER) - #define ALIGN(x) __declspec(align(x)) -#elif defined(__GNUC__) - #define ALIGN(x) __attribute__((aligned(x))) -#else - #define ALIGN(x) -#endif +/* in bytes, variable digest size up to 512 bits (64 bytes) */ +enum { + BLAKE2_ID = 7 /* hash type unique */ +}; -#if defined(__cplusplus) - extern "C" { -#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 - { - byte digest_length; /* 1 */ - byte key_length; /* 2 */ - byte fanout; /* 3 */ - byte depth; /* 4 */ - word32 leaf_length; /* 8 */ - byte node_offset[6];/* 14 */ - byte node_depth; /* 15 */ - byte inner_length; /* 16 */ - /* byte reserved[0]; */ - byte salt[BLAKE2B_SALTBYTES]; /* 24 */ - byte personal[BLAKE2S_PERSONALBYTES]; /* 32 */ - } blake2s_param; - - ALIGN( 64 ) typedef struct __blake2s_state - { - word32 h[8]; - word32 t[2]; - word32 f[2]; - byte buf[2 * BLAKE2S_BLOCKBYTES]; - word64 buflen; - byte last_node; - } blake2s_state ; - - typedef struct __blake2b_param - { - byte digest_length; /* 1 */ - byte key_length; /* 2 */ - byte fanout; /* 3 */ - byte depth; /* 4 */ - word32 leaf_length; /* 8 */ - word64 node_offset; /* 16 */ - byte node_depth; /* 17 */ - byte inner_length; /* 18 */ - byte reserved[14]; /* 32 */ - byte salt[BLAKE2B_SALTBYTES]; /* 48 */ - byte personal[BLAKE2B_PERSONALBYTES]; /* 64 */ - } blake2b_param; - - ALIGN( 64 ) typedef struct __blake2b_state - { - word64 h[8]; - word64 t[2]; - word64 f[2]; - byte buf[2 * BLAKE2B_BLOCKBYTES]; - word64 buflen; - byte last_node; - } blake2b_state; - - typedef struct __blake2sp_state - { - blake2s_state S[8][1]; - blake2s_state R[1]; - byte buf[8 * BLAKE2S_BLOCKBYTES]; - word64 buflen; - } blake2sp_state; - - typedef struct __blake2bp_state - { - blake2b_state S[4][1]; - blake2b_state R[1]; - byte buf[4 * BLAKE2B_BLOCKBYTES]; - word64 buflen; - } blake2bp_state; -#pragma pack(pop) - - /* Streaming API */ - int blake2s_init( blake2s_state *S, const byte outlen ); - int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key, const byte keylen ); - int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); - int blake2s_update( blake2s_state *S, const byte *in, word64 inlen ); - int blake2s_final( blake2s_state *S, byte *out, byte outlen ); - - CYASSL_API int blake2b_init( blake2b_state *S, const byte outlen ); - int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key, const byte keylen ); - int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); - CYASSL_API int blake2b_update( blake2b_state *S, const byte *in, word64 inlen ); - CYASSL_API int blake2b_final( blake2b_state *S, byte *out, byte outlen ); - - int blake2sp_init( blake2sp_state *S, const byte outlen ); - int blake2sp_init_key( blake2sp_state *S, const byte outlen, const void *key, const byte keylen ); - int blake2sp_update( blake2sp_state *S, const byte *in, word64 inlen ); - int blake2sp_final( blake2sp_state *S, byte *out, byte outlen ); - - int blake2bp_init( blake2bp_state *S, const byte outlen ); - int blake2bp_init_key( blake2bp_state *S, const byte outlen, const void *key, const byte keylen ); - int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen ); - int blake2bp_final( blake2bp_state *S, byte *out, byte outlen ); - - /* Simple API */ - int blake2s( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); - int blake2b( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); - - int blake2sp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); - int blake2bp( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ); - - static inline int blake2( byte *out, const void *in, const void *key, const byte outlen, const word64 inlen, byte keylen ) - { - return blake2b( out, in, key, outlen, inlen, keylen ); - } +/* BLAKE2 digest */ +typedef struct Blake2 { + blake2b_state S[1]; /* our state */ + word32 digestSz; /* digest size used on init */ +} Blake2; + + +CYASSL_API int InitBlake2(Blake2*, word32); +CYASSL_API int Blake2Update(Blake2*, const byte*, word32); +CYASSL_API int Blake2Final(Blake2*, byte*, word32); -/* CTaoCrypt API */ #if defined(__cplusplus) } #endif #endif /* CTAOCRYPT_BLAKE2_H */ +#endif /* HAVE_BLAKE2 */