Botan  2.4.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_ARMV8)
55  if(CPUID::has_arm_sha2())
56  {
57  return SHA_256::compress_digest_armv8(digest, input, blocks);
58  }
59 #endif
60 
61  uint32_t A = digest[0], B = digest[1], C = digest[2],
62  D = digest[3], E = digest[4], F = digest[5],
63  G = digest[6], H = digest[7];
64 
65  for(size_t i = 0; i != blocks; ++i)
66  {
67  uint32_t W00 = load_be<uint32_t>(input, 0);
68  uint32_t W01 = load_be<uint32_t>(input, 1);
69  uint32_t W02 = load_be<uint32_t>(input, 2);
70  uint32_t W03 = load_be<uint32_t>(input, 3);
71  uint32_t W04 = load_be<uint32_t>(input, 4);
72  uint32_t W05 = load_be<uint32_t>(input, 5);
73  uint32_t W06 = load_be<uint32_t>(input, 6);
74  uint32_t W07 = load_be<uint32_t>(input, 7);
75  uint32_t W08 = load_be<uint32_t>(input, 8);
76  uint32_t W09 = load_be<uint32_t>(input, 9);
77  uint32_t W10 = load_be<uint32_t>(input, 10);
78  uint32_t W11 = load_be<uint32_t>(input, 11);
79  uint32_t W12 = load_be<uint32_t>(input, 12);
80  uint32_t W13 = load_be<uint32_t>(input, 13);
81  uint32_t W14 = load_be<uint32_t>(input, 14);
82  uint32_t W15 = load_be<uint32_t>(input, 15);
83 
84  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
85  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
86  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
87  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
88  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
89  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
90  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
91  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
92  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
93  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
94  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
95  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
96  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
97  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
98  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
99  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
100  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
101  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
102  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
103  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
104  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
105  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
106  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
107  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
108  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
109  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
110  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
111  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
112  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
113  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
114  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
115  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
116  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
117  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
118  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
119  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
120  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
121  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
122  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
123  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
124  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
125  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
126  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
127  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
128  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
129  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
130  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
131  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
132  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
133  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
134  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
135  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
136  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
137  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
138  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
139  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
140  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
141  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
142  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
143  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
144  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
145  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
146  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
147  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
148 
149  A = (digest[0] += A);
150  B = (digest[1] += B);
151  C = (digest[2] += C);
152  D = (digest[3] += D);
153  E = (digest[4] += E);
154  F = (digest[5] += F);
155  G = (digest[6] += G);
156  H = (digest[7] += H);
157 
158  input += 64;
159  }
160  }
161 
162 /*
163 * SHA-224 compression function
164 */
165 void SHA_224::compress_n(const uint8_t input[], size_t blocks)
166  {
167  SHA_256::compress_digest(m_digest, input, blocks);
168  }
169 
170 /*
171 * Copy out the digest
172 */
173 void SHA_224::copy_out(uint8_t output[])
174  {
175  copy_out_vec_be(output, output_length(), m_digest);
176  }
177 
178 /*
179 * Clear memory of sensitive data
180 */
182  {
184  m_digest[0] = 0xC1059ED8;
185  m_digest[1] = 0x367CD507;
186  m_digest[2] = 0x3070DD17;
187  m_digest[3] = 0xF70E5939;
188  m_digest[4] = 0xFFC00B31;
189  m_digest[5] = 0x68581511;
190  m_digest[6] = 0x64F98FA7;
191  m_digest[7] = 0xBEFA4FA4;
192  }
193 
194 /*
195 * SHA-256 compression function
196 */
197 void SHA_256::compress_n(const uint8_t input[], size_t blocks)
198  {
199  SHA_256::compress_digest(m_digest, input, blocks);
200  }
201 
202 /*
203 * Copy out the digest
204 */
205 void SHA_256::copy_out(uint8_t output[])
206  {
207  copy_out_vec_be(output, output_length(), m_digest);
208  }
209 
210 /*
211 * Clear memory of sensitive data
212 */
214  {
216  m_digest[0] = 0x6A09E667;
217  m_digest[1] = 0xBB67AE85;
218  m_digest[2] = 0x3C6EF372;
219  m_digest[3] = 0xA54FF53A;
220  m_digest[4] = 0x510E527F;
221  m_digest[5] = 0x9B05688C;
222  m_digest[6] = 0x1F83D9AB;
223  m_digest[7] = 0x5BE0CD19;
224  }
225 
226 }
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:213
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
#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:181