Add sha512Half and SHA512HashHasher:

These routines replace existing code to compute SHA512-Half hashes.
The new code accumulates serialized data into a hashing context
instead of allocating a buffer, for improved performance.
This commit is contained in:
Vinnie Falco
2015-05-27 09:27:35 -07:00
parent 06823349f9
commit c25184cc88
25 changed files with 357 additions and 323 deletions

View File

@@ -367,6 +367,10 @@
<ClCompile Include="..\..\src\beast\beast\crypto\Crypto.unity.cpp"> <ClCompile Include="..\..\src\beast\beast\crypto\Crypto.unity.cpp">
<ExcludedFromBuild>True</ExcludedFromBuild> <ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClInclude Include="..\..\src\beast\beast\crypto\impl\sha512_context.h">
</ClInclude>
<ClInclude Include="..\..\src\beast\beast\crypto\sha512.h">
</ClInclude>
<ClCompile Include="..\..\src\beast\beast\crypto\tests\base64.test.cpp"> <ClCompile Include="..\..\src\beast\beast\crypto\tests\base64.test.cpp">
<ExcludedFromBuild>True</ExcludedFromBuild> <ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile> </ClCompile>
@@ -1964,6 +1968,8 @@
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\basics\seconds_clock.h"> <ClInclude Include="..\..\src\ripple\basics\seconds_clock.h">
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\basics\SHA512Half.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\Slice.h"> <ClInclude Include="..\..\src\ripple\basics\Slice.h">
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\basics\strHex.h"> <ClInclude Include="..\..\src\ripple\basics\strHex.h">
@@ -2852,10 +2858,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\protocol\tests\Serializer.test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\protocol\tests\STAmount.test.cpp"> <ClCompile Include="..\..\src\ripple\protocol\tests\STAmount.test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>

View File

@@ -43,6 +43,9 @@
<Filter Include="beast\crypto"> <Filter Include="beast\crypto">
<UniqueIdentifier>{ED78E906-CE72-CDEF-3018-99E02603C37E}</UniqueIdentifier> <UniqueIdentifier>{ED78E906-CE72-CDEF-3018-99E02603C37E}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="beast\crypto\impl">
<UniqueIdentifier>{398085D5-BDC7-D75C-2A1D-73395AC7A158}</UniqueIdentifier>
</Filter>
<Filter Include="beast\crypto\tests"> <Filter Include="beast\crypto\tests">
<UniqueIdentifier>{FF9CBE50-56A0-7B59-C99F-C873605C844B}</UniqueIdentifier> <UniqueIdentifier>{FF9CBE50-56A0-7B59-C99F-C873605C844B}</UniqueIdentifier>
</Filter> </Filter>
@@ -804,6 +807,12 @@
<ClCompile Include="..\..\src\beast\beast\crypto\Crypto.unity.cpp"> <ClCompile Include="..\..\src\beast\beast\crypto\Crypto.unity.cpp">
<Filter>beast\crypto</Filter> <Filter>beast\crypto</Filter>
</ClCompile> </ClCompile>
<ClInclude Include="..\..\src\beast\beast\crypto\impl\sha512_context.h">
<Filter>beast\crypto\impl</Filter>
</ClInclude>
<ClInclude Include="..\..\src\beast\beast\crypto\sha512.h">
<Filter>beast\crypto</Filter>
</ClInclude>
<ClCompile Include="..\..\src\beast\beast\crypto\tests\base64.test.cpp"> <ClCompile Include="..\..\src\beast\beast\crypto\tests\base64.test.cpp">
<Filter>beast\crypto\tests</Filter> <Filter>beast\crypto\tests</Filter>
</ClCompile> </ClCompile>
@@ -2667,6 +2676,9 @@
<ClInclude Include="..\..\src\ripple\basics\seconds_clock.h"> <ClInclude Include="..\..\src\ripple\basics\seconds_clock.h">
<Filter>ripple\basics</Filter> <Filter>ripple\basics</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\basics\SHA512Half.h">
<Filter>ripple\basics</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\Slice.h"> <ClInclude Include="..\..\src\ripple\basics\Slice.h">
<Filter>ripple\basics</Filter> <Filter>ripple\basics</Filter>
</ClInclude> </ClInclude>
@@ -3528,9 +3540,6 @@
<ClCompile Include="..\..\src\ripple\protocol\tests\RippleAddress.test.cpp"> <ClCompile Include="..\..\src\ripple\protocol\tests\RippleAddress.test.cpp">
<Filter>ripple\protocol\tests</Filter> <Filter>ripple\protocol\tests</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\protocol\tests\Serializer.test.cpp">
<Filter>ripple\protocol\tests</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\protocol\tests\STAmount.test.cpp"> <ClCompile Include="..\..\src\ripple\protocol\tests\STAmount.test.cpp">
<Filter>ripple\protocol\tests</Filter> <Filter>ripple\protocol\tests</Filter>
</ClCompile> </ClCompile>

View File

@@ -23,6 +23,7 @@
#include <ripple/app/misc/NetworkOPs.h> #include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/tx/TransactionMaster.h> #include <ripple/app/tx/TransactionMaster.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/core/JobQueue.h> #include <ripple/core/JobQueue.h>
#include <ripple/nodestore/Database.h> #include <ripple/nodestore/Database.h>
#include <ripple/protocol/HashPrefix.h> #include <ripple/protocol/HashPrefix.h>
@@ -82,7 +83,7 @@ bool ConsensusTransSetSF::haveNode (const SHAMapNodeID& id, uint256 const& nodeH
Serializer s; Serializer s;
s.add32 (HashPrefix::transactionID); s.add32 (HashPrefix::transactionID);
txn->getSTransaction ()->add (s); txn->getSTransaction ()->add (s);
assert (s.getSHA512Half () == nodeHash); assert(sha512Half(s.slice()) == nodeHash);
nodeData = s.peekData (); nodeData = s.peekData ();
return true; return true;
} }

