Remove per-process hash seeding for gcc.

* gcc now seeds hardened_hash per instance.

* Other platforms were already doing this.
This commit is contained in:
Howard Hinnant
2019-10-28 14:55:28 -04:00
committed by Nik Bougalis
parent 5c1dd87fab
commit 14075630d4

View File

@@ -32,17 +32,6 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
// When set to 1, makes the seed per-process instead
// of per default-constructed instance of hardened_hash
//
#ifndef RIPPLE_NO_HARDENED_HASH_INSTANCE_SEED
# ifdef __GLIBCXX__
# define RIPPLE_NO_HARDENED_HASH_INSTANCE_SEED 1
# else
# define RIPPLE_NO_HARDENED_HASH_INSTANCE_SEED 0
# endif
#endif
namespace ripple { namespace ripple {
namespace detail { namespace detail {
@@ -71,69 +60,10 @@ make_seed_pair() noexcept
} }
template <class HashAlgorithm, bool ProcessSeeded>
class basic_hardened_hash;
/**
* Seed functor once per process
*/
template <class HashAlgorithm>
class basic_hardened_hash <HashAlgorithm, true>
{
private:
static
detail::seed_pair const&
init_seed_pair()
{
static detail::seed_pair const p = detail::make_seed_pair<>();
return p;
}
public:
explicit basic_hardened_hash() = default;
using result_type = typename HashAlgorithm::result_type;
template <class T>
result_type
operator()(T const& t) const noexcept
{
auto const [seed0, seed1] = init_seed_pair();
HashAlgorithm h(seed0, seed1);
hash_append(h, t);
return static_cast<result_type>(h);
}
};
/** /**
* Seed functor once per construction * Seed functor once per construction
*/
template <class HashAlgorithm>
class basic_hardened_hash<HashAlgorithm, false>
{
private:
detail::seed_pair m_seeds;
public: A std compatible hash adapter that resists adversarial inputs.
using result_type = typename HashAlgorithm::result_type;
basic_hardened_hash()
: m_seeds (detail::make_seed_pair<>())
{}
template <class T>
result_type
operator()(T const& t) const noexcept
{
HashAlgorithm h(m_seeds.first, m_seeds.second);
hash_append(h, t);
return static_cast<result_type>(h);
}
};
//------------------------------------------------------------------------------
/** A std compatible hash adapter that resists adversarial inputs.
For this to work, T must implement in its own namespace: For this to work, T must implement in its own namespace:
@code @code
@@ -159,13 +89,29 @@ public:
template parameter (the hashing algorithm). For details template parameter (the hashing algorithm). For details
see https://131002.net/siphash/#at see https://131002.net/siphash/#at
*/ */
#if RIPPLE_NO_HARDENED_HASH_INSTANCE_SEED
template <class HashAlgorithm = beast::xxhasher> template <class HashAlgorithm = beast::xxhasher>
using hardened_hash = basic_hardened_hash<HashAlgorithm, true>; class hardened_hash
#else {
template <class HashAlgorithm = beast::xxhasher> private:
using hardened_hash = basic_hardened_hash<HashAlgorithm, false>; detail::seed_pair m_seeds;
#endif
public:
using result_type = typename HashAlgorithm::result_type;
hardened_hash()
: m_seeds (detail::make_seed_pair<>())
{}
template <class T>
result_type
operator()(T const& t) const noexcept
{
HashAlgorithm h(m_seeds.first, m_seeds.second);
hash_append(h, t);
return static_cast<result_type>(h);
}
};
} // ripple } // ripple