|
15 | 15 | namespace math { namespace checksums { namespace hash { |
16 | 16 |
|
17 | 17 | template<typename CountType, |
18 | | - const std::uint16_t MessageBufferSize> |
| 18 | + const std::uint16_t ResultBitCount, |
| 19 | + const std::uint16_t MessageBufferSize, |
| 20 | + const std::uint16_t MessageLengthTotalBitCount> |
19 | 21 | class hash_base |
20 | 22 | { |
21 | 23 | public: |
22 | 24 | using count_type = CountType; |
23 | 25 |
|
24 | | - static_assert( ( std::numeric_limits<count_type>::is_specialized == true) |
25 | | - && ( std::numeric_limits<count_type>::is_integer == true) |
26 | | - && ( std::numeric_limits<count_type>::is_signed == false) |
27 | | - && ( std::numeric_limits<count_type>::radix == 2) |
28 | | - && ((std::numeric_limits<count_type>::digits % 8) == 0), |
29 | | - "Error: The count type must be an unsigned integer with radix 2, having a multiple of 8 bits"); |
| 26 | + using result_type = std::array<std::uint8_t, static_cast<std::size_t>(ResultBitCount / static_cast<std::uint16_t>(UINT8_C(8)))>; |
| 27 | + |
| 28 | + static_assert |
| 29 | + ( |
| 30 | + std::numeric_limits<count_type>::is_specialized |
| 31 | + && std::numeric_limits<count_type>::is_integer |
| 32 | + && (!std::numeric_limits<count_type>::is_signed) |
| 33 | + && (std::numeric_limits<count_type>::radix == static_cast<int>(INT8_C(2))) |
| 34 | + && (std::numeric_limits<count_type>::digits >= static_cast<int>(INT8_C(16))) |
| 35 | + && (static_cast<int>(std::numeric_limits<count_type>::digits % static_cast<int>(INT8_C(8))) == static_cast<int>(INT8_C(0))), |
| 36 | + "Error: The count type must be an unsigned integer with radix 2, that is 16 bits or wider, and having a multiple of 8 bits" |
| 37 | + ); |
30 | 38 |
|
31 | 39 | virtual ~hash_base() = default; |
32 | 40 |
|
33 | 41 | virtual auto initialize() -> void |
34 | 42 | { |
35 | 43 | message_index = static_cast<std::uint_least16_t>(UINT8_C(0)); |
36 | 44 | message_length_total = static_cast<count_type>(UINT8_C(0)); |
| 45 | + |
| 46 | + message_buffer.fill(static_cast<std::uint8_t>(UINT8_C(0))); |
37 | 47 | } |
38 | 48 |
|
39 | 49 | auto process(const std::uint8_t* message, const count_type count) -> void |
|
47 | 57 | message_length_total = static_cast<count_type> (message_length_total + process_chunk_size); |
48 | 58 | process_index = static_cast<count_type> (process_index + process_chunk_size); |
49 | 59 |
|
50 | | - if(message_index == message_buffer_static_size) |
| 60 | + if(message_index == message_buffer_static_size()) |
51 | 61 | { |
52 | | - this->perform_algorithm(); |
| 62 | + my_perform_algorithm(); |
53 | 63 | } |
54 | 64 |
|
55 | 65 | process_chunk_size = (std::min)(static_cast<count_type>(count - process_index), |
56 | | - static_cast<count_type>(message_buffer_static_size - message_index)); |
| 66 | + static_cast<count_type>(message_buffer_static_size() - message_index)); |
57 | 67 |
|
58 | 68 | std::copy(message + static_cast<std::size_t>(process_index), |
59 | 69 | message + static_cast<std::size_t>(process_index + process_chunk_size), |
|
64 | 74 |
|
65 | 75 | auto finalize() -> void |
66 | 76 | { |
67 | | - message_block_type the_last_message_block; |
68 | | - |
69 | | - std::copy(message_buffer.cbegin(), |
70 | | - message_buffer.cbegin() + message_index, |
71 | | - the_last_message_block.begin()); |
72 | | - |
73 | 77 | // Create the padding. Begin by setting the leading padding byte to 0x80. |
74 | | - the_last_message_block[message_index] = static_cast<std::uint8_t>(UINT8_C(0x80)); |
| 78 | + message_buffer[message_index] = static_cast<std::uint8_t>(UINT8_C(0x80)); |
75 | 79 |
|
76 | 80 | ++message_index; |
77 | 81 |
|
78 | | - // Fill the rest of the padding bytes with zero. |
79 | | - std::fill(the_last_message_block.begin() + message_index, |
80 | | - the_last_message_block.end(), |
81 | | - static_cast<std::uint8_t>(UINT8_C(0))); |
| 82 | + // We need an extra block because there are not enough bytes |
| 83 | + // available for storing the total bit count. So we must |
| 84 | + // transform the current block, then create and pad yet another |
| 85 | + // additional block. |
82 | 86 |
|
83 | | - // Do we need an extra block? If so, then transform the |
84 | | - // current block and pad an additional block. |
85 | | - if(message_index > static_cast<std::uint16_t>(message_buffer_static_size - 8U)) |
| 87 | + if(static_cast<std::uint16_t>(message_index + static_cast<std::uint_least16_t>(message_length_total_width())) > message_buffer_static_size()) |
86 | 88 | { |
87 | | - message_buffer = the_last_message_block; |
88 | | - |
89 | | - perform_algorithm(); |
90 | | - |
91 | | - the_last_message_block.fill(static_cast<std::uint8_t>(UINT8_C(0))); |
| 89 | + my_perform_algorithm(); |
92 | 90 | } |
93 | 91 |
|
94 | | - // Encode the number of bits. Simultaneously convert the number of bytes |
95 | | - // to the number of bits by performing a left-shift of 3 on the byte-array. |
96 | | - // The hash_sha1 stores the 8 bytes of the bit counter in reverse order, |
97 | | - // with the lowest byte being stored at the highest position of the buffer |
98 | | - auto carry = static_cast<std::uint8_t>(UINT8_C(0)); |
99 | | - |
100 | | - auto local_message_length_total = static_cast<std::uint64_t>(message_length_total); |
| 92 | + // Encode the total number of bits in the final transform buffer. |
| 93 | + // Here, we can easily see the conversion of the number of bytes |
| 94 | + // to the number of bits. This is done by performing a left-shift |
| 95 | + // of 3 on the variable "message_length_total". |
101 | 96 |
|
102 | | - std::for_each(the_last_message_block.rbegin(), |
103 | | - the_last_message_block.rbegin() + static_cast<std::size_t>(UINT8_C(8)), |
104 | | - [&carry, &local_message_length_total](std::uint8_t& the_byte) |
105 | | - { |
106 | | - const std::uint_least16_t the_word = |
107 | | - static_cast<std::uint_least16_t>(local_message_length_total << static_cast<unsigned>(UINT8_C(3))); |
| 97 | + auto carry = static_cast<std::uint_fast8_t>(UINT8_C(0)); |
108 | 98 |
|
109 | | - the_byte = static_cast<std::uint8_t>(the_word | carry); |
110 | | - |
111 | | - local_message_length_total >>= static_cast<unsigned>(UINT8_C(8)); |
112 | | - |
113 | | - carry = |
114 | | - static_cast<std::uint8_t> |
115 | | - ( |
116 | | - static_cast<std::uint8_t>(the_word >> static_cast<unsigned>(UINT8_C(8))) |
117 | | - & static_cast<std::uint8_t>(UINT8_C(0x07)) |
118 | | - ); |
119 | | - }); |
120 | | - |
121 | | - message_length_total = static_cast<count_type>(local_message_length_total); |
| 99 | + for(auto ri = message_buffer.rbegin(); |
| 100 | + ri != message_buffer.rbegin() + message_length_total_width(); |
| 101 | + ++ri) |
| 102 | + { |
| 103 | + const std::uint_least16_t the_word = |
| 104 | + static_cast<std::uint_least16_t> |
| 105 | + ( |
| 106 | + static_cast<std::uint_least16_t>(message_length_total) << static_cast<unsigned>(UINT8_C(3)) |
| 107 | + ); |
| 108 | + |
| 109 | + *ri = static_cast<std::uint8_t>(the_word | carry); |
| 110 | + |
| 111 | + message_length_total = |
| 112 | + static_cast<count_type> |
| 113 | + ( |
| 114 | + message_length_total >> static_cast<unsigned>(UINT8_C(8)) |
| 115 | + ); |
| 116 | + |
| 117 | + carry = |
| 118 | + static_cast<std::uint_fast8_t> |
| 119 | + ( |
| 120 | + static_cast<std::uint_fast8_t>(the_word >> static_cast<unsigned>(UINT8_C(8))) |
| 121 | + & static_cast<std::uint_fast8_t>(UINT8_C(0x07)) |
| 122 | + ); |
| 123 | + } |
122 | 124 |
|
123 | | - message_buffer = the_last_message_block; |
| 125 | + my_perform_algorithm(); |
| 126 | + } |
124 | 127 |
|
125 | | - this->perform_algorithm(); |
| 128 | + auto get_result(typename result_type::pointer result) -> void |
| 129 | + { |
| 130 | + // Extract the hash result from the message digest state. |
| 131 | + detail::convert_uint32_input_to_uint8_output_reverse |
| 132 | + ( |
| 133 | + transform_context.data(), |
| 134 | + transform_context.data() + static_cast<std::size_t>(std::tuple_size<result_type>::value / sizeof(std::uint32_t)), |
| 135 | + result |
| 136 | + ); |
126 | 137 | } |
127 | 138 |
|
128 | 139 | auto hash(const std::uint8_t* message, const count_type count) -> void |
|
133 | 144 | } |
134 | 145 |
|
135 | 146 | protected: |
136 | | - static constexpr auto message_buffer_static_size = MessageBufferSize; |
| 147 | + using message_block_type = std::array<std::uint8_t, static_cast<std::size_t>(MessageBufferSize)>; |
| 148 | + |
| 149 | + using context_type = std::array<std::uint32_t, static_cast<std::size_t>(std::tuple_size<result_type>::value / sizeof(std::uint32_t))>; |
137 | 150 |
|
138 | | - using message_block_type = std::array<std::uint8_t, static_cast<std::size_t>(message_buffer_static_size)>; |
| 151 | + static constexpr auto message_buffer_static_size() -> std::uint16_t |
| 152 | + { |
| 153 | + return static_cast<std::uint16_t>(std::tuple_size<message_block_type>::value); |
| 154 | + } |
139 | 155 |
|
140 | 156 | std::uint_least16_t message_index { }; |
141 | 157 | count_type message_length_total { }; |
142 | 158 | message_block_type message_buffer { }; |
| 159 | + context_type transform_context { }; |
143 | 160 |
|
144 | 161 | hash_base() = default; |
145 | 162 |
|
|
150 | 167 | auto operator=(hash_base&& other) noexcept -> hash_base& = default; |
151 | 168 |
|
152 | 169 | private: |
| 170 | + static constexpr auto message_length_total_width() noexcept -> std::uint16_t |
| 171 | + { |
| 172 | + return |
| 173 | + static_cast<std::uint16_t> |
| 174 | + ( |
| 175 | + MessageLengthTotalBitCount / static_cast<std::uint16_t>(UINT8_C(8)) |
| 176 | + ); |
| 177 | + } |
| 178 | + |
153 | 179 | virtual auto perform_algorithm() -> void = 0; |
| 180 | + |
| 181 | + auto my_perform_algorithm() -> void |
| 182 | + { |
| 183 | + this->perform_algorithm(); |
| 184 | + |
| 185 | + message_index = static_cast<std::uint_least16_t>(UINT8_C(0)); |
| 186 | + |
| 187 | + message_buffer.fill(static_cast<std::uint8_t>(UINT8_C(0))); |
| 188 | + } |
154 | 189 | }; |
155 | 190 |
|
156 | 191 | } } } // namespace math::checksums::hash |
|
0 commit comments