View File

@@ -26,15 +26,16 @@
#include <ripple/app/ledger/LedgerToJson.h> #include <ripple/app/ledger/LedgerToJson.h>
#include <ripple/app/ledger/OrderBookDB.h> #include <ripple/app/ledger/OrderBookDB.h>
#include <ripple/app/ledger/PendingSaves.h> #include <ripple/app/ledger/PendingSaves.h>
#include <ripple/core/DatabaseCon.h>
#include <ripple/core/SociDB.h>
#include <ripple/app/main/Application.h> #include <ripple/app/main/Application.h>
#include <ripple/app/misc/IHashRouter.h> #include <ripple/app/misc/IHashRouter.h>
#include <ripple/app/misc/NetworkOPs.h> #include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/tx/TransactionMaster.h> #include <ripple/app/tx/TransactionMaster.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/LoggedTimings.h> #include <ripple/basics/LoggedTimings.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/core/DatabaseCon.h>
#include <ripple/core/SociDB.h>
#include <ripple/protocol/Indexes.h> #include <ripple/protocol/Indexes.h>
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
#include <ripple/core/Config.h> #include <ripple/core/Config.h>
@@ -245,11 +246,18 @@ void Ledger::updateHash ()
mAccountHash.zero (); mAccountHash.zero ();
} }
// VFALCO TODO Fix this hard coded magic number 122 // VFALCO This has to match addRaw
Serializer s (122); mHash = sha512Half(
s.add32 (HashPrefix::ledgerMaster); HashPrefix::ledgerMaster,
addRaw (s); std::uint32_t(mLedgerSeq),
mHash = s.getSHA512Half (); std::uint64_t(mTotCoins),
mParentHash,
mTransHash,
mAccountHash,
std::uint32_t(mParentCloseTime),
std::uint32_t(mCloseTime),
std::uint8_t(mCloseResolution),
std::uint8_t(mCloseFlags));
mValidHash = true; mValidHash = true;
} }

View File

