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/beast/crypto/ripemd.h>
25 #include <ripple/beast/crypto/sha2.h>
26 #include <ripple/beast/hash/endian.h>
27 #include <algorithm>
28 #include <array>
29 
30 namespace ripple {
31 
32 /* Message digest functions used in the Ripple Protocol
33 
34  Modeled to meet the requirements of `Hasher` in the
35  `hash_append` interface, currently in proposal:
36 
37  N3980 "Types Don't Know #"
38  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3980.html
39 */
40 
41 //------------------------------------------------------------------------------
42 
48 {
49 public:
51 
53 
55 
56  void
57  operator()(void const* data, std::size_t size) noexcept;
58 
59  explicit operator result_type() noexcept;
60 
61 private:
62  char ctx_[96];
63 };
64 
70 {
71 public:
73 
75 
77 
78  void
79  operator()(void const* data, std::size_t size) noexcept;
80 
81  explicit operator result_type() noexcept;
82 
83 private:
84  char ctx_[216];
85 };
86 
92 {
93 public:
95 
97 
99 
100  void
101  operator()(void const* data, std::size_t size) noexcept;
102 
103  explicit operator result_type() noexcept;
104 
105 private:
106  char ctx_[112];
107 };
108 
109 //------------------------------------------------------------------------------
110 
111 // Aliases to choose the correct digest implementation
112 
113 #if USE_BEAST_HASHER
117 #else
121 #endif
122 
123 //------------------------------------------------------------------------------
124 
141 {
142 private:
144 
145 public:
147 
149 
150  void
151  operator()(void const* data, std::size_t size) noexcept
152  {
153  h_(data, size);
154  }
155 
156  explicit operator result_type() noexcept
157  {
158  auto const d0 = sha256_hasher::result_type(h_);
159  ripemd160_hasher rh;
160  rh(d0.data(), d0.size());
162  }
163 };
164 
165 //------------------------------------------------------------------------------
166 
167 namespace detail {
168 
174 template <bool Secure>
176 {
177 private:
179 
180 public:
182 
184 
186  {
188  }
189 
190  void
191  operator()(void const* data, std::size_t size) noexcept
192  {
193  h_(data, size);
194  }
195 
196  explicit operator result_type() noexcept
197  {
198  auto const digest = sha512_hasher::result_type(h_);
199  result_type result;
200  std::copy(digest.begin(), digest.begin() + 32, result.begin());
201  return result;
202  }
203 
204 private:
205  inline void erase(std::false_type)
206  {
207  }
208 
209  inline void erase(std::true_type)
210  {
211  secure_erase(&h_, sizeof(h_));
212  }
213 };
214 
215 } // namespace detail
216 
218 
219 // secure version
221 
222 //------------------------------------------------------------------------------
223 
225 template <class... Args>
227 sha512Half(Args const&... args)
228 {
230  using beast::hash_append;
231  hash_append(h, args...);
232  return static_cast<typename sha512_half_hasher::result_type>(h);
233 }
234 
241 template <class... Args>
243 sha512Half_s(Args const&... args)
244 {
246  using beast::hash_append;
247  hash_append(h, args...);
248  return static_cast<typename sha512_half_hasher_s::result_type>(h);
249 }
250 
251 } // namespace ripple
252 
253 #endif
std::integral_constant
ripple::openssl_sha256_hasher
SHA-256 digest.
Definition: digest.h:91
ripple::openssl_ripemd160_hasher::result_type
std::array< std::uint8_t, 20 > result_type
Definition: digest.h:52
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:175
beast::endian::big
@ big
ripple::openssl_sha512_hasher
SHA-512 digest.
Definition: digest.h:69
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:191
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:243
algorithm
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:493
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:43
ripple::erase
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:171
ripple::base_uint< 256 >
beast::sha256_hasher
detail::mac_facade< detail::sha256_context, false > sha256_hasher
Definition: sha2.h:28
beast::sha512_hasher
detail::mac_facade< detail::sha512_context, false > sha512_hasher
Definition: sha2.h:33
ripple::openssl_sha512_hasher::result_type
std::array< std::uint8_t, 64 > result_type
Definition: digest.h:74
ripple::ripesha_hasher
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:140
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::true_type)
Definition: digest.h:209
beast::endian::native
@ native
array
ripple::detail::basic_sha512_half_hasher::h_
sha512_hasher h_
Definition: digest.h:178
std::copy
T copy(T... args)
ripple::ripesha_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:151
ripple::ripesha_hasher::h_
sha256_hasher h_
Definition: digest.h:143
ripple::detail::basic_sha512_half_hasher::~basic_sha512_half_hasher
~basic_sha512_half_hasher()
Definition: digest.h:185
beast::ripemd160_hasher
detail::mac_facade< detail::ripemd160_context, false > ripemd160_hasher
Definition: ripemd.h:28
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::false_type)
Definition: digest.h:205
ripple::openssl_sha256_hasher::result_type
std::array< std::uint8_t, 32 > result_type
Definition: digest.h:96
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:183
ripple::base_uint::begin
iterator begin()
Definition: base_uint.h:114
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:232
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:227
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:195
std::size_t
ripple::openssl_ripemd160_hasher::ctx_
char ctx_[96]
Definition: digest.h:62
ripple::openssl_ripemd160_hasher
RIPEMD-160 digest.
Definition: digest.h:47
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
beast::endian
endian
Definition: endian.h:30
ripple::openssl_ripemd160_hasher::endian
static const beast::endian endian
Definition: digest.h:50