diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index 14744bd826..4a73380ee7 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -367,6 +367,10 @@ True + + + + True @@ -1964,6 +1968,8 @@ + + @@ -2852,10 +2858,6 @@ True True - - True - True - True True diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index 1cffe2eb4f..c1e00daa63 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -43,6 +43,9 @@ {ED78E906-CE72-CDEF-3018-99E02603C37E} + + {398085D5-BDC7-D75C-2A1D-73395AC7A158} + {FF9CBE50-56A0-7B59-C99F-C873605C844B} @@ -804,6 +807,12 @@ beast\crypto + + beast\crypto\impl + + + beast\crypto + beast\crypto\tests @@ -2667,6 +2676,9 @@ ripple\basics + + ripple\basics + ripple\basics @@ -3528,9 +3540,6 @@ ripple\protocol\tests - - ripple\protocol\tests - ripple\protocol\tests diff --git a/src/ripple/app/ledger/ConsensusTransSetSF.cpp b/src/ripple/app/ledger/ConsensusTransSetSF.cpp index a6822739a5..2341c6ef19 100644 --- a/src/ripple/app/ledger/ConsensusTransSetSF.cpp +++ b/src/ripple/app/ledger/ConsensusTransSetSF.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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; } diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 418dfc26bf..fd4bf1c26b 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -26,15 +26,16 @@ #include #include #include -#include -#include #include #include #include #include #include #include +#include #include +#include +#include #include #include #include @@ -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; } diff --git a/src/ripple/app/ledger/LedgerProposal.cpp b/src/ripple/app/ledger/LedgerProposal.cpp index 26242f3f92..99d4366927 100644 --- a/src/ripple/app/ledger/LedgerProposal.cpp +++ b/src/ripple/app/ledger/LedgerProposal.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -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 diff --git a/src/ripple/app/main/Main.cpp b/src/ripple/app/main/Main.cpp index ed2e1bb809..6c75c540ca 100644 --- a/src/ripple/app/main/Main.cpp +++ b/src/ripple/app/main/Main.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -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) + diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index 1aa9915bdf..e186e0d012 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include #include #include @@ -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; diff --git a/src/ripple/app/misc/UniqueNodeList.cpp b/src/ripple/app/misc/UniqueNodeList.cpp index 1a95407f84..60887e7aad 100644 --- a/src/ripple/app/misc/UniqueNodeList.cpp +++ b/src/ripple/app/misc/UniqueNodeList.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -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? diff --git a/src/ripple/basics/SHA512Half.h b/src/ripple/basics/SHA512Half.h new file mode 100644 index 0000000000..affeb21547 --- /dev/null +++ b/src/ripple/basics/SHA512Half.h @@ -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 +#include // needed by most callers +#include +#include +#include // + +namespace ripple { + +namespace detail { + +template +class SHA512HalfHasher +{ +private: + using hasher_type = + std::conditional_t; + + 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; + +/** Returns the SHA512-Half of a series of objects. */ +template +SHA512HalfHasher::result_type +sha512Half (Args const&... args) +{ + SHA512HalfHasher h; + using beast::hash_append; + hash_append(h, args...); + return static_cast(h); +} + +/** Returns the SHA512-Half of a series of objects. + + Postconditions: + Temporary memory storing copies of + input messages will be cleared. +*/ +template +SHA512HalfHasher::result_type +sha512Half_s (Args const&... args) +{ + detail::SHA512HalfHasher h; + using beast::hash_append; + hash_append(h, args...); + return static_cast(h); +} + +} + +#endif diff --git a/src/ripple/basics/Slice.h b/src/ripple/basics/Slice.h index 8b320e3ad3..51a40cc701 100644 --- a/src/ripple/basics/Slice.h +++ b/src/ripple/basics/Slice.h @@ -25,6 +25,9 @@ #include #include #include +#include +#include +#include // 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 Stream& operator<<(Stream& s, Slice const& v) { @@ -120,6 +124,24 @@ Stream& operator<<(Stream& s, Slice const& v) return s; } +template +std::enable_if_t< + std::is_same::value || + std::is_same::value, + Slice +> +make_Slice (std::vector const& v) +{ + return Slice(v.data(), v.size()); +} + +template +Slice +make_Slice (std::basic_string const& s) +{ + return Slice(s.data(), s.size()); +} + } // ripple #endif diff --git a/src/ripple/basics/base_uint.h b/src/ripple/basics/base_uint.h index 9f0af6b32e..e1ebc16616 100644 --- a/src/ripple/basics/base_uint.h +++ b/src/ripple/basics/base_uint.h @@ -30,10 +30,9 @@ #include #include #include - #include - #include +#include using beast::zero; using beast::Zero; @@ -290,11 +289,13 @@ public: return *this; } - template - friend void hash_append(Hasher& h, base_uint const& a) noexcept + template > + 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 -struct hash> -{ - using argument_type = ripple::base_uint; +struct is_uniquely_represented> + : public std::true_type + {}; - std::size_t - operator()(argument_type const& u) const - { - return ripple::hardened_hash<>{}(u); - } -}; - -} // boost +} // beast #endif diff --git a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp index 804d2c4986..2ba7b39a52 100644 --- a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp +++ b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -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(data), bytes, - reinterpret_cast (j)); - return j[0]; -} - template void copy_uint32 (FwdIt out, std::uint32_t v) @@ -86,10 +78,9 @@ static bignum generateRootDeterministicKey (uint128 const& seed) std::array 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); diff --git a/src/ripple/nodestore/impl/DatabaseImp.h b/src/ripple/nodestore/impl/DatabaseImp.h index a47cfcf72a..40d4af1714 100644 --- a/src/ripple/nodestore/impl/DatabaseImp.h +++ b/src/ripple/nodestore/impl/DatabaseImp.h @@ -23,10 +23,12 @@ #include #include #include -#include #include #include #include +#include +#include +#include #include #include #include @@ -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); diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index ba65af9dcc..c32087c275 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -1390,10 +1391,12 @@ PeerImp::onMessage (std::shared_ptr 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 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; diff --git a/src/ripple/overlay/impl/TMHello.cpp b/src/ripple/overlay/impl/TMHello.cpp index 4f3bf9d0b0..14f158a31b 100644 --- a/src/ripple/overlay/impl/TMHello.cpp +++ b/src/ripple/overlay/impl/TMHello.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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; } diff --git a/src/ripple/protocol/HashPrefix.h b/src/ripple/protocol/HashPrefix.h index 5b34118c40..a20472818a 100644 --- a/src/ripple/protocol/HashPrefix.h +++ b/src/ripple/protocol/HashPrefix.h @@ -20,6 +20,8 @@ #ifndef RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED #define RIPPLE_PROTOCOL_HASHPREFIX_H_INCLUDED +#include +#include #include namespace ripple { @@ -97,6 +99,15 @@ public: static HashPrefix const manifest; }; +template +void +hash_append (Hasher& h, HashPrefix const& hp) noexcept +{ + using beast::hash_append; + hash_append(h, + static_cast(hp)); +} + } // ripple #endif diff --git a/src/ripple/protocol/Serializer.h b/src/ripple/protocol/Serializer.h index 159a1dba90..9135e0c7cc 100644 --- a/src/ripple/protocol/Serializer.h +++ b/src/ripple/protocol/Serializer.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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 (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 ::value && - sizeof(T) == 1>* = nullptr> -inline -uint256 -getSHA512Half (std::vector const& v) -{ - return getSHA512Half(v.data(), v.size()); -} - } // ripple #endif diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index 516746eec6..83643a30dc 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include #include @@ -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 const uNext ( from_hex_text("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 diff --git a/src/ripple/protocol/impl/RippleAddress.cpp b/src/ripple/protocol/impl/RippleAddress.cpp index 4ac4abd05f..c9292f582b 100644 --- a/src/ripple/protocol/impl/RippleAddress.cpp +++ b/src/ripple/protocol/impl/RippleAddress.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -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) diff --git a/src/ripple/protocol/impl/Serializer.cpp b/src/ripple/protocol/impl/Serializer.cpp index 90b628d36b..24725af95f 100644 --- a/src/ripple/protocol/impl/Serializer.cpp +++ b/src/ripple/protocol/impl/Serializer.cpp @@ -19,9 +19,8 @@ #include #include +#include #include -#include -#include 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 (prefix >> 24); - be_prefix[1] = static_cast ((prefix >> 16) & 0xff); - be_prefix[2] = static_cast ((prefix >> 8) & 0xff); - be_prefix[3] = static_cast (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 (&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 (getVLDataLength ()); } - -//------------------------------------------------------------------------------ - -uint256 -getSHA512Half (void const* data, int len) -{ - uint256 j[2]; - SHA512 ( - reinterpret_cast( - data), len, (unsigned char*) j); - return j[0]; -} - } // ripple diff --git a/src/ripple/protocol/tests/RippleAddress.test.cpp b/src/ripple/protocol/tests/RippleAddress.test.cpp index c6bdfcba1b..0261e8b374 100644 --- a/src/ripple/protocol/tests/RippleAddress.test.cpp +++ b/src/ripple/protocol/tests/RippleAddress.test.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include #include #include @@ -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); diff --git a/src/ripple/protocol/tests/Serializer.test.cpp b/src/ripple/protocol/tests/Serializer.test.cpp deleted file mode 100644 index 3ae89ace08..0000000000 --- a/src/ripple/protocol/tests/Serializer.test.cpp +++ /dev/null @@ -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 -#include -#include - -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 diff --git a/src/ripple/shamap/impl/SHAMapTreeNode.cpp b/src/ripple/shamap/impl/SHAMapTreeNode.cpp index e5b3586962..80e9631ac1 100644 --- a/src/ripple/shamap/impl/SHAMapTreeNode.cpp +++ b/src/ripple/shamap/impl/SHAMapTreeNode.cpp @@ -20,11 +20,15 @@ #include #include #include +#include +#include #include #include #include #include +#include + namespace ripple { std::mutex SHAMapTreeNode::childLock; @@ -106,7 +110,10 @@ SHAMapTreeNode::SHAMapTreeNode (Blob const& rawNode, if (type == 0) { // transaction - mItem = std::make_shared (s.getPrefixHash (HashPrefix::transactionID), s.peekData ()); + mItem = std::make_shared( + 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 (getSHA512Half (rawNode), s.peekData ()); + mItem = std::make_shared( + 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 (mHashes), sizeof (mHashes)); + // VFALCO This code assumes the layout of a base_uint + nh = sha512Half(HashPrefix::innerNode, + Slice(reinterpret_cast(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(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); diff --git a/src/ripple/shamap/tests/FetchPack.test.cpp b/src/ripple/shamap/tests/FetchPack.test.cpp index 08e0c0699d..eb2e4ab51d 100644 --- a/src/ripple/shamap/tests/FetchPack.test.cpp +++ b/src/ripple/shamap/tests/FetchPack.test.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -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); } diff --git a/src/ripple/unity/protocol.cpp b/src/ripple/unity/protocol.cpp index 62c4acab89..545cb9c3c4 100644 --- a/src/ripple/unity/protocol.cpp +++ b/src/ripple/unity/protocol.cpp @@ -59,7 +59,6 @@ #include #include #include -#include #include #include #include