@@ -19,6 +19,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/app/ledger/LedgerProposal.h> #include <ripple/app/ledger/LedgerProposal.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/core/Config.h> #include <ripple/core/Config.h>
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/HashPrefix.h> #include <ripple/protocol/HashPrefix.h>
@@ -63,15 +64,12 @@ LedgerProposal::LedgerProposal (
uint256 LedgerProposal::getSigningHash () const uint256 LedgerProposal::getSigningHash () const
{ {
Serializer s ((32 + 32 + 32 + 256 + 256) / 8); return sha512Half(
HashPrefix::proposal,
s.add32 (HashPrefix::proposal); std::uint32_t(mProposeSeq),
s.add32 (mProposeSeq); std::uint32_t(mCloseTime),
s.add32 (mCloseTime); mPreviousLedger,
s.add256 (mPreviousLedger); mCurrentHash);
s.add256 (mCurrentHash);
return s.getSHA512Half ();
} }
bool LedgerProposal::checkSign (std::string const& signature) const bool LedgerProposal::checkSign (std::string const& signature) const

View File

@@ -19,6 +19,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/app/main/Application.h> #include <ripple/app/main/Application.h>
#include <ripple/basics/CheckLibraryVersions.h> #include <ripple/basics/CheckLibraryVersions.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
@@ -511,6 +512,10 @@ int main (int argc, char** argv)
// https://svn.boost.org/trac/boost/ticket/10657 // https://svn.boost.org/trac/boost/ticket/10657
(void)beast::Time::currentTimeMillis(); (void)beast::Time::currentTimeMillis();
#ifdef _MSC_VER
ripple::sha512_deprecatedMSVCWorkaround();
#endif
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
auto constexpr gccver = (__GNUC__ * 100 * 100) + auto constexpr gccver = (__GNUC__ * 100 * 100) +
(__GNUC_MINOR__ * 100) + (__GNUC_MINOR__ * 100) +

View File

@@ -41,6 +41,8 @@
#include <ripple/app/tx/TransactionMaster.h> #include <ripple/app/tx/TransactionMaster.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/Time.h> #include <ripple/basics/Time.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/basics/UptimeTimer.h> #include <ripple/basics/UptimeTimer.h>
#include <ripple/protocol/JsonFields.h> #include <ripple/protocol/JsonFields.h>
@@ -3244,7 +3246,7 @@ bool NetworkOPsImp::getFetchPack (uint256 const& hash, Blob& data)
mFetchPack.del (hash, false); mFetchPack.del (hash, false);
if (hash != getSHA512Half (data)) if (hash != sha512Half(make_Slice(data)))
{ {
m_journal.warning << "Bad entry in fetch pack"; m_journal.warning << "Bad entry in fetch pack";
return false; return false;

View File

@@ -25,6 +25,8 @@
#include <ripple/overlay/ClusterNodeStatus.h> #include <ripple/overlay/ClusterNodeStatus.h>
#include <ripple/app/misc/UniqueNodeList.h> #include <ripple/app/misc/UniqueNodeList.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/basics/Time.h> #include <ripple/basics/Time.h>
#include <ripple/core/Config.h> #include <ripple/core/Config.h>
@@ -1596,8 +1598,10 @@ bool UniqueNodeListImp::responseFetch (std::string const& strDomain, const boost
assert (bFound); assert (bFound);
(void) bFound; (void) bFound;
uint256 iSha256 = getSHA512Half (strSiteFile); uint256 iSha256 =
bool bChangedB = sdCurrent.iSha256 != iSha256; sha512Half(make_Slice(strSiteFile));
bool bChangedB =
sdCurrent.iSha256 != iSha256;
sdCurrent.strDomain = strDomain; sdCurrent.strDomain = strDomain;
// XXX If the node public key is changing, delete old public key information? // XXX If the node public key is changing, delete old public key information?

View File

@@ -0,0 +1,123 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2014 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_BASICS_SHA512HALF_H_INCLUDED
#define RIPPLE_BASICS_SHA512HALF_H_INCLUDED
#include <ripple/basics/base_uint.h>
#include <ripple/basics/Slice.h> // needed by most callers
#include <beast/crypto/sha512.h>
#include <beast/hash/hash_append.h>
#include <beast/cxx14/type_traits.h> // <type_traits>
namespace ripple {
namespace detail {
template <bool Secure>
class SHA512HalfHasher
{
private:
using hasher_type =
std::conditional_t<Secure,
beast::sha512_hasher_s,
beast::sha512_hasher>;
hasher_type hasher_;
public:
static beast::endian const endian =
beast::endian::big;
using result_type = uint256;
void
operator() (void const* data,
std::size_t size) noexcept
{
hasher_(data, size);
}
result_type
finish() noexcept
{
result_type digest;
auto const result = static_cast<
typename decltype(hasher_)::result_type>
(hasher_);
std::memcpy(digest.data(),
result.data(), 32);
return digest;
};
explicit
operator result_type() noexcept
{
return finish();
}
};
} // detail
#ifdef _MSC_VER
// Call from main to fix magic statics pre-VS2015
inline
void
sha512_deprecatedMSVCWorkaround()
{
beast::sha512_hasher h;
auto const digest = static_cast<
beast::sha512_hasher::result_type>(h);
}
#endif
using SHA512HalfHasher = detail::SHA512HalfHasher<false>;
/** Returns the SHA512-Half of a series of objects. */
template <class... Args>
SHA512HalfHasher::result_type
sha512Half (Args const&... args)
{
SHA512HalfHasher h;
using beast::hash_append;
hash_append(h, args...);
return static_cast<typename
SHA512HalfHasher::result_type>(h);
}
/** Returns the SHA512-Half of a series of objects.
Postconditions:
Temporary memory storing copies of
input messages will be cleared.
*/
template <class... Args>
SHA512HalfHasher::result_type
sha512Half_s (Args const&... args)
{
detail::SHA512HalfHasher<true> h;
using beast::hash_append;
hash_append(h, args...);
return static_cast<typename
SHA512HalfHasher::result_type>(h);
}
}
#endif

View File

@@ -25,6 +25,9 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <string>
#include <vector>
#include <beast/cxx14/type_traits.h> // <type_traits>
namespace ripple { namespace ripple {
@@ -85,7 +88,7 @@ inline
void void
hash_append (Hasher& h, Slice const& v) hash_append (Hasher& h, Slice const& v)
{ {
h.append(v.data(), v.size()); h(v.data(), v.size());
} }
inline inline
@@ -113,6 +116,7 @@ operator< (Slice const& lhs, Slice const& rhs) noexcept
rhs.data(), rhs.data() + rhs.size()); rhs.data(), rhs.data() + rhs.size());
} }
template <class Stream> template <class Stream>
Stream& operator<<(Stream& s, Slice const& v) Stream& operator<<(Stream& s, Slice const& v)
{ {
@@ -120,6 +124,24 @@ Stream& operator<<(Stream& s, Slice const& v)
return s; return s;
} }
template <class T, class Alloc>
std::enable_if_t<
std::is_same<T, char>::value ||
std::is_same<T, unsigned char>::value,
Slice
>
make_Slice (std::vector<T, Alloc> const& v)
{
return Slice(v.data(), v.size());
}
template <class Traits, class Alloc>
Slice
make_Slice (std::basic_string<char, Traits, Alloc> const& s)
{
return Slice(s.data(), s.size());
}
} // ripple } // ripple
#endif #endif

View File

@@ -30,10 +30,9 @@
#include <ripple/basics/strHex.h> #include <ripple/basics/strHex.h>
#include <ripple/basics/hardened_hash.h> #include <ripple/basics/hardened_hash.h>
#include <beast/utility/Zero.h> #include <beast/utility/Zero.h>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <functional> #include <functional>
#include <type_traits>
using beast::zero; using beast::zero;
using beast::Zero; using beast::Zero;
@@ -290,11 +289,13 @@ public:
return *this; return *this;
} }
template <class Hasher> template <class Hasher,
friend void hash_append(Hasher& h, base_uint const& a) noexcept class = std::enable_if_t<Hasher::endian != beast::endian::native>>
friend void hash_append(
Hasher& h, base_uint const& a) noexcept
{ {
using beast::hash_append; // Do not allow any endian transformations on this memory
hash_append (h, a.pn); h(a.pn, sizeof(a.pn));
} }
bool SetHexExact (const char* psz) bool SetHexExact (const char* psz)
@@ -534,23 +535,20 @@ inline std::ostream& operator<< (
return out << to_string (u); return out << to_string (u);
} }
static_assert(sizeof(uint128) == 128/8, "There should be no padding bytes");
static_assert(sizeof(uint160) == 160/8, "There should be no padding bytes");
static_assert(sizeof(uint256) == 256/8, "There should be no padding bytes");
} // rippled } // rippled
namespace boost namespace beast
{ {
template <std::size_t Bits, class Tag> template <std::size_t Bits, class Tag>
struct hash<ripple::base_uint<Bits, Tag>> struct is_uniquely_represented<ripple::base_uint<Bits, Tag>>
{ : public std::true_type
using argument_type = ripple::base_uint<Bits, Tag>; {};
std::size_t } // beast
operator()(argument_type const& u) const
{
return ripple::hardened_hash<>{}(u);
}
};
} // boost
#endif #endif

View File

