Remove ed25519 and lib library circular dependency (#1870)
This commit is contained in:
parent
c45bfae43d
commit
6ef586bfa9
29 changed files with 127 additions and 83 deletions
|
@ -288,6 +288,7 @@ add_library (blake2
|
|||
|
||||
target_compile_definitions(blake2 PRIVATE -D__SSE2__)
|
||||
|
||||
add_subdirectory(nano/crypto_lib)
|
||||
add_subdirectory(nano/secure)
|
||||
add_subdirectory(nano/lib)
|
||||
add_subdirectory(nano/node)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <nano/core_test/testutil.hpp>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/node/common.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <fstream>
|
||||
#include <nano/core_test/testutil.hpp>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/testing.hpp>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <nano/core_test/testutil.hpp>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/testing.hpp>
|
||||
#include <nano/node/websocket.hpp>
|
||||
#include <sstream>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/jsonconfig.hpp>
|
||||
#include <nano/lib/timer.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
|
|
8
nano/crypto_lib/CMakeLists.txt
Normal file
8
nano/crypto_lib/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
add_library (crypto_lib
|
||||
interface.cpp
|
||||
random_pool.cpp
|
||||
random_pool.hpp)
|
||||
|
||||
target_link_libraries (crypto_lib
|
||||
blake2
|
||||
${CRYPTOPP_LIBRARY})
|
34
nano/crypto_lib/interface.cpp
Normal file
34
nano/crypto_lib/interface.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <crypto/blake2/blake2.h>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
|
||||
extern "C" {
|
||||
#include <crypto/ed25519-donna/ed25519-hash-custom.h>
|
||||
void ed25519_randombytes_unsafe (void * out, size_t outlen)
|
||||
{
|
||||
nano::random_pool::generate_block (reinterpret_cast<uint8_t *> (out), outlen);
|
||||
}
|
||||
void ed25519_hash_init (ed25519_hash_context * ctx)
|
||||
{
|
||||
ctx->blake2 = new blake2b_state;
|
||||
blake2b_init (reinterpret_cast<blake2b_state *> (ctx->blake2), 64);
|
||||
}
|
||||
|
||||
void ed25519_hash_update (ed25519_hash_context * ctx, uint8_t const * in, size_t inlen)
|
||||
{
|
||||
blake2b_update (reinterpret_cast<blake2b_state *> (ctx->blake2), in, inlen);
|
||||
}
|
||||
|
||||
void ed25519_hash_final (ed25519_hash_context * ctx, uint8_t * out)
|
||||
{
|
||||
blake2b_final (reinterpret_cast<blake2b_state *> (ctx->blake2), out, 64);
|
||||
delete reinterpret_cast<blake2b_state *> (ctx->blake2);
|
||||
}
|
||||
|
||||
void ed25519_hash (uint8_t * out, uint8_t const * in, size_t inlen)
|
||||
{
|
||||
ed25519_hash_context ctx;
|
||||
ed25519_hash_init (&ctx);
|
||||
ed25519_hash_update (&ctx, in, inlen);
|
||||
ed25519_hash_final (&ctx, out);
|
||||
}
|
||||
}
|
22
nano/crypto_lib/random_pool.cpp
Normal file
22
nano/crypto_lib/random_pool.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
|
||||
std::mutex nano::random_pool::mutex;
|
||||
CryptoPP::AutoSeededRandomPool nano::random_pool::pool;
|
||||
|
||||
void nano::random_pool::generate_block (unsigned char * output, size_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
pool.GenerateBlock (output, size);
|
||||
}
|
||||
|
||||
unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
return pool.GenerateWord32 (min, max);
|
||||
}
|
||||
|
||||
unsigned char nano::random_pool::generate_byte ()
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
return pool.GenerateByte ();
|
||||
}
|
31
nano/crypto_lib/random_pool.hpp
Normal file
31
nano/crypto_lib/random_pool.hpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#include <crypto/cryptopp/osrng.h>
|
||||
#include <mutex>
|
||||
|
||||
namespace nano
|
||||
{
|
||||
/** While this uses CryptoPP do not call any of these functions from global scope, as they depend on global variables inside the CryptoPP library which may not have been initialized yet due to an undefined order for globals in different translation units. To make sure this is not an issue, there should be no ASAN warnings at startup on Mac/Clang in the CryptoPP files. */
|
||||
class random_pool
|
||||
{
|
||||
public:
|
||||
static void generate_block (unsigned char * output, size_t size);
|
||||
static unsigned generate_word32 (unsigned min, unsigned max);
|
||||
static unsigned char generate_byte ();
|
||||
|
||||
template <class Iter>
|
||||
static void shuffle (Iter begin, Iter end)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
pool.Shuffle (begin, end);
|
||||
}
|
||||
|
||||
random_pool () = delete;
|
||||
random_pool (random_pool const &) = delete;
|
||||
random_pool & operator= (random_pool const &) = delete;
|
||||
|
||||
private:
|
||||
static std::mutex mutex;
|
||||
static CryptoPP::AutoSeededRandomPool pool;
|
||||
};
|
||||
}
|
|
@ -34,6 +34,8 @@ add_library (nano_lib
|
|||
|
||||
target_link_libraries (nano_lib
|
||||
xxhash
|
||||
ed25519
|
||||
crypto_lib
|
||||
blake2
|
||||
${CRYPTOPP_LIBRARY}
|
||||
Boost::boost)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/blocks.hpp>
|
||||
#include <nano/lib/numbers.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
|
|
|
@ -4,10 +4,9 @@
|
|||
|
||||
#include <crypto/ed25519-donna/ed25519.h>
|
||||
|
||||
#include <crypto/blake2/blake2.h>
|
||||
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/blocks.hpp>
|
||||
#include <nano/lib/config.hpp>
|
||||
#include <nano/lib/numbers.hpp>
|
||||
|
@ -140,34 +139,4 @@ char * xrb_work_transaction (const char * transaction)
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#include <crypto/ed25519-donna/ed25519-hash-custom.h>
|
||||
void ed25519_randombytes_unsafe (void * out, size_t outlen)
|
||||
{
|
||||
nano::random_pool::generate_block (reinterpret_cast<uint8_t *> (out), outlen);
|
||||
}
|
||||
void ed25519_hash_init (ed25519_hash_context * ctx)
|
||||
{
|
||||
ctx->blake2 = new blake2b_state;
|
||||
blake2b_init (reinterpret_cast<blake2b_state *> (ctx->blake2), 64);
|
||||
}
|
||||
|
||||
void ed25519_hash_update (ed25519_hash_context * ctx, uint8_t const * in, size_t inlen)
|
||||
{
|
||||
blake2b_update (reinterpret_cast<blake2b_state *> (ctx->blake2), in, inlen);
|
||||
}
|
||||
|
||||
void ed25519_hash_final (ed25519_hash_context * ctx, uint8_t * out)
|
||||
{
|
||||
blake2b_final (reinterpret_cast<blake2b_state *> (ctx->blake2), out, 64);
|
||||
delete reinterpret_cast<blake2b_state *> (ctx->blake2);
|
||||
}
|
||||
|
||||
void ed25519_hash (uint8_t * out, uint8_t const * in, size_t inlen)
|
||||
{
|
||||
ed25519_hash_context ctx;
|
||||
ed25519_hash_init (&ctx);
|
||||
ed25519_hash_update (&ctx, in, inlen);
|
||||
ed25519_hash_final (&ctx, out);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include <nano/lib/numbers.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/numbers.hpp>
|
||||
|
||||
#include <crypto/ed25519-donna/ed25519.h>
|
||||
|
||||
#include <crypto/blake2/blake2.h>
|
||||
|
@ -8,27 +10,6 @@
|
|||
#include <crypto/cryptopp/aes.h>
|
||||
#include <crypto/cryptopp/modes.h>
|
||||
|
||||
std::mutex nano::random_pool::mutex;
|
||||
CryptoPP::AutoSeededRandomPool nano::random_pool::pool;
|
||||
|
||||
void nano::random_pool::generate_block (unsigned char * output, size_t size)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
pool.GenerateBlock (output, size);
|
||||
}
|
||||
|
||||
unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
return pool.GenerateWord32 (min, max);
|
||||
}
|
||||
|
||||
unsigned char nano::random_pool::generate_byte ()
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
return pool.GenerateByte ();
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
char const * base58_reverse ("~012345678~~~~~~~9:;<=>?@~ABCDE~FGHIJKLMNOP~~~~~~QRSTUVWXYZ[~\\]^_`abcdefghi");
|
||||
|
|
|
@ -6,30 +6,6 @@
|
|||
|
||||
namespace nano
|
||||
{
|
||||
/** While this uses CryptoPP do not call any of these functions from global scope, as they depend on global variables inside the CryptoPP library which may not have been initialized yet due to an undefined order for globals in different translation units. To make sure this is not an issue, there should be no ASAN warnings at startup on Mac/Clang in the CryptoPP files. */
|
||||
class random_pool
|
||||
{
|
||||
public:
|
||||
static void generate_block (unsigned char * output, size_t size);
|
||||
static unsigned generate_word32 (unsigned min, unsigned max);
|
||||
static unsigned char generate_byte ();
|
||||
|
||||
template <class Iter>
|
||||
static void shuffle (Iter begin, Iter end)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk (mutex);
|
||||
pool.Shuffle (begin, end);
|
||||
}
|
||||
|
||||
random_pool () = delete;
|
||||
random_pool (random_pool const &) = delete;
|
||||
random_pool & operator= (random_pool const &) = delete;
|
||||
|
||||
private:
|
||||
static std::mutex mutex;
|
||||
static CryptoPP::AutoSeededRandomPool pool;
|
||||
};
|
||||
|
||||
using uint128_t = boost::multiprecision::uint128_t;
|
||||
using uint256_t = boost::multiprecision::uint256_t;
|
||||
using uint512_t = boost::multiprecision::uint512_t;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <nano/lib/work.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/blocks.hpp>
|
||||
#include <nano/lib/work.hpp>
|
||||
#include <nano/node/xorshift.hpp>
|
||||
|
||||
#include <future>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/nano_node/daemon.hpp>
|
||||
#include <nano/node/cli.hpp>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/errors.hpp>
|
||||
#include <nano/lib/jsonconfig.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <nano/node/bootstrap.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/common.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
#include <nano/node/transport/tcp.hpp>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <nano/node/lmdb.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/node/common.hpp>
|
||||
#include <nano/secure/versioning.hpp>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <nano/node/node.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/interface.h>
|
||||
#include <nano/lib/timer.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/jsonconfig.hpp>
|
||||
#include <nano/node/nodeconfig.hpp>
|
||||
// NOTE: to reduce compile times, this include can be replaced by more narrow includes
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <nano/node/openclwork.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
#include <nano/node/wallet.hpp>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <cstdlib>
|
||||
#include <nano/core_test/testutil.hpp>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/common.hpp>
|
||||
#include <nano/node/testing.hpp>
|
||||
#include <nano/node/transport/udp.hpp>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
#include <nano/node/transport/udp.hpp>
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/node/wallet.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/utility.hpp>
|
||||
#include <nano/node/node.hpp>
|
||||
#include <nano/node/wallet.hpp>
|
||||
#include <nano/node/xorshift.hpp>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/config.hpp>
|
||||
#include <nano/node/testing.hpp>
|
||||
#include <nano/qt/qt.hpp>
|
||||
|
|
|
@ -45,8 +45,9 @@ add_library (secure
|
|||
versioning.cpp)
|
||||
|
||||
target_link_libraries(secure
|
||||
ed25519
|
||||
nano_lib
|
||||
ed25519
|
||||
crypto_lib
|
||||
lmdb
|
||||
Boost::boost
|
||||
Boost::system
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#include <nano/secure/common.hpp>
|
||||
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/lib/interface.h>
|
||||
#include <nano/lib/numbers.hpp>
|
||||
#include <nano/node/common.hpp>
|
||||
#include <nano/secure/blockstore.hpp>
|
||||
#include <nano/secure/common.hpp>
|
||||
#include <nano/secure/versioning.hpp>
|
||||
|
||||
#include <boost/endian/conversion.hpp>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <nano/core_test/testutil.hpp>
|
||||
#include <nano/crypto_lib/random_pool.hpp>
|
||||
#include <nano/node/testing.hpp>
|
||||
#include <nano/node/transport/udp.hpp>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue