Botan  2.10.0
Crypto and TLS for C++11
sha2_64.cpp
Go to the documentation of this file.
1 /*
2 * SHA-{384,512}
3 * (C) 1999-2011,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/sha2_64.h>
9 #include <botan/rotate.h>
10 #include <botan/cpuid.h>
11 
12 namespace Botan {
13 
14 namespace {
15 
16 std::string sha512_provider()
17  {
18 #if defined(BOTAN_HAS_SHA2_64_BMI2)
19  if(CPUID::has_bmi2())
20  {
21  return "bmi2";
22  }
23 #endif
24 
25  return "base";
26  }
27 
28 }
29 
30 std::unique_ptr<HashFunction> SHA_384::copy_state() const
31  {
32  return std::unique_ptr<HashFunction>(new SHA_384(*this));
33  }
34 
35 std::unique_ptr<HashFunction> SHA_512::copy_state() const
36  {
37  return std::unique_ptr<HashFunction>(new SHA_512(*this));
38  }
39 
40 std::unique_ptr<HashFunction> SHA_512_256::copy_state() const
41  {
42  return std::unique_ptr<HashFunction>(new SHA_512_256(*this));
43  }
44 
45 /*
46 * SHA-512 F1 Function
47 *
48 * Use a macro as many compilers won't inline a function this big,
49 * even though it is much faster if inlined.
50 */
51 #define SHA2_64_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic) \
52  do { \
53  const uint64_t E_rho = rotr<14>(E) ^ rotr<18>(E) ^ rotr<41>(E); \
54  const uint64_t A_rho = rotr<28>(A) ^ rotr<34>(A) ^ rotr<39>(A); \
55  const uint64_t M2_sigma = rotr<19>(M2) ^ rotr<61>(M2) ^ (M2 >> 6); \
56  const uint64_t M4_sigma = rotr<1>(M4) ^ rotr<8>(M4) ^ (M4 >> 7); \
57  H += magic + E_rho + ((E & F) ^ (~E & G)) + M1; \
58  D += H; \
59  H += A_rho + ((A & B) | ((A | B) & C)); \
60  M1 += M2_sigma + M3 + M4_sigma; \
61  } while(0);
62 
63 /*
64 * SHA-{384,512} Compression Function
65 */
66 //static
68  const uint8_t input[], size_t blocks)
69  {
70 #if defined(BOTAN_HAS_SHA2_64_BMI2)
71  if(CPUID::has_bmi2())
72  {
73  return compress_digest_bmi2(digest, input, blocks);
74  }
75 #endif
76 
77  uint64_t A = digest[0], B = digest[1], C = digest[2],
78  D = digest[3], E = digest[4], F = digest[5],
79  G = digest[6], H = digest[7];
80 
81  for(size_t i = 0; i != blocks; ++i)
82  {
83  uint64_t W00 = load_be<uint64_t>(input, 0);
84  uint64_t W01 = load_be<uint64_t>(input, 1);
85  uint64_t W02 = load_be<uint64_t>(input, 2);
86  uint64_t W03 = load_be<uint64_t>(input, 3);
87  uint64_t W04 = load_be<uint64_t>(input, 4);
88  uint64_t W05 = load_be<uint64_t>(input, 5);
89  uint64_t W06 = load_be<uint64_t>(input, 6);
90  uint64_t W07 = load_be<uint64_t>(input, 7);
91  uint64_t W08 = load_be<uint64_t>(input, 8);
92  uint64_t W09 = load_be<uint64_t>(input, 9);
93  uint64_t W10 = load_be<uint64_t>(input, 10);
94  uint64_t W11 = load_be<uint64_t>(input, 11);
95  uint64_t W12 = load_be<uint64_t>(input, 12);
96  uint64_t W13 = load_be<uint64_t>(input, 13);
97  uint64_t W14 = load_be<uint64_t>(input, 14);
98  uint64_t W15 = load_be<uint64_t>(input, 15);
99 
100  SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98D728AE22);
101  SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x7137449123EF65CD);
102  SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCFEC4D3B2F);
103  SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA58189DBBC);
104  SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25BF348B538);
105  SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1B605D019);
106  SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4AF194F9B);
107  SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5DA6D8118);
108  SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98A3030242);
109  SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B0145706FBE);
110  SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE4EE4B28C);
111  SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3D5FFB4E2);
112  SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74F27B896F);
113  SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE3B1696B1);
114  SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A725C71235);
115  SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174CF692694);
116  SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C19EF14AD2);
117  SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786384F25E3);
118  SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC68B8CD5B5);
119  SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC77AC9C65);
120  SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F592B0275);
121  SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA6EA6E483);
122  SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DCBD41FBD4);
123  SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA831153B5);
124  SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152EE66DFAB);
125  SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D2DB43210);
126  SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C898FB213F);
127  SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7BEEF0EE4);
128  SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF33DA88FC2);
129  SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147930AA725);
130  SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351E003826F);
131  SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x142929670A0E6E70);
132  SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A8546D22FFC);
133  SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B21385C26C926);
134  SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC5AC42AED);
135  SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D139D95B3DF);
136  SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A73548BAF63DE);
137  SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB3C77B2A8);
138  SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E47EDAEE6);
139  SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C851482353B);
140  SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A14CF10364);
141  SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664BBC423001);
142  SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70D0F89791);
143  SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A30654BE30);
144  SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819D6EF5218);
145  SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD69906245565A910);
146  SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E35855771202A);
147  SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA07032BBD1B8);
148  SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116B8D2D0C8);
149  SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C085141AB53);
150  SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774CDF8EEB99);
151  SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5E19B48A8);
152  SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3C5C95A63);
153  SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4AE3418ACB);
154  SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F7763E373);
155  SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3D6B2B8A3);
156  SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE5DEFB2FC);
157  SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F43172F60);
158  SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814A1F0AB72);
159  SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC702081A6439EC);
160  SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA23631E28);
161  SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEBDE82BDE9);
162  SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7B2C67915);
163  SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2E372532B);
164  SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xCA273ECEEA26619C);
165  SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xD186B8C721C0C207);
166  SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xEADA7DD6CDE0EB1E);
167  SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xF57D4F7FEE6ED178);
168  SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x06F067AA72176FBA);
169  SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x0A637DC5A2C898A6);
170  SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x113F9804BEF90DAE);
171  SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x1B710B35131C471B);
172  SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x28DB77F523047D84);
173  SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x32CAAB7B40C72493);
174  SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x3C9EBE0A15C9BEBC);
175  SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x431D67C49C100D4C);
176  SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x4CC5D4BECB3E42B6);
177  SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x597F299CFC657E2A);
178  SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x5FCB6FAB3AD6FAEC);
179  SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x6C44198C4A475817);
180 
181  A = (digest[0] += A);
182  B = (digest[1] += B);
183  C = (digest[2] += C);
184  D = (digest[3] += D);
185  E = (digest[4] += E);
186  F = (digest[5] += F);
187  G = (digest[6] += G);
188  H = (digest[7] += H);
189 
190  input += 128;
191  }
192  }
193 
194 #undef SHA2_64_F
195 
196 std::string SHA_512_256::provider() const
197  {
198  return sha512_provider();
199  }
200 
201 std::string SHA_384::provider() const
202  {
203  return sha512_provider();
204  }
205 
206 std::string SHA_512::provider() const
207  {
208  return sha512_provider();
209  }
210 
211 void SHA_512_256::compress_n(const uint8_t input[], size_t blocks)
212  {
213  SHA_512::compress_digest(m_digest, input, blocks);
214  }
215 
216 void SHA_384::compress_n(const uint8_t input[], size_t blocks)
217  {
218  SHA_512::compress_digest(m_digest, input, blocks);
219  }
220 
221 void SHA_512::compress_n(const uint8_t input[], size_t blocks)
222  {
223  SHA_512::compress_digest(m_digest, input, blocks);
224  }
225 
226 void SHA_512_256::copy_out(uint8_t output[])
227  {
228  copy_out_vec_be(output, output_length(), m_digest);
229  }
230 
231 void SHA_384::copy_out(uint8_t output[])
232  {
233  copy_out_vec_be(output, output_length(), m_digest);
234  }
235 
236 void SHA_512::copy_out(uint8_t output[])
237  {
238  copy_out_vec_be(output, output_length(), m_digest);
239  }
240 
242  {
244  m_digest[0] = 0x22312194FC2BF72C;
245  m_digest[1] = 0x9F555FA3C84C64C2;
246  m_digest[2] = 0x2393B86B6F53B151;
247  m_digest[3] = 0x963877195940EABD;
248  m_digest[4] = 0x96283EE2A88EFFE3;
249  m_digest[5] = 0xBE5E1E2553863992;
250  m_digest[6] = 0x2B0199FC2C85B8AA;
251  m_digest[7] = 0x0EB72DDC81C52CA2;
252  }
253 
255  {
257  m_digest[0] = 0xCBBB9D5DC1059ED8;
258  m_digest[1] = 0x629A292A367CD507;
259  m_digest[2] = 0x9159015A3070DD17;
260  m_digest[3] = 0x152FECD8F70E5939;
261  m_digest[4] = 0x67332667FFC00B31;
262  m_digest[5] = 0x8EB44A8768581511;
263  m_digest[6] = 0xDB0C2E0D64F98FA7;
264  m_digest[7] = 0x47B5481DBEFA4FA4;
265  }
266 
268  {
270  m_digest[0] = 0x6A09E667F3BCC908;
271  m_digest[1] = 0xBB67AE8584CAA73B;
272  m_digest[2] = 0x3C6EF372FE94F82B;
273  m_digest[3] = 0xA54FF53A5F1D36F1;
274  m_digest[4] = 0x510E527FADE682D1;
275  m_digest[5] = 0x9B05688C2B3E6C1F;
276  m_digest[6] = 0x1F83D9ABFB41BD6B;
277  m_digest[7] = 0x5BE0CD19137E2179;
278  }
279 
280 }
std::string provider() const override
Definition: sha2_64.cpp:206
std::unique_ptr< HashFunction > copy_state() const override
Definition: sha2_64.cpp:35
void copy_out_vec_be(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
Definition: loadstor.h:671
void clear() override
Definition: sha2_64.cpp:254
void clear() override
Definition: mdx_hash.cpp:41
std::string provider() const override
Definition: sha2_64.cpp:201
uint64_t load_be< uint64_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:215
static void compress_digest(secure_vector< uint64_t > &digest, const uint8_t input[], size_t blocks)
Definition: sha2_64.cpp:67
std::unique_ptr< HashFunction > copy_state() const override
Definition: sha2_64.cpp:40
size_t output_length() const override
Definition: sha2_64.h:22
#define SHA2_64_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic)
Definition: sha2_64.cpp:51
Definition: alg_id.cpp:13
std::string provider() const override
Definition: sha2_64.cpp:196
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void clear() override
Definition: sha2_64.cpp:267
void clear() override
Definition: sha2_64.cpp:241
std::unique_ptr< HashFunction > copy_state() const override
Definition: sha2_64.cpp:30