/*-1* Copyright (c) 2021 The FreeBSD Foundation2*3* This software was developed by Andrew Turner under sponsorship from4* the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>2930#include <arm_neon.h>3132#include "sha256c_impl.h"3334void __hidden35SHA256_Transform_arm64_impl(uint32_t * state, const unsigned char block[64],36const uint32_t K[64])37{38uint32x4_t W[4];39uint32x4_t S[2];40uint32x4_t S_start[2];41uint32x4_t K_tmp, S_tmp;42int i;4344#define A64_LOAD_W(x) \45W[x] = vld1q_u32((const uint32_t *)(&block[(x) * 16])); \46W[x] = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(W[x])))4748/* 1. Prepare the first part of the message schedule W. */49A64_LOAD_W(0);50A64_LOAD_W(1);51A64_LOAD_W(2);52A64_LOAD_W(3);5354/* 2. Initialize working variables. */55S[0] = vld1q_u32(&state[0]);56S[1] = vld1q_u32(&state[4]);5758S_start[0] = S[0];59S_start[1] = S[1];6061/* 3. Mix. */62for (i = 0; i < 64; i += 16) {63#define A64_RNDr(i, ii) \64K_tmp = vaddq_u32(W[i], vld1q_u32(&K[ii + i * 4])); \65S_tmp = vsha256hq_u32(S[0], S[1], K_tmp); \66S[1] = vsha256h2q_u32(S[1], S[0], K_tmp); \67S[0] = S_tmp6869A64_RNDr(0, i);70A64_RNDr(1, i);71A64_RNDr(2, i);72A64_RNDr(3, i);7374if (i == 48)75break;7677#define A64_MSCH(x) \78W[x] = vsha256su0q_u32(W[x], W[(x + 1) % 4]); \79W[x] = vsha256su1q_u32(W[x], W[(x + 2) % 4], W[(x + 3) % 4])8081A64_MSCH(0);82A64_MSCH(1);83A64_MSCH(2);84A64_MSCH(3);85}8687/* 4. Mix local working variables into global state */88S[0] = vaddq_u32(S[0], S_start[0]);89S[1] = vaddq_u32(S[1], S_start[1]);9091vst1q_u32(&state[0], S[0]);92vst1q_u32(&state[4], S[1]);93}949596