Coverage Report

Created: 2020-05-23 13:54

/src/botan/src/lib/hash/sha2_32/sha2_32.cpp
Line
Count
Source (jump to first uncovered line)
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/sha2_32.h>
10
#include <botan/loadstor.h>
11
#include <botan/rotate.h>
12
#include <botan/cpuid.h>
13
14
namespace Botan {
15
16
namespace {
17
18
std::string sha256_provider()
19
0
   {
20
0
#if defined(BOTAN_HAS_SHA2_32_X86)
21
0
   if(CPUID::has_intel_sha())
22
0
      {
23
0
      return "shani";
24
0
      }
25
0
#endif
26
0
27
0
#if defined(BOTAN_HAS_SHA2_32_X86_BMI2)
28
0
   if(CPUID::has_bmi2())
29
0
      {
30
0
      return "bmi2";
31
0
      }
32
0
#endif
33
0
34
#if defined(BOTAN_HAS_SHA2_32_ARMV8)
35
   if(CPUID::has_arm_sha2())
36
      {
37
      return "armv8";
38
      }
39
#endif
40
41
0
   return "base";
42
0
   }
43
44
}
45
46
std::unique_ptr<HashFunction> SHA_224::copy_state() const
47
0
   {
48
0
   return std::unique_ptr<HashFunction>(new SHA_224(*this));
49
0
   }
50
51
std::unique_ptr<HashFunction> SHA_256::copy_state() const
52
0
   {
53
0
   return std::unique_ptr<HashFunction>(new SHA_256(*this));
54
0
   }
55
56
/*
57
* SHA-256 F1 Function
58
*
59
* Use a macro as many compilers won't inline a function this big,
60
* even though it is much faster if inlined.
61
*/
62
0
#define SHA2_32_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic) do {               \
63
0
   uint32_t A_rho = rotr<2>(A) ^ rotr<13>(A) ^ rotr<22>(A); \
64
0
   uint32_t E_rho = rotr<6>(E) ^ rotr<11>(E) ^ rotr<25>(E); \
65
0
   uint32_t M2_sigma = rotr<17>(M2) ^ rotr<19>(M2) ^ (M2 >> 10);    \
66
0
   uint32_t M4_sigma = rotr<7>(M4) ^ rotr<18>(M4) ^ (M4 >> 3);      \
67
0
   H += magic + E_rho + ((E & F) ^ (~E & G)) + M1;                                  \
68
0
   D += H;                                                                          \
69
0
   H += A_rho + ((A & B) | ((A | B) & C));                                          \
70
0
   M1 += M2_sigma + M3 + M4_sigma;                                                  \
71
0
   } while(0);
72
73
/*
74
* SHA-224 / SHA-256 compression function
75
*/
76
void SHA_256::compress_digest(secure_vector<uint32_t>& digest,
77
                              const uint8_t input[], size_t blocks)
