ripemd.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_HASH_RIPEMD_HPP
27 #define CRYPTO3_HASH_RIPEMD_HPP
28 
31 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace hashes {
39  template<std::size_t DigestBits>
42 
43  constexpr static const std::size_t word_bits = policy_type::word_bits;
45 
46  constexpr static const std::size_t state_bits = policy_type::state_bits;
47  constexpr static const std::size_t state_words = policy_type::state_words;
48  typedef typename policy_type::state_type state_type;
49 
50  constexpr static const std::size_t block_bits = policy_type::block_bits;
51  constexpr static const std::size_t block_words = policy_type::block_words;
53  };
54 
55  template<std::size_t DigestBits>
56  struct ripemd_compressor : public basic_ripemd_compressor<DigestBits> { };
57 
58  template<>
59  struct ripemd_compressor<128> : public basic_ripemd_compressor<128> {
60  static void process_block(state_type &state, const block_type &block) {
61  // ripemd works on two 'lines' in parallel
62  // all variables worked on by line 1 are suffixed with 1
63  // all variables for line 2 with 2
64 
65  word_type A1 = state[0], B1 = state[1], C1 = state[2], D1 = state[3];
66  word_type A2 = state[0], B2 = state[1], C2 = state[2], D2 = state[3];
67 
68  // round 1
69 
70  for (int j = 0; j < 16; ++j) {
71  policy_type::transform<policy_type::f1>(A1, B1, C1, D1, block[policy_type::r1[j]], 0x00000000,
72  policy_type::s1[j]);
73  policy_type::transform<policy_type::f4>(A2, B2, C2, D2, block[policy_type::r2[j]], 0x50a28be6,
74  policy_type::s2[j]);
75  }
76  // round 2
77 
78  for (int j = 16; j < 32; ++j) {
79  policy_type::transform<policy_type::f2>(A1, B1, C1, D1, block[policy_type::r1[j]], 0x5a827999,
80  policy_type::s1[j]);
81  policy_type::transform<policy_type::f3>(A2, B2, C2, D2, block[policy_type::r2[j]], 0x5c4dd124,
82  policy_type::s2[j]);
83  }
84  // round 3
85 
86  for (int j = 32; j < 48; ++j) {
87  policy_type::transform<policy_type::f3>(A1, B1, C1, D1, block[policy_type::r1[j]], 0x6ed9eba1,
88  policy_type::s1[j]);
89  policy_type::transform<policy_type::f2>(A2, B2, C2, D2, block[policy_type::r2[j]], 0x6d703ef3,
90  policy_type::s2[j]);
91  }
92  // round 4
93 
94  for (int j = 48; j < 64; ++j) {
95  policy_type::transform<policy_type::f4>(A1, B1, C1, D1, block[policy_type::r1[j]], 0x8f1bbcdc,
96  policy_type::s1[j]);
97  policy_type::transform<policy_type::f1>(A2, B2, C2, D2, block[policy_type::r2[j]], 0x00000000,
98  policy_type::s2[j]);
99  }
100 
101  word_type T = state[1] + C1 + D2;
102  state[1] = state[2] + D1 + A2;
103  state[2] = state[3] + A1 + B2;
104  state[3] = state[0] + B1 + C2;
105  state[0] = T;
106  }
107  };
108 
109  template<>
110  struct ripemd_compressor<160> : public basic_ripemd_compressor<160> {
111  static void process_block(state_type &state, const block_type &block) {
112  word_type A1 = state[0], B1 = state[1], C1 = state[2], D1 = state[3], E1 = state[4];
113  word_type A2 = state[0], B2 = state[1], C2 = state[2], D2 = state[3], E2 = state[4];
114 
115  // round 1
116 
117  for (int j = 0; j < 16; ++j) {
118  policy_type::transform<policy_type::f1>(A1, B1, C1, D1, E1, block[policy_type::r1[j]],
119  0x00000000, policy_type::s1[j]);
120  policy_type::transform<policy_type::f5>(A2, B2, C2, D2, E2, block[policy_type::r2[j]],
121  0x50a28be6, policy_type::s2[j]);
122  }
123  // round 2
124 
125  for (int j = 16; j < 32; ++j) {
126  policy_type::transform<policy_type::f2>(A1, B1, C1, D1, E1, block[policy_type::r1[j]],
127  0x5a827999, policy_type::s1[j]);
128  policy_type::transform<policy_type::f4>(A2, B2, C2, D2, E2, block[policy_type::r2[j]],
129  0x5c4dd124, policy_type::s2[j]);
130  }
131  // round 3
132 
133  for (int j = 32; j < 48; ++j) {
134  policy_type::transform<policy_type::f3>(A1, B1, C1, D1, E1, block[policy_type::r1[j]],
135  0x6ed9eba1, policy_type::s1[j]);
136  policy_type::transform<policy_type::f3>(A2, B2, C2, D2, E2, block[policy_type::r2[j]],
137  0x6d703ef3, policy_type::s2[j]);
138  }
139  // round 4
140 
141  for (int j = 48; j < 64; ++j) {
142  policy_type::transform<policy_type::f4>(A1, B1, C1, D1, E1, block[policy_type::r1[j]],
143  0x8f1bbcdc, policy_type::s1[j]);
144  policy_type::transform<policy_type::f2>(A2, B2, C2, D2, E2, block[policy_type::r2[j]],
145  0x7a6d76e9, policy_type::s2[j]);
146  }
147  // round 5
148 
149  for (int j = 64; j < 80; ++j) {
150  policy_type::transform<policy_type::f5>(A1, B1, C1, D1, E1, block[policy_type::r1[j]],
151  0xa953fd4e, policy_type::s1[j]);
152  policy_type::transform<policy_type::f1>(A2, B2, C2, D2, E2, block[policy_type::r2[j]],
153  0x00000000, policy_type::s2[j]);
154  }
155 
156  word_type T = state[1] + C1 + D2;
157  state[1] = state[2] + D1 + E2;
158  state[2] = state[3] + E1 + A2;
159  state[3] = state[4] + A1 + B2;
160  state[4] = state[0] + B1 + C2;
161  state[0] = T;
162  }
163  };
164 
165  template<>
166  struct ripemd_compressor<256> : public basic_ripemd_compressor<256> {
167  static void process_block(state_type &state, const block_type &block) {
168  state_type Y = {0};
169  std::copy(state.begin(), state.end(), Y.begin());
170 
171  // round 1
172 
173  for (int j = 0; j < 16; ++j) {
174  policy_type::transform<policy_type::f1>(Y[0], Y[1], Y[2], Y[3], block[policy_type::r1[j]],
175  0x00000000, policy_type::s1[j]);
176  policy_type::transform<policy_type::f4>(Y[4], Y[5], Y[6], Y[7], block[policy_type::r2[j]],
177  0x50a28be6, policy_type::s2[j]);
178  }
179  std::swap(Y[0], Y[4]);
180  // round 2
181 
182  for (int j = 16; j < 32; ++j) {
183  policy_type::transform<policy_type::f2>(Y[0], Y[1], Y[2], Y[3], block[policy_type::r1[j]],
184  0x5a827999, policy_type::s1[j]);
185  policy_type::transform<policy_type::f3>(Y[4], Y[5], Y[6], Y[7], block[policy_type::r2[j]],
186  0x5c4dd124, policy_type::s2[j]);
187  }
188  std::swap(Y[1], Y[5]);
189  // round 3
190 
191  for (int j = 32; j < 48; ++j) {
192  policy_type::transform<policy_type::f3>(Y[0], Y[1], Y[2], Y[3], block[policy_type::r1[j]],
193  0x6ed9eba1, policy_type::s1[j]);
194  policy_type::transform<policy_type::f2>(Y[4], Y[5], Y[6], Y[7], block[policy_type::r2[j]],
195  0x6d703ef3, policy_type::s2[j]);
196  }
197  std::swap(Y[2], Y[6]);
198  // round 4
199 
200  for (int j = 48; j < 64; ++j) {
201  policy_type::transform<policy_type::f4>(Y[0], Y[1], Y[2], Y[3], block[policy_type::r1[j]],
202  0x8f1bbcdc, policy_type::s1[j]);
203  policy_type::transform<policy_type::f1>(Y[4], Y[5], Y[6], Y[7], block[policy_type::r2[j]],
204  0x00000000, policy_type::s2[j]);
205  }
206  std::swap(Y[3], Y[7]);
207 
208 
209  for (std::size_t i = 0; i < policy_type::state_words; ++i) {
210  state[i] += Y[i];
211  }
212  }
213  };
214 
215  template<>
216  struct ripemd_compressor<320> : public basic_ripemd_compressor<320> {
217  static void process_block(state_type &state, const block_type &block) {
218  state_type Y = {0};
219  std::copy(state.begin(), state.end(), Y.begin());
220 
221  // round 1
222 
223  for (int j = 0; j < 16; ++j) {
224  policy_type::transform<policy_type::f1>(Y[0], Y[1], Y[2], Y[3], Y[4], block[policy_type::r1[j]],
225  0x00000000, policy_type::s1[j]);
226  policy_type::transform<policy_type::f5>(Y[5], Y[6], Y[7], Y[8], Y[9], block[policy_type::r2[j]],
227  0x50a28be6, policy_type::s2[j]);
228  }
229  std::swap(Y[1], Y[6]);
230  // round 2
231 
232  for (int j = 16; j < 32; ++j) {
233  policy_type::transform<policy_type::f2>(Y[0], Y[1], Y[2], Y[3], Y[4], block[policy_type::r1[j]],
234  0x5a827999, policy_type::s1[j]);
235  policy_type::transform<policy_type::f4>(Y[5], Y[6], Y[7], Y[8], Y[9], block[policy_type::r2[j]],
236  0x5c4dd124, policy_type::s2[j]);
237  }
238  std::swap(Y[3], Y[8]);
239  // round 3
240 
241  for (int j = 32; j < 48; ++j) {
242  policy_type::transform<policy_type::f3>(Y[0], Y[1], Y[2], Y[3], Y[4], block[policy_type::r1[j]],
243  0x6ed9eba1, policy_type::s1[j]);
244  policy_type::transform<policy_type::f3>(Y[5], Y[6], Y[7], Y[8], Y[9], block[policy_type::r2[j]],
245  0x6d703ef3, policy_type::s2[j]);
246  }
247  std::swap(Y[0], Y[5]);
248  // round 4
249 
250  for (int j = 48; j < 64; ++j) {
251  policy_type::transform<policy_type::f4>(Y[0], Y[1], Y[2], Y[3], Y[4], block[policy_type::r1[j]],
252  0x8f1bbcdc, policy_type::s1[j]);
253  policy_type::transform<policy_type::f2>(Y[5], Y[6], Y[7], Y[8], Y[9], block[policy_type::r2[j]],
254  0x7a6d76e9, policy_type::s2[j]);
255  }
256  std::swap(Y[2], Y[7]);
257  // round 5
258 
259  for (int j = 64; j < 80; ++j) {
260  policy_type::transform<policy_type::f5>(Y[0], Y[1], Y[2], Y[3], Y[4], block[policy_type::r1[j]],
261  0xa953fd4e, policy_type::s1[j]);
262  policy_type::transform<policy_type::f1>(Y[5], Y[6], Y[7], Y[8], Y[9], block[policy_type::r2[j]],
263  0x00000000, policy_type::s2[j]);
264  }
265  std::swap(Y[4], Y[9]);
266 
267 
268  for (std::size_t i = 0; i < policy_type::state_words; ++i) {
269  state[i] += Y[i];
270  }
271  }
272  };
273 
281  template<std::size_t DigestBits>
282  class ripemd {
284 
285  public:
286  constexpr static const std::size_t word_bits = policy_type::word_bits;
288 
289  constexpr static const std::size_t block_bits = policy_type::block_bits;
290  constexpr static const std::size_t block_words = policy_type::block_words;
292 
293  constexpr static const std::size_t digest_bits = DigestBits;
295 
296  constexpr static const std::size_t pkcs_id_size = policy_type::pkcs_id_size;
297  constexpr static const std::size_t pkcs_id_bits = policy_type::pkcs_id_bits;
299 
300  constexpr static const pkcs_id_type pkcs_id = policy_type::pkcs_id;
301  constexpr static const std::uint8_t ieee1363_hash_id = policy_type::ieee1363_hash_id;
302 
303  struct construction {
304  struct params_type {
306 
307  constexpr static const std::size_t length_bits = policy_type::length_bits;
308  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
309  };
310 
311  typedef merkle_damgard_construction<params_type, typename policy_type::iv_generator,
315  };
316 
317  template<typename StateAccumulator, std::size_t ValueBits>
319  struct params_type {
321 
322  constexpr static const std::size_t value_bits = ValueBits;
323  };
324 
326  };
327  };
328 
333  } // namespace hashes
334  } // namespace crypto3
335 } // namespace nil
336 
337 #endif
This will do the usual Merkle-Damgård-style strengthening, padding with a 1 bit, then 0 bits as neede...
Definition: hash/include/nil/crypto3/hash/detail/block_stream_processor.hpp:55
Definition: merkle_damgard_padding.hpp:39
Definition: merkle_damgard_construction.hpp:55
Ripemd. Family of configurable hashes, developed as an open alternative to SHA.
Definition: ripemd.hpp:282
constexpr static const pkcs_id_type pkcs_id
Definition: ripemd.hpp:300
policy_type::pkcs_id_type pkcs_id_type
Definition: ripemd.hpp:298
constexpr static const std::uint8_t ieee1363_hash_id
Definition: ripemd.hpp:301
constexpr static const std::size_t pkcs_id_size
Definition: ripemd.hpp:296
constexpr static const std::size_t pkcs_id_bits
Definition: ripemd.hpp:297
policy_type::block_type block_type
Definition: ripemd.hpp:291
constexpr static const std::size_t digest_bits
Definition: ripemd.hpp:293
constexpr static const std::size_t word_bits
Definition: ripemd.hpp:286
policy_type::word_type word_type
Definition: ripemd.hpp:287
policy_type::digest_type digest_type
Definition: ripemd.hpp:294
constexpr static const std::size_t block_words
Definition: ripemd.hpp:290
constexpr static const std::size_t block_bits
Definition: ripemd.hpp:289
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
ripemd< 160 > ripemd160
Definition: ripemd.hpp:330
ripemd< 256 > ripemd256
Definition: ripemd.hpp:331
ripemd< 320 > ripemd320
Definition: ripemd.hpp:332
ripemd< 128 > ripemd128
Definition: ripemd.hpp:329
Definition: pair.hpp:31
constexpr static const std::size_t word_bits
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:88
boost::uint_t< word_bits >::exact word_type
Definition: block/include/nil/crypto3/detail/basic_functions.hpp:89
constexpr static const std::size_t word_bits
Definition: ripemd.hpp:43
constexpr static const std::size_t block_words
Definition: ripemd.hpp:51
policy_type::word_type word_type
Definition: ripemd.hpp:44
constexpr static const std::size_t state_words
Definition: ripemd.hpp:47
policy_type::state_type state_type
Definition: ripemd.hpp:48
constexpr static const std::size_t state_bits
Definition: ripemd.hpp:46
constexpr static const std::size_t block_bits
Definition: ripemd.hpp:50
detail::ripemd_functions< DigestBits > policy_type
Definition: ripemd.hpp:41
policy_type::block_type block_type
Definition: ripemd.hpp:52
constexpr static const key_indexes_type r2
Definition: ripemd_policy.hpp:71
std::array< word_type, block_words > block_type
Definition: ripemd_policy.hpp:43
constexpr static const std::size_t block_words
Definition: ripemd_policy.hpp:42
constexpr static const std::size_t pkcs_id_bits
Definition: ripemd_policy.hpp:55
constexpr static const key_indexes_type s1
Definition: ripemd_policy.hpp:78
constexpr static const std::size_t length_bits
Definition: ripemd_policy.hpp:45
std::array< std::uint8_t, pkcs_id_size > pkcs_id_type
Definition: ripemd_policy.hpp:56
constexpr static const key_indexes_type s2
Definition: ripemd_policy.hpp:84
constexpr static const std::uint8_t ieee1363_hash_id
Definition: ripemd_policy.hpp:52
constexpr static const std::size_t digest_bits
Definition: ripemd_policy.hpp:49
constexpr static const key_indexes_type r1
Definition: ripemd_policy.hpp:65
constexpr static const std::size_t pkcs_id_size
Definition: ripemd_policy.hpp:54
constexpr static const pkcs_id_type pkcs_id
Definition: ripemd_policy.hpp:58
constexpr static const std::size_t block_bits
Definition: ripemd_policy.hpp:41
Definition: ripemd_functions.hpp:36
policy_type::word_type word_type
Definition: ripemd_functions.hpp:39
Definition: ripemd_policy.hpp:112
constexpr static const std::size_t digest_bits
Definition: ripemd.hpp:308
constexpr static const std::size_t length_bits
Definition: ripemd.hpp:307
policy_type::digest_endian digest_endian
Definition: ripemd.hpp:305
merkle_damgard_construction< params_type, typename policy_type::iv_generator, ripemd_compressor< DigestBits >, detail::merkle_damgard_padding< policy_type > > type
Definition: ripemd.hpp:314
constexpr static const std::size_t value_bits
Definition: ripemd.hpp:322
policy_type::digest_endian digest_endian
Definition: ripemd.hpp:320
block_stream_processor< construction, StateAccumulator, params_type > type
Definition: ripemd.hpp:325
static void process_block(state_type &state, const block_type &block)
Definition: ripemd.hpp:60
static void process_block(state_type &state, const block_type &block)
Definition: ripemd.hpp:111
static void process_block(state_type &state, const block_type &block)
Definition: ripemd.hpp:167
static void process_block(state_type &state, const block_type &block)
Definition: ripemd.hpp:217
Definition: algebra/include/nil/crypto3/detail/stream_endian.hpp:45