@@ -21,6 +21,7 @@
#include <ripple/crypto/GenerateDeterministicKey.h> #include <ripple/crypto/GenerateDeterministicKey.h>
#include <ripple/crypto/impl/ec_key.h> #include <ripple/crypto/impl/ec_key.h>
#include <ripple/crypto/impl/openssl.h> #include <ripple/crypto/impl/openssl.h>
#include <ripple/basics/SHA512Half.h>
#include <array> #include <array>
#include <string> #include <string>
#include <openssl/pem.h> #include <openssl/pem.h>
@@ -48,15 +49,6 @@ static Blob serialize_ec_point (ec_point const& point)
return result; return result;
} }
uint256
getSHA512Half (void const* data, std::size_t bytes)
{
uint256 j[2];
SHA512 (reinterpret_cast<unsigned char const*>(data), bytes,
reinterpret_cast<unsigned char*> (j));
return j[0];
}
template <class FwdIt> template <class FwdIt>
void void
copy_uint32 (FwdIt out, std::uint32_t v) copy_uint32 (FwdIt out, std::uint32_t v)
@@ -86,10 +78,9 @@ static bignum generateRootDeterministicKey (uint128 const& seed)
std::array<std::uint8_t, 20> buf; std::array<std::uint8_t, 20> buf;
std::copy(seed.begin(), seed.end(), buf.begin()); std::copy(seed.begin(), seed.end(), buf.begin());
copy_uint32 (buf.begin() + 16, seq++); copy_uint32 (buf.begin() + 16, seq++);
uint256 root = getSHA512Half (buf.data(), buf.size()); auto root = sha512Half(buf);
std::fill (buf.begin(), buf.end(), 0); // security erase std::fill (buf.begin(), buf.end(), 0); // security erase
privKey.assign ((unsigned char const*) &root, sizeof (root)); privKey.assign ((unsigned char const*) &root, sizeof (root));
root.zero(); // security erase root.zero(); // security erase
} }
while (privKey.is_zero() || privKey >= secp256k1_order); while (privKey.is_zero() || privKey >= secp256k1_order);
@@ -146,10 +137,9 @@ static bignum makeHash (Blob const& pubGen, int seq, bignum const& order)
std::copy (pubGen.begin(), pubGen.end(), buf.begin()); std::copy (pubGen.begin(), pubGen.end(), buf.begin());
copy_uint32 (buf.begin() + 33, seq); copy_uint32 (buf.begin() + 33, seq);
copy_uint32 (buf.begin() + 37, subSeq++); copy_uint32 (buf.begin() + 37, subSeq++);
uint256 root = getSHA512Half (buf.data(), buf.size()); auto root = sha512Half_s(buf);
std::fill(buf.begin(), buf.end(), 0); // security erase std::fill(buf.begin(), buf.end(), 0); // security erase
result.assign ((unsigned char const*) &root, sizeof (root)); result.assign ((unsigned char const*) &root, sizeof (root));
root.zero(); // security erase
} }
while (result.is_zero() || result >= order); while (result.is_zero() || result >= order);

View File

@@ -23,10 +23,12 @@
#include <ripple/nodestore/Database.h> #include <ripple/nodestore/Database.h>
#include <ripple/nodestore/Scheduler.h> #include <ripple/nodestore/Scheduler.h>
#include <ripple/nodestore/impl/Tuning.h> #include <ripple/nodestore/impl/Tuning.h>
#include <ripple/basics/TaggedCache.h>
#include <ripple/basics/KeyCache.h> #include <ripple/basics/KeyCache.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/seconds_clock.h> #include <ripple/basics/seconds_clock.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/TaggedCache.h>
#include <beast/threads/Thread.h> #include <beast/threads/Thread.h>
#include <ripple/nodestore/ScopedMetrics.h> #include <ripple/nodestore/ScopedMetrics.h>
#include <chrono> #include <chrono>
@@ -289,7 +291,7 @@ public:
type, std::move(data), hash); type, std::move(data), hash);
#if RIPPLE_VERIFY_NODEOBJECT_KEYS #if RIPPLE_VERIFY_NODEOBJECT_KEYS
assert (hash == getSHA512Half (data)); assert (hash == sha512Hash(make_Slice(data)));
#endif #endif
m_cache.canonicalize (hash, object, true); m_cache.canonicalize (hash, object, true);

View File

@@ -28,6 +28,7 @@
#include <ripple/overlay/ClusterNodeStatus.h> #include <ripple/overlay/ClusterNodeStatus.h>
#include <ripple/app/misc/UniqueNodeList.h> #include <ripple/app/misc/UniqueNodeList.h>
#include <ripple/app/tx/InboundTransactions.h> #include <ripple/app/tx/InboundTransactions.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/basics/UptimeTimer.h> #include <ripple/basics/UptimeTimer.h>
#include <ripple/core/JobQueue.h> #include <ripple/core/JobQueue.h>
@@ -1390,10 +1391,12 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMValidation> const& m)
try try
{ {
Serializer s (m->validation ()); STValidation::pointer val;
SerialIter sit (s); {
STValidation::pointer val = std::make_shared < SerialIter sit (make_Slice(m->validation()));
STValidation> (std::ref (sit), false); val = std::make_shared <
STValidation> (std::ref (sit), false);
}
if (closeTime > (120 + val->getFieldU32(sfSigningTime))) if (closeTime > (120 + val->getFieldU32(sfSigningTime)))
{ {
@@ -1402,8 +1405,8 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMValidation> const& m)
return; return;
} }
if (! getApp().getHashRouter ().addSuppressionPeer ( if (! getApp().getHashRouter ().addSuppressionPeer(
s.getSHA512Half(), id_)) sha512Half(make_Slice(m->validation())), id_))
{ {
p_journal_.trace << "Validation: duplicate"; p_journal_.trace << "Validation: duplicate";
return; return;

View File

@@ -22,6 +22,7 @@
#include <ripple/app/main/Application.h> #include <ripple/app/main/Application.h>
#include <ripple/app/main/LocalCredentials.h> #include <ripple/app/main/LocalCredentials.h>
#include <ripple/app/misc/NetworkOPs.h> #include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/protocol/BuildInfo.h> #include <ripple/protocol/BuildInfo.h>
#include <ripple/overlay/impl/TMHello.h> #include <ripple/overlay/impl/TMHello.h>
#include <beast/crypto/base64.h> #include <beast/crypto/base64.h>
@@ -98,7 +99,7 @@ makeSharedValue (SSL* ssl, beast::Journal journal)
// Finally, derive the actual cookie for the values that // Finally, derive the actual cookie for the values that
// we have calculated. // we have calculated.
result.first = getSHA512Half (sha1, sizeof(sha1)); result.first = sha512Half(Slice(sha1, sizeof(sha1)));
result.second = true; result.second = true;
return result; return result;
} }

View File

@@ -20,6 +20,8 @@
#ifndef RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED #ifndef RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED
#define RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED #define RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED
#include <beast/hash/hash_append.h>
#include <beast/utility/noexcept.h>
#include <cstdint> #include <cstdint>
namespace ripple { namespace ripple {
@@ -97,6 +99,15 @@ public:
static HashPrefix const manifest; static HashPrefix const manifest;
}; };
template <class Hasher>
void
hash_append (Hasher& h, HashPrefix const& hp) noexcept
{
using beast::hash_append;
hash_append(h,
static_cast<std::uint32_t>(hp));
}
} // ripple } // ripple
#endif #endif