78
593k
   {
79
593k
#if defined(BOTAN_HAS_SHA2_32_X86)
80
593k
   if(CPUID::has_intel_sha())
81
0
      {
82
0
      return SHA_256::compress_digest_x86(digest, input, blocks);
83
0
      }
84
593k
#endif
85
593k
86
593k
#if defined(BOTAN_HAS_SHA2_32_X86_BMI2)
87
593k
   if(CPUID::has_bmi2())
88
593k
      {
89
593k
      return SHA_256::compress_digest_x86_bmi2(digest, input, blocks);
90
593k
      }
91
0
#endif
92
0
93
#if defined(BOTAN_HAS_SHA2_32_ARMV8)
94
   if(CPUID::has_arm_sha2())
95
      {
96
      return SHA_256::compress_digest_armv8(digest, input, blocks);
97
      }
98
#endif
99
100
0
   uint32_t A = digest[0], B = digest[1], C = digest[2],
101
0
            D = digest[3], E = digest[4], F = digest[5],
102
0
            G = digest[6], H = digest[7];
103
0
104
0
   for(size_t i = 0; i != blocks; ++i)
105
0
      {
106
0
      uint32_t W00 = load_be<uint32_t>(input,  0);
107
0
      uint32_t W01 = load_be<uint32_t>(input,  1);
108
0
      uint32_t W02 = load_be<uint32_t>(input,  2);
109
0
      uint32_t W03 = load_be<uint32_t>(input,  3);
110
0
      uint32_t W04 = load_be<uint32_t>(input,  4);
111
0
      uint32_t W05 = load_be<uint32_t>(input,  5);
112
0
      uint32_t W06 = load_be<uint32_t>(input,  6);
113
0
      uint32_t W07 = load_be<uint32_t>(input,  7);
114
0
      uint32_t W08 = load_be<uint32_t>(input,  8);
115
0
      uint32_t W09 = load_be<uint32_t>(input,  9);
116
0
      uint32_t W10 = load_be<uint32_t>(input, 10);
117
0
      uint32_t W11 = load_be<uint32_t>(input, 11);
118
0
      uint32_t W12 = load_be<uint32_t>(input, 12);
119
0
      uint32_t W13 = load_be<uint32_t>(input, 13);
120
0
      uint32_t W14 = load_be<uint32_t>(input, 14);
121
0
      uint32_t W15 = load_be<uint32_t>(input, 15);
122
0
123
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
124
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
125
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
126
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
127
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
128
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
129
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
130
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
131
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
132
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
133
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
134
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
135
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
136
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
137
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
138
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
139
0
140
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
141
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
142
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
143
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
144
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
145
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
146
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
147
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
148
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
149
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
150
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
151
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
152
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
153
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
154
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
155
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
156
0
157
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
158
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
159
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
160
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
161
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
162
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
163
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
164
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
165
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
166
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
167
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
168
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
169
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
170
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
171
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
172
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
173
0
174
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
175
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
176
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
177
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
178
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
179
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
180
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
181
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
182
0
      SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
183
0
      SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
184
0
      SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
185
0
      SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
186
0
      SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
187
0
      SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
188
0
      SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
189
0
      SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
190
0
191
0
      A = (digest[0] += A);
192
0
      B = (digest[1] += B);
193
0
      C = (digest[2] += C);
194
0
      D = (digest[3] += D);
195
0
      E = (digest[4] += E);
196
0
      F = (digest[5] += F);
197
0
      G = (digest[6] += G);
198
0
      H = (digest[7] += H);
199
0
200
0
      input += 64;
201
0
      }
202
0
   }
203
204
std::string SHA_224::provider() const
205
0
   {
206
0
   return sha256_provider();
207
0
   }
208
209
std::string SHA_256::provider() const
210
0
   {
211
0
   return sha256_provider();
212
0
   }
213
214
/*
215
* SHA-224 compression function
216
*/
217
void SHA_224::compress_n(const uint8_t input[], size_t blocks)
218
22
   {
219
22
   SHA_256::compress_digest(m_digest, input, blocks);
220
22
   }
221
222
/*
223
* Copy out the digest
224
*/
225
void SHA_224::copy_out(uint8_t output[])
226
9
   {
227
9
   copy_out_vec_be(output, output_length(), m_digest);
228
9
   }
229
230
/*
231
* Clear memory of sensitive data
232
*/
233
void SHA_224::clear()
234
20
   {
235
20
   MDx_HashFunction::clear();
236
20
   m_digest[0] = 0xC1059ED8;
237
20
   m_digest[1] = 0x367CD507;
238
20
   m_digest[2] = 0x3070DD17;
239
20
   m_digest[3] = 0xF70E5939;
240
20
   m_digest[4] = 0xFFC00B31;
241
20
   m_digest[5] = 0x68581511;
242
20
   m_digest[6] = 0x64F98FA7;
243
20
   m_digest[7] = 0xBEFA4FA4;
244
20
   }
245
246
/*
247
* SHA-256 compression function
248
*/
249
void SHA_256::compress_n(const uint8_t input[], size_t blocks)
250
593k
   {
251
593k
   SHA_256::compress_digest(m_digest, input, blocks);
252
593k
   }
253
254
/*
255
* Copy out the digest
256
*/
257
void SHA_256::copy_out(uint8_t output[])
258
278k
   {
259
278k
   copy_out_vec_be(output, output_length(), m_digest);
260
278k
   }
261
262
/*
263
* Clear memory of sensitive data
264
*/
265
void SHA_256::clear()
266
398k
   {
267
398k
   MDx_HashFunction::clear();
268
398k
   m_digest[0] = 0x6A09E667;
269
398k
   m_digest[1] = 0xBB67AE85;
270
398k
   m_digest[2] = 0x3C6EF372;
271
398k
   m_digest[3] = 0xA54FF53A;
272
398k
   m_digest[4] = 0x510E527F;
273
398k
   m_digest[5] = 0x9B05688C;
274
398k
   m_digest[6] = 0x1F83D9AB;
275
398k
   m_digest[7] = 0x5BE0CD19;
276
398k
   }
277
278
}