rippled
digest.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PROTOCOL_DIGEST_H_INCLUDED
21 #define RIPPLE_PROTOCOL_DIGEST_H_INCLUDED
22 
23 #include <ripple/basics/base_uint.h>
24 #include <ripple/crypto/secure_erase.h>
25 #include <boost/endian/conversion.hpp>
26 #include <algorithm>
27 #include <array>
28 
29 namespace ripple {
30 
40 //------------------------------------------------------------------------------
41 
47 {
48 public:
49  static constexpr auto const endian = boost::endian::order::native;
50 
52 
54 
55  void
56  operator()(void const* data, std::size_t size) noexcept;
57 
58  explicit operator result_type() noexcept;
59 
60 private:
61  char ctx_[96];
62 };
63 
69 {
70 public:
71  static constexpr auto const endian = boost::endian::order::native;
72 
74 
76 
77  void
78  operator()(void const* data, std::size_t size) noexcept;
79 
80  explicit operator result_type() noexcept;
81 
82 private:
83  char ctx_[216];
84 };
85 
91 {
92 public:
93  static constexpr auto const endian = boost::endian::order::native;
94 
96 
98 
99  void
100  operator()(void const* data, std::size_t size) noexcept;
101 
102  explicit operator result_type() noexcept;
103 
104 private:
105  char ctx_[112];
106 };
107 
108 //------------------------------------------------------------------------------
109 
113 
114 //------------------------------------------------------------------------------
115 
132 {
133 private:
135 
136 public:
137  static constexpr auto const endian = boost::endian::order::native;
138 
140 
141  void
142  operator()(void const* data, std::size_t size) noexcept
143  {
144  h_(data, size);
145  }
146 
147  explicit operator result_type() noexcept
148  {
149  auto const d0 = sha256_hasher::result_type(h_);
150  ripemd160_hasher rh;
151  rh(d0.data(), d0.size());
153  }
154 };
155 
156 //------------------------------------------------------------------------------
157 
158 namespace detail {
159 
165 template <bool Secure>
167 {
168 private:
170 
171 public:
172  static constexpr auto const endian = boost::endian::order::big;
173 
175 
177  {
179  }
180 
181  void
182  operator()(void const* data, std::size_t size) noexcept
183  {
184  h_(data, size);
185  }
186 
187  explicit operator result_type() noexcept
188  {
189  auto const digest = sha512_hasher::result_type(h_);
190  return result_type::fromVoid(digest.data());
191  }
192 
193 private:
194  inline void erase(std::false_type)
195  {
196  }
197 
198  inline void erase(std::true_type)
199  {
200  secure_erase(&h_, sizeof(h_));
201  }
202 };
203 
204 } // namespace detail
205 
207 
208 // secure version
210 
211 //------------------------------------------------------------------------------
212 
214 template <class... Args>
216 sha512Half(Args const&... args)
217 {
219  using beast::hash_append;
220  hash_append(h, args...);
221  return static_cast<typename sha512_half_hasher::result_type>(h);
222 }
223 
230 template <class... Args>
232 sha512Half_s(Args const&... args)
233 {
235  using beast::hash_append;
236  hash_append(h, args...);
237  return static_cast<typename sha512_half_hasher_s::result_type>(h);
238 }
239 
240 } // namespace ripple
241 
242 #endif
std::integral_constant
ripple::openssl_sha256_hasher
SHA-256 digest.
Definition: digest.h:90
ripple::openssl_ripemd160_hasher::result_type
std::array< std::uint8_t, 20 > result_type
Definition: digest.h:51
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:166
ripple::openssl_sha512_hasher
SHA-512 digest.
Definition: digest.h:68
ripple::openssl_ripemd160_hasher::openssl_ripemd160_hasher
openssl_ripemd160_hasher()
Definition: digest.cpp:27
ripple::detail::basic_sha512_half_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:182
ripple::openssl_ripemd160_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.cpp:38
ripple::sha512Half_s
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:232
algorithm
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::erase
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:171
ripple::base_uint< 256 >
ripple::openssl_sha512_hasher::result_type
std::array< std::uint8_t, 64 > result_type
Definition: digest.h:73
ripple::ripesha_hasher
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:131
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::true_type)
Definition: digest.h:198
array
ripple::detail::basic_sha512_half_hasher::h_
sha512_hasher h_
Definition: digest.h:169
ripple::ripesha_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:142
ripple::ripesha_hasher::h_
sha256_hasher h_
Definition: digest.h:134
ripple::detail::basic_sha512_half_hasher::~basic_sha512_half_hasher
~basic_sha512_half_hasher()
Definition: digest.h:176
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::false_type)
Definition: digest.h:194
ripple::openssl_sha256_hasher::result_type
std::array< std::uint8_t, 32 > result_type
Definition: digest.h:95
ripple::openssl_ripemd160_hasher::endian
static constexpr const auto endian
Definition: digest.h:49
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::basic_sha512_half_hasher::result_type
uint256 result_type
Definition: digest.h:174
beast::hash_append
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition: hash_append.h:236
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:216
std::size_t
ripple::openssl_ripemd160_hasher::ctx_
char ctx_[96]
Definition: digest.h:61
ripple::openssl_ripemd160_hasher
Message digest functions used in the codebase.
Definition: digest.h:46
ripple::hash_append
void hash_append(Hasher &h, ValidatorBlobInfo const &blobInfo)
Definition: ValidatorList.h:897
ripple::secure_erase
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
Definition: secure_erase.cpp:26