View File

@@ -23,6 +23,7 @@
#include <ripple/protocol/SField.h> #include <ripple/protocol/SField.h>
#include <ripple/basics/base_uint.h> #include <ripple/basics/base_uint.h>
#include <ripple/basics/Buffer.h> #include <ripple/basics/Buffer.h>
#include <ripple/basics/Slice.h>
#include <beast/utility/noexcept.h> #include <beast/utility/noexcept.h>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
@@ -65,6 +66,11 @@ public:
; ;
} }
Slice slice() const noexcept
{
return Slice(mData.data(), mData.size());
}
std::size_t std::size_t
size() const noexcept size() const noexcept
{ {
@@ -169,21 +175,6 @@ public:
// DEPRECATED // DEPRECATED
uint256 getSHA512Half() const; uint256 getSHA512Half() const;
// prefix hash functions
static uint256 getPrefixHash (std::uint32_t prefix, const unsigned char* data, int len);
uint256 getPrefixHash (std::uint32_t prefix) const
{
return getPrefixHash (prefix, & (mData.front ()), mData.size ());
}
static uint256 getPrefixHash (std::uint32_t prefix, Blob const& data)
{
return getPrefixHash (prefix, & (data.front ()), data.size ());
}
static uint256 getPrefixHash (std::uint32_t prefix, std::string const& strData)
{
return getPrefixHash (prefix, reinterpret_cast<const unsigned char*> (strData.data ()), strData.size ());
}
// totality functions // totality functions
Blob const& peekData () const Blob const& peekData () const
{ {
@@ -317,10 +308,17 @@ private:
std::uint8_t const* p_; std::uint8_t const* p_;
std::size_t remain_; std::size_t remain_;
std::size_t used_ = 0; std::size_t used_ = 0;
public: public:
SerialIter (void const* data, SerialIter (void const* data,
std::size_t size) noexcept; std::size_t size) noexcept;
SerialIter (Slice const& slice)
: SerialIter(slice.data(), slice.size())
{
}
// VFALCO TODO Remove this overload use Slice instead
explicit explicit
SerialIter (std::string const& s) noexcept SerialIter (std::string const& s) noexcept
: SerialIter(s.data(), s.size()) : SerialIter(s.data(), s.size())
@@ -429,30 +427,6 @@ SerialIter::getBitString()
return u; return u;
} }
//------------------------------------------------------------------------------
uint256
getSHA512Half (void const* data, int len);
// DEPRECATED
inline
uint256
getSHA512Half (std::string const& s)
{
return getSHA512Half(s.data(), s.size());
}
// DEPRECATED
template <class T,
std::enable_if_t<std::is_integral<T>::value &&
sizeof(T) == 1>* = nullptr>
inline
uint256
getSHA512Half (std::vector<T> const& v)
{
return getSHA512Half(v.data(), v.size());
}
} // ripple } // ripple
#endif #endif

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/protocol/Indexes.h> #include <ripple/protocol/Indexes.h>
#include <beast/utility/static_initializer.h> #include <beast/utility/static_initializer.h>
@@ -27,9 +28,7 @@ namespace ripple {
uint256 uint256
getLedgerHashIndex () getLedgerHashIndex ()
{ {
Serializer s (2); return sha512Half(std::uint16_t(spaceSkipList));
s.add16 (spaceSkipList);
return s.getSHA512Half ();
} }
// Get the index of the node that holds the set of 256 ledgers that includes // Get the index of the node that holds the set of 256 ledgers that includes
@@ -38,39 +37,31 @@ getLedgerHashIndex ()
uint256 uint256
getLedgerHashIndex (std::uint32_t desiredLedgerIndex) getLedgerHashIndex (std::uint32_t desiredLedgerIndex)
{ {
Serializer s (6); return sha512Half(
s.add16 (spaceSkipList); std::uint16_t(spaceSkipList),
s.add32 (desiredLedgerIndex >> 16); std::uint32_t(desiredLedgerIndex >> 16));
return s.getSHA512Half ();
} }
// get the index of the node that holds the enabled amendments // get the index of the node that holds the enabled amendments
uint256 uint256
getLedgerAmendmentIndex () getLedgerAmendmentIndex ()
{ {
Serializer s (2); return sha512Half(std::uint16_t(spaceAmendment));
s.add16 (spaceAmendment);
return s.getSHA512Half ();
} }
// get the index of the node that holds the fee schedule // get the index of the node that holds the fee schedule
uint256 uint256
getLedgerFeeIndex () getLedgerFeeIndex ()
{ {
Serializer s (2); return sha512Half(std::uint16_t(spaceFee));
s.add16 (spaceFee);
return s.getSHA512Half ();
} }
uint256 uint256
getAccountRootIndex (Account const& account) getAccountRootIndex (Account const& account)
{ {
Serializer s (22); return sha512Half(
std::uint16_t(spaceAccount),
s.add16 (spaceAccount); account);
s.add160 (account);
return s.getSHA512Half ();
} }
uint256 uint256
@@ -82,72 +73,52 @@ getAccountRootIndex (const RippleAddress & account)
uint256 uint256
getGeneratorIndex (Account const& uGeneratorID) getGeneratorIndex (Account const& uGeneratorID)
{ {
Serializer s (22); return sha512Half(
std::uint16_t(spaceGenerator),
s.add16 (spaceGenerator); uGeneratorID);
s.add160 (uGeneratorID);
return s.getSHA512Half ();
} }
uint256 uint256
getBookBase (Book const& book) getBookBase (Book const& book)
{ {
Serializer s (82);
assert (isConsistent (book)); assert (isConsistent (book));
s.add16 (spaceBookDir);
s.add160 (book.in.currency);
s.add160 (book.out.currency);
s.add160 (book.in.account);
s.add160 (book.out.account);
// Return with quality 0. // Return with quality 0.
return getQualityIndex (s.getSHA512Half ()); return getQualityIndex(sha512Half(
std::uint16_t(spaceBookDir),
book.in.currency,
book.out.currency,
book.in.account,
book.out.account));
} }
uint256 uint256
getOfferIndex (Account const& account, std::uint32_t uSequence) getOfferIndex (Account const& account, std::uint32_t uSequence)
{ {
Serializer s (26); return sha512Half(
std::uint16_t(spaceOffer),
s.add16 (spaceOffer); account,
s.add160 (account); std::uint32_t(uSequence));
s.add32 (uSequence);
return s.getSHA512Half ();
} }
uint256 uint256
getOwnerDirIndex (Account const& account) getOwnerDirIndex (Account const& account)
{ {
Serializer s (22); return sha512Half(
std::uint16_t(spaceOwnerDir),
s.add16 (spaceOwnerDir); account);
s.add160 (account);
return s.getSHA512Half ();
} }
uint256 uint256
getDirNodeIndex (uint256 const& uDirRoot, const std::uint64_t uNodeIndex) getDirNodeIndex (uint256 const& uDirRoot, const std::uint64_t uNodeIndex)
{ {
if (uNodeIndex) if (uNodeIndex == 0)
{
Serializer s (42);
s.add16 (spaceDirNode);
s.add256 (uDirRoot);
s.add64 (uNodeIndex);
return s.getSHA512Half ();
}
else
{
return uDirRoot; return uDirRoot;
}
return sha512Half(
std::uint16_t(spaceDirNode),
uDirRoot,
std::uint64_t(uNodeIndex));
} }
uint256 uint256
@@ -161,6 +132,7 @@ getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir)
uint256 uNode (uBase); uint256 uNode (uBase);
// TODO(tom): there must be a better way. // TODO(tom): there must be a better way.
// VFALCO [base_uint] This assumes a certain storage format
((std::uint64_t*) uNode.end ())[-1] = htobe64 (uNodeDir); ((std::uint64_t*) uNode.end ())[-1] = htobe64 (uNodeDir);
return uNode; return uNode;
@@ -171,49 +143,39 @@ getQualityNext (uint256 const& uBase)
{ {
static beast::static_initializer<uint256> const uNext ( static beast::static_initializer<uint256> const uNext (
from_hex_text<uint256>("10000000000000000")); from_hex_text<uint256>("10000000000000000"));
return uBase + *uNext; return uBase + *uNext;
} }
std::uint64_t std::uint64_t
getQuality (uint256 const& uBase) getQuality (uint256 const& uBase)
{ {
// VFALCO [base_uint] This assumes a certain storage format
return be64toh (((std::uint64_t*) uBase.end ())[-1]); return be64toh (((std::uint64_t*) uBase.end ())[-1]);
} }
uint256 uint256
getTicketIndex (Account const& account, std::uint32_t uSequence) getTicketIndex (Account const& account, std::uint32_t uSequence)
{ {
Serializer s (26); return sha512Half(
std::uint16_t(spaceTicket),
s.add16 (spaceTicket); account,
s.add160 (account); std::uint32_t(uSequence));
s.add32 (uSequence);
return s.getSHA512Half ();
} }
uint256 uint256
getRippleStateIndex (Account const& a, Account const& b, Currency const& currency) getRippleStateIndex (Account const& a, Account const& b, Currency const& currency)
{ {
Serializer s (62);
s.add16 (spaceRipple);
if (a < b) if (a < b)
{ return sha512Half(
s.add160 (a); std::uint16_t(spaceRipple),
s.add160 (b); a,
} b,
else currency);
{ return sha512Half(
s.add160 (b); std::uint16_t(spaceRipple),
s.add160 (a); b,
} a,
currency);
s.add160 (currency);
return s.getSHA512Half ();
} }
uint256 uint256
@@ -225,12 +187,9 @@ getRippleStateIndex (Account const& a, Issue const& issue)
uint256 uint256
getSignerListIndex (Account const& account) getSignerListIndex (Account const& account)
{ {
Serializer s (22); return sha512Half(
std::uint16_t(spaceSignerList),
s.add16 (spaceSignerList); // 2 account);
s.add160 (account); // 20
return s.getSHA512Half ();
} }
} // namespace ripple } // ripple

View File

@@ -19,6 +19,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/crypto/ECDSA.h> #include <ripple/crypto/ECDSA.h>
#include <ripple/crypto/ECIES.h> #include <ripple/crypto/ECIES.h>
@@ -63,16 +64,8 @@ bool isCanonicalEd25519Signature (std::uint8_t const* signature)
static static
uint128 PassPhraseToKey (std::string const& passPhrase) uint128 PassPhraseToKey (std::string const& passPhrase)
{ {
Serializer s; return uint128::fromVoid(sha512Half_s(
make_Slice(passPhrase)).data());
s.addRaw (passPhrase);
// NIKB TODO this calling sequence is a bit ugly; this should be improved.
uint256 hash256 = s.getSHA512Half ();
uint128 ret (uint128::fromVoid (hash256.data()));
s.secureErase ();
return ret;
} }
static static
@@ -543,8 +536,9 @@ bool RippleAddress::accountPublicVerify (
&& isCanonicalEd25519Signature (signature); && isCanonicalEd25519Signature (signature);
} }
uint256 const uHash = getSHA512Half (message); return verifySignature (getAccountPublic(),
return verifySignature (getAccountPublic(), uHash, vucSig, fullyCanonical); sha512Half(make_Slice(message)), vucSig,
fullyCanonical);
} }
RippleAddress RippleAddress::createAccountID (Account const& account) RippleAddress RippleAddress::createAccountID (Account const& account)
@@ -632,9 +626,8 @@ Blob RippleAddress::accountPrivateSign (Blob const& message) const
return signature; return signature;
} }
uint256 const uHash = getSHA512Half (message); Blob result = ECDSASign(
sha512Half(make_Slice(message)), getAccountPrivate());
Blob result = ECDSASign (uHash, getAccountPrivate());
bool const ok = !result.empty(); bool const ok = !result.empty();
CondLog (!ok, lsWARNING, RippleAddress) CondLog (!ok, lsWARNING, RippleAddress)
@@ -893,14 +886,8 @@ RippleAddress RippleAddress::createSeedGeneric (std::string const& strText)
uint256 keyFromSeed (uint128 const& seed) uint256 keyFromSeed (uint128 const& seed)
{ {
Serializer s; return sha512Half_s(Slice(
seed.data(), seed.size()));
s.add128 (seed);
uint256 result = s.getSHA512Half();
s.secureErase ();
return result;
} }
RippleAddress getSeedFromRPC (Json::Value const& params) RippleAddress getSeedFromRPC (Json::Value const& params)

