xref: /third_party/openssl/crypto/sha/sha512.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * SHA512 low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <stdio.h>
17e1051a39Sopenharmony_ci#include <openssl/opensslconf.h>
18e1051a39Sopenharmony_ci/*-
19e1051a39Sopenharmony_ci * IMPLEMENTATION NOTES.
20e1051a39Sopenharmony_ci *
21e1051a39Sopenharmony_ci * As you might have noticed 32-bit hash algorithms:
22e1051a39Sopenharmony_ci *
23e1051a39Sopenharmony_ci * - permit SHA_LONG to be wider than 32-bit
24e1051a39Sopenharmony_ci * - optimized versions implement two transform functions: one operating
25e1051a39Sopenharmony_ci *   on [aligned] data in host byte order and one - on data in input
26e1051a39Sopenharmony_ci *   stream byte order;
27e1051a39Sopenharmony_ci * - share common byte-order neutral collector and padding function
28e1051a39Sopenharmony_ci *   implementations, ../md32_common.h;
29e1051a39Sopenharmony_ci *
30e1051a39Sopenharmony_ci * Neither of the above applies to this SHA-512 implementations. Reasons
31e1051a39Sopenharmony_ci * [in reverse order] are:
32e1051a39Sopenharmony_ci *
33e1051a39Sopenharmony_ci * - it's the only 64-bit hash algorithm for the moment of this writing,
34e1051a39Sopenharmony_ci *   there is no need for common collector/padding implementation [yet];
35e1051a39Sopenharmony_ci * - by supporting only one transform function [which operates on
36e1051a39Sopenharmony_ci *   *aligned* data in input stream byte order, big-endian in this case]
37e1051a39Sopenharmony_ci *   we minimize burden of maintenance in two ways: a) collector/padding
38e1051a39Sopenharmony_ci *   function is simpler; b) only one transform function to stare at;
39e1051a39Sopenharmony_ci * - SHA_LONG64 is required to be exactly 64-bit in order to be able to
40e1051a39Sopenharmony_ci *   apply a number of optimizations to mitigate potential performance
41e1051a39Sopenharmony_ci *   penalties caused by previous design decision;
42e1051a39Sopenharmony_ci *
43e1051a39Sopenharmony_ci * Caveat lector.
44e1051a39Sopenharmony_ci *
45e1051a39Sopenharmony_ci * Implementation relies on the fact that "long long" is 64-bit on
46e1051a39Sopenharmony_ci * both 32- and 64-bit platforms. If some compiler vendor comes up
47e1051a39Sopenharmony_ci * with 128-bit long long, adjustment to sha.h would be required.
48e1051a39Sopenharmony_ci * As this implementation relies on 64-bit integer type, it's totally
49e1051a39Sopenharmony_ci * inappropriate for platforms which don't support it, most notably
50e1051a39Sopenharmony_ci * 16-bit platforms.
51e1051a39Sopenharmony_ci */
52e1051a39Sopenharmony_ci#include <stdlib.h>
53e1051a39Sopenharmony_ci#include <string.h>
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci#include <openssl/crypto.h>
56e1051a39Sopenharmony_ci#include <openssl/sha.h>
57e1051a39Sopenharmony_ci#include <openssl/opensslv.h>
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
60e1051a39Sopenharmony_ci#include "crypto/sha.h"
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
63e1051a39Sopenharmony_ci    defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \
64e1051a39Sopenharmony_ci    defined(__s390__) || defined(__s390x__) || \
65e1051a39Sopenharmony_ci    defined(__aarch64__) || \
66e1051a39Sopenharmony_ci    defined(SHA512_ASM)
67e1051a39Sopenharmony_ci# define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
68e1051a39Sopenharmony_ci#endif
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ci#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
71e1051a39Sopenharmony_ci# define U64(C)     C##UI64
72e1051a39Sopenharmony_ci#elif defined(__arch64__)
73e1051a39Sopenharmony_ci# define U64(C)     C##UL
74e1051a39Sopenharmony_ci#else
75e1051a39Sopenharmony_ci# define U64(C)     C##ULL
76e1051a39Sopenharmony_ci#endif
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ciint sha512_224_init(SHA512_CTX *c)
79e1051a39Sopenharmony_ci{
80e1051a39Sopenharmony_ci    c->h[0] = U64(0x8c3d37c819544da2);
81e1051a39Sopenharmony_ci    c->h[1] = U64(0x73e1996689dcd4d6);
82e1051a39Sopenharmony_ci    c->h[2] = U64(0x1dfab7ae32ff9c82);
83e1051a39Sopenharmony_ci    c->h[3] = U64(0x679dd514582f9fcf);
84e1051a39Sopenharmony_ci    c->h[4] = U64(0x0f6d2b697bd44da8);
85e1051a39Sopenharmony_ci    c->h[5] = U64(0x77e36f7304c48942);
86e1051a39Sopenharmony_ci    c->h[6] = U64(0x3f9d85a86a1d36c8);
87e1051a39Sopenharmony_ci    c->h[7] = U64(0x1112e6ad91d692a1);
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    c->Nl = 0;
90e1051a39Sopenharmony_ci    c->Nh = 0;
91e1051a39Sopenharmony_ci    c->num = 0;
92e1051a39Sopenharmony_ci    c->md_len = SHA224_DIGEST_LENGTH;
93e1051a39Sopenharmony_ci    return 1;
94e1051a39Sopenharmony_ci}
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ciint sha512_256_init(SHA512_CTX *c)
97e1051a39Sopenharmony_ci{
98e1051a39Sopenharmony_ci    c->h[0] = U64(0x22312194fc2bf72c);
99e1051a39Sopenharmony_ci    c->h[1] = U64(0x9f555fa3c84c64c2);
100e1051a39Sopenharmony_ci    c->h[2] = U64(0x2393b86b6f53b151);
101e1051a39Sopenharmony_ci    c->h[3] = U64(0x963877195940eabd);
102e1051a39Sopenharmony_ci    c->h[4] = U64(0x96283ee2a88effe3);
103e1051a39Sopenharmony_ci    c->h[5] = U64(0xbe5e1e2553863992);
104e1051a39Sopenharmony_ci    c->h[6] = U64(0x2b0199fc2c85b8aa);
105e1051a39Sopenharmony_ci    c->h[7] = U64(0x0eb72ddc81c52ca2);
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci    c->Nl = 0;
108e1051a39Sopenharmony_ci    c->Nh = 0;
109e1051a39Sopenharmony_ci    c->num = 0;
110e1051a39Sopenharmony_ci    c->md_len = SHA256_DIGEST_LENGTH;
111e1051a39Sopenharmony_ci    return 1;
112e1051a39Sopenharmony_ci}
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ciint SHA384_Init(SHA512_CTX *c)
115e1051a39Sopenharmony_ci{
116e1051a39Sopenharmony_ci    c->h[0] = U64(0xcbbb9d5dc1059ed8);
117e1051a39Sopenharmony_ci    c->h[1] = U64(0x629a292a367cd507);
118e1051a39Sopenharmony_ci    c->h[2] = U64(0x9159015a3070dd17);
119e1051a39Sopenharmony_ci    c->h[3] = U64(0x152fecd8f70e5939);
120e1051a39Sopenharmony_ci    c->h[4] = U64(0x67332667ffc00b31);
121e1051a39Sopenharmony_ci    c->h[5] = U64(0x8eb44a8768581511);
122e1051a39Sopenharmony_ci    c->h[6] = U64(0xdb0c2e0d64f98fa7);
123e1051a39Sopenharmony_ci    c->h[7] = U64(0x47b5481dbefa4fa4);
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ci    c->Nl = 0;
126e1051a39Sopenharmony_ci    c->Nh = 0;
127e1051a39Sopenharmony_ci    c->num = 0;
128e1051a39Sopenharmony_ci    c->md_len = SHA384_DIGEST_LENGTH;
129e1051a39Sopenharmony_ci    return 1;
130e1051a39Sopenharmony_ci}
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ciint SHA512_Init(SHA512_CTX *c)
133e1051a39Sopenharmony_ci{
134e1051a39Sopenharmony_ci    c->h[0] = U64(0x6a09e667f3bcc908);
135e1051a39Sopenharmony_ci    c->h[1] = U64(0xbb67ae8584caa73b);
136e1051a39Sopenharmony_ci    c->h[2] = U64(0x3c6ef372fe94f82b);
137e1051a39Sopenharmony_ci    c->h[3] = U64(0xa54ff53a5f1d36f1);
138e1051a39Sopenharmony_ci    c->h[4] = U64(0x510e527fade682d1);
139e1051a39Sopenharmony_ci    c->h[5] = U64(0x9b05688c2b3e6c1f);
140e1051a39Sopenharmony_ci    c->h[6] = U64(0x1f83d9abfb41bd6b);
141e1051a39Sopenharmony_ci    c->h[7] = U64(0x5be0cd19137e2179);
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci    c->Nl = 0;
144e1051a39Sopenharmony_ci    c->Nh = 0;
145e1051a39Sopenharmony_ci    c->num = 0;
146e1051a39Sopenharmony_ci    c->md_len = SHA512_DIGEST_LENGTH;
147e1051a39Sopenharmony_ci    return 1;
148e1051a39Sopenharmony_ci}
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ci#ifndef SHA512_ASM
151e1051a39Sopenharmony_cistatic
152e1051a39Sopenharmony_ci#endif
153e1051a39Sopenharmony_civoid sha512_block_data_order(SHA512_CTX *ctx, const void *in, size_t num);
154e1051a39Sopenharmony_ci
155e1051a39Sopenharmony_ciint SHA512_Final(unsigned char *md, SHA512_CTX *c)
156e1051a39Sopenharmony_ci{
157e1051a39Sopenharmony_ci    unsigned char *p = (unsigned char *)c->u.p;
158e1051a39Sopenharmony_ci    size_t n = c->num;
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ci    p[n] = 0x80;                /* There always is a room for one */
161e1051a39Sopenharmony_ci    n++;
162e1051a39Sopenharmony_ci    if (n > (sizeof(c->u) - 16)) {
163e1051a39Sopenharmony_ci        memset(p + n, 0, sizeof(c->u) - n);
164e1051a39Sopenharmony_ci        n = 0;
165e1051a39Sopenharmony_ci        sha512_block_data_order(c, p, 1);
166e1051a39Sopenharmony_ci    }
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci    memset(p + n, 0, sizeof(c->u) - 16 - n);
169e1051a39Sopenharmony_ci#ifdef  B_ENDIAN
170e1051a39Sopenharmony_ci    c->u.d[SHA_LBLOCK - 2] = c->Nh;
171e1051a39Sopenharmony_ci    c->u.d[SHA_LBLOCK - 1] = c->Nl;
172e1051a39Sopenharmony_ci#else
173e1051a39Sopenharmony_ci    p[sizeof(c->u) - 1] = (unsigned char)(c->Nl);
174e1051a39Sopenharmony_ci    p[sizeof(c->u) - 2] = (unsigned char)(c->Nl >> 8);
175e1051a39Sopenharmony_ci    p[sizeof(c->u) - 3] = (unsigned char)(c->Nl >> 16);
176e1051a39Sopenharmony_ci    p[sizeof(c->u) - 4] = (unsigned char)(c->Nl >> 24);
177e1051a39Sopenharmony_ci    p[sizeof(c->u) - 5] = (unsigned char)(c->Nl >> 32);
178e1051a39Sopenharmony_ci    p[sizeof(c->u) - 6] = (unsigned char)(c->Nl >> 40);
179e1051a39Sopenharmony_ci    p[sizeof(c->u) - 7] = (unsigned char)(c->Nl >> 48);
180e1051a39Sopenharmony_ci    p[sizeof(c->u) - 8] = (unsigned char)(c->Nl >> 56);
181e1051a39Sopenharmony_ci    p[sizeof(c->u) - 9] = (unsigned char)(c->Nh);
182e1051a39Sopenharmony_ci    p[sizeof(c->u) - 10] = (unsigned char)(c->Nh >> 8);
183e1051a39Sopenharmony_ci    p[sizeof(c->u) - 11] = (unsigned char)(c->Nh >> 16);
184e1051a39Sopenharmony_ci    p[sizeof(c->u) - 12] = (unsigned char)(c->Nh >> 24);
185e1051a39Sopenharmony_ci    p[sizeof(c->u) - 13] = (unsigned char)(c->Nh >> 32);
186e1051a39Sopenharmony_ci    p[sizeof(c->u) - 14] = (unsigned char)(c->Nh >> 40);
187e1051a39Sopenharmony_ci    p[sizeof(c->u) - 15] = (unsigned char)(c->Nh >> 48);
188e1051a39Sopenharmony_ci    p[sizeof(c->u) - 16] = (unsigned char)(c->Nh >> 56);
189e1051a39Sopenharmony_ci#endif
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci    sha512_block_data_order(c, p, 1);
192e1051a39Sopenharmony_ci
193e1051a39Sopenharmony_ci    if (md == 0)
194e1051a39Sopenharmony_ci        return 0;
195e1051a39Sopenharmony_ci
196e1051a39Sopenharmony_ci    switch (c->md_len) {
197e1051a39Sopenharmony_ci    /* Let compiler decide if it's appropriate to unroll... */
198e1051a39Sopenharmony_ci    case SHA224_DIGEST_LENGTH:
199e1051a39Sopenharmony_ci        for (n = 0; n < SHA224_DIGEST_LENGTH / 8; n++) {
200e1051a39Sopenharmony_ci            SHA_LONG64 t = c->h[n];
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 56);
203e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 48);
204e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 40);
205e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 32);
206e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 24);
207e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 16);
208e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 8);
209e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t);
210e1051a39Sopenharmony_ci        }
211e1051a39Sopenharmony_ci        /*
212e1051a39Sopenharmony_ci         * For 224 bits, there are four bytes left over that have to be
213e1051a39Sopenharmony_ci         * processed separately.
214e1051a39Sopenharmony_ci         */
215e1051a39Sopenharmony_ci        {
216e1051a39Sopenharmony_ci            SHA_LONG64 t = c->h[SHA224_DIGEST_LENGTH / 8];
217e1051a39Sopenharmony_ci
218e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 56);
219e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 48);
220e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 40);
221e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 32);
222e1051a39Sopenharmony_ci        }
223e1051a39Sopenharmony_ci        break;
224e1051a39Sopenharmony_ci    case SHA256_DIGEST_LENGTH:
225e1051a39Sopenharmony_ci        for (n = 0; n < SHA256_DIGEST_LENGTH / 8; n++) {
226e1051a39Sopenharmony_ci            SHA_LONG64 t = c->h[n];
227e1051a39Sopenharmony_ci
228e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 56);
229e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 48);
230e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 40);
231e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 32);
232e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 24);
233e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 16);
234e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 8);
235e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t);
236e1051a39Sopenharmony_ci        }
237e1051a39Sopenharmony_ci        break;
238e1051a39Sopenharmony_ci    case SHA384_DIGEST_LENGTH:
239e1051a39Sopenharmony_ci        for (n = 0; n < SHA384_DIGEST_LENGTH / 8; n++) {
240e1051a39Sopenharmony_ci            SHA_LONG64 t = c->h[n];
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 56);
243e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 48);
244e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 40);
245e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 32);
246e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 24);
247e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 16);
248e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 8);
249e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t);
250e1051a39Sopenharmony_ci        }
251e1051a39Sopenharmony_ci        break;
252e1051a39Sopenharmony_ci    case SHA512_DIGEST_LENGTH:
253e1051a39Sopenharmony_ci        for (n = 0; n < SHA512_DIGEST_LENGTH / 8; n++) {
254e1051a39Sopenharmony_ci            SHA_LONG64 t = c->h[n];
255e1051a39Sopenharmony_ci
256e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 56);
257e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 48);
258e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 40);
259e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 32);
260e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 24);
261e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 16);
262e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t >> 8);
263e1051a39Sopenharmony_ci            *(md++) = (unsigned char)(t);
264e1051a39Sopenharmony_ci        }
265e1051a39Sopenharmony_ci        break;
266e1051a39Sopenharmony_ci    /* ... as well as make sure md_len is not abused. */
267e1051a39Sopenharmony_ci    default:
268e1051a39Sopenharmony_ci        return 0;
269e1051a39Sopenharmony_ci    }
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_ci    return 1;
272e1051a39Sopenharmony_ci}
273e1051a39Sopenharmony_ci
274e1051a39Sopenharmony_ciint SHA384_Final(unsigned char *md, SHA512_CTX *c)
275e1051a39Sopenharmony_ci{
276e1051a39Sopenharmony_ci    return SHA512_Final(md, c);
277e1051a39Sopenharmony_ci}
278e1051a39Sopenharmony_ci
279e1051a39Sopenharmony_ciint SHA512_Update(SHA512_CTX *c, const void *_data, size_t len)
280e1051a39Sopenharmony_ci{
281e1051a39Sopenharmony_ci    SHA_LONG64 l;
282e1051a39Sopenharmony_ci    unsigned char *p = c->u.p;
283e1051a39Sopenharmony_ci    const unsigned char *data = (const unsigned char *)_data;
284e1051a39Sopenharmony_ci
285e1051a39Sopenharmony_ci    if (len == 0)
286e1051a39Sopenharmony_ci        return 1;
287e1051a39Sopenharmony_ci
288e1051a39Sopenharmony_ci    l = (c->Nl + (((SHA_LONG64) len) << 3)) & U64(0xffffffffffffffff);
289e1051a39Sopenharmony_ci    if (l < c->Nl)
290e1051a39Sopenharmony_ci        c->Nh++;
291e1051a39Sopenharmony_ci    if (sizeof(len) >= 8)
292e1051a39Sopenharmony_ci        c->Nh += (((SHA_LONG64) len) >> 61);
293e1051a39Sopenharmony_ci    c->Nl = l;
294e1051a39Sopenharmony_ci
295e1051a39Sopenharmony_ci    if (c->num != 0) {
296e1051a39Sopenharmony_ci        size_t n = sizeof(c->u) - c->num;
297e1051a39Sopenharmony_ci
298e1051a39Sopenharmony_ci        if (len < n) {
299e1051a39Sopenharmony_ci            memcpy(p + c->num, data, len), c->num += (unsigned int)len;
300e1051a39Sopenharmony_ci            return 1;
301e1051a39Sopenharmony_ci        } else {
302e1051a39Sopenharmony_ci            memcpy(p + c->num, data, n), c->num = 0;
303e1051a39Sopenharmony_ci            len -= n, data += n;
304e1051a39Sopenharmony_ci            sha512_block_data_order(c, p, 1);
305e1051a39Sopenharmony_ci        }
306e1051a39Sopenharmony_ci    }
307e1051a39Sopenharmony_ci
308e1051a39Sopenharmony_ci    if (len >= sizeof(c->u)) {
309e1051a39Sopenharmony_ci#ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
310e1051a39Sopenharmony_ci        if ((size_t)data % sizeof(c->u.d[0]) != 0)
311e1051a39Sopenharmony_ci            while (len >= sizeof(c->u))
312e1051a39Sopenharmony_ci                memcpy(p, data, sizeof(c->u)),
313e1051a39Sopenharmony_ci                sha512_block_data_order(c, p, 1),
314e1051a39Sopenharmony_ci                len -= sizeof(c->u), data += sizeof(c->u);
315e1051a39Sopenharmony_ci        else
316e1051a39Sopenharmony_ci#endif
317e1051a39Sopenharmony_ci            sha512_block_data_order(c, data, len / sizeof(c->u)),
318e1051a39Sopenharmony_ci            data += len, len %= sizeof(c->u), data -= len;
319e1051a39Sopenharmony_ci    }
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    if (len != 0)
322e1051a39Sopenharmony_ci        memcpy(p, data, len), c->num = (int)len;
323e1051a39Sopenharmony_ci
324e1051a39Sopenharmony_ci    return 1;
325e1051a39Sopenharmony_ci}
326e1051a39Sopenharmony_ci
327e1051a39Sopenharmony_ciint SHA384_Update(SHA512_CTX *c, const void *data, size_t len)
328e1051a39Sopenharmony_ci{
329e1051a39Sopenharmony_ci    return SHA512_Update(c, data, len);
330e1051a39Sopenharmony_ci}
331e1051a39Sopenharmony_ci
332e1051a39Sopenharmony_civoid SHA512_Transform(SHA512_CTX *c, const unsigned char *data)
333e1051a39Sopenharmony_ci{
334e1051a39Sopenharmony_ci#ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
335e1051a39Sopenharmony_ci    if ((size_t)data % sizeof(c->u.d[0]) != 0)
336e1051a39Sopenharmony_ci        memcpy(c->u.p, data, sizeof(c->u.p)), data = c->u.p;
337e1051a39Sopenharmony_ci#endif
338e1051a39Sopenharmony_ci    sha512_block_data_order(c, data, 1);
339e1051a39Sopenharmony_ci}
340e1051a39Sopenharmony_ci
341e1051a39Sopenharmony_ci#ifndef SHA512_ASM
342e1051a39Sopenharmony_cistatic const SHA_LONG64 K512[80] = {
343e1051a39Sopenharmony_ci    U64(0x428a2f98d728ae22), U64(0x7137449123ef65cd),
344e1051a39Sopenharmony_ci    U64(0xb5c0fbcfec4d3b2f), U64(0xe9b5dba58189dbbc),
345e1051a39Sopenharmony_ci    U64(0x3956c25bf348b538), U64(0x59f111f1b605d019),
346e1051a39Sopenharmony_ci    U64(0x923f82a4af194f9b), U64(0xab1c5ed5da6d8118),
347e1051a39Sopenharmony_ci    U64(0xd807aa98a3030242), U64(0x12835b0145706fbe),
348e1051a39Sopenharmony_ci    U64(0x243185be4ee4b28c), U64(0x550c7dc3d5ffb4e2),
349e1051a39Sopenharmony_ci    U64(0x72be5d74f27b896f), U64(0x80deb1fe3b1696b1),
350e1051a39Sopenharmony_ci    U64(0x9bdc06a725c71235), U64(0xc19bf174cf692694),
351e1051a39Sopenharmony_ci    U64(0xe49b69c19ef14ad2), U64(0xefbe4786384f25e3),
352e1051a39Sopenharmony_ci    U64(0x0fc19dc68b8cd5b5), U64(0x240ca1cc77ac9c65),
353e1051a39Sopenharmony_ci    U64(0x2de92c6f592b0275), U64(0x4a7484aa6ea6e483),
354e1051a39Sopenharmony_ci    U64(0x5cb0a9dcbd41fbd4), U64(0x76f988da831153b5),
355e1051a39Sopenharmony_ci    U64(0x983e5152ee66dfab), U64(0xa831c66d2db43210),
356e1051a39Sopenharmony_ci    U64(0xb00327c898fb213f), U64(0xbf597fc7beef0ee4),
357e1051a39Sopenharmony_ci    U64(0xc6e00bf33da88fc2), U64(0xd5a79147930aa725),
358e1051a39Sopenharmony_ci    U64(0x06ca6351e003826f), U64(0x142929670a0e6e70),
359e1051a39Sopenharmony_ci    U64(0x27b70a8546d22ffc), U64(0x2e1b21385c26c926),
360e1051a39Sopenharmony_ci    U64(0x4d2c6dfc5ac42aed), U64(0x53380d139d95b3df),
361e1051a39Sopenharmony_ci    U64(0x650a73548baf63de), U64(0x766a0abb3c77b2a8),
362e1051a39Sopenharmony_ci    U64(0x81c2c92e47edaee6), U64(0x92722c851482353b),
363e1051a39Sopenharmony_ci    U64(0xa2bfe8a14cf10364), U64(0xa81a664bbc423001),
364e1051a39Sopenharmony_ci    U64(0xc24b8b70d0f89791), U64(0xc76c51a30654be30),
365e1051a39Sopenharmony_ci    U64(0xd192e819d6ef5218), U64(0xd69906245565a910),
366e1051a39Sopenharmony_ci    U64(0xf40e35855771202a), U64(0x106aa07032bbd1b8),
367e1051a39Sopenharmony_ci    U64(0x19a4c116b8d2d0c8), U64(0x1e376c085141ab53),
368e1051a39Sopenharmony_ci    U64(0x2748774cdf8eeb99), U64(0x34b0bcb5e19b48a8),
369e1051a39Sopenharmony_ci    U64(0x391c0cb3c5c95a63), U64(0x4ed8aa4ae3418acb),
370e1051a39Sopenharmony_ci    U64(0x5b9cca4f7763e373), U64(0x682e6ff3d6b2b8a3),
371e1051a39Sopenharmony_ci    U64(0x748f82ee5defb2fc), U64(0x78a5636f43172f60),
372e1051a39Sopenharmony_ci    U64(0x84c87814a1f0ab72), U64(0x8cc702081a6439ec),
373e1051a39Sopenharmony_ci    U64(0x90befffa23631e28), U64(0xa4506cebde82bde9),
374e1051a39Sopenharmony_ci    U64(0xbef9a3f7b2c67915), U64(0xc67178f2e372532b),
375e1051a39Sopenharmony_ci    U64(0xca273eceea26619c), U64(0xd186b8c721c0c207),
376e1051a39Sopenharmony_ci    U64(0xeada7dd6cde0eb1e), U64(0xf57d4f7fee6ed178),
377e1051a39Sopenharmony_ci    U64(0x06f067aa72176fba), U64(0x0a637dc5a2c898a6),
378e1051a39Sopenharmony_ci    U64(0x113f9804bef90dae), U64(0x1b710b35131c471b),
379e1051a39Sopenharmony_ci    U64(0x28db77f523047d84), U64(0x32caab7b40c72493),
380e1051a39Sopenharmony_ci    U64(0x3c9ebe0a15c9bebc), U64(0x431d67c49c100d4c),
381e1051a39Sopenharmony_ci    U64(0x4cc5d4becb3e42b6), U64(0x597f299cfc657e2a),
382e1051a39Sopenharmony_ci    U64(0x5fcb6fab3ad6faec), U64(0x6c44198c4a475817)
383e1051a39Sopenharmony_ci};
384e1051a39Sopenharmony_ci
385e1051a39Sopenharmony_ci# ifndef PEDANTIC
386e1051a39Sopenharmony_ci#  if defined(__GNUC__) && __GNUC__>=2 && \
387e1051a39Sopenharmony_ci      !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
388e1051a39Sopenharmony_ci#   if defined(__x86_64) || defined(__x86_64__)
389e1051a39Sopenharmony_ci#    define ROTR(a,n)    ({ SHA_LONG64 ret;             \
390e1051a39Sopenharmony_ci                                asm ("rorq %1,%0"       \
391e1051a39Sopenharmony_ci                                : "=r"(ret)             \
392e1051a39Sopenharmony_ci                                : "J"(n),"0"(a)         \
393e1051a39Sopenharmony_ci                                : "cc"); ret;           })
394e1051a39Sopenharmony_ci#    if !defined(B_ENDIAN)
395e1051a39Sopenharmony_ci#     define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x)));  \
396e1051a39Sopenharmony_ci                                asm ("bswapq    %0"             \
397e1051a39Sopenharmony_ci                                : "=r"(ret)                     \
398e1051a39Sopenharmony_ci                                : "0"(ret)); ret;               })
399e1051a39Sopenharmony_ci#    endif
400e1051a39Sopenharmony_ci#   elif (defined(__i386) || defined(__i386__)) && !defined(B_ENDIAN)
401e1051a39Sopenharmony_ci#    if defined(I386_ONLY)
402e1051a39Sopenharmony_ci#     define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
403e1051a39Sopenharmony_ci                          unsigned int hi=p[0],lo=p[1];          \
404e1051a39Sopenharmony_ci                                asm("xchgb %%ah,%%al;xchgb %%dh,%%dl;"\
405e1051a39Sopenharmony_ci                                    "roll $16,%%eax; roll $16,%%edx; "\
406e1051a39Sopenharmony_ci                                    "xchgb %%ah,%%al;xchgb %%dh,%%dl;"\
407e1051a39Sopenharmony_ci                                : "=a"(lo),"=d"(hi)             \
408e1051a39Sopenharmony_ci                                : "0"(lo),"1"(hi) : "cc");      \
409e1051a39Sopenharmony_ci                                ((SHA_LONG64)hi)<<32|lo;        })
410e1051a39Sopenharmony_ci#    else
411e1051a39Sopenharmony_ci#     define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
412e1051a39Sopenharmony_ci                          unsigned int hi=p[0],lo=p[1];         \
413e1051a39Sopenharmony_ci                                asm ("bswapl %0; bswapl %1;"    \
414e1051a39Sopenharmony_ci                                : "=r"(lo),"=r"(hi)             \
415e1051a39Sopenharmony_ci                                : "0"(lo),"1"(hi));             \
416e1051a39Sopenharmony_ci                                ((SHA_LONG64)hi)<<32|lo;        })
417e1051a39Sopenharmony_ci#    endif
418e1051a39Sopenharmony_ci#   elif (defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64)
419e1051a39Sopenharmony_ci#    define ROTR(a,n)    ({ SHA_LONG64 ret;             \
420e1051a39Sopenharmony_ci                                asm ("rotrdi %0,%1,%2"  \
421e1051a39Sopenharmony_ci                                : "=r"(ret)             \
422e1051a39Sopenharmony_ci                                : "r"(a),"K"(n)); ret;  })
423e1051a39Sopenharmony_ci#   elif defined(__aarch64__)
424e1051a39Sopenharmony_ci#    define ROTR(a,n)    ({ SHA_LONG64 ret;             \
425e1051a39Sopenharmony_ci                                asm ("ror %0,%1,%2"     \
426e1051a39Sopenharmony_ci                                : "=r"(ret)             \
427e1051a39Sopenharmony_ci                                : "r"(a),"I"(n)); ret;  })
428e1051a39Sopenharmony_ci#    if  defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
429e1051a39Sopenharmony_ci        __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
430e1051a39Sopenharmony_ci#     define PULL64(x)   ({ SHA_LONG64 ret;                     \
431e1051a39Sopenharmony_ci                                asm ("rev       %0,%1"          \
432e1051a39Sopenharmony_ci                                : "=r"(ret)                     \
433e1051a39Sopenharmony_ci                                : "r"(*((const SHA_LONG64 *)(&(x))))); ret; })
434e1051a39Sopenharmony_ci#    endif
435e1051a39Sopenharmony_ci#   endif
436e1051a39Sopenharmony_ci#  elif defined(_MSC_VER)
437e1051a39Sopenharmony_ci#   if defined(_WIN64)         /* applies to both IA-64 and AMD64 */
438e1051a39Sopenharmony_ci#    pragma intrinsic(_rotr64)
439e1051a39Sopenharmony_ci#    define ROTR(a,n)    _rotr64((a),n)
440e1051a39Sopenharmony_ci#   endif
441e1051a39Sopenharmony_ci#   if defined(_M_IX86) && !defined(OPENSSL_NO_ASM) && \
442e1051a39Sopenharmony_ci       !defined(OPENSSL_NO_INLINE_ASM)
443e1051a39Sopenharmony_ci#    if defined(I386_ONLY)
444e1051a39Sopenharmony_cistatic SHA_LONG64 __fastcall __pull64be(const void *x)
445e1051a39Sopenharmony_ci{
446e1051a39Sopenharmony_ci    _asm mov  edx,[ecx + 0]
447e1051a39Sopenharmony_ci    _asm mov  eax,[ecx + 4]
448e1051a39Sopenharmony_ci    _asm xchg dh, dl
449e1051a39Sopenharmony_ci    _asm xchg ah, al
450e1051a39Sopenharmony_ci    _asm rol  edx, 16
451e1051a39Sopenharmony_ci    _asm rol  eax, 16
452e1051a39Sopenharmony_ci    _asm xchg dh, dl
453e1051a39Sopenharmony_ci    _asm xchg ah, al
454e1051a39Sopenharmony_ci}
455e1051a39Sopenharmony_ci#    else
456e1051a39Sopenharmony_cistatic SHA_LONG64 __fastcall __pull64be(const void *x)
457e1051a39Sopenharmony_ci{
458e1051a39Sopenharmony_ci    _asm mov   edx,[ecx + 0]
459e1051a39Sopenharmony_ci    _asm mov   eax,[ecx + 4]
460e1051a39Sopenharmony_ci    _asm bswap edx
461e1051a39Sopenharmony_ci    _asm bswap eax
462e1051a39Sopenharmony_ci}
463e1051a39Sopenharmony_ci#    endif
464e1051a39Sopenharmony_ci#    define PULL64(x) __pull64be(&(x))
465e1051a39Sopenharmony_ci#   endif
466e1051a39Sopenharmony_ci#  endif
467e1051a39Sopenharmony_ci# endif
468e1051a39Sopenharmony_ci# ifndef PULL64
469e1051a39Sopenharmony_ci#  define B(x,j)    (((SHA_LONG64)(*(((const unsigned char *)(&x))+j)))<<((7-j)*8))
470e1051a39Sopenharmony_ci#  define PULL64(x) (B(x,0)|B(x,1)|B(x,2)|B(x,3)|B(x,4)|B(x,5)|B(x,6)|B(x,7))
471e1051a39Sopenharmony_ci# endif
472e1051a39Sopenharmony_ci# ifndef ROTR
473e1051a39Sopenharmony_ci#  define ROTR(x,s)       (((x)>>s) | (x)<<(64-s))
474e1051a39Sopenharmony_ci# endif
475e1051a39Sopenharmony_ci# define Sigma0(x)       (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
476e1051a39Sopenharmony_ci# define Sigma1(x)       (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
477e1051a39Sopenharmony_ci# define sigma0(x)       (ROTR((x),1)  ^ ROTR((x),8)  ^ ((x)>>7))
478e1051a39Sopenharmony_ci# define sigma1(x)       (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
479e1051a39Sopenharmony_ci# define Ch(x,y,z)       (((x) & (y)) ^ ((~(x)) & (z)))
480e1051a39Sopenharmony_ci# define Maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
481e1051a39Sopenharmony_ci
482e1051a39Sopenharmony_ci# if defined(__i386) || defined(__i386__) || defined(_M_IX86)
483e1051a39Sopenharmony_ci/*
484e1051a39Sopenharmony_ci * This code should give better results on 32-bit CPU with less than
485e1051a39Sopenharmony_ci * ~24 registers, both size and performance wise...
486e1051a39Sopenharmony_ci */
487e1051a39Sopenharmony_ci
488e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in,
489e1051a39Sopenharmony_ci                                    size_t num)
490e1051a39Sopenharmony_ci{
491e1051a39Sopenharmony_ci    const SHA_LONG64 *W = in;
492e1051a39Sopenharmony_ci    SHA_LONG64 A, E, T;
493e1051a39Sopenharmony_ci    SHA_LONG64 X[9 + 80], *F;
494e1051a39Sopenharmony_ci    int i;
495e1051a39Sopenharmony_ci
496e1051a39Sopenharmony_ci    while (num--) {
497e1051a39Sopenharmony_ci
498e1051a39Sopenharmony_ci        F = X + 80;
499e1051a39Sopenharmony_ci        A = ctx->h[0];
500e1051a39Sopenharmony_ci        F[1] = ctx->h[1];
501e1051a39Sopenharmony_ci        F[2] = ctx->h[2];
502e1051a39Sopenharmony_ci        F[3] = ctx->h[3];
503e1051a39Sopenharmony_ci        E = ctx->h[4];
504e1051a39Sopenharmony_ci        F[5] = ctx->h[5];
505e1051a39Sopenharmony_ci        F[6] = ctx->h[6];
506e1051a39Sopenharmony_ci        F[7] = ctx->h[7];
507e1051a39Sopenharmony_ci
508e1051a39Sopenharmony_ci        for (i = 0; i < 16; i++, F--) {
509e1051a39Sopenharmony_ci#  ifdef B_ENDIAN
510e1051a39Sopenharmony_ci            T = W[i];
511e1051a39Sopenharmony_ci#  else
512e1051a39Sopenharmony_ci            T = PULL64(W[i]);
513e1051a39Sopenharmony_ci#  endif
514e1051a39Sopenharmony_ci            F[0] = A;
515e1051a39Sopenharmony_ci            F[4] = E;
516e1051a39Sopenharmony_ci            F[8] = T;
517e1051a39Sopenharmony_ci            T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i];
518e1051a39Sopenharmony_ci            E = F[3] + T;
519e1051a39Sopenharmony_ci            A = T + Sigma0(A) + Maj(A, F[1], F[2]);
520e1051a39Sopenharmony_ci        }
521e1051a39Sopenharmony_ci
522e1051a39Sopenharmony_ci        for (; i < 80; i++, F--) {
523e1051a39Sopenharmony_ci            T = sigma0(F[8 + 16 - 1]);
524e1051a39Sopenharmony_ci            T += sigma1(F[8 + 16 - 14]);
525e1051a39Sopenharmony_ci            T += F[8 + 16] + F[8 + 16 - 9];
526e1051a39Sopenharmony_ci
527e1051a39Sopenharmony_ci            F[0] = A;
528e1051a39Sopenharmony_ci            F[4] = E;
529e1051a39Sopenharmony_ci            F[8] = T;
530e1051a39Sopenharmony_ci            T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i];
531e1051a39Sopenharmony_ci            E = F[3] + T;
532e1051a39Sopenharmony_ci            A = T + Sigma0(A) + Maj(A, F[1], F[2]);
533e1051a39Sopenharmony_ci        }
534e1051a39Sopenharmony_ci
535e1051a39Sopenharmony_ci        ctx->h[0] += A;
536e1051a39Sopenharmony_ci        ctx->h[1] += F[1];
537e1051a39Sopenharmony_ci        ctx->h[2] += F[2];
538e1051a39Sopenharmony_ci        ctx->h[3] += F[3];
539e1051a39Sopenharmony_ci        ctx->h[4] += E;
540e1051a39Sopenharmony_ci        ctx->h[5] += F[5];
541e1051a39Sopenharmony_ci        ctx->h[6] += F[6];
542e1051a39Sopenharmony_ci        ctx->h[7] += F[7];
543e1051a39Sopenharmony_ci
544e1051a39Sopenharmony_ci        W += SHA_LBLOCK;
545e1051a39Sopenharmony_ci    }
546e1051a39Sopenharmony_ci}
547e1051a39Sopenharmony_ci
548e1051a39Sopenharmony_ci# elif defined(OPENSSL_SMALL_FOOTPRINT)
549e1051a39Sopenharmony_ci
550e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in,
551e1051a39Sopenharmony_ci                                    size_t num)
552e1051a39Sopenharmony_ci{
553e1051a39Sopenharmony_ci    const SHA_LONG64 *W = in;
554e1051a39Sopenharmony_ci    SHA_LONG64 a, b, c, d, e, f, g, h, s0, s1, T1, T2;
555e1051a39Sopenharmony_ci    SHA_LONG64 X[16];
556e1051a39Sopenharmony_ci    int i;
557e1051a39Sopenharmony_ci
558e1051a39Sopenharmony_ci    while (num--) {
559e1051a39Sopenharmony_ci
560e1051a39Sopenharmony_ci        a = ctx->h[0];
561e1051a39Sopenharmony_ci        b = ctx->h[1];
562e1051a39Sopenharmony_ci        c = ctx->h[2];
563e1051a39Sopenharmony_ci        d = ctx->h[3];
564e1051a39Sopenharmony_ci        e = ctx->h[4];
565e1051a39Sopenharmony_ci        f = ctx->h[5];
566e1051a39Sopenharmony_ci        g = ctx->h[6];
567e1051a39Sopenharmony_ci        h = ctx->h[7];
568e1051a39Sopenharmony_ci
569e1051a39Sopenharmony_ci        for (i = 0; i < 16; i++) {
570e1051a39Sopenharmony_ci#  ifdef B_ENDIAN
571e1051a39Sopenharmony_ci            T1 = X[i] = W[i];
572e1051a39Sopenharmony_ci#  else
573e1051a39Sopenharmony_ci            T1 = X[i] = PULL64(W[i]);
574e1051a39Sopenharmony_ci#  endif
575e1051a39Sopenharmony_ci            T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i];
576e1051a39Sopenharmony_ci            T2 = Sigma0(a) + Maj(a, b, c);
577e1051a39Sopenharmony_ci            h = g;
578e1051a39Sopenharmony_ci            g = f;
579e1051a39Sopenharmony_ci            f = e;
580e1051a39Sopenharmony_ci            e = d + T1;
581e1051a39Sopenharmony_ci            d = c;
582e1051a39Sopenharmony_ci            c = b;
583e1051a39Sopenharmony_ci            b = a;
584e1051a39Sopenharmony_ci            a = T1 + T2;
585e1051a39Sopenharmony_ci        }
586e1051a39Sopenharmony_ci
587e1051a39Sopenharmony_ci        for (; i < 80; i++) {
588e1051a39Sopenharmony_ci            s0 = X[(i + 1) & 0x0f];
589e1051a39Sopenharmony_ci            s0 = sigma0(s0);
590e1051a39Sopenharmony_ci            s1 = X[(i + 14) & 0x0f];
591e1051a39Sopenharmony_ci            s1 = sigma1(s1);
592e1051a39Sopenharmony_ci
593e1051a39Sopenharmony_ci            T1 = X[i & 0xf] += s0 + s1 + X[(i + 9) & 0xf];
594e1051a39Sopenharmony_ci            T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i];
595e1051a39Sopenharmony_ci            T2 = Sigma0(a) + Maj(a, b, c);
596e1051a39Sopenharmony_ci            h = g;
597e1051a39Sopenharmony_ci            g = f;
598e1051a39Sopenharmony_ci            f = e;
599e1051a39Sopenharmony_ci            e = d + T1;
600e1051a39Sopenharmony_ci            d = c;
601e1051a39Sopenharmony_ci            c = b;
602e1051a39Sopenharmony_ci            b = a;
603e1051a39Sopenharmony_ci            a = T1 + T2;
604e1051a39Sopenharmony_ci        }
605e1051a39Sopenharmony_ci
606e1051a39Sopenharmony_ci        ctx->h[0] += a;
607e1051a39Sopenharmony_ci        ctx->h[1] += b;
608e1051a39Sopenharmony_ci        ctx->h[2] += c;
609e1051a39Sopenharmony_ci        ctx->h[3] += d;
610e1051a39Sopenharmony_ci        ctx->h[4] += e;
611e1051a39Sopenharmony_ci        ctx->h[5] += f;
612e1051a39Sopenharmony_ci        ctx->h[6] += g;
613e1051a39Sopenharmony_ci        ctx->h[7] += h;
614e1051a39Sopenharmony_ci
615e1051a39Sopenharmony_ci        W += SHA_LBLOCK;
616e1051a39Sopenharmony_ci    }
617e1051a39Sopenharmony_ci}
618e1051a39Sopenharmony_ci
619e1051a39Sopenharmony_ci# else
620e1051a39Sopenharmony_ci#  define ROUND_00_15(i,a,b,c,d,e,f,g,h)        do {    \
621e1051a39Sopenharmony_ci        T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i];      \
622e1051a39Sopenharmony_ci        h = Sigma0(a) + Maj(a,b,c);                     \
623e1051a39Sopenharmony_ci        d += T1;        h += T1;                        } while (0)
624e1051a39Sopenharmony_ci
625e1051a39Sopenharmony_ci#  define ROUND_16_80(i,j,a,b,c,d,e,f,g,h,X)    do {    \
626e1051a39Sopenharmony_ci        s0 = X[(j+1)&0x0f];     s0 = sigma0(s0);        \
627e1051a39Sopenharmony_ci        s1 = X[(j+14)&0x0f];    s1 = sigma1(s1);        \
628e1051a39Sopenharmony_ci        T1 = X[(j)&0x0f] += s0 + s1 + X[(j+9)&0x0f];    \
629e1051a39Sopenharmony_ci        ROUND_00_15(i+j,a,b,c,d,e,f,g,h);               } while (0)
630e1051a39Sopenharmony_ci
631e1051a39Sopenharmony_cistatic void sha512_block_data_order(SHA512_CTX *ctx, const void *in,
632e1051a39Sopenharmony_ci                                    size_t num)
633e1051a39Sopenharmony_ci{
634e1051a39Sopenharmony_ci    const SHA_LONG64 *W = in;
635e1051a39Sopenharmony_ci    SHA_LONG64 a, b, c, d, e, f, g, h, s0, s1, T1;
636e1051a39Sopenharmony_ci    SHA_LONG64 X[16];
637e1051a39Sopenharmony_ci    int i;
638e1051a39Sopenharmony_ci
639e1051a39Sopenharmony_ci    while (num--) {
640e1051a39Sopenharmony_ci
641e1051a39Sopenharmony_ci        a = ctx->h[0];
642e1051a39Sopenharmony_ci        b = ctx->h[1];
643e1051a39Sopenharmony_ci        c = ctx->h[2];
644e1051a39Sopenharmony_ci        d = ctx->h[3];
645e1051a39Sopenharmony_ci        e = ctx->h[4];
646e1051a39Sopenharmony_ci        f = ctx->h[5];
647e1051a39Sopenharmony_ci        g = ctx->h[6];
648e1051a39Sopenharmony_ci        h = ctx->h[7];
649e1051a39Sopenharmony_ci
650e1051a39Sopenharmony_ci#  ifdef B_ENDIAN
651e1051a39Sopenharmony_ci        T1 = X[0] = W[0];
652e1051a39Sopenharmony_ci        ROUND_00_15(0, a, b, c, d, e, f, g, h);
653e1051a39Sopenharmony_ci        T1 = X[1] = W[1];
654e1051a39Sopenharmony_ci        ROUND_00_15(1, h, a, b, c, d, e, f, g);
655e1051a39Sopenharmony_ci        T1 = X[2] = W[2];
656e1051a39Sopenharmony_ci        ROUND_00_15(2, g, h, a, b, c, d, e, f);
657e1051a39Sopenharmony_ci        T1 = X[3] = W[3];
658e1051a39Sopenharmony_ci        ROUND_00_15(3, f, g, h, a, b, c, d, e);
659e1051a39Sopenharmony_ci        T1 = X[4] = W[4];
660e1051a39Sopenharmony_ci        ROUND_00_15(4, e, f, g, h, a, b, c, d);
661e1051a39Sopenharmony_ci        T1 = X[5] = W[5];
662e1051a39Sopenharmony_ci        ROUND_00_15(5, d, e, f, g, h, a, b, c);
663e1051a39Sopenharmony_ci        T1 = X[6] = W[6];
664e1051a39Sopenharmony_ci        ROUND_00_15(6, c, d, e, f, g, h, a, b);
665e1051a39Sopenharmony_ci        T1 = X[7] = W[7];
666e1051a39Sopenharmony_ci        ROUND_00_15(7, b, c, d, e, f, g, h, a);
667e1051a39Sopenharmony_ci        T1 = X[8] = W[8];
668e1051a39Sopenharmony_ci        ROUND_00_15(8, a, b, c, d, e, f, g, h);
669e1051a39Sopenharmony_ci        T1 = X[9] = W[9];
670e1051a39Sopenharmony_ci        ROUND_00_15(9, h, a, b, c, d, e, f, g);
671e1051a39Sopenharmony_ci        T1 = X[10] = W[10];
672e1051a39Sopenharmony_ci        ROUND_00_15(10, g, h, a, b, c, d, e, f);
673e1051a39Sopenharmony_ci        T1 = X[11] = W[11];
674e1051a39Sopenharmony_ci        ROUND_00_15(11, f, g, h, a, b, c, d, e);
675e1051a39Sopenharmony_ci        T1 = X[12] = W[12];
676e1051a39Sopenharmony_ci        ROUND_00_15(12, e, f, g, h, a, b, c, d);
677e1051a39Sopenharmony_ci        T1 = X[13] = W[13];
678e1051a39Sopenharmony_ci        ROUND_00_15(13, d, e, f, g, h, a, b, c);
679e1051a39Sopenharmony_ci        T1 = X[14] = W[14];
680e1051a39Sopenharmony_ci        ROUND_00_15(14, c, d, e, f, g, h, a, b);
681e1051a39Sopenharmony_ci        T1 = X[15] = W[15];
682e1051a39Sopenharmony_ci        ROUND_00_15(15, b, c, d, e, f, g, h, a);
683e1051a39Sopenharmony_ci#  else
684e1051a39Sopenharmony_ci        T1 = X[0] = PULL64(W[0]);
685e1051a39Sopenharmony_ci        ROUND_00_15(0, a, b, c, d, e, f, g, h);
686e1051a39Sopenharmony_ci        T1 = X[1] = PULL64(W[1]);
687e1051a39Sopenharmony_ci        ROUND_00_15(1, h, a, b, c, d, e, f, g);
688e1051a39Sopenharmony_ci        T1 = X[2] = PULL64(W[2]);
689e1051a39Sopenharmony_ci        ROUND_00_15(2, g, h, a, b, c, d, e, f);
690e1051a39Sopenharmony_ci        T1 = X[3] = PULL64(W[3]);
691e1051a39Sopenharmony_ci        ROUND_00_15(3, f, g, h, a, b, c, d, e);
692e1051a39Sopenharmony_ci        T1 = X[4] = PULL64(W[4]);
693e1051a39Sopenharmony_ci        ROUND_00_15(4, e, f, g, h, a, b, c, d);
694e1051a39Sopenharmony_ci        T1 = X[5] = PULL64(W[5]);
695e1051a39Sopenharmony_ci        ROUND_00_15(5, d, e, f, g, h, a, b, c);
696e1051a39Sopenharmony_ci        T1 = X[6] = PULL64(W[6]);
697e1051a39Sopenharmony_ci        ROUND_00_15(6, c, d, e, f, g, h, a, b);
698e1051a39Sopenharmony_ci        T1 = X[7] = PULL64(W[7]);
699e1051a39Sopenharmony_ci        ROUND_00_15(7, b, c, d, e, f, g, h, a);
700e1051a39Sopenharmony_ci        T1 = X[8] = PULL64(W[8]);
701e1051a39Sopenharmony_ci        ROUND_00_15(8, a, b, c, d, e, f, g, h);
702e1051a39Sopenharmony_ci        T1 = X[9] = PULL64(W[9]);
703e1051a39Sopenharmony_ci        ROUND_00_15(9, h, a, b, c, d, e, f, g);
704e1051a39Sopenharmony_ci        T1 = X[10] = PULL64(W[10]);
705e1051a39Sopenharmony_ci        ROUND_00_15(10, g, h, a, b, c, d, e, f);
706e1051a39Sopenharmony_ci        T1 = X[11] = PULL64(W[11]);
707e1051a39Sopenharmony_ci        ROUND_00_15(11, f, g, h, a, b, c, d, e);
708e1051a39Sopenharmony_ci        T1 = X[12] = PULL64(W[12]);
709e1051a39Sopenharmony_ci        ROUND_00_15(12, e, f, g, h, a, b, c, d);
710e1051a39Sopenharmony_ci        T1 = X[13] = PULL64(W[13]);
711e1051a39Sopenharmony_ci        ROUND_00_15(13, d, e, f, g, h, a, b, c);
712e1051a39Sopenharmony_ci        T1 = X[14] = PULL64(W[14]);
713e1051a39Sopenharmony_ci        ROUND_00_15(14, c, d, e, f, g, h, a, b);
714e1051a39Sopenharmony_ci        T1 = X[15] = PULL64(W[15]);
715e1051a39Sopenharmony_ci        ROUND_00_15(15, b, c, d, e, f, g, h, a);
716e1051a39Sopenharmony_ci#  endif
717e1051a39Sopenharmony_ci
718e1051a39Sopenharmony_ci        for (i = 16; i < 80; i += 16) {
719e1051a39Sopenharmony_ci            ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X);
720e1051a39Sopenharmony_ci            ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X);
721e1051a39Sopenharmony_ci            ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X);
722e1051a39Sopenharmony_ci            ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X);
723e1051a39Sopenharmony_ci            ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X);
724e1051a39Sopenharmony_ci            ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X);
725e1051a39Sopenharmony_ci            ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X);
726e1051a39Sopenharmony_ci            ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X);
727e1051a39Sopenharmony_ci            ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X);
728e1051a39Sopenharmony_ci            ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X);
729e1051a39Sopenharmony_ci            ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X);
730e1051a39Sopenharmony_ci            ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X);
731e1051a39Sopenharmony_ci            ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X);
732e1051a39Sopenharmony_ci            ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X);
733e1051a39Sopenharmony_ci            ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X);
734e1051a39Sopenharmony_ci            ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X);
735e1051a39Sopenharmony_ci        }
736e1051a39Sopenharmony_ci
737e1051a39Sopenharmony_ci        ctx->h[0] += a;
738e1051a39Sopenharmony_ci        ctx->h[1] += b;
739e1051a39Sopenharmony_ci        ctx->h[2] += c;
740e1051a39Sopenharmony_ci        ctx->h[3] += d;
741e1051a39Sopenharmony_ci        ctx->h[4] += e;
742e1051a39Sopenharmony_ci        ctx->h[5] += f;
743e1051a39Sopenharmony_ci        ctx->h[6] += g;
744e1051a39Sopenharmony_ci        ctx->h[7] += h;
745e1051a39Sopenharmony_ci
746e1051a39Sopenharmony_ci        W += SHA_LBLOCK;
747e1051a39Sopenharmony_ci    }
748e1051a39Sopenharmony_ci}
749e1051a39Sopenharmony_ci
750e1051a39Sopenharmony_ci# endif
751e1051a39Sopenharmony_ci
752e1051a39Sopenharmony_ci#endif                         /* SHA512_ASM */
753