diff --git a/Builds/VisualStudio2013/RippleD.vcxproj b/Builds/VisualStudio2013/RippleD.vcxproj index d1d5088e7..365becc70 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj +++ b/Builds/VisualStudio2013/RippleD.vcxproj @@ -1971,8 +1971,6 @@ - - diff --git a/Builds/VisualStudio2013/RippleD.vcxproj.filters b/Builds/VisualStudio2013/RippleD.vcxproj.filters index c933b2d66..b0ec0f015 100644 --- a/Builds/VisualStudio2013/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2013/RippleD.vcxproj.filters @@ -2721,9 +2721,6 @@ ripple\basics - - ripple\basics - ripple\basics diff --git a/src/ripple/app/ledger/ConsensusTransSetSF.cpp b/src/ripple/app/ledger/ConsensusTransSetSF.cpp index 6d284514e..5b51524ec 100644 --- a/src/ripple/app/ledger/ConsensusTransSetSF.cpp +++ b/src/ripple/app/ledger/ConsensusTransSetSF.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 38b871b6a..9f4421648 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/app/ledger/LedgerProposal.cpp b/src/ripple/app/ledger/LedgerProposal.cpp index 99d436692..c53bbe62d 100644 --- a/src/ripple/app/ledger/LedgerProposal.cpp +++ b/src/ripple/app/ledger/LedgerProposal.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/ripple/app/main/Main.cpp b/src/ripple/app/main/Main.cpp index 6c5ade1eb..63f658e93 100644 --- a/src/ripple/app/main/Main.cpp +++ b/src/ripple/app/main/Main.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index b137d76cc..ca04632ec 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/app/misc/UniqueNodeList.cpp b/src/ripple/app/misc/UniqueNodeList.cpp index 8e051d60b..fc33590fd 100644 --- a/src/ripple/app/misc/UniqueNodeList.cpp +++ b/src/ripple/app/misc/UniqueNodeList.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/basics/SHA512Half.h b/src/ripple/basics/SHA512Half.h deleted file mode 100644 index ae282b938..000000000 --- a/src/ripple/basics/SHA512Half.h +++ /dev/null @@ -1,123 +0,0 @@ -//------------------------------------------------------------------------------ -/* - 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/crypto/impl/GenerateDeterministicKey.cpp b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp index 2ba7b39a5..54e7b728f 100644 --- a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp +++ b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/nodestore/impl/DatabaseImp.h b/src/ripple/nodestore/impl/DatabaseImp.h index 3319e7efd..acb4e7360 100644 --- a/src/ripple/nodestore/impl/DatabaseImp.h +++ b/src/ripple/nodestore/impl/DatabaseImp.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index 798c45608..4d469a358 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/overlay/impl/TMHello.cpp b/src/ripple/overlay/impl/TMHello.cpp index 14f158a31..725beadd0 100644 --- a/src/ripple/overlay/impl/TMHello.cpp +++ b/src/ripple/overlay/impl/TMHello.cpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/protocol/digest.h b/src/ripple/protocol/digest.h index ae81f3c1f..845c3dbfa 100644 --- a/src/ripple/protocol/digest.h +++ b/src/ripple/protocol/digest.h @@ -20,10 +20,12 @@ #ifndef RIPPLE_PROTOCOL_DIGEST_H_INCLUDED #define RIPPLE_PROTOCOL_DIGEST_H_INCLUDED +#include #include #include #include #include +#include #include namespace ripple { @@ -65,6 +67,32 @@ private: char ctx_[96]; }; +/** SHA-512 digest + + @note This uses the OpenSSL implementation +*/ +struct openssl_sha512_hasher +{ +public: + static beast::endian const endian = + beast::endian::native; + + using result_type = + std::array; + + openssl_sha512_hasher(); + + void + operator()(void const* data, + std::size_t size) noexcept; + + explicit + operator result_type() noexcept; + +private: + char ctx_[216]; +}; + /** SHA-256 digest @note This uses the OpenSSL implementation @@ -98,9 +126,11 @@ private: #if RIPPLE_USE_OPENSSL using ripemd160_hasher = openssl_ripemd160_hasher; using sha256_hasher = openssl_sha256_hasher; +using sha512_hasher = openssl_sha512_hasher; #else using ripemd160_hasher = beast::ripemd160_hasher; using sha256_hasher = beast::sha256_hasher; +using sha512_hasher = beast::sha512_hasher; #endif //------------------------------------------------------------------------------ @@ -119,8 +149,6 @@ using sha256_hasher = beast::sha256_hasher; formula for calculating the account identifier. Meets the requirements of Hasher (in hash_append) - - @param digest A buffer of at least 20 bytes */ struct ripesha_hasher { @@ -144,15 +172,126 @@ public: explicit operator result_type() noexcept { - auto const d0 = static_cast< - decltype(h_)::result_type>(h_); + auto const d0 = + sha256_hasher::result_type(h_); ripemd160_hasher rh; rh(d0.data(), d0.size()); - return static_cast< - decltype(rh)::result_type>(rh); + return ripemd160_hasher::result_type(rh); } }; +//------------------------------------------------------------------------------ + +namespace detail { + +/** Returns the SHA512-Half digest of a message. + + The SHA512-Half is the first 256 bits of the + SHA-512 digest of the message. +*/ +template +struct basic_sha512_half_hasher +{ +private: + sha512_hasher h_; + +public: + static beast::endian const endian = + beast::endian::big; + + using result_type = uint256; + + ~basic_sha512_half_hasher() + { + erase(std::integral_constant< + bool, Secure>{}); + } + + void + operator()(void const* data, + std::size_t size) noexcept + { + h_(data, size); + } + + explicit + operator result_type() noexcept + { + auto const digest = + sha512_hasher::result_type(h_); + result_type result; + std::copy(digest.begin(), + digest.begin() + 32, result.begin()); + return result; + } + +private: + inline + void + erase (std::false_type) + { + } + + inline + void + erase (std::true_type) + { + beast::secure_erase(&h_, sizeof(h_)); + } +}; + +} // detail + +using sha512_half_hasher = + detail::basic_sha512_half_hasher; + +// secure version +using sha512_half_hasher_s = + detail::basic_sha512_half_hasher; + +//------------------------------------------------------------------------------ + +#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 + +/** Returns the SHA512-Half of a series of objects. */ +template +sha512_half_hasher::result_type +sha512Half (Args const&... args) +{ + sha512_half_hasher 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 +sha512_half_hasher_s::result_type +sha512Half_s (Args const&... args) +{ + sha512_half_hasher_s h; + using beast::hash_append; + hash_append(h, args...); + return static_cast(h); +} + } // ripple #endif diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index 4b8ab9cb7..4f6342176 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -18,7 +18,7 @@ //============================================================================== #include -#include +#include #include #include #include diff --git a/src/ripple/protocol/impl/RippleAddress.cpp b/src/ripple/protocol/impl/RippleAddress.cpp index db089ac28..7c4a436f6 100644 --- a/src/ripple/protocol/impl/RippleAddress.cpp +++ b/src/ripple/protocol/impl/RippleAddress.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/ripple/protocol/impl/Serializer.cpp b/src/ripple/protocol/impl/Serializer.cpp index 71c34f8b6..3d817b079 100644 --- a/src/ripple/protocol/impl/Serializer.cpp +++ b/src/ripple/protocol/impl/Serializer.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include namespace ripple { diff --git a/src/ripple/protocol/impl/digest.cpp b/src/ripple/protocol/impl/digest.cpp index c17d173b0..a369017fe 100644 --- a/src/ripple/protocol/impl/digest.cpp +++ b/src/ripple/protocol/impl/digest.cpp @@ -53,6 +53,38 @@ openssl_ripemd160_hasher::operator result_type() noexcept return digest; } +//------------------------------------------------------------------------------ + +openssl_sha512_hasher::openssl_sha512_hasher() +{ + static_assert(sizeof(decltype( + openssl_sha512_hasher::ctx_)) == + sizeof(SHA512_CTX), ""); + auto const ctx = reinterpret_cast< + SHA512_CTX*>(ctx_); + SHA512_Init(ctx); +} + +void +openssl_sha512_hasher::operator()(void const* data, + std::size_t size) noexcept +{ + auto const ctx = reinterpret_cast< + SHA512_CTX*>(ctx_); + SHA512_Update(ctx, data, size); +} + +openssl_sha512_hasher::operator result_type() noexcept +{ + auto const ctx = reinterpret_cast< + SHA512_CTX*>(ctx_); + result_type digest; + SHA512_Final(digest.data(), ctx); + return digest; +} + +//------------------------------------------------------------------------------ + openssl_sha256_hasher::openssl_sha256_hasher() { static_assert(sizeof(decltype( diff --git a/src/ripple/protocol/tests/RippleAddress.test.cpp b/src/ripple/protocol/tests/RippleAddress.test.cpp index 73820c075..95adaf536 100644 --- a/src/ripple/protocol/tests/RippleAddress.test.cpp +++ b/src/ripple/protocol/tests/RippleAddress.test.cpp @@ -18,7 +18,7 @@ //============================================================================== #include -#include +#include #include #include #include diff --git a/src/ripple/shamap/impl/SHAMapTreeNode.cpp b/src/ripple/shamap/impl/SHAMapTreeNode.cpp index f14407f21..c1f564087 100644 --- a/src/ripple/shamap/impl/SHAMapTreeNode.cpp +++ b/src/ripple/shamap/impl/SHAMapTreeNode.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include @@ -275,13 +275,6 @@ SHAMapInnerNode::updateHash() nh = sha512Half(HashPrefix::innerNode, Slice(reinterpret_cast(mHashes), sizeof (mHashes))); -#if RIPPLE_VERIFY_NODEOBJECT_KEYS - SHA512HalfHasher h; - using beast::hash_append; - hash_append(h, HashPrefix::innerNode, mHashes); - assert (nh == sha512Half( - static_cast(h))); -#endif } if (nh == mHash) return false; diff --git a/src/ripple/shamap/tests/FetchPack.test.cpp b/src/ripple/shamap/tests/FetchPack.test.cpp index 468695331..4ec25dc53 100644 --- a/src/ripple/shamap/tests/FetchPack.test.cpp +++ b/src/ripple/shamap/tests/FetchPack.test.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include