Path: blob/master/lib/crypto/riscv/sha256-riscv64-zvknha_or_zvknhb-zvkb.S
26285 views
/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */1//2// This file is dual-licensed, meaning that you can use it under your3// choice of either of the following two licenses:4//5// Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.6//7// Licensed under the Apache License 2.0 (the "License"). You can obtain8// a copy in the file LICENSE in the source distribution or at9// https://www.openssl.org/source/license.html10//11// or12//13// Copyright (c) 2023, Christoph Müllner <christoph.muellner@vrull.eu>14// Copyright (c) 2023, Phoebe Chen <phoebe.chen@sifive.com>15// Copyright 2024 Google LLC16// All rights reserved.17//18// Redistribution and use in source and binary forms, with or without19// modification, are permitted provided that the following conditions20// are met:21// 1. Redistributions of source code must retain the above copyright22// notice, this list of conditions and the following disclaimer.23// 2. Redistributions in binary form must reproduce the above copyright24// notice, this list of conditions and the following disclaimer in the25// documentation and/or other materials provided with the distribution.26//27// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS28// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT29// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR30// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT31// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,32// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT33// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,34// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY35// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT36// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE37// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3839// The generated code of this file depends on the following RISC-V extensions:40// - RV64I41// - RISC-V Vector ('V') with VLEN >= 12842// - RISC-V Vector SHA-2 Secure Hash extension ('Zvknha' or 'Zvknhb')43// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')4445#include <linux/linkage.h>4647.text48.option arch, +zvknha, +zvkb4950#define STATEP a051#define DATA a152#define NUM_BLOCKS a25354#define STATEP_C a35556#define MASK v057#define INDICES v158#define W0 v259#define W1 v360#define W2 v461#define W3 v562#define VTMP v663#define FEBA v764#define HGDC v865#define K0 v1066#define K1 v1167#define K2 v1268#define K3 v1369#define K4 v1470#define K5 v1571#define K6 v1672#define K7 v1773#define K8 v1874#define K9 v1975#define K10 v2076#define K11 v2177#define K12 v2278#define K13 v2379#define K14 v2480#define K15 v2581#define PREV_FEBA v2682#define PREV_HGDC v278384// Do 4 rounds of SHA-256. w0 contains the current 4 message schedule words.85//86// If not all the message schedule words have been computed yet, then this also87// computes 4 more message schedule words. w1-w3 contain the next 3 groups of 488// message schedule words; this macro computes the group after w3 and writes it89// to w0. This means that the next (w0, w1, w2, w3) is the current (w1, w2, w3,90// w0), so the caller must cycle through the registers accordingly.91.macro sha256_4rounds last, k, w0, w1, w2, w392vadd.vv VTMP, \k, \w093vsha2cl.vv HGDC, FEBA, VTMP94vsha2ch.vv FEBA, HGDC, VTMP95.if !\last96vmerge.vvm VTMP, \w2, \w1, MASK97vsha2ms.vv \w0, VTMP, \w398.endif99.endm100101.macro sha256_16rounds last, k0, k1, k2, k3102sha256_4rounds \last, \k0, W0, W1, W2, W3103sha256_4rounds \last, \k1, W1, W2, W3, W0104sha256_4rounds \last, \k2, W2, W3, W0, W1105sha256_4rounds \last, \k3, W3, W0, W1, W2106.endm107108// void sha256_transform_zvknha_or_zvknhb_zvkb(struct sha256_block_state *state,109// const u8 *data, size_t nblocks);110SYM_FUNC_START(sha256_transform_zvknha_or_zvknhb_zvkb)111112// Load the round constants into K0-K15.113vsetivli zero, 4, e32, m1, ta, ma114la t0, K256115vle32.v K0, (t0)116addi t0, t0, 16117vle32.v K1, (t0)118addi t0, t0, 16119vle32.v K2, (t0)120addi t0, t0, 16121vle32.v K3, (t0)122addi t0, t0, 16123vle32.v K4, (t0)124addi t0, t0, 16125vle32.v K5, (t0)126addi t0, t0, 16127vle32.v K6, (t0)128addi t0, t0, 16129vle32.v K7, (t0)130addi t0, t0, 16131vle32.v K8, (t0)132addi t0, t0, 16133vle32.v K9, (t0)134addi t0, t0, 16135vle32.v K10, (t0)136addi t0, t0, 16137vle32.v K11, (t0)138addi t0, t0, 16139vle32.v K12, (t0)140addi t0, t0, 16141vle32.v K13, (t0)142addi t0, t0, 16143vle32.v K14, (t0)144addi t0, t0, 16145vle32.v K15, (t0)146147// Setup mask for the vmerge to replace the first word (idx==0) in148// message scheduling. There are 4 words, so an 8-bit mask suffices.149vsetivli zero, 1, e8, m1, ta, ma150vmv.v.i MASK, 0x01151152// Load the state. The state is stored as {a,b,c,d,e,f,g,h}, but we153// need {f,e,b,a},{h,g,d,c}. The dst vtype is e32m1 and the index vtype154// is e8mf4. We use index-load with the i8 indices {20, 16, 4, 0},155// loaded using the 32-bit little endian value 0x00041014.156li t0, 0x00041014157vsetivli zero, 1, e32, m1, ta, ma158vmv.v.x INDICES, t0159addi STATEP_C, STATEP, 8160vsetivli zero, 4, e32, m1, ta, ma161vluxei8.v FEBA, (STATEP), INDICES162vluxei8.v HGDC, (STATEP_C), INDICES163164.Lnext_block:165addi NUM_BLOCKS, NUM_BLOCKS, -1166167// Save the previous state, as it's needed later.168vmv.v.v PREV_FEBA, FEBA169vmv.v.v PREV_HGDC, HGDC170171// Load the next 512-bit message block and endian-swap each 32-bit word.172vle32.v W0, (DATA)173vrev8.v W0, W0174addi DATA, DATA, 16175vle32.v W1, (DATA)176vrev8.v W1, W1177addi DATA, DATA, 16178vle32.v W2, (DATA)179vrev8.v W2, W2180addi DATA, DATA, 16181vle32.v W3, (DATA)182vrev8.v W3, W3183addi DATA, DATA, 16184185// Do the 64 rounds of SHA-256.186sha256_16rounds 0, K0, K1, K2, K3187sha256_16rounds 0, K4, K5, K6, K7188sha256_16rounds 0, K8, K9, K10, K11189sha256_16rounds 1, K12, K13, K14, K15190191// Add the previous state.192vadd.vv FEBA, FEBA, PREV_FEBA193vadd.vv HGDC, HGDC, PREV_HGDC194195// Repeat if more blocks remain.196bnez NUM_BLOCKS, .Lnext_block197198// Store the new state and return.199vsuxei8.v FEBA, (STATEP), INDICES200vsuxei8.v HGDC, (STATEP_C), INDICES201ret202SYM_FUNC_END(sha256_transform_zvknha_or_zvknhb_zvkb)203204.section ".rodata"205.p2align 2206.type K256, @object207K256:208.word 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5209.word 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5210.word 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3211.word 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174212.word 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc213.word 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da214.word 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7215.word 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967216.word 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13217.word 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85218.word 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3219.word 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070220.word 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5221.word 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3222.word 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208223.word 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2224.size K256, . - K256225226227