Skip to content

Commit

Permalink
Generate hashes with random keys to avoid collisions
Browse files Browse the repository at this point in the history
Address TODO in src/util/common/hashmap.hpp which calls for
randomly generated hash keys. Requires use of libsodium for
crypto-safe random number generation.

NOTE: This causes certain tests to fail currently because of
the added randomness. This will be fixed shortly.

Signed-off-by: Michael Maurer <[email protected]>
  • Loading branch information
maurermi committed Mar 20, 2023
1 parent 3a33f7e commit 7e3171d
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ find_library(NURAFT_LIBRARY nuraft REQUIRED)
find_library(GTEST_LIBRARY gtest REQUIRED)
find_library(GTEST_MAIN_LIBRARY gtest_main REQUIRED)
find_package(benchmark REQUIRED)
find_library(SODIUM_LIBRARY libsodium.a REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")

Expand Down
3 changes: 2 additions & 1 deletion benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ target_link_libraries(run_benchmarks ${GTEST_LIBRARY}
crypto
secp256k1
${LEVELDB_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
12 changes: 12 additions & 0 deletions scripts/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,15 @@ if [ ! -f "${PYTHON_TIDY}" ]; then
wget https://raw.githubusercontent.com/llvm/llvm-project/e837ce2a32369b2e9e8e5d60270c072c7dd63827/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
$SUDO mv run-clang-tidy.py /usr/local/bin
fi

wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
rm -rf libsodium-stable
tar xzvf LATEST.tar.gz
cd libsodium-stable
./configure
make && make check
$SUDO make install
cd ../
rm -rf libsodium-stable
rm LATEST.tar.gz

3 changes: 2 additions & 1 deletion src/uhs/atomizer/archiver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ target_link_libraries(archiverd archiver
${LEVELDB_LIBRARY}
${NURAFT_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/atomizer/atomizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ target_link_libraries(atomizer-raftd atomizer_raft
${NURAFT_LIBRARY}
${LEVELDB_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/atomizer/sentinel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ target_link_libraries(sentineld sentinel
serialization
crypto
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/atomizer/shard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ target_link_libraries(shardd shard
${LEVELDB_LIBRARY}
${NURAFT_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/atomizer/watchtower/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ target_link_libraries(watchtowerd watchtower
crypto
${NURAFT_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ target_link_libraries(client-cli client
crypto
secp256k1
${NURAFT_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/twophase/coordinator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ target_link_libraries(coordinatord coordinator
${NURAFT_LIBRARY}
${LEVELDB_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/twophase/locking_shard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ target_link_libraries(locking-shardd locking_shard
secp256k1
${NURAFT_LIBRARY}
${LEVELDB_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion src/uhs/twophase/sentinel_2pc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ target_link_libraries(sentineld-2pc sentinel_2pc
common
${NURAFT_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
18 changes: 5 additions & 13 deletions src/util/common/hashmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@

#include <array>
#include <cstring>
#include <sodium.h>

namespace cbdc::hashing {
/// \brief SipHash function to generate STL data structure hash keys for
/// system IDs.
///
/// [TODO: Uses randomly generated initialization keys to insure that
/// colliding values will differ from run to run.] For transaction data
/// that comes from users, it is important to key data in sets and hashmaps
/// non-deterministically. Otherwise, malicious actors might pre-calculate
/// colliding values and spam the system with them to cripple performance.
/// \tparam T hash-like type to hash.
template<class T>
struct const_sip_hash {
static_assert(std::is_trivial_v<T>);
auto operator()(T const& tx) const noexcept -> size_t {
// TODO: pick the initialization key at random.
static constexpr std::array<uint64_t, 2> siphash_key{0x1337,
0x1337};
std::array<uint64_t, 2> siphash_key{randombytes_random(),
randombytes_random()};
CSipHasher hasher(siphash_key[0], siphash_key[1]);
std::array<unsigned char, sizeof(tx)> tx_arr{};
std::memcpy(tx_arr.data(), &tx, sizeof(tx));
Expand All @@ -41,9 +34,8 @@ namespace cbdc::hashing {
template<>
struct const_sip_hash<buffer> {
auto operator()(const buffer& buf) const noexcept -> size_t {
// TODO: pick the initialization key at random.
static constexpr std::array<uint64_t, 2> siphash_key{0x1337,
0x1337};
std::array<uint64_t, 2> siphash_key{randombytes_random(),
randombytes_random()};
CSipHasher hasher(siphash_key[0], siphash_key[1]);
std::vector<unsigned char> arr(buf.size());
std::memcpy(arr.data(), buf.data(), buf.size());
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ target_link_libraries(run_integration_tests ${GTEST_LIBRARY}
secp256k1
${LEVELDB_LIBRARY}
${NURAFT_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ target_link_libraries(run_unit_tests ${GTEST_LIBRARY}
secp256k1
${LEVELDB_LIBRARY}
${NURAFT_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
6 changes: 4 additions & 2 deletions tools/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ target_link_libraries(twophase-gen sentinel
crypto
${NURAFT_LIBRARY}
secp256k1
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})

add_executable(atomizer-cli-watchtower atomizer-cli-watchtower.cpp)
target_link_libraries(atomizer-cli-watchtower watchtower
Expand All @@ -30,4 +31,5 @@ target_link_libraries(atomizer-cli-watchtower watchtower
crypto
secp256k1
${NURAFT_LIBRARY}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT}
${SODIUM_LIBRARY})
3 changes: 2 additions & 1 deletion tools/shard-seeder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ target_link_libraries(shard-seeder transaction
crypto
${LEVELDB_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
secp256k1)
secp256k1
${SODIUM_LIBRARY})

0 comments on commit 7e3171d

Please sign in to comment.