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">
<ExcludedFromBuild>True</ExcludedFromBuild>
</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">
<ExcludedFromBuild>True</ExcludedFromBuild>
</ClCompile>
@@ -1964,6 +1968,8 @@
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\seconds_clock.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\SHA512Half.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\Slice.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\basics\strHex.h">
@@ -2852,10 +2858,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</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">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -25,6 +25,8 @@
#include <ripple/overlay/ClusterNodeStatus.h>
#include <ripple/app/misc/UniqueNodeList.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/Time.h>
#include <ripple/core/Config.h>
@@ -1596,8 +1598,10 @@ bool UniqueNodeListImp::responseFetch (std::string const& strDomain, const boost
assert (bFound);
(void) bFound;
uint256 iSha256 = getSHA512Half (strSiteFile);
bool bChangedB = sdCurrent.iSha256 != iSha256;
uint256 iSha256 =
sha512Half(make_Slice(strSiteFile));
bool bChangedB =
sdCurrent.iSha256 != iSha256;
sdCurrent.strDomain = strDomain;
// 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 <cassert>
#include <cstdint>
#include <string>
#include <vector>
#include <beast/cxx14/type_traits.h> // <type_traits>
namespace ripple {
@@ -85,7 +88,7 @@ inline
void
hash_append (Hasher& h, Slice const& v)
{
h.append(v.data(), v.size());
h(v.data(), v.size());
}
inline
@@ -113,6 +116,7 @@ operator< (Slice const& lhs, Slice const& rhs) noexcept
rhs.data(), rhs.data() + rhs.size());
}
template <class Stream>
Stream& operator<<(Stream& s, Slice const& v)
{
@@ -120,6 +124,24 @@ Stream& operator<<(Stream& s, Slice const& v)
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
#endif

View File

@@ -30,10 +30,9 @@
#include <ripple/basics/strHex.h>
#include <ripple/basics/hardened_hash.h>
#include <beast/utility/Zero.h>
#include <boost/functional/hash.hpp>
#include <functional>
#include <type_traits>
using beast::zero;
using beast::Zero;
@@ -290,11 +289,13 @@ public:
return *this;
}
template <class Hasher>
friend void hash_append(Hasher& h, base_uint const& a) noexcept
template <class Hasher,
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;
hash_append (h, a.pn);
// Do not allow any endian transformations on this memory
h(a.pn, sizeof(a.pn));
}
bool SetHexExact (const char* psz)
@@ -534,23 +535,20 @@ inline std::ostream& operator<< (
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
namespace boost
namespace beast
{
template <std::size_t Bits, class Tag>
struct hash<ripple::base_uint<Bits, Tag>>
{
using argument_type = ripple::base_uint<Bits, Tag>;
struct is_uniquely_represented<ripple::base_uint<Bits, Tag>>
: public std::true_type
{};
std::size_t
operator()(argument_type const& u) const
{
return ripple::hardened_hash<>{}(u);
}
};
} // boost
} // beast
#endif

View File

@@ -21,6 +21,7 @@
#include <ripple/crypto/GenerateDeterministicKey.h>
#include <ripple/crypto/impl/ec_key.h>
#include <ripple/crypto/impl/openssl.h>
#include <ripple/basics/SHA512Half.h>
#include <array>
#include <string>
#include <openssl/pem.h>
@@ -48,15 +49,6 @@ static Blob serialize_ec_point (ec_point const& point)
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>
void
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::copy(seed.begin(), seed.end(), buf.begin());
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
privKey.assign ((unsigned char const*) &root, sizeof (root));
root.zero(); // security erase
}
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());
copy_uint32 (buf.begin() + 33, seq);
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
result.assign ((unsigned char const*) &root, sizeof (root));
root.zero(); // security erase
}
while (result.is_zero() || result >= order);

View File

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

View File

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

View File

@@ -22,6 +22,7 @@
#include <ripple/app/main/Application.h>
#include <ripple/app/main/LocalCredentials.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/protocol/BuildInfo.h>
#include <ripple/overlay/impl/TMHello.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
// we have calculated.
result.first = getSHA512Half (sha1, sizeof(sha1));
result.first = sha512Half(Slice(sha1, sizeof(sha1)));
result.second = true;
return result;
}

View File

