2 * Copyright (c) 2021 The FreeBSD Foundation
4 * This software was developed by Andrew Turner under sponsorship from
5 * the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
34 #include "sha256c_impl.h"
37 SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64],
42 uint32x4_t S_start[2];
43 uint32x4_t K_tmp, S_tmp;
46 #define A64_LOAD_W(x) \
47 W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16])); \
48 W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x])))
50 /* 1. Prepare the first part of the message schedule W. */
56 /* 2. Initialize working variables. */
57 S[0] = vld1q_u32(&state[0]);
58 S[1] = vld1q_u32(&state[4]);
64 for (i = 0; i < 64; i += 16) {
65 #define A64_RNDr(i, ii) \
66 K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4])); \
67 S_tmp = vsha256hq_u32(S[0], S[1], K_tmp); \
68 S[1] = vsha256h2q_u32(S[1], S[0], K_tmp); \
80 W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]); \
81 W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4])
89 /* 4. Mix local working variables into global state */
90 S[0] = vaddq_u32(S[0], S_start[0]);
91 S[1] = vaddq_u32(S[1], S_start[1]);
93 vst1q_u32(&state[0], S[0]);
94 vst1q_u32(&state[4], S[1]);