From 6895464e0cd5c5ab27a84e689666071d0555a186 Mon Sep 17 00:00:00 2001 From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net> Date: Sun, 20 Oct 2019 17:58:38 +0700 Subject: [PATCH] [*] Rework --- CMakeLists.txt | 236 +++++++++++++++++++++++------------------------ dap_chain_sign.c | 2 +- dap_hash.c | 75 +++++++-------- dap_hash.h | 195 ++++++++++++++++++++------------------- 4 files changed, 250 insertions(+), 258 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cc4baa..f4bf345 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,118 +1,118 @@ -cmake_minimum_required(VERSION 2.8) -project (dap_chain_crypto) - -set(DAP_CHAIN_CRYPTO_SRCS - dap_hash.c - dap_hash_fusion.c - dap_hash_keccak.c - dap_hash_slow.c - dap_chain_pkey.c - dap_chain_sign.c - dap_chain_cert.c - dap_chain_cert_file.c - ) - -set(DAP_CHAIN_CRYPTO_HEADERS - dap_hash.h - dap_hash_fusion.h - dap_hash_keccak.h - dap_hash_slow.h - dap_chain_pkey.h - dap_chain_sign.h - dap_chain_cert.h - dap_chain_cert_file.h - ) - -if(NOT (${SUBMODULES_NO_BUILD} MATCHES ON)) - set(SUBMODULES_NO_BUILD ON) - - # Check whether we're on a 32-bit or 64-bit system - if(CMAKE_SIZEOF_VOID_P EQUAL "8") - set(DEFAULT_BUILD_64 ON) - else() - set(DEFAULT_BUILD_64 OFF) - endif() - option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64}) - - add_definitions ("-DDAP_SERVER") - add_definitions ("-DNODE_NETNAME=\"kelvin\"") - - set(_CCOPT "-Wall -O2 -pg -fPIC -fno-pie -no-pie") - set(_LOPT "-pg") - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}") - set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}") - - if (NOT (TARGET dap_core)) - add_subdirectory(libdap) - endif() - if (NOT (TARGET dap_crypto)) - add_subdirectory(libdap-crypto) - endif() - if (NOT (TARGET dap_chain)) - add_subdirectory(libdap-chain) - endif() - if (NOT (TARGET dap_chain_mempool)) - add_subdirectory(libdap-chain-mempool) - endif() - if (NOT (TARGET dap_server_core)) - add_subdirectory(libdap-server-core) - endif() - if (NOT (TARGET dap_chain_net)) - add_subdirectory(libdap-chain-net) - endif() - if (NOT (TARGET dap_chain_global_db)) - add_subdirectory(libdap-chain-global-db) - endif() - if (NOT (TARGET dap_client)) - add_subdirectory(libdap-client) - endif() - if (NOT (TARGET dap_server)) - add_subdirectory(libdap-server) - endif() - if (NOT (TARGET dap_udp_server)) - add_subdirectory(libdap-server-udp) - endif() - if (NOT (TARGET libdap-stream)) - add_subdirectory(libdap-stream) - endif() - if (NOT (TARGET dap_stream_ch)) - add_subdirectory(libdap-stream-ch) - endif() - if (NOT (TARGET dap_stream_ch_chain)) - add_subdirectory(libdap-stream-ch-chain) - endif() - if (NOT (TARGET dap_stream_ch_chain_net)) - add_subdirectory(libdap-stream-ch-chain-net) - endif() - if (NOT (TARGET dap_chain_wallet)) - add_subdirectory(libdap-chain-wallet) - endif() - if (NOT (TARGET dap_chain_net_srv)) - add_subdirectory(libdap-chain-net-srv) - endif() - if (NOT (TARGET dap_server_http_db_auth)) - add_subdirectory(libdap-server-http-db-auth) - endif() - if (NOT (TARGET dap_chain_gdb)) - add_subdirectory(libdap-chain-gdb) - endif() -endif() - -add_subdirectory(monero_crypto) - -include_directories("${monero_crypto_INCLUDE_DIRS}") -add_definitions ("${monero_crypto_DEFINITIONS}") - -add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_CRYPTO_SRCS} ${DAP_CHAIN_CRYPTO_HEADERS}) - -target_include_directories(dap_chain_crypto INTERFACE .) -target_link_libraries(dap_chain_crypto dap_core dap_crypto dap_chain monero_crypto) - -set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) -set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) - -if (${BUILD_DAP_CHAIN_CRYPTO_TESTS} MATCHES ON) - enable_testing() - add_subdirectory(test) -endif() +cmake_minimum_required(VERSION 2.8) +project (dap_chain_crypto) + +set(DAP_CHAIN_CRYPTO_SRCS + dap_hash.c + dap_hash_fusion.c + dap_hash_keccak.c + dap_hash_slow.c + dap_chain_pkey.c + dap_chain_sign.c + dap_chain_cert.c + dap_chain_cert_file.c + ) + +set(DAP_CHAIN_CRYPTO_HEADERS + dap_hash.h + dap_hash_fusion.h + dap_hash_keccak.h + dap_hash_slow.h + dap_chain_pkey.h + dap_chain_sign.h + dap_chain_cert.h + dap_chain_cert_file.h + ) + +if(NOT (${SUBMODULES_NO_BUILD} MATCHES ON)) + set(SUBMODULES_NO_BUILD ON) + + # Check whether we're on a 32-bit or 64-bit system + if(CMAKE_SIZEOF_VOID_P EQUAL "8") + set(DEFAULT_BUILD_64 ON) + else() + set(DEFAULT_BUILD_64 OFF) + endif() + option(BUILD_64 "Build for 64-bit? 'OFF' builds for 32-bit." ${DEFAULT_BUILD_64}) + + add_definitions ("-DDAP_SERVER") + add_definitions ("-DNODE_NETNAME=\"kelvin\"") + + set(_CCOPT "-Wall -O2 -pg -fPIC -fno-pie -no-pie") + set(_LOPT "-pg") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_CCOPT}") + set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${_LOPT}") + + if (NOT (TARGET dap_core)) + add_subdirectory(libdap) + endif() + if (NOT (TARGET dap_crypto)) + add_subdirectory(libdap-crypto) + endif() + if (NOT (TARGET dap_chain)) + add_subdirectory(libdap-chain) + endif() + if (NOT (TARGET dap_chain_mempool)) + add_subdirectory(libdap-chain-mempool) + endif() + if (NOT (TARGET dap_server_core)) + add_subdirectory(libdap-server-core) + endif() + if (NOT (TARGET dap_chain_net)) + add_subdirectory(libdap-chain-net) + endif() + if (NOT (TARGET dap_chain_global_db)) + add_subdirectory(libdap-chain-global-db) + endif() + if (NOT (TARGET dap_client)) + add_subdirectory(libdap-client) + endif() + if (NOT (TARGET dap_server)) + add_subdirectory(libdap-server) + endif() + if (NOT (TARGET dap_udp_server)) + add_subdirectory(libdap-server-udp) + endif() + if (NOT (TARGET libdap-stream)) + add_subdirectory(libdap-stream) + endif() + if (NOT (TARGET dap_stream_ch)) + add_subdirectory(libdap-stream-ch) + endif() + if (NOT (TARGET dap_stream_ch_chain)) + add_subdirectory(libdap-stream-ch-chain) + endif() + if (NOT (TARGET dap_stream_ch_chain_net)) + add_subdirectory(libdap-stream-ch-chain-net) + endif() + if (NOT (TARGET dap_chain_wallet)) + add_subdirectory(libdap-chain-wallet) + endif() + if (NOT (TARGET dap_chain_net_srv)) + add_subdirectory(libdap-chain-net-srv) + endif() + if (NOT (TARGET dap_server_http_db_auth)) + add_subdirectory(libdap-server-http-db-auth) + endif() + if (NOT (TARGET dap_chain_gdb)) + add_subdirectory(libdap-chain-gdb) + endif() +endif() + +add_subdirectory(monero_crypto) + +include_directories("${monero_crypto_INCLUDE_DIRS}") +add_definitions ("${monero_crypto_DEFINITIONS}") + +add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_CRYPTO_SRCS} ${DAP_CHAIN_CRYPTO_HEADERS}) + +target_include_directories(dap_chain_crypto INTERFACE .) +target_link_libraries(dap_chain_crypto dap_core dap_crypto dap_chain monero_crypto) + +set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) +set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) + +if (${BUILD_DAP_CHAIN_CRYPTO_TESTS} MATCHES ON) + enable_testing() + add_subdirectory(test) +endif() diff --git a/dap_chain_sign.c b/dap_chain_sign.c index f84d3e1..92f8b10 100755 --- a/dap_chain_sign.c +++ b/dap_chain_sign.c @@ -240,7 +240,7 @@ dap_enc_key_t *dap_chain_sign_to_enc_key(dap_chain_sign_t * a_chain_sign) { dap_enc_key_t * l_ret = dap_enc_key_new( dap_chain_sign_type_to_key_type( a_chain_sign->header.type ) ); size_t l_pkey_size = 0; - const uint8_t *l_pkey = dap_chain_sign_get_pkey(a_chain_sign, &l_pkey_size); + uint8_t *l_pkey = dap_chain_sign_get_pkey(a_chain_sign, &l_pkey_size); // deserialize public key dap_enc_key_deserealize_pub_key(l_ret, l_pkey, l_pkey_size); return l_ret; diff --git a/dap_hash.c b/dap_hash.c index 49b0d91..0fe8fb5 100755 --- a/dap_hash.c +++ b/dap_hash.c @@ -1,44 +1,31 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <kahovski@gmail.com> - * DeM Labs Inc. https://demlabs.net - * DeM Labs Open source community https://github.com/demlabsinc - * Copyright (c) 2017-2018 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) 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 3 of the License, or - (at your option) any later version. - - DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "dap_common.h" -#include "dap_hash.h" - -#include "KeccakHash.h" -#include "SimpleFIPS202.h" - -#define LOG_TAG "dap_hash" - -/** -int dap_hash_fast(const void *a_data_in, size_t a_data_in_size, dap_chain_hash_fast_t *a_hash_out) -{ - if(!a_data_in || !a_data_in_size || !a_hash_out) - return -1; -// dap_hash(a_data_in, a_data_in_size, a_hash_out->raw, sizeof(a_hash_out->raw), -// DAP_HASH_TYPE_KECCAK); - SHA3_256( (unsigned char *)a_hash_out, (const unsigned char *)a_data_in, a_data_in_size ); - - return 1; -} -**/ +/* + * Authors: + * Dmitriy A. Gearasimov <kahovski@gmail.com> + * DeM Labs Inc. https://demlabs.net + * DeM Labs Open source community https://github.com/demlabsinc + * Copyright (c) 2017-2018 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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 3 of the License, or + (at your option) any later version. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "dap_common.h" +#include "dap_hash.h" + +#include "KeccakHash.h" +#include "SimpleFIPS202.h" + +#define LOG_TAG "dap_hash" diff --git a/dap_hash.h b/dap_hash.h index 6b993a5..088ea10 100755 --- a/dap_hash.h +++ b/dap_hash.h @@ -1,95 +1,100 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <kahovski@gmail.com> - * DeM Labs Inc. https://demlabs.net - * DeM Labs Open source community https://github.com/demlabsinc - * Copyright (c) 2017-2018 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) 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 3 of the License, or - (at your option) any later version. - - DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ -#pragma once -#include <stddef.h> -#include <stdbool.h> -#include <stdint.h> - -#include "dap_hash_slow.h" -#include "dap_hash_keccak.h" -#include "dap_chain_common.h" - -#include "KeccakHash.h" -#include "SimpleFIPS202.h" - -#define DAP_HASH_FAST_SIZE 32 - -typedef enum dap_hash_type { - DAP_HASH_TYPE_KECCAK = 0, - DAP_HASH_TYPE_SLOW_0 = 1, -} dap_hash_type_t; - -static inline int dap_hash_fast( const void *a_data_in, size_t a_data_in_size, void *a_hash_out ) -{ - if (!a_data_in || !a_data_in_size || !a_hash_out) - return -1; - -// dap_hash(a_data_in, a_data_in_size, a_hash_out->raw, sizeof(a_hash_out->raw), -// DAP_HASH_TYPE_KECCAK); - - SHA3_256( (unsigned char *)a_hash_out, (const unsigned char *)a_data_in, a_data_in_size ); - - return 1; -} - -static inline void dap_hash(const void * a_data_in, size_t a_data_in_size, - void * a_data_out, size_t a_data_out_size, - dap_hash_type_t a_type ){ - switch (a_type){ - case DAP_HASH_TYPE_KECCAK: -// dap_hash_keccak( a_data_in, a_data_in_size, a_data_out, a_data_out_size ); - SHA3_256( (unsigned char *)a_data_out, (const unsigned char *)a_data_in, a_data_in_size ); - break; - case DAP_HASH_TYPE_SLOW_0: - if( a_data_out_size>= dap_hash_slow_size() ){ - dap_hash_slow(a_data_in,a_data_in_size,(char*) a_data_out); - } - break; - } -} - -static uint64_t blank_hash[4] = { 0, 0, 0, 0 }; - -static inline bool dap_hash_fast_compare( void *a_hash1, void *a_hash2 ) -{ - if( !memcmp(a_hash1, a_hash2, DAP_HASH_FAST_SIZE) ) - return true; - - return false; -} - -static inline bool dap_hash_fast_is_blank( void *a_hash ) -{ -// uint8_t *l_hast_bytes = (uint8_t*) a_hash; -// for(size_t i = 0; i < sizeof(dap_chain_hash_fast_t); i++) { -// if(l_hast_bytes[i]) -// return false; -// } - return dap_hash_fast_compare( a_hash, &blank_hash[0] ); -} - - -//int dap_hash_fast(const void *a_data_in, size_t a_data_in_size, dap_chain_hash_fast_t *a_hash_out); -//bool dap_hash_fast_is_blank(dap_chain_hash_fast_t *a_hash); -//bool dap_hash_fast_compare(dap_chain_hash_fast_t *a_hash1, dap_chain_hash_fast_t *a_hash2); +/* + * Authors: + * Dmitriy A. Gearasimov <kahovski@gmail.com> + * DeM Labs Inc. https://demlabs.net + * DeM Labs Open source community https://github.com/demlabsinc + * Copyright (c) 2017-2018 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) 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 3 of the License, or + (at your option) any later version. + + DAP 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 any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ +#pragma once +#include <stddef.h> +#include <stdbool.h> +#include <stdint.h> + +#include "dap_hash_slow.h" +#include "dap_hash_keccak.h" +#include "dap_chain_common.h" + +#include "KeccakHash.h" +#include "SimpleFIPS202.h" + +#define DAP_HASH_FAST_SIZE 32 + +typedef enum dap_hash_type { + DAP_HASH_TYPE_KECCAK = 0, + DAP_HASH_TYPE_SLOW_0 = 1, +} dap_hash_type_t; + + +static inline void dap_hash(const void * a_data_in, size_t a_data_in_size, + void * a_data_out, size_t a_data_out_size, + dap_hash_type_t a_type ){ + switch (a_type){ + case DAP_HASH_TYPE_KECCAK: +// dap_hash_keccak( a_data_in, a_data_in_size, a_data_out, a_data_out_size ); + SHA3_256( (unsigned char *)a_data_out, (const unsigned char *)a_data_in, a_data_in_size ); + break; + case DAP_HASH_TYPE_SLOW_0: + if( a_data_out_size>= dap_hash_slow_size() ){ + dap_hash_slow(a_data_in,a_data_in_size,(char*) a_data_out); + } + break; + } +} + +static inline bool dap_hash_fast( const void *a_data_in, size_t a_data_in_size, dap_chain_hash_fast_t *a_hash_out ) +{ + if ( (a_data_in == NULL) || (a_data_in_size == 0) || (a_hash_out == NULL) ) + return false; + + dap_hash(a_data_in, a_data_in_size, a_hash_out->raw, sizeof(a_hash_out->raw ), + DAP_HASH_TYPE_KECCAK); + + //SHA3_256( (unsigned char *)a_hash_out, (const unsigned char *)a_data_in, a_data_in_size ); + + return true; +} + + +/** + * @brief dap_hash_fast_compare + * @param a_hash1 + * @param a_hash2 + * @return + */ +static inline bool dap_hash_fast_compare(dap_chain_hash_fast_t *a_hash1, dap_chain_hash_fast_t *a_hash2) +{ + if(!a_hash1 || !a_hash2) + return false; + if(!memcmp(a_hash1, a_hash2, sizeof(dap_chain_hash_fast_t))) + return true; + return false; +} + +static inline bool dap_hash_fast_is_blank( dap_chain_hash_fast_t *a_hash ) +{ + static dap_chain_hash_fast_t l_blank_hash = { 0}; +// uint8_t *l_hast_bytes = (uint8_t*) a_hash; +// for(size_t i = 0; i < sizeof(dap_chain_hash_fast_t); i++) { +// if(l_hast_bytes[i]) +// return false; +// } + return dap_hash_fast_compare( a_hash, &l_blank_hash); +} + + -- GitLab