@@ -20,6 +20,8 @@
#ifndef RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED
#define RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED
#include <beast/hash/hash_append.h>
#include <beast/utility/noexcept.h>
#include <cstdint>
namespace ripple {
@@ -97,6 +99,15 @@ public:
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
#endif

View File

@@ -23,6 +23,7 @@
#include <ripple/protocol/SField.h>
#include <ripple/basics/base_uint.h>
#include <ripple/basics/Buffer.h>
#include <ripple/basics/Slice.h>
#include <beast/utility/noexcept.h>
#include <cassert>
#include <cstdint>
@@ -65,6 +66,11 @@ public:
;
}
Slice slice() const noexcept
{
return Slice(mData.data(), mData.size());
}
std::size_t
size() const noexcept
{
@@ -169,21 +175,6 @@ public:
// DEPRECATED
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
Blob const& peekData () const
{
@@ -317,10 +308,17 @@ private:
std::uint8_t const* p_;
std::size_t remain_;
std::size_t used_ = 0;
public:
SerialIter (void const* data,
std::size_t size) noexcept;
SerialIter (Slice const& slice)
: SerialIter(slice.data(), slice.size())
{
}
// VFALCO TODO Remove this overload use Slice instead
explicit
SerialIter (std::string const& s) noexcept
: SerialIter(s.data(), s.size())
@@ -429,30 +427,6 @@ SerialIter::getBitString()
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
#endif

View File

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

View File

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

View File

@@ -19,9 +19,8 @@
#include <BeastConfig.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/protocol/Serializer.h>
#include <openssl/ripemd.h>
#include <openssl/pem.h>
namespace ripple {
@@ -298,25 +297,7 @@ Blob Serializer::getRaw (int offset, int length) const
uint256 Serializer::getSHA512Half () const
{
return ripple::getSHA512Half (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];
return sha512Half(make_Slice(mData));
}
int Serializer::addVL (Blob const& vector)
@@ -674,17 +655,4 @@ SerialIter::getVLBuffer()
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

View File

@@ -18,6 +18,7 @@
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/TestSuite.h>
#include <ripple/protocol/RippleAddress.h>
#include <ripple/protocol/RipplePublicKey.h>
@@ -46,7 +47,7 @@ public:
// Check node signing.
Blob vucTextSrc = strCopy ("Hello, nurse!");
uint256 uHash = getSHA512Half (vucTextSrc);
uint256 uHash = sha512Half(make_Slice(vucTextSrc));
Blob 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 <ripple/shamap/SHAMapTreeNode.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/SHA512Half.h>
#include <ripple/basics/Slice.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/protocol/HashPrefix.h>
#include <beast/module/core/text/LexicalCast.h>
#include <mutex>
#include <openssl/sha.h>
namespace ripple {
std::mutex SHAMapTreeNode::childLock;
@@ -106,7 +110,10 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode,
if (type == 0)
{
// 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;
}
else if (type == 1)
@@ -195,7 +202,9 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode,
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;
}
else if (prefix == HashPrefix::leafNode)
@@ -276,15 +285,16 @@ bool SHAMapTreeNode::updateHash ()
{
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
Serializer s;
s.add32 (HashPrefix::innerNode);
for (int i = 0; i < 16; ++i)
s.add256 (mHashes[i]);
assert (nh == s.getSHA512Half ());
SHA512HalfHasher h;
using beast::hash_append;
hash_append(h, HashPrefix::innerNode, mHashes);
assert (nh == sha512Half(
static_cast<uint256>(h)));
#endif
}
else
@@ -292,7 +302,8 @@ bool SHAMapTreeNode::updateHash ()
}
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)
{
@@ -300,15 +311,15 @@ bool SHAMapTreeNode::updateHash ()
s.add32 (HashPrefix::leafNode);
s.addRaw (mItem->peekData ());
s.add256 (mItem->getTag ());
nh = s.getSHA512Half ();
nh = sha512Half(HashPrefix::leafNode,
make_Slice(mItem->peekData()),
mItem->getTag());
}
else if (mType == tnTRANSACTION_MD)
{
Serializer s (mItem->size() + (256 + 32) / 8);
s.add32 (HashPrefix::txNode);
s.addRaw (mItem->peekData ());
s.add256 (mItem->getTag ());
nh = s.getSHA512Half ();
nh = sha512Half(HashPrefix::txNode,
make_Slice(mItem->peekData()),
mItem->getTag());
}
else
assert (false);

View File

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

View File

@@ -59,7 +59,6 @@
#include <ripple/protocol/tests/Issue.test.cpp>
#include <ripple/protocol/tests/Quality.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/STObject.test.cpp>
#include <ripple/protocol/tests/STTx.test.cpp>