/src/boringssl/crypto/fipsmodule/sha/sha512.c.inc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] */ |
56 | | |
57 | | #include <string.h> |
58 | | |
59 | | #include <openssl/mem.h> |
60 | | |
61 | | #include "../../internal.h" |
62 | | #include "../bcm_interface.h" |
63 | | #include "../service_indicator/internal.h" |
64 | | #include "internal.h" |
65 | | |
66 | | |
67 | | // The 32-bit hash algorithms share a common byte-order neutral collector and |
68 | | // padding function implementations that operate on unaligned data, |
69 | | // ../digest/md32_common.h. SHA-512 is the only 64-bit hash algorithm, as of |
70 | | // this writing, so there is no need for a common collector/padding |
71 | | // implementation yet. |
72 | | |
73 | | static void sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha); |
74 | | |
75 | 277 | bcm_infallible BCM_sha384_init(SHA512_CTX *sha) { |
76 | 277 | sha->h[0] = UINT64_C(0xcbbb9d5dc1059ed8); |
77 | 277 | sha->h[1] = UINT64_C(0x629a292a367cd507); |
78 | 277 | sha->h[2] = UINT64_C(0x9159015a3070dd17); |
79 | 277 | sha->h[3] = UINT64_C(0x152fecd8f70e5939); |
80 | 277 | sha->h[4] = UINT64_C(0x67332667ffc00b31); |
81 | 277 | sha->h[5] = UINT64_C(0x8eb44a8768581511); |
82 | 277 | sha->h[6] = UINT64_C(0xdb0c2e0d64f98fa7); |
83 | 277 | sha->h[7] = UINT64_C(0x47b5481dbefa4fa4); |
84 | | |
85 | 277 | sha->Nl = 0; |
86 | 277 | sha->Nh = 0; |
87 | 277 | sha->num = 0; |
88 | 277 | sha->md_len = BCM_SHA384_DIGEST_LENGTH; |
89 | 277 | return bcm_infallible_approved; |
90 | 277 | } |
91 | | |
92 | | |
93 | 345 | bcm_infallible BCM_sha512_init(SHA512_CTX *sha) { |
94 | 345 | sha->h[0] = UINT64_C(0x6a09e667f3bcc908); |
95 | 345 | sha->h[1] = UINT64_C(0xbb67ae8584caa73b); |
96 | 345 | sha->h[2] = UINT64_C(0x3c6ef372fe94f82b); |
97 | 345 | sha->h[3] = UINT64_C(0xa54ff53a5f1d36f1); |
98 | 345 | sha->h[4] = UINT64_C(0x510e527fade682d1); |
99 | 345 | sha->h[5] = UINT64_C(0x9b05688c2b3e6c1f); |
100 | 345 | sha->h[6] = UINT64_C(0x1f83d9abfb41bd6b); |
101 | 345 | sha->h[7] = UINT64_C(0x5be0cd19137e2179); |
102 | | |
103 | 345 | sha->Nl = 0; |
104 | 345 | sha->Nh = 0; |
105 | 345 | sha->num = 0; |
106 | 345 | sha->md_len = BCM_SHA512_DIGEST_LENGTH; |
107 | 345 | return bcm_infallible_approved; |
108 | 345 | } |
109 | | |
110 | 77 | bcm_infallible BCM_sha512_256_init(SHA512_CTX *sha) { |
111 | 77 | sha->h[0] = UINT64_C(0x22312194fc2bf72c); |
112 | 77 | sha->h[1] = UINT64_C(0x9f555fa3c84c64c2); |
113 | 77 | sha->h[2] = UINT64_C(0x2393b86b6f53b151); |
114 | 77 | sha->h[3] = UINT64_C(0x963877195940eabd); |
115 | 77 | sha->h[4] = UINT64_C(0x96283ee2a88effe3); |
116 | 77 | sha->h[5] = UINT64_C(0xbe5e1e2553863992); |
117 | 77 | sha->h[6] = UINT64_C(0x2b0199fc2c85b8aa); |
118 | 77 | sha->h[7] = UINT64_C(0x0eb72ddc81c52ca2); |
119 | | |
120 | 77 | sha->Nl = 0; |
121 | 77 | sha->Nh = 0; |
122 | 77 | sha->num = 0; |
123 | 77 | sha->md_len = BCM_SHA512_256_DIGEST_LENGTH; |
124 | 77 | return bcm_infallible_approved; |
125 | 77 | } |
126 | | |
127 | | #if !defined(SHA512_ASM) |
128 | | static void sha512_block_data_order(uint64_t state[8], const uint8_t *in, |
129 | | size_t num_blocks); |
130 | | #endif |
131 | | |
132 | | |
133 | | bcm_infallible BCM_sha384_final(uint8_t out[BCM_SHA384_DIGEST_LENGTH], |
134 | 2.13k | SHA512_CTX *sha) { |
135 | | // This function must be paired with |BCM_sha384_init|, which sets |
136 | | // |sha->md_len| to |BCM_SHA384_DIGEST_LENGTH|. |
137 | 2.13k | assert(sha->md_len == BCM_SHA384_DIGEST_LENGTH); |
138 | 2.13k | sha512_final_impl(out, BCM_SHA384_DIGEST_LENGTH, sha); |
139 | 2.13k | return bcm_infallible_approved; |
140 | 2.13k | } |
141 | | |
142 | | bcm_infallible BCM_sha384_update(SHA512_CTX *sha, const void *data, |
143 | 7.65k | size_t len) { |
144 | 7.65k | return BCM_sha512_update(sha, data, len); |
145 | 7.65k | } |
146 | | |
147 | | bcm_infallible BCM_sha512_256_update(SHA512_CTX *sha, const void *data, |
148 | 7.42k | size_t len) { |
149 | 7.42k | return BCM_sha512_update(sha, data, len); |
150 | 7.42k | } |
151 | | |
152 | | bcm_infallible BCM_sha512_256_final(uint8_t out[BCM_SHA512_256_DIGEST_LENGTH], |
153 | 821 | SHA512_CTX *sha) { |
154 | | // This function must be paired with |BCM_sha512_256_init|, which sets |
155 | | // |sha->md_len| to |BCM_SHA512_256_DIGEST_LENGTH|. |
156 | 821 | assert(sha->md_len == BCM_SHA512_256_DIGEST_LENGTH); |
157 | 821 | sha512_final_impl(out, BCM_SHA512_256_DIGEST_LENGTH, sha); |
158 | 821 | return bcm_infallible_approved; |
159 | 821 | } |
160 | | |
161 | | bcm_infallible BCM_sha512_transform(SHA512_CTX *c, |
162 | 0 | const uint8_t block[SHA512_CBLOCK]) { |
163 | 0 | sha512_block_data_order(c->h, block, 1); |
164 | 0 | return bcm_infallible_approved; |
165 | 0 | } |
166 | | |
167 | | bcm_infallible BCM_sha512_update(SHA512_CTX *c, const void *in_data, |
168 | 34.0k | size_t len) { |
169 | 34.0k | uint64_t l; |
170 | 34.0k | uint8_t *p = c->p; |
171 | 34.0k | const uint8_t *data = in_data; |
172 | | |
173 | 34.0k | if (len == 0) { |
174 | 23.1k | return bcm_infallible_approved; |
175 | 23.1k | } |
176 | | |
177 | 10.8k | l = (c->Nl + (((uint64_t)len) << 3)) & UINT64_C(0xffffffffffffffff); |
178 | 10.8k | if (l < c->Nl) { |
179 | 0 | c->Nh++; |
180 | 0 | } |
181 | 10.8k | if (sizeof(len) >= 8) { |
182 | 10.8k | c->Nh += (((uint64_t)len) >> 61); |
183 | 10.8k | } |
184 | 10.8k | c->Nl = l; |
185 | | |
186 | 10.8k | if (c->num != 0) { |
187 | 4.91k | size_t n = sizeof(c->p) - c->num; |
188 | | |
189 | 4.91k | if (len < n) { |
190 | 2.91k | OPENSSL_memcpy(p + c->num, data, len); |
191 | 2.91k | c->num += (unsigned int)len; |
192 | 2.91k | return 1; |
193 | 2.91k | } else { |
194 | 1.99k | OPENSSL_memcpy(p + c->num, data, n), c->num = 0; |
195 | 1.99k | len -= n; |
196 | 1.99k | data += n; |
197 | 1.99k | sha512_block_data_order(c->h, p, 1); |
198 | 1.99k | } |
199 | 4.91k | } |
200 | | |
201 | 7.97k | if (len >= sizeof(c->p)) { |
202 | 2.81k | sha512_block_data_order(c->h, data, len / sizeof(c->p)); |
203 | 2.81k | data += len; |
204 | 2.81k | len %= sizeof(c->p); |
205 | 2.81k | data -= len; |
206 | 2.81k | } |
207 | | |
208 | 7.97k | if (len != 0) { |
209 | 7.46k | OPENSSL_memcpy(p, data, len); |
210 | 7.46k | c->num = (int)len; |
211 | 7.46k | } |
212 | | |
213 | 7.97k | return bcm_infallible_approved; |
214 | 10.8k | } |
215 | | |
216 | | bcm_infallible BCM_sha512_final(uint8_t out[BCM_SHA512_DIGEST_LENGTH], |
217 | 2.66k | SHA512_CTX *sha) { |
218 | | // Ideally we would assert |sha->md_len| is |BCM_SHA512_DIGEST_LENGTH| to |
219 | | // match the size hint, but calling code often pairs |BCM_sha384_init| with |
220 | | // |BCM_sha512_final| and expects |sha->md_len| to carry the size over. |
221 | | // |
222 | | // TODO(davidben): Add an assert and fix code to match them up. |
223 | 2.66k | sha512_final_impl(out, sha->md_len, sha); |
224 | 2.66k | return bcm_infallible_approved; |
225 | 2.66k | } |
226 | | |
227 | 5.61k | static void sha512_final_impl(uint8_t *out, size_t md_len, SHA512_CTX *sha) { |
228 | 5.61k | uint8_t *p = sha->p; |
229 | 5.61k | size_t n = sha->num; |
230 | | |
231 | 5.61k | p[n] = 0x80; // There always is a room for one |
232 | 5.61k | n++; |
233 | 5.61k | if (n > (sizeof(sha->p) - 16)) { |
234 | 686 | OPENSSL_memset(p + n, 0, sizeof(sha->p) - n); |
235 | 686 | n = 0; |
236 | 686 | sha512_block_data_order(sha->h, p, 1); |
237 | 686 | } |
238 | | |
239 | 5.61k | OPENSSL_memset(p + n, 0, sizeof(sha->p) - 16 - n); |
240 | 5.61k | CRYPTO_store_u64_be(p + sizeof(sha->p) - 16, sha->Nh); |
241 | 5.61k | CRYPTO_store_u64_be(p + sizeof(sha->p) - 8, sha->Nl); |
242 | | |
243 | 5.61k | sha512_block_data_order(sha->h, p, 1); |
244 | | |
245 | 5.61k | assert(md_len % 8 == 0); |
246 | 5.61k | const size_t out_words = md_len / 8; |
247 | 43.0k | for (size_t i = 0; i < out_words; i++) { |
248 | 37.4k | CRYPTO_store_u64_be(out, sha->h[i]); |
249 | 37.4k | out += 8; |
250 | 37.4k | } |
251 | | |
252 | 5.61k | FIPS_service_indicator_update_state(); |
253 | 5.61k | } |
254 | | |
255 | | #if !defined(SHA512_ASM) |
256 | | |
257 | | #if !defined(SHA512_ASM_NOHW) |
258 | | static const uint64_t K512[80] = { |
259 | | UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), |
260 | | UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc), |
261 | | UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), |
262 | | UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118), |
263 | | UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), |
264 | | UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2), |
265 | | UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), |
266 | | UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694), |
267 | | UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), |
268 | | UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65), |
269 | | UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), |
270 | | UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5), |
271 | | UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), |
272 | | UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4), |
273 | | UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), |
274 | | UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70), |
275 | | UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), |
276 | | UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df), |
277 | | UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), |
278 | | UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b), |
279 | | UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), |
280 | | UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30), |
281 | | UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), |
282 | | UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8), |
283 | | UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), |
284 | | UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8), |
285 | | UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), |
286 | | UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3), |
287 | | UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), |
288 | | UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec), |
289 | | UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), |
290 | | UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b), |
291 | | UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), |
292 | | UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178), |
293 | | UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), |
294 | | UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b), |
295 | | UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), |
296 | | UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c), |
297 | | UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), |
298 | | UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817), |
299 | | }; |
300 | | |
301 | | #define Sigma0(x) \ |
302 | 10.4M | (CRYPTO_rotr_u64((x), 28) ^ CRYPTO_rotr_u64((x), 34) ^ \ |
303 | 10.4M | CRYPTO_rotr_u64((x), 39)) |
304 | | #define Sigma1(x) \ |
305 | 10.4M | (CRYPTO_rotr_u64((x), 14) ^ CRYPTO_rotr_u64((x), 18) ^ \ |
306 | 10.4M | CRYPTO_rotr_u64((x), 41)) |
307 | | #define sigma0(x) \ |
308 | 8.34M | (CRYPTO_rotr_u64((x), 1) ^ CRYPTO_rotr_u64((x), 8) ^ ((x) >> 7)) |
309 | | #define sigma1(x) \ |
310 | 8.34M | (CRYPTO_rotr_u64((x), 19) ^ CRYPTO_rotr_u64((x), 61) ^ ((x) >> 6)) |
311 | | |
312 | 10.4M | #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) |
313 | 10.4M | #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
314 | | |
315 | | |
316 | | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) |
317 | | // This code should give better results on 32-bit CPU with less than |
318 | | // ~24 registers, both size and performance wise... |
319 | | static void sha512_block_data_order_nohw(uint64_t state[8], const uint8_t *in, |
320 | | size_t num) { |
321 | | uint64_t A, E, T; |
322 | | uint64_t X[9 + 80], *F; |
323 | | int i; |
324 | | |
325 | | while (num--) { |
326 | | F = X + 80; |
327 | | A = state[0]; |
328 | | F[1] = state[1]; |
329 | | F[2] = state[2]; |
330 | | F[3] = state[3]; |
331 | | E = state[4]; |
332 | | F[5] = state[5]; |
333 | | F[6] = state[6]; |
334 | | F[7] = state[7]; |
335 | | |
336 | | for (i = 0; i < 16; i++, F--) { |
337 | | T = CRYPTO_load_u64_be(in + i * 8); |
338 | | F[0] = A; |
339 | | F[4] = E; |
340 | | F[8] = T; |
341 | | T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; |
342 | | E = F[3] + T; |
343 | | A = T + Sigma0(A) + Maj(A, F[1], F[2]); |
344 | | } |
345 | | |
346 | | for (; i < 80; i++, F--) { |
347 | | T = sigma0(F[8 + 16 - 1]); |
348 | | T += sigma1(F[8 + 16 - 14]); |
349 | | T += F[8 + 16] + F[8 + 16 - 9]; |
350 | | |
351 | | F[0] = A; |
352 | | F[4] = E; |
353 | | F[8] = T; |
354 | | T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i]; |
355 | | E = F[3] + T; |
356 | | A = T + Sigma0(A) + Maj(A, F[1], F[2]); |
357 | | } |
358 | | |
359 | | state[0] += A; |
360 | | state[1] += F[1]; |
361 | | state[2] += F[2]; |
362 | | state[3] += F[3]; |
363 | | state[4] += E; |
364 | | state[5] += F[5]; |
365 | | state[6] += F[6]; |
366 | | state[7] += F[7]; |
367 | | |
368 | | in += 16 * 8; |
369 | | } |
370 | | } |
371 | | |
372 | | #else |
373 | | |
374 | | #define ROUND_00_15(i, a, b, c, d, e, f, g, h) \ |
375 | 10.4M | do { \ |
376 | 10.4M | T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i]; \ |
377 | 10.4M | h = Sigma0(a) + Maj(a, b, c); \ |
378 | 10.4M | d += T1; \ |
379 | 10.4M | h += T1; \ |
380 | 10.4M | } while (0) |
381 | | |
382 | | #define ROUND_16_80(i, j, a, b, c, d, e, f, g, h, X) \ |
383 | 8.34M | do { \ |
384 | 8.34M | s0 = X[(j + 1) & 0x0f]; \ |
385 | 8.34M | s0 = sigma0(s0); \ |
386 | 8.34M | s1 = X[(j + 14) & 0x0f]; \ |
387 | 8.34M | s1 = sigma1(s1); \ |
388 | 8.34M | T1 = X[(j) & 0x0f] += s0 + s1 + X[(j + 9) & 0x0f]; \ |
389 | 8.34M | ROUND_00_15(i + j, a, b, c, d, e, f, g, h); \ |
390 | 8.34M | } while (0) |
391 | | |
392 | | static void sha512_block_data_order_nohw(uint64_t state[8], const uint8_t *in, |
393 | 8.67k | size_t num) { |
394 | 8.67k | uint64_t a, b, c, d, e, f, g, h, s0, s1, T1; |
395 | 8.67k | uint64_t X[16]; |
396 | 8.67k | int i; |
397 | | |
398 | 138k | while (num--) { |
399 | 130k | a = state[0]; |
400 | 130k | b = state[1]; |
401 | 130k | c = state[2]; |
402 | 130k | d = state[3]; |
403 | 130k | e = state[4]; |
404 | 130k | f = state[5]; |
405 | 130k | g = state[6]; |
406 | 130k | h = state[7]; |
407 | | |
408 | 130k | T1 = X[0] = CRYPTO_load_u64_be(in); |
409 | 130k | ROUND_00_15(0, a, b, c, d, e, f, g, h); |
410 | 130k | T1 = X[1] = CRYPTO_load_u64_be(in + 8); |
411 | 130k | ROUND_00_15(1, h, a, b, c, d, e, f, g); |
412 | 130k | T1 = X[2] = CRYPTO_load_u64_be(in + 2 * 8); |
413 | 130k | ROUND_00_15(2, g, h, a, b, c, d, e, f); |
414 | 130k | T1 = X[3] = CRYPTO_load_u64_be(in + 3 * 8); |
415 | 130k | ROUND_00_15(3, f, g, h, a, b, c, d, e); |
416 | 130k | T1 = X[4] = CRYPTO_load_u64_be(in + 4 * 8); |
417 | 130k | ROUND_00_15(4, e, f, g, h, a, b, c, d); |
418 | 130k | T1 = X[5] = CRYPTO_load_u64_be(in + 5 * 8); |
419 | 130k | ROUND_00_15(5, d, e, f, g, h, a, b, c); |
420 | 130k | T1 = X[6] = CRYPTO_load_u64_be(in + 6 * 8); |
421 | 130k | ROUND_00_15(6, c, d, e, f, g, h, a, b); |
422 | 130k | T1 = X[7] = CRYPTO_load_u64_be(in + 7 * 8); |
423 | 130k | ROUND_00_15(7, b, c, d, e, f, g, h, a); |
424 | 130k | T1 = X[8] = CRYPTO_load_u64_be(in + 8 * 8); |
425 | 130k | ROUND_00_15(8, a, b, c, d, e, f, g, h); |
426 | 130k | T1 = X[9] = CRYPTO_load_u64_be(in + 9 * 8); |
427 | 130k | ROUND_00_15(9, h, a, b, c, d, e, f, g); |
428 | 130k | T1 = X[10] = CRYPTO_load_u64_be(in + 10 * 8); |
429 | 130k | ROUND_00_15(10, g, h, a, b, c, d, e, f); |
430 | 130k | T1 = X[11] = CRYPTO_load_u64_be(in + 11 * 8); |
431 | 130k | ROUND_00_15(11, f, g, h, a, b, c, d, e); |
432 | 130k | T1 = X[12] = CRYPTO_load_u64_be(in + 12 * 8); |
433 | 130k | ROUND_00_15(12, e, f, g, h, a, b, c, d); |
434 | 130k | T1 = X[13] = CRYPTO_load_u64_be(in + 13 * 8); |
435 | 130k | ROUND_00_15(13, d, e, f, g, h, a, b, c); |
436 | 130k | T1 = X[14] = CRYPTO_load_u64_be(in + 14 * 8); |
437 | 130k | ROUND_00_15(14, c, d, e, f, g, h, a, b); |
438 | 130k | T1 = X[15] = CRYPTO_load_u64_be(in + 15 * 8); |
439 | 130k | ROUND_00_15(15, b, c, d, e, f, g, h, a); |
440 | | |
441 | 651k | for (i = 16; i < 80; i += 16) { |
442 | 521k | ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X); |
443 | 521k | ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X); |
444 | 521k | ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X); |
445 | 521k | ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X); |
446 | 521k | ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X); |
447 | 521k | ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X); |
448 | 521k | ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X); |
449 | 521k | ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X); |
450 | 521k | ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X); |
451 | 521k | ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X); |
452 | 521k | ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X); |
453 | 521k | ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X); |
454 | 521k | ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X); |
455 | 521k | ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X); |
456 | 521k | ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X); |
457 | 521k | ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X); |
458 | 521k | } |
459 | | |
460 | 130k | state[0] += a; |
461 | 130k | state[1] += b; |
462 | 130k | state[2] += c; |
463 | 130k | state[3] += d; |
464 | 130k | state[4] += e; |
465 | 130k | state[5] += f; |
466 | 130k | state[6] += g; |
467 | 130k | state[7] += h; |
468 | | |
469 | 130k | in += 16 * 8; |
470 | 130k | } |
471 | 8.67k | } |
472 | | |
473 | | #endif |
474 | | |
475 | | #endif // !SHA512_ASM_NOHW |
476 | | |
477 | | static void sha512_block_data_order(uint64_t state[8], const uint8_t *data, |
478 | 8.67k | size_t num) { |
479 | | #if defined(SHA512_ASM_HW) |
480 | | if (sha512_hw_capable()) { |
481 | | sha512_block_data_order_hw(state, data, num); |
482 | | return; |
483 | | } |
484 | | #endif |
485 | | #if defined(SHA512_ASM_AVX) |
486 | | if (sha512_avx_capable()) { |
487 | | sha512_block_data_order_avx(state, data, num); |
488 | | return; |
489 | | } |
490 | | #endif |
491 | | #if defined(SHA512_ASM_SSSE3) |
492 | | if (sha512_ssse3_capable()) { |
493 | | sha512_block_data_order_ssse3(state, data, num); |
494 | | return; |
495 | | } |
496 | | #endif |
497 | | #if defined(SHA512_ASM_NEON) |
498 | | if (CRYPTO_is_NEON_capable()) { |
499 | | sha512_block_data_order_neon(state, data, num); |
500 | | return; |
501 | | } |
502 | | #endif |
503 | 8.67k | sha512_block_data_order_nohw(state, data, num); |
504 | 8.67k | } |
505 | | |
506 | | #endif // !SHA512_ASM |
507 | | |
508 | | #undef Sigma0 |
509 | | #undef Sigma1 |
510 | | #undef sigma0 |
511 | | #undef sigma1 |
512 | | #undef Ch |
513 | | #undef Maj |
514 | | #undef ROUND_00_15 |
515 | | #undef ROUND_16_80 |