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