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:
50  static beast::endian const endian =
52 
53  using result_type =
55 
57 
58  void
59  operator()(void const* data,
60  std::size_t size) noexcept;
61 
62  explicit
63  operator result_type() noexcept;
64 
65 private:
66  char ctx_[96];
67 };
68 
74 {
75 public:
76  static beast::endian const endian =
78 
79  using result_type =
81 
83 
84  void
85  operator()(void const* data,
86  std::size_t size) noexcept;
87 
88  explicit
89  operator result_type() noexcept;
90 
91 private:
92  char ctx_[216];
93 };
94 
100 {
101 public:
102  static beast::endian const endian =
104 
105  using result_type =
107 
109 
110  void
111  operator()(void const* data,
112  std::size_t size) noexcept;
113 
114  explicit
115  operator result_type() noexcept;
116 
117 private:
118  char ctx_[112];
119 };
120 
121 //------------------------------------------------------------------------------
122 
123 // Aliases to choose the correct digest implementation
124 
125 #if USE_BEAST_HASHER
129 #else
133 #endif
134 
135 //------------------------------------------------------------------------------
136 
153 {
154 private:
156 
157 public:
158  static beast::endian const endian =
160 
161  using result_type =
163 
164  void
165  operator()(void const* data,
166  std::size_t size) noexcept
167  {
168  h_(data, size);
169  }
170 
171  explicit
172  operator result_type() noexcept
173  {
174  auto const d0 =
176  ripemd160_hasher rh;
177  rh(d0.data(), d0.size());
179  }
180 };
181 
182 //------------------------------------------------------------------------------
183 
184 namespace detail {
185 
191 template <bool Secure>
193 {
194 private:
196 
197 public:
198  static beast::endian const endian =
200 
202 
204  {
206  bool, Secure>{});
207  }
208 
209  void
210  operator()(void const* data,
211  std::size_t size) noexcept
212  {
213  h_(data, size);
214  }
215 
216  explicit
217  operator result_type() noexcept
218  {
219  auto const digest =
221  result_type result;
222  std::copy(digest.begin(),
223  digest.begin() + 32, result.begin());
224  return result;
225  }
226 
227 private:
228  inline
229  void
231  {
232  }
233 
234  inline
235  void
237  {
238  beast::secure_erase(&h_, sizeof(h_));
239  }
240 };
241 
242 } // detail
243 
244 using sha512_half_hasher =
246 
247 // secure version
248 using sha512_half_hasher_s =
250 
251 //------------------------------------------------------------------------------
252 
253 #ifdef _MSC_VER
254 // Call from main to fix magic statics pre-VS2015
255 inline
256 void
258 {
260  auto const digest = static_cast<
262 }
263 #endif
264 
266 template <class... Args>
268 sha512Half (Args const&... args)
269 {
271  using beast::hash_append;
272  hash_append(h, args...);
273  return static_cast<typename
275 }
276 
283 template <class... Args>
285 sha512Half_s (Args const&... args)
286 {
288  using beast::hash_append;
289  hash_append(h, args...);
290  return static_cast<typename
292 }
293 
294 } // ripple
295 
296 #endif
beast::sha512_hasher
detail::mac_facade< detail::sha512_context, false > sha512_hasher
Definition: sha2.h:36
std::integral_constant
ripple::openssl_sha256_hasher
SHA-256 digest.
Definition: digest.h:99
ripple::openssl_ripemd160_hasher::result_type
std::array< std::uint8_t, 20 > result_type
Definition: digest.h:54
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::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:192
beast::sha256_hasher
detail::mac_facade< detail::sha256_context, false > sha256_hasher
Definition: sha2.h:29
beast::endian::big
@ big
ripple::openssl_sha512_hasher
SHA-512 digest.
Definition: digest.h:73
ripple::openssl_ripemd160_hasher::openssl_ripemd160_hasher
openssl_ripemd160_hasher()
Definition: digest.cpp:27
ripple::sha512_half_hasher
detail::basic_sha512_half_hasher< false > sha512_half_hasher
Definition: digest.h:245
ripple::detail::basic_sha512_half_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:210
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:285
algorithm
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:44
ripple::erase
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:201
ripple::base_uint< 256 >
ripple::openssl_sha512_hasher::result_type
std::array< std::uint8_t, 64 > result_type
Definition: digest.h:80
ripple::ripesha_hasher
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:152
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::true_type)
Definition: digest.h:236
beast::endian::native
@ native
array
ripple::detail::basic_sha512_half_hasher::h_
sha512_hasher h_
Definition: digest.h:195
std::copy
T copy(T... args)
ripple::ripesha_hasher::operator()
void operator()(void const *data, std::size_t size) noexcept
Definition: digest.h:165
beast::secure_erase
void secure_erase(void *dest, std::size_t bytes)
Guaranteed to fill memory with zeroes.
Definition: secure_erase.h:83
ripple::ripesha_hasher::h_
sha256_hasher h_
Definition: digest.h:155
ripple::detail::basic_sha512_half_hasher::~basic_sha512_half_hasher
~basic_sha512_half_hasher()
Definition: digest.h:203
ripple::detail::basic_sha512_half_hasher::erase
void erase(std::false_type)
Definition: digest.h:230
beast::ripemd160_hasher
detail::mac_facade< detail::ripemd160_context, false > ripemd160_hasher
Definition: ripemd.h:29
ripple::openssl_sha256_hasher::result_type
std::array< std::uint8_t, 32 > result_type
Definition: digest.h:106
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:201
ripple::base_uint::begin
iterator begin()
Definition: base_uint.h:106
beast::detail::mac_facade
Definition: mac_facade.h:33
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:268
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:151
std::size_t
ripple::openssl_ripemd160_hasher::ctx_
char ctx_[96]
Definition: digest.h:66
ripple::openssl_ripemd160_hasher
RIPEMD-160 digest.
Definition: digest.h:47
ripple::sha512_deprecatedMSVCWorkaround
void sha512_deprecatedMSVCWorkaround()
Definition: digest.h:257
beast::endian
endian
Definition: endian.h:30
ripple::openssl_ripemd160_hasher::endian
static const beast::endian endian
Definition: digest.h:50