sha2_policy.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_DETAIL_SHA2_POLICY_HPP
27 #define CRYPTO3_HASH_DETAIL_SHA2_POLICY_HPP
28 
30 
31 #include <nil/crypto3/detail/static_digest.hpp>
32 
33 #include <array>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace hashes {
38  namespace detail {
39 
40  template<std::size_t CipherVersion>
42 
43  constexpr static const std::size_t cipher_version = CipherVersion;
45 
46  constexpr static const std::size_t word_bits = block_cipher_type::word_bits;
48 
49  constexpr static const std::size_t state_bits = block_cipher_type::block_bits;
50  constexpr static const std::size_t state_words = block_cipher_type::block_words;
52 
53  constexpr static const std::size_t block_bits = block_cipher_type::key_bits;
54  constexpr static const std::size_t block_words = block_cipher_type::key_words;
56 
57  constexpr static const std::size_t length_bits = word_bits * 2;
58 
60  };
61 
62  template<std::size_t Version>
63  struct sha2_policy;
64 
65  template<>
66  struct sha2_policy<224> : public basic_sha2_policy<256> {
67 
68  constexpr static const std::size_t digest_bits = 224;
69  constexpr static const std::uint8_t ieee1363_hash_id = 0x38;
70 
71  constexpr static const std::size_t pkcs_id_size = 0;
72  constexpr static const std::size_t pkcs_id_bits = pkcs_id_size * CHAR_BIT;
73  typedef std::array<std::uint8_t, pkcs_id_size> pkcs_id_type;
74 
75  constexpr static const pkcs_id_type pkcs_id = {};
76 
78 
79  struct iv_generator {
80  state_type const &operator()() const {
81  static state_type const H0 = {{0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31,
82  0x68581511, 0x64f98fa7, 0xbefa4fa4}};
83  return H0;
84  }
85  };
86  };
87 
88  template<>
89  struct sha2_policy<256> : public basic_sha2_policy<256> {
90 
91  constexpr static const std::size_t digest_bits = 256;
92  constexpr static const std::uint8_t ieee1363_hash_id = 0x34;
93 
95 
96  constexpr static const std::size_t pkcs_id_size = 19;
97  constexpr static const std::size_t pkcs_id_bits = pkcs_id_size * CHAR_BIT;
98  typedef std::array<std::uint8_t, pkcs_id_size> pkcs_id_type;
99 
100  constexpr static const pkcs_id_type pkcs_id = {0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60,
101  0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
102  0x04, 0x05, 0x00, 0x04, 0x20};
103 
104  struct iv_generator {
105  state_type const &operator()() const {
106  static state_type const H0 = {{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f,
107  0x9b05688c, 0x1f83d9ab, 0x5be0cd19}};
108  return H0;
109  }
110  };
111  };
112 
113  template<>
114  struct sha2_policy<384> : public basic_sha2_policy<512> {
115 
116  constexpr static const std::size_t digest_bits = 384;
117  constexpr static const std::uint8_t ieee1363_hash_id = 0x36;
118 
119  constexpr static const std::size_t pkcs_id_size = 0;
120  constexpr static const std::size_t pkcs_id_bits = pkcs_id_size * CHAR_BIT;
121  typedef std::array<std::uint8_t, pkcs_id_size> pkcs_id_type;
122 
123  constexpr static const pkcs_id_type pkcs_id = {};
124 
126 
127  struct iv_generator {
128  state_type const &operator()() const {
129  static state_type const H0 = {{UINT64_C(0xcbbb9d5dc1059ed8), UINT64_C(0x629a292a367cd507),
130  UINT64_C(0x9159015a3070dd17), UINT64_C(0x152fecd8f70e5939),
131  UINT64_C(0x67332667ffc00b31), UINT64_C(0x8eb44a8768581511),
132  UINT64_C(0xdb0c2e0d64f98fa7), UINT64_C(0x47b5481dbefa4fa4)}};
133  return H0;
134  }
135  };
136  };
137 
138  template<>
139  struct sha2_policy<512> : public basic_sha2_policy<512> {
140 
141  constexpr static const std::size_t digest_bits = 512;
142  constexpr static const std::uint8_t ieee1363_hash_id = 0x35;
143 
145  constexpr static const std::size_t pkcs_id_size = 19;
146  constexpr static const std::size_t pkcs_id_bits = pkcs_id_size * CHAR_BIT;
147  typedef std::array<std::uint8_t, pkcs_id_size> pkcs_id_type;
148 
149  constexpr static const pkcs_id_type pkcs_id = {0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60,
150  0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
151  0x04, 0x05, 0x00, 0x04, 0x40};
152 
153  struct iv_generator {
154  state_type const &operator()() const {
155  static state_type const H0 = {{UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
156  UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
157  UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
158  UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179)}};
159  return H0;
160  }
161  };
162  };
163 
165 
167 
169 
171 
172  } // namespace detail
173  } // namespace hashes
174  } // namespace crypto3
175 } // namespace nil
176 
177 #endif // CRYPTO3_HASH_DETAIL_SHA2_POLICY_HPP
Shacal2. Merkle-Damgård construction foundation for SHA2 hashes. Accepts up to a 512-bit key....
Definition: shacal2.hpp:66
constexpr static const std::size_t key_words
Definition: shacal2.hpp:77
policy_type::key_type key_type
Definition: shacal2.hpp:78
policy_type::word_type word_type
Definition: shacal2.hpp:74
constexpr static const std::size_t block_bits
Definition: shacal2.hpp:80
constexpr static const std::size_t key_bits
Definition: shacal2.hpp:76
constexpr static const std::size_t word_bits
Definition: shacal2.hpp:73
policy_type::block_type block_type
Definition: shacal2.hpp:82
constexpr static const std::size_t block_words
Definition: shacal2.hpp:81
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
Definition: pair.hpp:31
block_cipher_type::block_type state_type
Definition: sha2_policy.hpp:51
constexpr static const std::size_t word_bits
Definition: sha2_policy.hpp:46
stream_endian::big_octet_big_bit digest_endian
Definition: sha2_policy.hpp:59
constexpr static const std::size_t block_bits
Definition: sha2_policy.hpp:53
constexpr static const std::size_t block_words
Definition: sha2_policy.hpp:54
constexpr static const std::size_t length_bits
Definition: sha2_policy.hpp:57
constexpr static const std::size_t state_bits
Definition: sha2_policy.hpp:49
constexpr static const std::size_t state_words
Definition: sha2_policy.hpp:50
block_cipher_type::key_type block_type
Definition: sha2_policy.hpp:55
block::shacal2< cipher_version > block_cipher_type
Definition: sha2_policy.hpp:44
constexpr static const std::size_t cipher_version
Definition: sha2_policy.hpp:43
block_cipher_type::word_type word_type
Definition: sha2_policy.hpp:47
state_type const & operator()() const
Definition: sha2_policy.hpp:80
std::array< std::uint8_t, pkcs_id_size > pkcs_id_type
Definition: sha2_policy.hpp:73
constexpr static const pkcs_id_type pkcs_id
Definition: sha2_policy.hpp:75
static_digest< digest_bits > digest_type
Definition: sha2_policy.hpp:77
state_type const & operator()() const
Definition: sha2_policy.hpp:105
constexpr static const pkcs_id_type pkcs_id
Definition: sha2_policy.hpp:100
std::array< std::uint8_t, pkcs_id_size > pkcs_id_type
Definition: sha2_policy.hpp:98
static_digest< digest_bits > digest_type
Definition: sha2_policy.hpp:94
state_type const & operator()() const
Definition: sha2_policy.hpp:128
constexpr static const pkcs_id_type pkcs_id
Definition: sha2_policy.hpp:123
std::array< std::uint8_t, pkcs_id_size > pkcs_id_type
Definition: sha2_policy.hpp:121
static_digest< digest_bits > digest_type
Definition: sha2_policy.hpp:125
state_type const & operator()() const
Definition: sha2_policy.hpp:154
constexpr static const pkcs_id_type pkcs_id
Definition: sha2_policy.hpp:149
static_digest< digest_bits > digest_type
Definition: sha2_policy.hpp:144
std::array< std::uint8_t, pkcs_id_size > pkcs_id_type
Definition: sha2_policy.hpp:147
Definition: sha2_policy.hpp:63
Definition: algebra/include/nil/crypto3/detail/stream_endian.hpp:39