View File

@@ -19,9 +19,8 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/protocol/Serializer.h> #include <ripple/protocol/Serializer.h>
#include <openssl/ripemd.h>
#include <openssl/pem.h>
namespace ripple { namespace ripple {
@@ -298,25 +297,7 @@ Blob Serializer::getRaw (int offset, int length) const
uint256 Serializer::getSHA512Half () const uint256 Serializer::getSHA512Half () const
{ {
return ripple::getSHA512Half (mData); return sha512Half(make_Slice(mData));
}
uint256 Serializer::getPrefixHash (std::uint32_t prefix, const unsigned char* data, int len)
{
char be_prefix[4];
be_prefix[0] = static_cast<unsigned char> (prefix >> 24);
be_prefix[1] = static_cast<unsigned char> ((prefix >> 16) & 0xff);
be_prefix[2] = static_cast<unsigned char> ((prefix >> 8) & 0xff);
be_prefix[3] = static_cast<unsigned char> (prefix & 0xff);
uint256 j[2];
SHA512_CTX ctx;
SHA512_Init (&ctx);
SHA512_Update (&ctx, &be_prefix[0], 4);
SHA512_Update (&ctx, data, len);
SHA512_Final (reinterpret_cast<unsigned char*> (&j[0]), &ctx);
return j[0];
} }
int Serializer::addVL (Blob const& vector) int Serializer::addVL (Blob const& vector)
@@ -674,17 +655,4 @@ SerialIter::getVLBuffer()
return getRawHelper<Buffer> (getVLDataLength ()); return getRawHelper<Buffer> (getVLDataLength ());
} }
//------------------------------------------------------------------------------
uint256
getSHA512Half (void const* data, int len)
{
uint256 j[2];
SHA512 (
reinterpret_cast<unsigned char const*>(
data), len, (unsigned char*) j);
return j[0];
}
} // ripple } // ripple

