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