diff --git a/contrib/epee/include/math_helper.h b/contrib/epee/include/math_helper.h index aea4f91a..9ae935ac 100644 --- a/contrib/epee/include/math_helper.h +++ b/contrib/epee/include/math_helper.h @@ -240,7 +240,7 @@ namespace math_helper } } -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(strict-aliasing) inline uint64_t generated_random_uint64() @@ -248,7 +248,7 @@ DISABLE_GCC_WARNING(strict-aliasing) boost::uuids::uuid id___ = boost::uuids::random_generator()(); return *reinterpret_cast(&id___.data[0]); //(*reinterpret_cast(&id___.data[0]) ^ *reinterpret_cast(&id___.data[8])); } -POP_WARNINGS +POP_GCC_WARNINGS template class once_a_time_seconds { diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index 00a4c9ff..2ab46a2e 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -54,7 +54,7 @@ #endif #include "os_defenitions.h" #include "warnings.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) @@ -1374,7 +1374,7 @@ namespace log_space if(!plogger) return false; return plogger->remove_logger(type); } -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(maybe-uninitialized) static int get_set_log_detalisation_level(bool is_need_set = false, int log_level_to_set = LOG_LEVEL_1) { @@ -1386,7 +1386,7 @@ DISABLE_GCC_WARNING(maybe-uninitialized) } return log_detalisation_level; } -POP_WARNINGS +POP_GCC_WARNINGS static int get_set_time_level(bool is_need_set = false, int time_log_level = LOG_LEVEL_0) { static int val_time_log_level = LOG_LEVEL_0; @@ -1705,6 +1705,6 @@ POP_WARNINGS } // namespace epee -POP_WARNINGS +POP_VS_WARNINGS #endif //_MISC_LOG_EX_H_ diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 4953508d..799cfd01 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -36,7 +36,7 @@ #include "misc_language.h" #include "warnings.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS namespace epee { namespace net_utils { /************************************************************************/ @@ -474,7 +474,7 @@ bool boosted_tcp_server::init_server(uint32_t port, const st CATCH_ENTRY_L0("boosted_tcp_server::init_server", false); } //----------------------------------------------------------------------------- -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(maybe-uninitialized) template bool boosted_tcp_server::init_server(const std::string port, const std::string& address) @@ -487,7 +487,7 @@ bool boosted_tcp_server::init_server(const std::string port, } return this->init_server(p, address); } -POP_WARNINGS +POP_GCC_WARNINGS //--------------------------------------------------------------------------------- template bool boosted_tcp_server::worker_thread() @@ -794,4 +794,4 @@ bool boosted_tcp_server::connect_async(const std::string& ad } } // namespace net_utils } // namespace epee -POP_WARNINGS +POP_VS_WARNINGS diff --git a/contrib/epee/include/net/http_protocol_handler.inl b/contrib/epee/include/net/http_protocol_handler.inl index b84bf47c..a5f42c4f 100644 --- a/contrib/epee/include/net/http_protocol_handler.inl +++ b/contrib/epee/include/net/http_protocol_handler.inl @@ -37,7 +37,7 @@ #define HTTP_MAX_PRE_COMMAND_LINE_CHARS 20 #define HTTP_MAX_HEADER_LEN 100000 -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(maybe-uninitialized) @@ -690,7 +690,7 @@ namespace net_utils } } -POP_WARNINGS +POP_GCC_WARNINGS //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- diff --git a/contrib/epee/include/storages/portable_storage_template_helper.h b/contrib/epee/include/storages/portable_storage_template_helper.h index d7471a51..896f9eb2 100644 --- a/contrib/epee/include/storages/portable_storage_template_helper.h +++ b/contrib/epee/include/storages/portable_storage_template_helper.h @@ -101,7 +101,7 @@ namespace epee return load_t_from_binary(out, f_buff); } //----------------------------------------------------------------------------------------------------------- -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) template bool store_t_to_binary(const t_struct& str_in, std::string& binary_buff, size_t indent = 0) @@ -110,7 +110,7 @@ DISABLE_VS_WARNINGS(4100) str_in.store(ps); return ps.store_to_binary(binary_buff); } -POP_WARNINGS +POP_VS_WARNINGS //----------------------------------------------------------------------------------------------------------- template std::string store_t_to_binary(const t_struct& str_in, size_t indent = 0) diff --git a/contrib/epee/include/storages/portable_storage_to_bin.h b/contrib/epee/include/storages/portable_storage_to_bin.h index cb74bc03..761d0874 100644 --- a/contrib/epee/include/storages/portable_storage_to_bin.h +++ b/contrib/epee/include/storages/portable_storage_to_bin.h @@ -45,7 +45,7 @@ namespace epee return sizeof(pack_value); } - PUSH_WARNINGS + PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(strict-aliasing) template size_t pack_varint(t_stream& strm, size_t val) @@ -67,7 +67,7 @@ namespace epee return pack_varint_t(strm, PORTABLE_RAW_SIZE_MARK_INT64, val); } } - POP_WARNINGS + POP_GCC_WARNINGS template bool put_string(t_stream& strm, const std::string& v) diff --git a/contrib/epee/include/storages/portable_storage_val_converters.h b/contrib/epee/include/storages/portable_storage_val_converters.h index e58ae349..caf7a322 100644 --- a/contrib/epee/include/storages/portable_storage_val_converters.h +++ b/contrib/epee/include/storages/portable_storage_val_converters.h @@ -41,33 +41,37 @@ namespace epee template void convert_int_to_uint(const from_type& from, to_type& to) { -PUSH_WARNINGS +PUSH_VS_WARNINGS +PUSH_GCC_WARNINGS DISABLE_VS_WARNINGS(4018) CHECK_AND_ASSERT_THROW_MES(from >=0, "unexpected int value with signed storage value less than 0, and unsigned receiver value: " << from); DISABLE_GCC_AND_CLANG_WARNING(sign-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); to = static_cast(from); -POP_WARNINGS +POP_GCC_WARNINGS +POP_VS_WARNINGS } template void convert_int_to_int(const from_type& from, to_type& to) { CHECK_AND_ASSERT_THROW_MES(from >= boost::numeric::bounds::lowest(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with lowest possible value = " << boost::numeric::bounds::lowest()); -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "int value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); -POP_WARNINGS +POP_GCC_WARNINGS to = static_cast(from); } template void convert_uint_to_any_int(const from_type& from, to_type& to) { -PUSH_WARNINGS +PUSH_VS_WARNINGS +PUSH_GCC_WARNINGS DISABLE_VS_WARNINGS(4018) DISABLE_CLANG_WARNING(tautological-constant-out-of-range-compare) CHECK_AND_ASSERT_THROW_MES(from <= std::numeric_limits::max(), "uint value overhead: try to set value " << from << " to type " << typeid(to_type).name() << " with max possible value = " << std::numeric_limits::max()); to = static_cast(from); -POP_WARNINGS +POP_GCC_WARNINGS +POP_VS_WARNINGS } template //is from signed, is from to signed diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index 8cd9af86..4b153d51 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -216,7 +216,7 @@ namespace string_tools return t_pod; } //---------------------------------------------------------------------------- -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(maybe-uninitialized) template inline bool get_xtype_from_string(OUT XType& val, const std::string& str_id) @@ -247,7 +247,7 @@ DISABLE_GCC_WARNING(maybe-uninitialized) return true; } -POP_WARNINGS +POP_GCC_WARNINGS //--------------------------------------------------- template bool get_xnum_from_hex_string(const std::string str, int_t& res ) diff --git a/contrib/epee/include/time_helper.h b/contrib/epee/include/time_helper.h index 457f0b2b..f11dae91 100644 --- a/contrib/epee/include/time_helper.h +++ b/contrib/epee/include/time_helper.h @@ -59,10 +59,10 @@ namespace misc_utils char tmpbuf[200] = {0}; tm* pt = NULL; -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4996) pt = localtime(&time_); -POP_WARNINGS +POP_VS_WARNINGS if(pt) strftime( tmpbuf, 199, "%d.%m.%Y %H:%M:%S", pt ); @@ -81,10 +81,10 @@ POP_WARNINGS char tmpbuf[200] = {0}; tm* pt = NULL; -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4996) pt = localtime(&time_); -POP_WARNINGS +POP_VS_WARNINGS if(pt) strftime( tmpbuf, 199, "%Y_%m_%d %H_%M_%S", pt ); @@ -109,10 +109,10 @@ POP_WARNINGS { char tmpbuf[200] = {0}; tm* pt = NULL; -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4996) pt = gmtime(&time_); -POP_WARNINGS +POP_VS_WARNINGS strftime( tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", pt ); return tmpbuf; } @@ -126,7 +126,7 @@ POP_WARNINGS tail = -tail; res = "-"; } -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4244) int days = tail/(60*60*24); tail = tail%(60*60*24); @@ -135,7 +135,7 @@ DISABLE_VS_WARNINGS(4244) int minutes = tail/(60); tail = tail%(60); int seconds = tail; -POP_WARNINGS +POP_VS_WARNINGS res += std::string("d") + boost::lexical_cast(days) + ".h" + boost::lexical_cast(hours) + ".m" + boost::lexical_cast(minutes) + ".s" + boost::lexical_cast(seconds); return res; } diff --git a/contrib/epee/include/warnings.h b/contrib/epee/include/warnings.h index 89f2ec79..bbdcd9fe 100644 --- a/contrib/epee/include/warnings.h +++ b/contrib/epee/include/warnings.h @@ -2,8 +2,12 @@ #if defined(_MSC_VER) -#define PUSH_WARNINGS __pragma(warning(push)) -#define POP_WARNINGS __pragma(warning(pop)) +#define PUSH_VS_WARNINGS __pragma(warning(push)) +#define POP_VS_WARNINGS __pragma(warning(pop)) + +#define PUSH_GCC_WARNINGS +#define POP_GCC_WARNINGS + #define DISABLE_VS_WARNINGS(w) __pragma(warning(disable: w)) #define DISABLE_GCC_WARNING(w) #define DISABLE_CLANG_WARNING(w) @@ -13,8 +17,12 @@ #include -#define PUSH_WARNINGS _Pragma("GCC diagnostic push") -#define POP_WARNINGS _Pragma("GCC diagnostic pop") +#define PUSH_VS_WARNINGS +#define POP_VS_WARNINGS + +#define PUSH_GCC_WARNINGS _Pragma("GCC diagnostic push") +#define POP_GCC_WARNINGS _Pragma("GCC diagnostic pop") + #define DISABLE_VS_WARNINGS(w) #if defined(__clang__) diff --git a/src/crypto/blake2-impl.h b/src/crypto/blake2-impl.h new file mode 100644 index 00000000..c1df82e0 --- /dev/null +++ b/src/crypto/blake2-impl.h @@ -0,0 +1,160 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves . You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ +#ifndef BLAKE2_IMPL_H +#define BLAKE2_IMPL_H + +#include +#include + +#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) + #if defined(_MSC_VER) + #define BLAKE2_INLINE __inline + #elif defined(__GNUC__) + #define BLAKE2_INLINE __inline__ + #else + #define BLAKE2_INLINE + #endif +#else + #define BLAKE2_INLINE inline +#endif + +static BLAKE2_INLINE uint32_t load32( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint32_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return (( uint32_t )( p[0] ) << 0) | + (( uint32_t )( p[1] ) << 8) | + (( uint32_t )( p[2] ) << 16) | + (( uint32_t )( p[3] ) << 24) ; +#endif +} + +static BLAKE2_INLINE uint64_t load64( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint64_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return (( uint64_t )( p[0] ) << 0) | + (( uint64_t )( p[1] ) << 8) | + (( uint64_t )( p[2] ) << 16) | + (( uint64_t )( p[3] ) << 24) | + (( uint64_t )( p[4] ) << 32) | + (( uint64_t )( p[5] ) << 40) | + (( uint64_t )( p[6] ) << 48) | + (( uint64_t )( p[7] ) << 56) ; +#endif +} + +static BLAKE2_INLINE uint16_t load16( const void *src ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + uint16_t w; + memcpy(&w, src, sizeof w); + return w; +#else + const uint8_t *p = ( const uint8_t * )src; + return ( uint16_t )((( uint32_t )( p[0] ) << 0) | + (( uint32_t )( p[1] ) << 8)); +#endif +} + +static BLAKE2_INLINE void store16( void *dst, uint16_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + *p++ = ( uint8_t )w; w >>= 8; + *p++ = ( uint8_t )w; +#endif +} + +static BLAKE2_INLINE void store32( void *dst, uint32_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); +#endif +} + +static BLAKE2_INLINE void store64( void *dst, uint64_t w ) +{ +#if defined(NATIVE_LITTLE_ENDIAN) + memcpy(dst, &w, sizeof w); +#else + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); + p[4] = (uint8_t)(w >> 32); + p[5] = (uint8_t)(w >> 40); + p[6] = (uint8_t)(w >> 48); + p[7] = (uint8_t)(w >> 56); +#endif +} + +static BLAKE2_INLINE uint64_t load48( const void *src ) +{ + const uint8_t *p = ( const uint8_t * )src; + return (( uint64_t )( p[0] ) << 0) | + (( uint64_t )( p[1] ) << 8) | + (( uint64_t )( p[2] ) << 16) | + (( uint64_t )( p[3] ) << 24) | + (( uint64_t )( p[4] ) << 32) | + (( uint64_t )( p[5] ) << 40) ; +} + +static BLAKE2_INLINE void store48( void *dst, uint64_t w ) +{ + uint8_t *p = ( uint8_t * )dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); + p[4] = (uint8_t)(w >> 32); + p[5] = (uint8_t)(w >> 40); +} + +static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c ) +{ + return ( w >> c ) | ( w << ( 32 - c ) ); +} + +static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c ) +{ + return ( w >> c ) | ( w << ( 64 - c ) ); +} + +/* prevents compiler optimizing out memset() */ +static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) +{ + static void *(*const volatile memset_v)(void *, int, size_t) = &memset; + memset_v(v, 0, n); +} + +#endif diff --git a/src/crypto/blake2.h b/src/crypto/blake2.h new file mode 100644 index 00000000..4949c650 --- /dev/null +++ b/src/crypto/blake2.h @@ -0,0 +1,200 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves . You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ +#ifndef BLAKE2_H +#define BLAKE2_H + +#include +#include + +#if defined(_MSC_VER) +#define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop)) +#else +#define BLAKE2_PACKED(x) x __attribute__((packed)) +#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 + }; + + typedef struct blake2s_state__ + { + uint32_t h[8]; + uint32_t t[2]; + uint32_t f[2]; + uint8_t buf[BLAKE2S_BLOCKBYTES]; + size_t buflen; + size_t outlen; + uint8_t last_node; + } blake2s_state; + + typedef struct blake2b_state__ + { + uint64_t h[8]; + uint64_t t[2]; + uint64_t f[2]; + uint8_t buf[BLAKE2B_BLOCKBYTES]; + size_t buflen; + size_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]; + size_t buflen; + size_t outlen; + } blake2sp_state; + + typedef struct blake2bp_state__ + { + blake2b_state S[4][1]; + blake2b_state R[1]; + uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; + size_t buflen; + size_t outlen; + } blake2bp_state; + + + BLAKE2_PACKED(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 */ + uint32_t node_offset; /* 12 */ + uint16_t xof_length; /* 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 */ + }); + + typedef struct blake2s_param__ blake2s_param; + + BLAKE2_PACKED(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 */ + uint32_t node_offset; /* 12 */ + uint32_t xof_length; /* 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 */ + }); + + typedef struct blake2b_param__ blake2b_param; + + typedef struct blake2xs_state__ + { + blake2s_state S[1]; + blake2s_param P[1]; + } blake2xs_state; + + typedef struct blake2xb_state__ + { + blake2b_state S[1]; + blake2b_param P[1]; + } blake2xb_state; + + + /* Padded structs result in a compile-time error */ + + enum { + BLAKE2_DUMMY_1 = 1/(sizeof(blake2s_param) == BLAKE2S_OUTBYTES), + BLAKE2_DUMMY_2 = 1/(sizeof(blake2b_param) == BLAKE2B_OUTBYTES) + }; +// static_assert(sizeof(blake2s_param) == BLAKE2S_OUTBYTES, "Wrong size of blake2s_param"); +// static_assert(sizeof(blake2b_param) == BLAKE2B_OUTBYTES, "Wrong size of blake2b_param"); + + + /* Streaming API */ + int blake2s_init( blake2s_state *S, size_t outlen ); + int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); + int blake2s_update( blake2s_state *S, const void *in, size_t inlen ); + int blake2s_final( blake2s_state *S, void *out, size_t outlen ); + + int blake2b_init( blake2b_state *S, size_t outlen ); + int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); + int blake2b_update( blake2b_state *S, const void *in, size_t inlen ); + int blake2b_final( blake2b_state *S, void *out, size_t outlen ); + + int blake2sp_init( blake2sp_state *S, size_t outlen ); + int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen ); + int blake2sp_final( blake2sp_state *S, void *out, size_t outlen ); + + int blake2bp_init( blake2bp_state *S, size_t outlen ); + int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen ); + int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen ); + int blake2bp_final( blake2bp_state *S, void *out, size_t outlen ); + + /* Variable output length API */ + int blake2xs_init( blake2xs_state *S, const size_t outlen ); + int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen ); + int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen ); + int blake2xs_final(blake2xs_state *S, void *out, size_t outlen); + + int blake2xb_init( blake2xb_state *S, const size_t outlen ); + int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen ); + int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen ); + int blake2xb_final(blake2xb_state *S, void *out, size_t outlen); + + /* Simple API */ + int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + + /* This is simply an alias for blake2b */ + int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/src/crypto/blake2b-ref.c b/src/crypto/blake2b-ref.c new file mode 100644 index 00000000..cd38b1ba --- /dev/null +++ b/src/crypto/blake2b-ref.c @@ -0,0 +1,379 @@ +/* + BLAKE2 reference source code package - reference C implementations + + Copyright 2012, Samuel Neves . You may use this under the + terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at + your option. The terms of these licenses can be found at: + + - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 + - OpenSSL license : https://www.openssl.org/source/license.html + - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 + + More information about the BLAKE2 hash function can be found at + https://blake2.net. +*/ + +#include +#include +#include + +#include "blake2.h" +#include "blake2-impl.h" + +static const uint64_t blake2b_IV[8] = +{ + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL +}; + +static const uint8_t blake2b_sigma[12][16] = +{ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , + { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , + { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , + { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , + { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , + { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , + { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , + { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , + { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } +}; + + +static void blake2b_set_lastnode( blake2b_state *S ) +{ + S->f[1] = (uint64_t)-1; +} + +/* Some helper functions, not necessarily useful */ +static int blake2b_is_lastblock( const blake2b_state *S ) +{ + return S->f[0] != 0; +} + +static void blake2b_set_lastblock( blake2b_state *S ) +{ + if( S->last_node ) blake2b_set_lastnode( S ); + + S->f[0] = (uint64_t)-1; +} + +static void blake2b_increment_counter( blake2b_state *S, const uint64_t inc ) +{ + S->t[0] += inc; + S->t[1] += ( S->t[0] < inc ); +} + +static void blake2b_init0( blake2b_state *S ) +{ + size_t i; + memset( S, 0, sizeof( blake2b_state ) ); + + for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i]; +} + +/* init xors IV with input parameter block */ +int blake2b_init_param( blake2b_state *S, const blake2b_param *P ) +{ + const uint8_t *p = ( const uint8_t * )( P ); + size_t i; + + blake2b_init0( S ); + + /* IV XOR ParamBlock */ + for( i = 0; i < 8; ++i ) + S->h[i] ^= load64( p + sizeof( S->h[i] ) * i ); + + S->outlen = P->digest_length; + return 0; +} + + + +int blake2b_init( blake2b_state *S, size_t outlen ) +{ + blake2b_param P[1]; + + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + + P->digest_length = (uint8_t)outlen; + P->key_length = 0; + P->fanout = 1; + P->depth = 1; + store32( &P->leaf_length, 0 ); + store32( &P->node_offset, 0 ); + store32( &P->xof_length, 0 ); + P->node_depth = 0; + P->inner_length = 0; + memset( P->reserved, 0, sizeof( P->reserved ) ); + memset( P->salt, 0, sizeof( P->salt ) ); + memset( P->personal, 0, sizeof( P->personal ) ); + return blake2b_init_param( S, P ); +} + + +int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ) +{ + blake2b_param P[1]; + + if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1; + + if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1; + + P->digest_length = (uint8_t)outlen; + P->key_length = (uint8_t)keylen; + P->fanout = 1; + P->depth = 1; + store32( &P->leaf_length, 0 ); + store32( &P->node_offset, 0 ); + store32( &P->xof_length, 0 ); + P->node_depth = 0; + P->inner_length = 0; + memset( P->reserved, 0, sizeof( P->reserved ) ); + memset( P->salt, 0, sizeof( P->salt ) ); + memset( P->personal, 0, sizeof( P->personal ) ); + + if( blake2b_init_param( S, P ) < 0 ) return -1; + + { + uint8_t block[BLAKE2B_BLOCKBYTES]; + 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 */ + } + return 0; +} + +#define G(r,i,a,b,c,d) \ + do { \ + a = a + b + m[blake2b_sigma[r][2*i+0]]; \ + d = rotr64(d ^ a, 32); \ + c = c + d; \ + b = rotr64(b ^ c, 24); \ + a = a + b + m[blake2b_sigma[r][2*i+1]]; \ + d = rotr64(d ^ a, 16); \ + c = c + d; \ + b = rotr64(b ^ c, 63); \ + } while(0) + +#define ROUND(r) \ + do { \ + G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \ + G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \ + G(r,2,v[ 2],v[ 6],v[10],v[14]); \ + G(r,3,v[ 3],v[ 7],v[11],v[15]); \ + G(r,4,v[ 0],v[ 5],v[10],v[15]); \ + G(r,5,v[ 1],v[ 6],v[11],v[12]); \ + G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \ + G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \ + } while(0) + +static void blake2b_compress( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES] ) +{ + uint64_t m[16]; + uint64_t v[16]; + size_t i; + + for( i = 0; i < 16; ++i ) { + m[i] = load64( block + i * sizeof( m[i] ) ); + } + + for( i = 0; i < 8; ++i ) { + v[i] = S->h[i]; + } + + v[ 8] = blake2b_IV[0]; + v[ 9] = blake2b_IV[1]; + v[10] = blake2b_IV[2]; + v[11] = blake2b_IV[3]; + v[12] = blake2b_IV[4] ^ S->t[0]; + v[13] = blake2b_IV[5] ^ S->t[1]; + v[14] = blake2b_IV[6] ^ S->f[0]; + v[15] = blake2b_IV[7] ^ S->f[1]; + + ROUND( 0 ); + ROUND( 1 ); + ROUND( 2 ); + ROUND( 3 ); + ROUND( 4 ); + ROUND( 5 ); + ROUND( 6 ); + ROUND( 7 ); + ROUND( 8 ); + ROUND( 9 ); + ROUND( 10 ); + ROUND( 11 ); + + for( i = 0; i < 8; ++i ) { + S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; + } +} + +#undef G +#undef ROUND + +int blake2b_update( blake2b_state *S, const void *pin, size_t inlen ) +{ + const unsigned char * in = (const unsigned char *)pin; + if( inlen > 0 ) + { + size_t left = S->buflen; + size_t fill = BLAKE2B_BLOCKBYTES - left; + if( inlen > fill ) + { + S->buflen = 0; + memcpy( S->buf + left, in, fill ); /* Fill buffer */ + blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES ); + blake2b_compress( S, S->buf ); /* Compress */ + in += fill; inlen -= fill; + while(inlen > BLAKE2B_BLOCKBYTES) { + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); + blake2b_compress( S, in ); + in += BLAKE2B_BLOCKBYTES; + inlen -= BLAKE2B_BLOCKBYTES; + } + } + memcpy( S->buf + S->buflen, in, inlen ); + S->buflen += inlen; + } + return 0; +} + +int blake2b_final( blake2b_state *S, void *out, size_t outlen ) +{ + uint8_t buffer[BLAKE2B_OUTBYTES] = {0}; + size_t i; + + if( out == NULL || outlen < S->outlen ) + return -1; + + if( blake2b_is_lastblock( S ) ) + return -1; + + blake2b_increment_counter( S, S->buflen ); + blake2b_set_lastblock( S ); + memset( S->buf + S->buflen, 0, BLAKE2B_BLOCKBYTES - S->buflen ); /* Padding */ + blake2b_compress( S, S->buf ); + + for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */ + store64( buffer + sizeof( S->h[i] ) * i, S->h[i] ); + + memcpy( out, buffer, S->outlen ); + secure_zero_memory(buffer, sizeof(buffer)); + return 0; +} + +/* inlen, at least, should be uint64_t. Others can be size_t. */ +int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) +{ + blake2b_state S[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; + + if( keylen > 0 ) + { + if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1; + } + else + { + if( blake2b_init( S, outlen ) < 0 ) return -1; + } + + blake2b_update( S, ( const uint8_t * )in, inlen ); + blake2b_final( S, out, outlen ); + return 0; +} + +int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ) { + return blake2b(out, outlen, in, inlen, key, keylen); +} + +#if defined(SUPERCOP) +int crypto_hash( unsigned char *out, unsigned char *in, unsigned long long inlen ) +{ + return blake2b( out, BLAKE2B_OUTBYTES, in, inlen, NULL, 0 ); +} +#endif + +#if defined(BLAKE2B_SELFTEST) +#include +#include "blake2-kat.h" +int main( void ) +{ + uint8_t key[BLAKE2B_KEYBYTES]; + uint8_t buf[BLAKE2_KAT_LENGTH]; + size_t i, step; + + for( i = 0; i < BLAKE2B_KEYBYTES; ++i ) + key[i] = ( uint8_t )i; + + for( i = 0; i < BLAKE2_KAT_LENGTH; ++i ) + buf[i] = ( uint8_t )i; + + /* Test simple API */ + for( i = 0; i < BLAKE2_KAT_LENGTH; ++i ) + { + uint8_t hash[BLAKE2B_OUTBYTES]; + blake2b( hash, BLAKE2B_OUTBYTES, buf, i, key, BLAKE2B_KEYBYTES ); + + if( 0 != memcmp( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) ) + { + goto fail; + } + } + + /* Test streaming API */ + for(step = 1; step < BLAKE2B_BLOCKBYTES; ++step) { + for (i = 0; i < BLAKE2_KAT_LENGTH; ++i) { + uint8_t hash[BLAKE2B_OUTBYTES]; + blake2b_state S; + uint8_t * p = buf; + size_t mlen = i; + int err = 0; + + if( (err = blake2b_init_key(&S, BLAKE2B_OUTBYTES, key, BLAKE2B_KEYBYTES)) < 0 ) { + goto fail; + } + + while (mlen >= step) { + if ( (err = blake2b_update(&S, p, step)) < 0 ) { + goto fail; + } + mlen -= step; + p += step; + } + if ( (err = blake2b_update(&S, p, mlen)) < 0) { + goto fail; + } + if ( (err = blake2b_final(&S, hash, BLAKE2B_OUTBYTES)) < 0) { + goto fail; + } + + if (0 != memcmp(hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES)) { + goto fail; + } + } + } + + puts( "ok" ); + return 0; +fail: + puts("error"); + return -1; +} +#endif diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp index ae9503a9..eba65c8c 100644 --- a/src/crypto/crypto.cpp +++ b/src/crypto/crypto.cpp @@ -304,7 +304,7 @@ namespace crypto { ge_tobytes(&image, &point2); } -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4200) struct rs_comm_entry { @@ -314,7 +314,7 @@ struct rs_comm_entry hash h; struct rs_comm_entry ab[]; }; -POP_WARNINGS +POP_VS_WARNINGS static inline size_t rs_comm_size(size_t pubs_count) { return sizeof(rs_comm)+pubs_count * sizeof(rs_comm_entry); diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 3a3fafd8..9026ed36 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -17,7 +17,7 @@ #include "warnings.h" -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_CLANG_WARNING(unused-private-field) @@ -236,3 +236,4 @@ POD_MAKE_COMPARABLE(crypto, signature) POD_MAKE_COMPARABLE(crypto, key_derivation) POD_MAKE_LESS_OPERATOR(crypto, hash) POD_MAKE_LESS_OPERATOR(crypto, key_image) +POP_GCC_WARNINGS \ No newline at end of file diff --git a/src/crypto/hash-ops.h b/src/crypto/hash-ops.h index b199700e..1f156b1b 100644 --- a/src/crypto/hash-ops.h +++ b/src/crypto/hash-ops.h @@ -22,7 +22,7 @@ static inline const void *cpadd(const void *p, size_t i) { return (const char *) p + i; } -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4267) static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t must be 4 or 8 bytes long"); static inline void place_length(uint8_t *buffer, size_t bufsize, size_t length) { @@ -32,7 +32,7 @@ static inline void place_length(uint8_t *buffer, size_t bufsize, size_t length) *(uint64_t *) padd(buffer, bufsize - 8) = swap64be(length); } } -POP_WARNINGS +POP_VS_WARNINGS #pragma pack(push, 1) union hash_state { diff --git a/src/crypto/hash.h b/src/crypto/hash.h index d803b6e4..7fb0573d 100644 --- a/src/crypto/hash.h +++ b/src/crypto/hash.h @@ -8,7 +8,11 @@ #include "common/pod-class.h" #include "generic-ops.h" - +#include "warnings.h" +PUSH_VS_WARNINGS +DISABLE_VS_WARNINGS(4804) +#include "blake2.h" +POP_VS_WARNINGS namespace crypto { extern "C" { @@ -41,6 +45,12 @@ namespace crypto { tree_hash(reinterpret_cast(hashes), count, reinterpret_cast(&root_hash)); } + inline hash blake2_hash(const void *data, std::size_t length) { + hash h; + blake2(&h, sizeof(h), data, length, nullptr, 0); + return h; + } + } POD_MAKE_HASHABLE(crypto, hash) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index b1f3b9cc..0a7091b8 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -255,13 +255,13 @@ namespace currency END_SERIALIZE() }; - struct tx_message + struct tx_derivation_hint { std::string msg; BEGIN_SERIALIZE() FIELD(msg) - END_SERIALIZE() + END_SERIALIZE() }; struct tx_service_attachment @@ -382,7 +382,7 @@ namespace currency END_SERIALIZE() }; - struct etc_tx_derivation_hint + struct etc_tx_uint16_t { uint16_t v; BEGIN_SERIALIZE() @@ -390,7 +390,7 @@ namespace currency END_SERIALIZE() }; - typedef boost::mpl::vector all_payload_types; + typedef boost::mpl::vector all_payload_types; typedef boost::make_variant_over::type attachment_v; typedef boost::make_variant_over::type extra_v; typedef boost::make_variant_over::type payload_items_v; @@ -602,7 +602,7 @@ SET_VARIANT_TAGS(currency::tx_comment, 7, "comment"); SET_VARIANT_TAGS(currency::tx_payer, 8, "payer"); SET_VARIANT_TAGS(std::string, 9, "string"); SET_VARIANT_TAGS(currency::tx_crypto_checksum, 10, "checksum"); -SET_VARIANT_TAGS(currency::tx_message, 11, "message"); +SET_VARIANT_TAGS(currency::tx_derivation_hint, 11, "derivation_hint"); SET_VARIANT_TAGS(currency::tx_service_attachment, 12, "attachment"); //SET_VARIANT_TAGS(currency::tx_onetime_secret_key, 13, "sec_key"); SET_VARIANT_TAGS(currency::etc_tx_details_unlock_time, 14, "unlock_time"); @@ -616,7 +616,7 @@ SET_VARIANT_TAGS(currency::extra_user_data, 19, "user_data"); SET_VARIANT_TAGS(currency::extra_alias_entry, 20, "alias_entry"); SET_VARIANT_TAGS(currency::extra_padding, 21, "extra_padding"); SET_VARIANT_TAGS(crypto::public_key, 22, "pub_key"); -SET_VARIANT_TAGS(currency::etc_tx_derivation_hint, 23, "derive_hint"); +SET_VARIANT_TAGS(currency::etc_tx_uint16_t, 23, "etc_tx_uint16"); SET_VARIANT_TAGS(uint16_t, 24, "derive_xor"); //txout_v SET_VARIANT_TAGS(currency::ref_by_id, 25, "ref_by_id"); diff --git a/src/currency_core/currency_boost_serialization.h b/src/currency_core/currency_boost_serialization.h index 30e85e92..443c98c2 100644 --- a/src/currency_core/currency_boost_serialization.h +++ b/src/currency_core/currency_boost_serialization.h @@ -107,7 +107,7 @@ namespace boost a & x.derivation_hash; } template - inline void serialize(Archive &a, currency::tx_message &x, const boost::serialization::version_type ver) + inline void serialize(Archive &a, currency::tx_derivation_hint &x, const boost::serialization::version_type ver) { a & x.msg; } @@ -211,7 +211,7 @@ namespace boost } template - inline void serialize(Archive &a, currency::etc_tx_derivation_hint &at, const boost::serialization::version_type ver) + inline void serialize(Archive &a, currency::etc_tx_uint16_t&at, const boost::serialization::version_type ver) { a & at.v; } diff --git a/src/currency_core/currency_core.h b/src/currency_core/currency_core.h index 57fc1241..7fa9313a 100644 --- a/src/currency_core/currency_core.h +++ b/src/currency_core/currency_core.h @@ -21,7 +21,7 @@ #include "warnings.h" #include "crypto/hash.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4355) namespace currency @@ -159,4 +159,4 @@ namespace currency }; } -POP_WARNINGS +POP_VS_WARNINGS diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index d412947e..8fd732a3 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -516,6 +516,17 @@ namespace currency return r; } //--------------------------------------------------------------- + uint16_t get_derivation_hint(const crypto::key_derivation& derivation) + { + crypto::hash h = blake2_hash(&derivation, sizeof(derivation)); + + uint16_t* pderiv_hash_as_array = (uint16_t*)&h; + uint16_t res = pderiv_hash_as_array[0]; + for (size_t i = 1; i != sizeof(h) / sizeof(uint16_t); i++) + res ^= pderiv_hash_as_array[i]; + return res; + } + //--------------------------------------------------------------- uint64_t get_string_uint64_hash(const std::string& str) { crypto::hash h = crypto::cn_fast_hash(str.data(), str.size()); @@ -523,15 +534,22 @@ namespace currency return phash_as_array[0] ^ phash_as_array[1] ^ phash_as_array[2] ^ phash_as_array[3]; } //--------------------------------------------------------------- - uint16_t get_derivation_xor(const crypto::key_derivation& derivation) + tx_derivation_hint make_tx_derivation_hint_from_uint16(uint16_t hint) { - uint16_t* pderiv_as_array = (uint16_t*)&derivation; - uint16_t res = pderiv_as_array[0]; - for (size_t i = 1; i != sizeof(derivation) / sizeof(uint16_t); i++) - res ^= pderiv_as_array[i]; - return res; + tx_derivation_hint dh = AUTO_VAL_INIT(dh); + dh.msg.assign((const char*)&hint, sizeof(hint)); + return dh; } //--------------------------------------------------------------- +// bool get_uint16_from_tx_derivation_hint(const tx_derivation_hint& dh, uint16_t& hint) +// { +// tx_derivation_hint dh; +// if (dh.msg.size() != sizeof(hint)) +// return false; +// hint = *((uint16_t*)dh.msg.data()); +// return true; +// } + //--------------------------------------------------------------- bool construct_tx_out(const tx_destination_entry& de, const crypto::secret_key& tx_sec_key, size_t output_index, transaction& tx, std::set& deriv_cache, uint8_t tx_outs_attr) { CHECK_AND_ASSERT_MES(de.addr.size() == 1 || (de.addr.size() > 1 && de.minimum_sigs <= de.addr.size()), false, "Invalid destination entry: amount: " << de.amount << " minimum_sigs: " << de.minimum_sigs << " addr.size(): " << de.addr.size()); @@ -551,12 +569,12 @@ namespace currency crypto::key_derivation derivation = AUTO_VAL_INIT(derivation); bool r = derive_public_key_from_target_address(apa, tx_sec_key, output_index, out_eph_public_key, derivation); CHECK_AND_ASSERT_MES(r, false, "failed to derive_public_key_from_target_address"); - etc_tx_derivation_hint dh = AUTO_VAL_INIT(dh); - dh.v = get_derivation_xor(derivation); - if (deriv_cache.count(dh.v) == 0) + + uint16_t hint = get_derivation_hint(derivation); + if (deriv_cache.count(hint) == 0) { - tx.extra.push_back(dh); - deriv_cache.insert(dh.v); + tx.extra.push_back(make_tx_derivation_hint_from_uint16(hint)); + deriv_cache.insert(hint); } } target_keys.push_back(out_eph_public_key); @@ -646,11 +664,6 @@ namespace currency crypto::chacha_crypt(m.acc_addr, m_key); m_was_crypted_entries = true; } - void operator()(tx_message& m) - { - crypto::chacha_crypt(m.msg, m_key); - m_was_crypted_entries = true; - } void operator()(tx_service_attachment& sa) { if (sa.flags&TX_SERVICE_ATTACHMENT_DEFLATE_BODY) @@ -686,13 +699,6 @@ namespace currency rdecrypted_att.push_back(local_comment); } - void operator()(const tx_message& m) - { - tx_message local_msg = m; - crypto::chacha_crypt(local_msg.msg, rkey); - rdecrypted_att.push_back(local_msg); - } - void operator()(const tx_service_attachment& sa) { tx_service_attachment local_sa = sa; @@ -1544,14 +1550,19 @@ namespace currency bool check_tx_derivation_hint(const transaction& tx, const crypto::key_derivation& derivation) { bool found_der_xor = false; - uint16_t my_derive_xor = get_derivation_xor(derivation); + uint16_t my_derive_xor = get_derivation_hint(derivation); + tx_derivation_hint dh = make_tx_derivation_hint_from_uint16(my_derive_xor); for (auto& e : tx.extra) { - if (e.type() == typeid(etc_tx_derivation_hint)) + if (e.type() == typeid(tx_derivation_hint)) { - found_der_xor = true; - if (my_derive_xor == boost::get(e).v) - return true; + const tx_derivation_hint& tdh = boost::get(e); + if (tdh.msg.size() == sizeof(uint16_t)) + { + found_der_xor = true; + if (dh.msg == tdh.msg) + return true; + } } } //if tx doesn't have any hints - feature is not supported, use full scan @@ -2204,9 +2215,9 @@ namespace currency return true; } - bool operator()(const tx_message& ee) + bool operator()(const tx_derivation_hint& ee) { - tv.type = "message"; + tv.type = "derivation_hint"; tv.short_view = std::to_string(ee.msg.size()) + " bytes"; tv.datails_view = epee::string_tools::buff_to_hex_nodelimer(ee.msg); @@ -2220,7 +2231,7 @@ namespace currency return true; } - bool operator()(const etc_tx_derivation_hint& dh) + bool operator()(const etc_tx_uint16_t& dh) { tv.type = "XOR"; tv.short_view = epee::string_tools::pod_to_hex(dh); diff --git a/src/currency_protocol/currency_protocol_handler.h b/src/currency_protocol/currency_protocol_handler.h index a03d6cf3..08dd8ea8 100644 --- a/src/currency_protocol/currency_protocol_handler.h +++ b/src/currency_protocol/currency_protocol_handler.h @@ -19,7 +19,7 @@ #undef LOG_DEFAULT_CHANNEL #define LOG_DEFAULT_CHANNEL "currency_protocol" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4355) #define ASYNC_RELAY_MODE @@ -145,4 +145,4 @@ namespace currency #undef LOG_DEFAULT_CHANNEL #define LOG_DEFAULT_CHANNEL NULL -POP_WARNINGS +POP_VS_WARNINGS diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index 35874afc..532e35d1 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -17,7 +17,7 @@ #include "currency_core/bc_offers_service.h" #include "serialization/binary_utils.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) class daemon_commands_handler @@ -874,4 +874,4 @@ private: }; -POP_WARNINGS \ No newline at end of file +POP_VS_WARNINGS \ No newline at end of file diff --git a/src/gui/qt-daemon/application/daemon_backend.h b/src/gui/qt-daemon/application/daemon_backend.h index 372919f3..1cffbedc 100644 --- a/src/gui/qt-daemon/application/daemon_backend.h +++ b/src/gui/qt-daemon/application/daemon_backend.h @@ -8,7 +8,7 @@ #include #include "warnings.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) DISABLE_VS_WARNINGS(4503) #include "include_base_utils.h" @@ -30,7 +30,7 @@ using namespace epee; #include "wallet/wallet2.h" #include "wallet_id_adapter.h" -POP_WARNINGS +POP_VS_WARNINGS namespace po = boost::program_options; diff --git a/src/gui/qt-daemon/application/view_iface.h b/src/gui/qt-daemon/application/view_iface.h index 41a66838..5fe8817c 100644 --- a/src/gui/qt-daemon/application/view_iface.h +++ b/src/gui/qt-daemon/application/view_iface.h @@ -11,7 +11,7 @@ #ifndef Q_MOC_RUN #include "warnings.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) DISABLE_VS_WARNINGS(4503) #include "serialization/keyvalue_serialization.h" @@ -20,7 +20,7 @@ DISABLE_VS_WARNINGS(4503) #include "wallet/wallet_rpc_server_commans_defs.h" #include "currency_core/offers_services_helpers.h" #include "currency_core/basic_api_response_codes.h" -POP_WARNINGS +POP_VS_WARNINGS #endif diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index 0c1a6276..2c0a840b 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -35,7 +35,7 @@ using namespace epee; #define CURRENT_P2P_STORAGE_ARCHIVE_VER (CURRENCY_FORMATION_VERSION+13) -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4355) namespace nodetool @@ -300,4 +300,4 @@ namespace nodetool #undef LOG_DEFAULT_CHANNEL #define LOG_DEFAULT_CHANNEL NULL -POP_WARNINGS +POP_VS_WARNINGS diff --git a/src/pch/stdafx.h b/src/pch/stdafx.h index 68e673c9..88ac8014 100644 --- a/src/pch/stdafx.h +++ b/src/pch/stdafx.h @@ -31,10 +31,10 @@ #include #include #include -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4800) #include -POP_WARNINGS +POP_VS_WARNINGS #include #include #include diff --git a/src/serialization/binary_archive.h b/src/serialization/binary_archive.h index 16ac2074..b951a106 100644 --- a/src/serialization/binary_archive.h +++ b/src/serialization/binary_archive.h @@ -16,7 +16,7 @@ #include "common/varint.h" #include "warnings.h" -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4244) DISABLE_VS_WARNINGS(4100) @@ -172,4 +172,4 @@ struct binary_archive : public binary_archive_base } }; -POP_WARNINGS +POP_VS_WARNINGS diff --git a/tests/core_tests/checkpoints_tests.cpp b/tests/core_tests/checkpoints_tests.cpp index 8f1ec3b5..50a47140 100644 --- a/tests/core_tests/checkpoints_tests.cpp +++ b/tests/core_tests/checkpoints_tests.cpp @@ -87,8 +87,8 @@ bool gen_checkpoints_attachments_basic::generate(std::vector& tx_comment cm; cm.comment = "This is tx comment."; - tx_message ms; - ms.msg = "This is tx message."; + std::string ms; + ms = "This is tx message."; std::vector attachments; attachments.push_back(cm); @@ -628,7 +628,7 @@ bool gen_no_attchments_in_coinbase::init_config_set_cp(currency::core& c, size_t crc.pos_minimum_heigh = 1; c.get_blockchain_storage().set_core_runtime_config(crc); - m_checkpoints.add_checkpoint(12, "2cb91dade404a4b1158d3250fe042399ecb12d1152b49900ea40d700bf4060ad"); + m_checkpoints.add_checkpoint(12, "2a6e13df811eccce121c0de4dbdcc640de1d37c8459c2c8ea02af39717779836"); c.set_checkpoints(currency::checkpoints(m_checkpoints)); return true; diff --git a/tests/core_tests/escrow_wallet_common.h b/tests/core_tests/escrow_wallet_common.h index 12ef5e3e..db6659a1 100644 --- a/tests/core_tests/escrow_wallet_common.h +++ b/tests/core_tests/escrow_wallet_common.h @@ -295,7 +295,7 @@ inline bool build_custom_escrow_proposal(const std::vector& ev attachments.push_back(sa); if (custom_config_mask & eccf_proposal_additional_attach) - attachments.push_back(tx_message({ get_random_text(1024) })); + attachments.push_back(std::string(get_random_text(1024))); r = fill_tx_sources(sources, events, head, a_keys, a_fee_proposal, nmix, used_sources); CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources failed"); @@ -404,7 +404,7 @@ inline bool build_custom_escrow_release_template( std::vector attachments; if (release_type == BC_ESCROW_SERVICE_INSTRUCTION_RELEASE_BURN) { - attachments.push_back(tx_message({ "We don't need no water -- so let all the coins burn!" })); // attachments are allowed and must be normally handled, so one of release template will always have them + attachments.push_back(std::string("We don't need no water -- so let all the coins burn!")); // attachments are allowed and must be normally handled, so one of release template will always have them attachments.push_back(tx_comment({ "Burn, all the coins, burn!" })); } diff --git a/tests/core_tests/multisig_wallet_tests.cpp b/tests/core_tests/multisig_wallet_tests.cpp index 8c46c176..169fa255 100644 --- a/tests/core_tests/multisig_wallet_tests.cpp +++ b/tests/core_tests/multisig_wallet_tests.cpp @@ -1622,7 +1622,7 @@ multisig_and_checkpoints::multisig_and_checkpoints() bool multisig_and_checkpoints::set_cp(currency::core& c, size_t ev_index, const std::vector& events) { currency::checkpoints checkpoints; - checkpoints.add_checkpoint(15, "726c5900f2ed7985619a54ecad3a5f5a11176a0873c51acb46ac60126a71c30a"); + checkpoints.add_checkpoint(15, "a8e535abb31cd2c07ebd65dbb8d4160954677a0cebcd3dadf81642297cc557af"); c.set_checkpoints(std::move(checkpoints)); return true; diff --git a/tests/core_tests/tx_validation.cpp b/tests/core_tests/tx_validation.cpp index 4d8ddc3b..5c6c91c6 100644 --- a/tests/core_tests/tx_validation.cpp +++ b/tests/core_tests/tx_validation.cpp @@ -791,12 +791,12 @@ bool gen_crypted_attachments::generate(std::vector& events) co pr.acc_addr = miner_account.get_keys().m_account_address; cm.comment = "Comandante Che Guevara"; - ms.msg = "Hasta Siempre, Comandante"; + //ms.msg = "Hasta Siempre, Comandante"; std::vector attachments; attachments.push_back(pr); attachments.push_back(cm); - attachments.push_back(ms); + //attachments.push_back(ms); MAKE_TX_LIST_START(events, txs_list, miner_account, bob_account, MK_TEST_COINS(1), blk_5); MAKE_TX_LIST_ATTACH(events, txs_list, miner_account, bob_account, MK_TEST_COINS(1), blk_5, attachments); @@ -934,7 +934,7 @@ bool gen_crypted_attachments::check_crypted_tx(currency::core& c, size_t ev_inde std::vector at; bool r = currency::decrypt_payload_items(true, *ptx_from_bc, bob_acc.get_keys(), at); CHECK_EQ(r, true); - CHECK_EQ(at.size(), 8); // custom attachments: 1) tx_payer, 2) tx_comment, 3) tx_message; system attachments: 4) tx_crypto_checksum; system extra: 5) tx pub key, 6) extra_attachment_info + CHECK_EQ(at.size(), 7); // custom attachments: 1) tx_payer, 2) tx_comment, 3) std::string; system attachments: 4) tx_crypto_checksum; system extra: 5) tx pub key, 6) extra_attachment_info currency::tx_payer decrypted_pr = AUTO_VAL_INIT(decrypted_pr); r = get_type_in_variant_container(at, decrypted_pr); @@ -958,16 +958,16 @@ bool gen_crypted_attachments::check_crypted_tx(currency::core& c, size_t ev_inde return false; } - currency::tx_message decrypted_message = AUTO_VAL_INIT(decrypted_message); - r = get_type_in_variant_container(at, decrypted_message); - CHECK_AND_ASSERT_MES(r, false, "get_type_in_variant_container failed"); - if (ms.msg != decrypted_message.msg) - { - LOG_ERROR("decrypted wrong data: " - << decrypted_message.msg - << "expected:" << ms.msg); - return false; - } +// currency::tx_message decrypted_message = AUTO_VAL_INIT(decrypted_message); +// r = get_type_in_variant_container(at, decrypted_message); +// CHECK_AND_ASSERT_MES(r, false, "get_type_in_variant_container failed"); +// if (ms.msg != decrypted_message.msg) +// { +// LOG_ERROR("decrypted wrong data: " +// << decrypted_message.msg +// << "expected:" << ms.msg); +// return false; +// } // now cancel attachment diff --git a/tests/core_tests/tx_validation.h b/tests/core_tests/tx_validation.h index 543d1458..6996f3de 100644 --- a/tests/core_tests/tx_validation.h +++ b/tests/core_tests/tx_validation.h @@ -118,7 +118,7 @@ struct gen_crypted_attachments : test_chain_unit_enchanced mutable uint64_t offers_before_canel; mutable currency::tx_payer pr; mutable currency::tx_comment cm; - mutable currency::tx_message ms; + //mutable currency::tx_message ms; }; struct gen_tx_extra_double_entry : test_chain_unit_enchanced diff --git a/tests/core_tests/wallet_tests.cpp b/tests/core_tests/wallet_tests.cpp index 511250fa..c2a23198 100644 --- a/tests/core_tests/wallet_tests.cpp +++ b/tests/core_tests/wallet_tests.cpp @@ -1439,8 +1439,8 @@ bool gen_wallet_decrypted_attachments::generate(std::vector& e a_tx_payer.acc_addr = miner_acc.get_keys().m_account_address; currency::tx_comment a_tx_comment = AUTO_VAL_INIT(a_tx_comment); a_tx_comment.comment = "Comandante Che Guevara"; - currency::tx_message a_tx_message = AUTO_VAL_INIT(a_tx_message); - a_tx_message.msg = "Hasta Siempre, Comandante"; + std::string a_tx_message = AUTO_VAL_INIT(a_tx_message); + a_tx_message = "Hasta Siempre, Comandante"; { diff --git a/tests/crypto/main.cpp b/tests/crypto/main.cpp index 037aaac8..3b9dcf50 100644 --- a/tests/crypto/main.cpp +++ b/tests/crypto/main.cpp @@ -15,7 +15,7 @@ #include "../io.h" #include "warnings.h" -PUSH_WARNINGS +PUSH_GCC_WARNINGS DISABLE_GCC_WARNING(strict-aliasing) @@ -250,4 +250,4 @@ error: } return error ? 1 : 0; } -POP_WARNINGS +POP_GCC_WARNINGS diff --git a/tests/hash/main.cpp b/tests/hash/main.cpp index fc8d6c00..0bea5ac7 100644 --- a/tests/hash/main.cpp +++ b/tests/hash/main.cpp @@ -16,7 +16,7 @@ using namespace std; using namespace crypto; typedef crypto::hash chash; -PUSH_WARNINGS +PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4297) extern "C" { static void hash_tree(const void *data, size_t length, char *hash) { @@ -26,7 +26,7 @@ extern "C" { tree_hash((const char (*)[32]) data, length >> 5, hash); } } -POP_WARNINGS +POP_VS_WARNINGS extern "C" typedef void hash_f(const void *, size_t, char *); struct hash_func { diff --git a/tests/performance_tests/blake2_test.h b/tests/performance_tests/blake2_test.h new file mode 100644 index 00000000..f9bcb434 --- /dev/null +++ b/tests/performance_tests/blake2_test.h @@ -0,0 +1,60 @@ +// Copyright (c) 2019 The Zano developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include +#include "include_base_utils.h" +#include "crypto/blake2.h" + + +inline +void test_blake2() +{ + std::string message = "The quick brown fox jumps over the lazy dog"; + static uint8_t buff[64] = {}; + static uint8_t buff2[2] = {}; + + performance_timer timer; + timer.start(); + for (size_t i = 0; i != 100000; i++) + { + *((size_t*)message.data()) = i; + blake2(buff, sizeof(buff), message.data(), message.size(), nullptr, 0); + } + + std::cout << "x100000 blake2 operations, out 64 bytes. Elapsed time: " << timer.elapsed_ms() << " msec" << std::endl; + + timer.start(); + for (size_t i = 0; i != 1000000; i++) + { + *((size_t*)message.data()) = i; + blake2(buff, sizeof(buff), message.data(), message.size(), nullptr, 0); + } + + std::cout << "x1000000 blake2 operations, out 64 bytes. Elapsed time: " << timer.elapsed_ms() << " msec" << std::endl; + + + timer.start(); + for (size_t i = 0; i != 1000000; i++) + { + *((size_t*)message.data()) = i; + blake2(buff2, sizeof(buff2), message.data(), message.size(), nullptr, 0); + } + + std::cout << "x1000000 blake2 operations, out 2 bytes. Elapsed time: " << timer.elapsed_ms() << " msec" << std::endl; + + static crypto::hash h = AUTO_VAL_INIT(h); + timer.start(); + for (size_t i = 0; i != 1000000; i++) + { + *((size_t*)message.data()) = i; + crypto::cn_fast_hash(message.data(), message.size(), h); + } + + std::cout << "x1000000 cn_fast_hash operations, out 32 bytes. Elapsed time: " << timer.elapsed_ms() << " msec" << std::endl; + + + //std::cout << epee::string_tools::pod_to_hex(buff); +} \ No newline at end of file diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index 7e9831a6..74a9da5c 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -18,6 +18,7 @@ #include "core_market_performance_test.h" #include "serialization_performance_test.h" #include "keccak_test.h" +#include "blake2_test.h" int main(int argc, char** argv) { @@ -32,8 +33,10 @@ int main(int argc, char** argv) set_process_affinity(1); set_thread_high_priority(); - performance_timer timer; - timer.start(); + test_blake2(); + + //performance_timer timer; + //timer.start(); //generate_scratchpad(); //generate_light_scratchpad(); @@ -68,12 +71,12 @@ int main(int argc, char** argv) */ //TEST_PERFORMANCE0(test_is_out_to_acc); //TEST_PERFORMANCE0(test_generate_key_image_helper); - TEST_PERFORMANCE0(test_generate_key_derivation); + //TEST_PERFORMANCE0(test_generate_key_derivation); //TEST_PERFORMANCE0(test_generate_key_image); //TEST_PERFORMANCE0(test_derive_public_key); //TEST_PERFORMANCE0(test_derive_secret_key); - std::cout << "Tests finished. Elapsed time: " << timer.elapsed_ms() / 1000 << " sec" << std::endl; + //std::cout << "Tests finished. Elapsed time: " << timer.elapsed_ms() / 1000 << " sec" << std::endl; return 0; }