Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/sha/sha256.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/sha.h>
5
6 #include <string.h>
7
8 #include <openssl/mem.h>
9
10 #include "../../internal.h"
11 #include "../digest/md32_common.h"
12 #include "internal.h"
13
14
15 int SHA224_Init(SHA256_CTX *sha) {
16 OPENSSL_memset(sha, 0, sizeof(SHA256_CTX));
17 sha->h[0] = 0xc1059ed8UL;
18 sha->h[1] = 0x367cd507UL;
19 sha->h[2] = 0x3070dd17UL;
20 sha->h[3] = 0xf70e5939UL;
21 sha->h[4] = 0xffc00b31UL;
22 sha->h[5] = 0x68581511UL;
23 sha->h[6] = 0x64f98fa7UL;
24 sha->h[7] = 0xbefa4fa4UL;
25 sha->md_len = SHA224_DIGEST_LENGTH;
26 return 1;
27 }
28
29 int SHA256_Init(SHA256_CTX *sha) {
30 OPENSSL_memset(sha, 0, sizeof(SHA256_CTX));
31 sha->h[0] = 0x6a09e667UL;
32 sha->h[1] = 0xbb67ae85UL;
33 sha->h[2] = 0x3c6ef372UL;
34 sha->h[3] = 0xa54ff53aUL;
35 sha->h[4] = 0x510e527fUL;
36 sha->h[5] = 0x9b05688cUL;
37 sha->h[6] = 0x1f83d9abUL;
38 sha->h[7] = 0x5be0cd19UL;
39 sha->md_len = SHA256_DIGEST_LENGTH;
40 return 1;
41 }
42
43 OPENSSL_STATIC_ASSERT(SHA256_CHAINING_LENGTH==SHA224_CHAINING_LENGTH,
44 sha256_and_sha224_have_same_chaining_length)
45
46 // sha256_init_from_state_impl is the implementation of
47 // SHA256_Init_from_state and SHA224_Init_from_state
48 // Note that the state h is always SHA256_CHAINING_LENGTH-byte long
49 static int sha256_init_from_state_impl(SHA256_CTX *sha, int md_len,
50 const uint8_t h[SHA256_CHAINING_LENGTH],
51 uint64_t n) {
52 if(n % ((uint64_t) SHA256_CBLOCK * 8) != 0) {
53 // n is not a multiple of the block size in bits, so it fails
54 return 0;
55 }
56
57 OPENSSL_memset(sha, 0, sizeof(SHA256_CTX));
58 sha->md_len = md_len;
59
60 const size_t out_words = SHA256_CHAINING_LENGTH / 4;
61 for (size_t i = 0; i < out_words; i++) {
62 sha->h[i] = CRYPTO_load_u32_be(h);
63 h += 4;
64 }
65
66 sha->Nh = n >> 32;
67 sha->Nl = n & 0xffffffff;
68
69 return 1;
70 }
71
72 int SHA224_Init_from_state(SHA256_CTX *sha,
73 const uint8_t h[SHA224_CHAINING_LENGTH],
74 uint64_t n) {
75 return sha256_init_from_state_impl(sha, SHA224_DIGEST_LENGTH, h, n);
76 }
77
78 int SHA256_Init_from_state(SHA256_CTX *sha,
79 const uint8_t h[SHA256_CHAINING_LENGTH],
80 uint64_t n) {
81 return sha256_init_from_state_impl(sha, SHA256_DIGEST_LENGTH, h, n);
82 }
83
84 uint8_t *SHA224(const uint8_t *data, size_t len,
85 uint8_t out[SHA224_DIGEST_LENGTH]) {
86 // We have to verify that all the SHA services actually succeed before
87 // updating the indicator state, so we lock the state here.
88 FIPS_service_indicator_lock_state();
89 SHA256_CTX ctx;
90 const int ok = SHA224_Init(&ctx) &&
91 SHA224_Update(&ctx, data, len) &&
92 SHA224_Final(out, &ctx);
93 FIPS_service_indicator_unlock_state();
94 if(ok) {
95 FIPS_service_indicator_update_state();
96 }
97 OPENSSL_cleanse(&ctx, sizeof(ctx));
98 return out;
99 }
100
101 uint8_t *SHA256(const uint8_t *data, size_t len,
102 uint8_t out[SHA256_DIGEST_LENGTH]) {
103 // We have to verify that all the SHA services actually succeed before
104 // updating the indicator state, so we lock the state here.
105 FIPS_service_indicator_lock_state();
106 SHA256_CTX ctx;
107 const int ok = SHA256_Init(&ctx) &&
108 SHA256_Update(&ctx, data, len) &&
109 SHA256_Final(out, &ctx);
110 FIPS_service_indicator_unlock_state();
111 if(ok) {
112 FIPS_service_indicator_update_state();
113 }
114 OPENSSL_cleanse(&ctx, sizeof(ctx));
115 return out;
116 }
117
118 #if !defined(SHA256_ASM)
119 static void sha256_block_data_order(uint32_t state[8], const uint8_t *in,
120 size_t num);
121 #endif
122
123 void SHA256_Transform(SHA256_CTX *c, const uint8_t data[SHA256_CBLOCK]) {
124 sha256_block_data_order(c->h, data, 1);
125 }
126
127 int SHA256_Update(SHA256_CTX *c, const void *data, size_t len) {
128 crypto_md32_update(&sha256_block_data_order, c->h, c->data, SHA256_CBLOCK,
129 &c->num, &c->Nh, &c->Nl, data, len);
130 return 1;
131 }
132
133 int SHA224_Update(SHA256_CTX *ctx, const void *data, size_t len) {
134 return SHA256_Update(ctx, data, len);
135 }
136
137 static int sha256_final_impl(uint8_t *out, size_t md_len, SHA256_CTX *c) {
138 crypto_md32_final(&sha256_block_data_order, c->h, c->data, SHA256_CBLOCK,
139 &c->num, c->Nh, c->Nl, /*is_big_endian=*/1);
140 if (c->md_len != md_len) {
141 return 0;
142 }
143
144 assert(md_len % 4 == 0);
145 const size_t out_words = md_len / 4;
146 for (size_t i = 0; i < out_words; i++) {
147 CRYPTO_store_u32_be(out, c->h[i]);
148 out += 4;
149 }
150 FIPS_service_indicator_update_state();
151 return 1;
152 }
153
154 int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *c) {
155 return sha256_final_impl(out, SHA256_DIGEST_LENGTH, c);
156 }
157
158 int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *ctx) {
159 return sha256_final_impl(out, SHA224_DIGEST_LENGTH, ctx);
160 }
161
162 // sha256_get_state_impl is the implementation of
163 // SHA256_get_state and SHA224_get_state
164 // Note that the state out_h is always SHA256_CHAINING_LENGTH-byte long
165 static int sha256_get_state_impl(SHA256_CTX *ctx,
166 uint8_t out_h[SHA256_CHAINING_LENGTH],
167 uint64_t *out_n) {
168 if (ctx->Nl % ((uint64_t)SHA256_CBLOCK * 8) != 0) {
169 // ctx->Nl is not a multiple of the block size in bits, so it fails
170 return 0;
171 }
172
173 const size_t out_words = SHA256_CHAINING_LENGTH / 4;
174 for (size_t i = 0; i < out_words; i++) {
175 CRYPTO_store_u32_be(out_h, ctx->h[i]);
176 out_h += 4;
177 }
178
179 *out_n = (((uint64_t)ctx->Nh) << 32) + ctx->Nl;
180
181 return 1;
182 }
183
184 int SHA224_get_state(SHA256_CTX *ctx, uint8_t out_h[SHA224_CHAINING_LENGTH],
185 uint64_t *out_n) {
186 return sha256_get_state_impl(ctx, out_h, out_n);
187 }
188
189 int SHA256_get_state(SHA256_CTX *ctx, uint8_t out_h[SHA256_CHAINING_LENGTH],
190 uint64_t *out_n) {
191 return sha256_get_state_impl(ctx, out_h, out_n);
192 }
193
194 #if !defined(SHA256_ASM)
195
196 #if !defined(SHA256_ASM_NOHW)
197 static const uint32_t K256[64] = {
198 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
199 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
200 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
201 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
202 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
203 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
204 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
205 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
206 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
207 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
208 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
209 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
210 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL};
211
212 // See FIPS 180-4, section 4.1.2.
213 #define Sigma0(x) \
214 (CRYPTO_rotr_u32((x), 2) ^ CRYPTO_rotr_u32((x), 13) ^ \
215 CRYPTO_rotr_u32((x), 22))
216 #define Sigma1(x) \
217 (CRYPTO_rotr_u32((x), 6) ^ CRYPTO_rotr_u32((x), 11) ^ \
218 CRYPTO_rotr_u32((x), 25))
219 #define sigma0(x) \
220 (CRYPTO_rotr_u32((x), 7) ^ CRYPTO_rotr_u32((x), 18) ^ ((x) >> 3))
221 #define sigma1(x) \
222 (CRYPTO_rotr_u32((x), 17) ^ CRYPTO_rotr_u32((x), 19) ^ ((x) >> 10))
223
224 #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
225 #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
226
227 #define ROUND_00_15(i, a, b, c, d, e, f, g, h) \
228 do { \
229 T1 += h + Sigma1(e) + Ch(e, f, g) + K256[i]; \
230 h = Sigma0(a) + Maj(a, b, c); \
231 d += T1; \
232 h += T1; \
233 } while (0)
234
235 #define ROUND_16_63(i, a, b, c, d, e, f, g, h, X) \
236 do { \
237 s0 = X[(i + 1) & 0x0f]; \
238 s0 = sigma0(s0); \
239 s1 = X[(i + 14) & 0x0f]; \
240 s1 = sigma1(s1); \
241 T1 = X[(i) & 0x0f] += s0 + s1 + X[(i + 9) & 0x0f]; \
242 ROUND_00_15(i, a, b, c, d, e, f, g, h); \
243 } while (0)
244
245 static void sha256_block_data_order_nohw(uint32_t state[8], const uint8_t *data,
246 size_t num) {
247 uint32_t a, b, c, d, e, f, g, h, s0, s1, T1;
248 uint32_t X[16];
249 int i;
250
251 while (num--) {
252 a = state[0];
253 b = state[1];
254 c = state[2];
255 d = state[3];
256 e = state[4];
257 f = state[5];
258 g = state[6];
259 h = state[7];
260
261 T1 = X[0] = CRYPTO_load_u32_be(data);
262 data += 4;
263 ROUND_00_15(0, a, b, c, d, e, f, g, h);
264 T1 = X[1] = CRYPTO_load_u32_be(data);
265 data += 4;
266 ROUND_00_15(1, h, a, b, c, d, e, f, g);
267 T1 = X[2] = CRYPTO_load_u32_be(data);
268 data += 4;
269 ROUND_00_15(2, g, h, a, b, c, d, e, f);
270 T1 = X[3] = CRYPTO_load_u32_be(data);
271 data += 4;
272 ROUND_00_15(3, f, g, h, a, b, c, d, e);
273 T1 = X[4] = CRYPTO_load_u32_be(data);
274 data += 4;
275 ROUND_00_15(4, e, f, g, h, a, b, c, d);
276 T1 = X[5] = CRYPTO_load_u32_be(data);
277 data += 4;
278 ROUND_00_15(5, d, e, f, g, h, a, b, c);
279 T1 = X[6] = CRYPTO_load_u32_be(data);
280 data += 4;
281 ROUND_00_15(6, c, d, e, f, g, h, a, b);
282 T1 = X[7] = CRYPTO_load_u32_be(data);
283 data += 4;
284 ROUND_00_15(7, b, c, d, e, f, g, h, a);
285 T1 = X[8] = CRYPTO_load_u32_be(data);
286 data += 4;
287 ROUND_00_15(8, a, b, c, d, e, f, g, h);
288 T1 = X[9] = CRYPTO_load_u32_be(data);
289 data += 4;
290 ROUND_00_15(9, h, a, b, c, d, e, f, g);
291 T1 = X[10] = CRYPTO_load_u32_be(data);
292 data += 4;
293 ROUND_00_15(10, g, h, a, b, c, d, e, f);
294 T1 = X[11] = CRYPTO_load_u32_be(data);
295 data += 4;
296 ROUND_00_15(11, f, g, h, a, b, c, d, e);
297 T1 = X[12] = CRYPTO_load_u32_be(data);
298 data += 4;
299 ROUND_00_15(12, e, f, g, h, a, b, c, d);
300 T1 = X[13] = CRYPTO_load_u32_be(data);
301 data += 4;
302 ROUND_00_15(13, d, e, f, g, h, a, b, c);
303 T1 = X[14] = CRYPTO_load_u32_be(data);
304 data += 4;
305 ROUND_00_15(14, c, d, e, f, g, h, a, b);
306 T1 = X[15] = CRYPTO_load_u32_be(data);
307 data += 4;
308 ROUND_00_15(15, b, c, d, e, f, g, h, a);
309
310 for (i = 16; i < 64; i += 8) {
311 ROUND_16_63(i + 0, a, b, c, d, e, f, g, h, X);
312 ROUND_16_63(i + 1, h, a, b, c, d, e, f, g, X);
313 ROUND_16_63(i + 2, g, h, a, b, c, d, e, f, X);
314 ROUND_16_63(i + 3, f, g, h, a, b, c, d, e, X);
315 ROUND_16_63(i + 4, e, f, g, h, a, b, c, d, X);
316 ROUND_16_63(i + 5, d, e, f, g, h, a, b, c, X);
317 ROUND_16_63(i + 6, c, d, e, f, g, h, a, b, X);
318 ROUND_16_63(i + 7, b, c, d, e, f, g, h, a, X);
319 }
320
321 state[0] += a;
322 state[1] += b;
323 state[2] += c;
324 state[3] += d;
325 state[4] += e;
326 state[5] += f;
327 state[6] += g;
328 state[7] += h;
329 }
330 }
331
332 #endif // !defined(SHA256_ASM_NOHW)
333
334 static void sha256_block_data_order(uint32_t state[8], const uint8_t *data,
335 size_t num) {
336 #if defined(SHA256_ASM_HW)
337 if (sha256_hw_capable()) {
338 sha256_block_data_order_hw(state, data, num);
339 return;
340 }
341 #endif
342 #if defined(SHA256_ASM_AVX) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
343 if (sha256_avx_capable()) {
344 sha256_block_data_order_avx(state, data, num);
345 return;
346 }
347 #endif
348 #if defined(SHA256_ASM_SSSE3)
349 if (sha256_ssse3_capable()) {
350 sha256_block_data_order_ssse3(state, data, num);
351 return;
352 }
353 #endif
354 #if defined(SHA256_ASM_NEON)
355 if (CRYPTO_is_NEON_capable()) {
356 sha256_block_data_order_neon(state, data, num);
357 return;
358 }
359 #endif
360 sha256_block_data_order_nohw(state, data, num);
361 }
362
363 #endif // !defined(SHA256_ASM)
364
365
366 void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data,
367 size_t num_blocks) {
368 sha256_block_data_order(state, data, num_blocks);
369 }
370
371 #undef Sigma0
372 #undef Sigma1
373 #undef sigma0
374 #undef sigma1
375 #undef Ch
376 #undef Maj
377 #undef ROUND_00_15
378 #undef ROUND_16_63