View File

@@ -18,6 +18,7 @@
//============================================================================== //==============================================================================
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/TestSuite.h> #include <ripple/basics/TestSuite.h>
#include <ripple/protocol/RippleAddress.h> #include <ripple/protocol/RippleAddress.h>
#include <ripple/protocol/RipplePublicKey.h> #include <ripple/protocol/RipplePublicKey.h>
@@ -46,7 +47,7 @@ public:
// Check node signing. // Check node signing.
Blob vucTextSrc = strCopy ("Hello, nurse!"); Blob vucTextSrc = strCopy ("Hello, nurse!");
uint256 uHash = getSHA512Half (vucTextSrc); uint256 uHash = sha512Half(make_Slice(vucTextSrc));
Blob vucTextSig; Blob vucTextSig;
naNodePrivate.signNodePrivate (uHash, vucTextSig); naNodePrivate.signNodePrivate (uHash, vucTextSig);

View File

@@ -1,45 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/protocol/Serializer.h>
#include <beast/unit_test/suite.h>
namespace ripple {
class Serializer_test : public beast::unit_test::suite
{
public:
void run ()
{
Serializer s1;
s1.add32 (3);
s1.add256 (uint256 ());
Serializer s2;
s2.add32 (0x12345600);
s2.addRaw (s1.peekData ());
expect (s1.getPrefixHash (0x12345600) == s2.getSHA512Half ());
}
};
BEAST_DEFINE_TESTSUITE(Serializer,ripple_data,ripple);
} // ripple

View File

