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