Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ed25519 and lib library circular dependency #1870

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/core_test/work_pool.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
8 changes: 8 additions & 0 deletions nano/crypto_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions nano/crypto_lib/interface.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions nano/crypto_lib/random_pool.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions nano/crypto_lib/random_pool.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
}
2 changes: 2 additions & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ add_library (nano_lib

target_link_libraries (nano_lib
xxhash
ed25519
crypto_lib
blake2
${CRYPTOPP_LIBRARY}
Boost::boost)
Expand Down
1 change: 1 addition & 0 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
33 changes: 1 addition & 32 deletions nano/lib/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}
}
25 changes: 3 additions & 22 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
#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>

#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");
Expand Down
24 changes: 0 additions & 24 deletions nano/lib/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions nano/lib/work.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/nano_wallet/entry.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/lmdb.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions nano/node/openclwork.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/node/transport/udp.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/node/node.hpp>
#include <nano/node/transport/udp.hpp>

Expand Down
3 changes: 2 additions & 1 deletion nano/node/wallet.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/qt_system/entry.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
3 changes: 2 additions & 1 deletion nano/secure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ add_library (secure
versioning.cpp)

target_link_libraries(secure
ed25519
nano_lib
ed25519
crypto_lib
lmdb
Boost::boost
Boost::system
Expand Down
4 changes: 3 additions & 1 deletion nano/secure/common.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 1 addition & 0 deletions nano/slow_test/node.cpp
Original file line number Diff line number Diff line change
@@ -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>

Expand Down