@@ -20,11 +20,15 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/shamap/SHAMapTreeNode.h> #include <ripple/shamap/SHAMapTreeNode.h>
#include <ripple/basics/Log.h> #include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/protocol/HashPrefix.h> #include <ripple/protocol/HashPrefix.h>
#include <beast/module/core/text/LexicalCast.h> #include <beast/module/core/text/LexicalCast.h>
#include <mutex> #include <mutex>
#include <openssl/sha.h>
namespace ripple { namespace ripple {
std::mutex SHAMapTreeNode::childLock; std::mutex SHAMapTreeNode::childLock;
@@ -106,7 +110,10 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode,
if (type == 0) if (type == 0)
{ {
// transaction // transaction
mItem = std::make_shared<SHAMapItem> (s.getPrefixHash (HashPrefix::transactionID), s.peekData ()); mItem = std::make_shared<SHAMapItem>(
sha512Half(HashPrefix::transactionID,
Slice(s.data(), s.size() - 1)),
s.peekData());
mType = tnTRANSACTION_NM; mType = tnTRANSACTION_NM;
} }
else if (type == 1) else if (type == 1)
@@ -195,7 +202,9 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode,
if (prefix == HashPrefix::transactionID) if (prefix == HashPrefix::transactionID)
{ {
mItem = std::make_shared<SHAMapItem> (getSHA512Half (rawNode), s.peekData ()); mItem = std::make_shared<SHAMapItem>(
sha512Half(make_Slice(rawNode)),
s.peekData ());
mType = tnTRANSACTION_NM; mType = tnTRANSACTION_NM;
} }
else if (prefix == HashPrefix::leafNode) else if (prefix == HashPrefix::leafNode)
@@ -276,15 +285,16 @@ bool SHAMapTreeNode::updateHash ()
{ {
if (mIsBranch != 0) if (mIsBranch != 0)
{ {
nh = Serializer::getPrefixHash (HashPrefix::innerNode, reinterpret_cast<unsigned char*> (mHashes), sizeof (mHashes)); // VFALCO This code assumes the layout of a base_uint
nh = sha512Half(HashPrefix::innerNode,
Slice(reinterpret_cast<unsigned char const*>(mHashes),
sizeof (mHashes)));
#if RIPPLE_VERIFY_NODEOBJECT_KEYS #if RIPPLE_VERIFY_NODEOBJECT_KEYS
Serializer s; SHA512HalfHasher h;
s.add32 (HashPrefix::innerNode); using beast::hash_append;
hash_append(h, HashPrefix::innerNode, mHashes);
for (int i = 0; i < 16; ++i) assert (nh == sha512Half(
s.add256 (mHashes[i]); static_cast<uint256>(h)));
assert (nh == s.getSHA512Half ());
#endif #endif
} }
else else
@@ -292,7 +302,8 @@ bool SHAMapTreeNode::updateHash ()
} }
else if (mType == tnTRANSACTION_NM) else if (mType == tnTRANSACTION_NM)
{ {
nh = Serializer::getPrefixHash (HashPrefix::transactionID, mItem->peekData ()); nh = sha512Half(HashPrefix::transactionID,
make_Slice(mItem->peekData()));
} }
else if (mType == tnACCOUNT_STATE) else if (mType == tnACCOUNT_STATE)
{ {
@@ -300,15 +311,15 @@ bool SHAMapTreeNode::updateHash ()
s.add32 (HashPrefix::leafNode); s.add32 (HashPrefix::leafNode);
s.addRaw (mItem->peekData ()); s.addRaw (mItem->peekData ());
s.add256 (mItem->getTag ()); s.add256 (mItem->getTag ());
nh = s.getSHA512Half (); nh = sha512Half(HashPrefix::leafNode,
make_Slice(mItem->peekData()),
mItem->getTag());
} }
else if (mType == tnTRANSACTION_MD) else if (mType == tnTRANSACTION_MD)
{ {
Serializer s (mItem->size() + (256 + 32) / 8); nh = sha512Half(HashPrefix::txNode,
s.add32 (HashPrefix::txNode); make_Slice(mItem->peekData()),
s.addRaw (mItem->peekData ()); mItem->getTag());
s.add256 (mItem->getTag ());
nh = s.getSHA512Half ();
} }
else else
assert (false); assert (false);

View File

@@ -20,6 +20,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/shamap/SHAMap.h> #include <ripple/shamap/SHAMap.h>
#include <ripple/shamap/tests/common.h> #include <ripple/shamap/tests/common.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/StringUtilities.h> #include <ripple/basics/StringUtilities.h>
#include <ripple/basics/UnorderedContainers.h> #include <ripple/basics/UnorderedContainers.h>
#include <ripple/protocol/UInt160.h> #include <ripple/protocol/UInt160.h>
@@ -107,8 +108,8 @@ public:
void on_fetch (Map& map, uint256 const& hash, Blob const& blob) void on_fetch (Map& map, uint256 const& hash, Blob const& blob)
{ {
Serializer s (blob); expect (sha512Half(make_Slice(blob)) == hash,
expect (s.getSHA512Half() == hash, "Hash mismatch"); "Hash mismatch");
map.emplace (hash, blob); map.emplace (hash, blob);
} }

View File

@@ -59,7 +59,6 @@
#include <ripple/protocol/tests/Issue.test.cpp> #include <ripple/protocol/tests/Issue.test.cpp>
#include <ripple/protocol/tests/Quality.test.cpp> #include <ripple/protocol/tests/Quality.test.cpp>
#include <ripple/protocol/tests/RippleAddress.test.cpp> #include <ripple/protocol/tests/RippleAddress.test.cpp>
#include <ripple/protocol/tests/Serializer.test.cpp>
#include <ripple/protocol/tests/STAmount.test.cpp> #include <ripple/protocol/tests/STAmount.test.cpp>
#include <ripple/protocol/tests/STObject.test.cpp> #include <ripple/protocol/tests/STObject.test.cpp>
#include <ripple/protocol/tests/STTx.test.cpp> #include <ripple/protocol/tests/STTx.test.cpp>