Botan 3.4.0
Crypto and TLS for C&
sha2_32.cpp
Go to the documentation of this file.
1/*
2* SHA-{224,256}
3* (C) 1999-2010,2017 Jack Lloyd
4* 2007 FlexSecure GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/sha2_32.h>
10
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/cpuid.h>
13#include <botan/internal/loadstor.h>
14#include <botan/internal/rotate.h>
15#include <botan/internal/sha2_32_f.h>
16#include <botan/internal/stl_util.h>
17
18namespace Botan {
19
20namespace {
21
22std::string sha256_provider() {
23#if defined(BOTAN_HAS_SHA2_32_X86)
24 if(CPUID::has_intel_sha()) {
25 return "shani";
26 }
27#endif
28
29#if defined(BOTAN_HAS_SHA2_32_X86_BMI2)
30 if(CPUID::has_bmi2()) {
31 return "bmi2";
32 }
33#endif
34
35#if defined(BOTAN_HAS_SHA2_32_ARMV8)
36 if(CPUID::has_arm_sha2()) {
37 return "armv8";
38 }
39#endif
40
41 return "base";
42}
43
44} // namespace
45
46/*
47* SHA-224 / SHA-256 compression function
48*/
49void SHA_256::compress_digest(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
50#if defined(BOTAN_HAS_SHA2_32_X86)
51 if(CPUID::has_intel_sha()) {
52 return SHA_256::compress_digest_x86(digest, input, blocks);
53 }
54#endif
55
56#if defined(BOTAN_HAS_SHA2_32_X86_BMI2)
57 if(CPUID::has_bmi2()) {
58 return SHA_256::compress_digest_x86_bmi2(digest, input, blocks);
59 }
60#endif
61
62#if defined(BOTAN_HAS_SHA2_32_ARMV8)
63 if(CPUID::has_arm_sha2()) {
64 return SHA_256::compress_digest_armv8(digest, input, blocks);
65 }
66#endif
67
68 uint32_t A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4], F = digest[5], G = digest[6],
69 H = digest[7];
70
71 BufferSlicer in(input);
72
73 for(size_t i = 0; i != blocks; ++i) {
74 const auto block = in.take(block_bytes);
75
76 uint32_t W00 = load_be<uint32_t>(block.data(), 0);
77 uint32_t W01 = load_be<uint32_t>(block.data(), 1);
78 uint32_t W02 = load_be<uint32_t>(block.data(), 2);
79 uint32_t W03 = load_be<uint32_t>(block.data(), 3);
80 uint32_t W04 = load_be<uint32_t>(block.data(), 4);
81 uint32_t W05 = load_be<uint32_t>(block.data(), 5);
82 uint32_t W06 = load_be<uint32_t>(block.data(), 6);
83 uint32_t W07 = load_be<uint32_t>(block.data(), 7);
84 uint32_t W08 = load_be<uint32_t>(block.data(), 8);
85 uint32_t W09 = load_be<uint32_t>(block.data(), 9);
86 uint32_t W10 = load_be<uint32_t>(block.data(), 10);
87 uint32_t W11 = load_be<uint32_t>(block.data(), 11);
88 uint32_t W12 = load_be<uint32_t>(block.data(), 12);
89 uint32_t W13 = load_be<uint32_t>(block.data(), 13);
90 uint32_t W14 = load_be<uint32_t>(block.data(), 14);
91 uint32_t W15 = load_be<uint32_t>(block.data(), 15);
92
93 SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
94 SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
95 SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
96 SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
97 SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
98 SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
99 SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
100 SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
101 SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
102 SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
103 SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
104 SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
105 SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
106 SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
107 SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
108 SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
109
110 SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
111 SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
112 SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
113 SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
114 SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
115 SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
116 SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
117 SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
118 SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
119 SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
120 SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
121 SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
122 SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
123 SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
124 SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
125 SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
126
127 SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
128 SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
129 SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
130 SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
131 SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
132 SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
133 SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
134 SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
135 SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
136 SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
137 SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
138 SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
139 SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
140 SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
141 SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
142 SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
143
144 SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
145 SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
146 SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
147 SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
148 SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
149 SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
150 SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
151 SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
152 SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
153 SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
154 SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
155 SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
156 SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
157 SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
158 SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
159 SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
160
161 A = (digest[0] += A);
162 B = (digest[1] += B);
163 C = (digest[2] += C);
164 D = (digest[3] += D);
165 E = (digest[4] += E);
166 F = (digest[5] += F);
167 G = (digest[6] += G);
168 H = (digest[7] += H);
169 }
170}
171
172std::string SHA_224::provider() const {
173 return sha256_provider();
174}
175
176void SHA_224::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
177 SHA_256::compress_digest(digest, input, blocks);
178}
179
181 digest.assign({0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939, 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4});
182}
183
184std::unique_ptr<HashFunction> SHA_224::new_object() const {
185 return std::make_unique<SHA_224>();
186}
187
188std::unique_ptr<HashFunction> SHA_224::copy_state() const {
189 return std::make_unique<SHA_224>(*this);
190}
191
192void SHA_224::add_data(std::span<const uint8_t> input) {
193 m_md.update(input);
194}
195
196void SHA_224::final_result(std::span<uint8_t> output) {
197 m_md.final(output);
198}
199
200std::string SHA_256::provider() const {
201 return sha256_provider();
202}
203
204void SHA_256::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
205 SHA_256::compress_digest(digest, input, blocks);
206}
207
209 digest.assign({0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19});
210}
211
212std::unique_ptr<HashFunction> SHA_256::new_object() const {
213 return std::make_unique<SHA_256>();
214}
215
216std::unique_ptr<HashFunction> SHA_256::copy_state() const {
217 return std::make_unique<SHA_256>(*this);
218}
219
220void SHA_256::add_data(std::span<const uint8_t> input) {
221 m_md.update(input);
222}
223
224void SHA_256::final_result(std::span<uint8_t> output) {
225 m_md.final(output);
226}
227
228} // namespace Botan
std::span< const uint8_t > take(const size_t count)
Definition stl_util.h:156
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_32.cpp:176
std::string provider() const override
Definition sha2_32.cpp:172
static void init(digest_type &digest)
Definition sha2_32.cpp:180
secure_vector< uint32_t > digest_type
Definition sha2_32.h:21
std::unique_ptr< HashFunction > new_object() const override
Definition sha2_32.cpp:184
std::unique_ptr< HashFunction > copy_state() const override
Definition sha2_32.cpp:188
static void compress_digest_x86(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
secure_vector< uint32_t > digest_type
Definition sha2_32.h:61
static void compress_digest_armv8(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_32.cpp:204
std::unique_ptr< HashFunction > new_object() const override
Definition sha2_32.cpp:212
std::unique_ptr< HashFunction > copy_state() const override
Definition sha2_32.cpp:216
std::string provider() const override
Definition sha2_32.cpp:200
static void compress_digest_x86_bmi2(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void init(digest_type &digest)
Definition sha2_32.cpp:208
static constexpr size_t block_bytes
Definition sha2_32.h:65
static void compress_digest(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_32.cpp:49
BOTAN_FORCE_INLINE void SHA2_32_F(uint32_t A, uint32_t B, uint32_t C, uint32_t &D, uint32_t E, uint32_t F, uint32_t G, uint32_t &H, uint32_t &M1, uint32_t M2, uint32_t M3, uint32_t M4, uint32_t magic)
Definition sha2_32_f.h:19