Path: blob/master/lib/crypto/riscv/sha512-riscv64-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 ('Zvknhb')43// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')4445#include <linux/linkage.h>4647.text48.option arch, +zvknhb, +zvkb4950#define STATEP a051#define DATA a152#define NUM_BLOCKS a25354#define STATEP_C a355#define K a45657#define MASK v058#define INDICES v159#define W0 v10 // LMUL=260#define W1 v12 // LMUL=261#define W2 v14 // LMUL=262#define W3 v16 // LMUL=263#define VTMP v20 // LMUL=264#define FEBA v22 // LMUL=265#define HGDC v24 // LMUL=266#define PREV_FEBA v26 // LMUL=267#define PREV_HGDC v28 // LMUL=26869// Do 4 rounds of SHA-512. w0 contains the current 4 message schedule words.70//71// If not all the message schedule words have been computed yet, then this also72// computes 4 more message schedule words. w1-w3 contain the next 3 groups of 473// message schedule words; this macro computes the group after w3 and writes it74// to w0. This means that the next (w0, w1, w2, w3) is the current (w1, w2, w3,75// w0), so the caller must cycle through the registers accordingly.76.macro sha512_4rounds last, w0, w1, w2, w377vle64.v VTMP, (K)78addi K, K, 3279vadd.vv VTMP, VTMP, \w080vsha2cl.vv HGDC, FEBA, VTMP81vsha2ch.vv FEBA, HGDC, VTMP82.if !\last83vmerge.vvm VTMP, \w2, \w1, MASK84vsha2ms.vv \w0, VTMP, \w385.endif86.endm8788.macro sha512_16rounds last89sha512_4rounds \last, W0, W1, W2, W390sha512_4rounds \last, W1, W2, W3, W091sha512_4rounds \last, W2, W3, W0, W192sha512_4rounds \last, W3, W0, W1, W293.endm9495// void sha512_transform_zvknhb_zvkb(struct sha512_block_state *state,96// const u8 *data, size_t nblocks);97SYM_FUNC_START(sha512_transform_zvknhb_zvkb)9899// Setup mask for the vmerge to replace the first word (idx==0) in100// message scheduling. There are 4 words, so an 8-bit mask suffices.101vsetivli zero, 1, e8, m1, ta, ma102vmv.v.i MASK, 0x01103104// Load the state. The state is stored as {a,b,c,d,e,f,g,h}, but we105// need {f,e,b,a},{h,g,d,c}. The dst vtype is e64m2 and the index vtype106// is e8mf4. We use index-load with the i8 indices {40, 32, 8, 0},107// loaded using the 32-bit little endian value 0x00082028.108li t0, 0x00082028109vsetivli zero, 1, e32, m1, ta, ma110vmv.v.x INDICES, t0111addi STATEP_C, STATEP, 16112vsetivli zero, 4, e64, m2, ta, ma113vluxei8.v FEBA, (STATEP), INDICES114vluxei8.v HGDC, (STATEP_C), INDICES115116.Lnext_block:117la K, K512118addi NUM_BLOCKS, NUM_BLOCKS, -1119120// Save the previous state, as it's needed later.121vmv.v.v PREV_FEBA, FEBA122vmv.v.v PREV_HGDC, HGDC123124// Load the next 1024-bit message block and endian-swap each 64-bit word125vle64.v W0, (DATA)126vrev8.v W0, W0127addi DATA, DATA, 32128vle64.v W1, (DATA)129vrev8.v W1, W1130addi DATA, DATA, 32131vle64.v W2, (DATA)132vrev8.v W2, W2133addi DATA, DATA, 32134vle64.v W3, (DATA)135vrev8.v W3, W3136addi DATA, DATA, 32137138// Do the 80 rounds of SHA-512.139sha512_16rounds 0140sha512_16rounds 0141sha512_16rounds 0142sha512_16rounds 0143sha512_16rounds 1144145// Add the previous state.146vadd.vv FEBA, FEBA, PREV_FEBA147vadd.vv HGDC, HGDC, PREV_HGDC148149// Repeat if more blocks remain.150bnez NUM_BLOCKS, .Lnext_block151152// Store the new state and return.153vsuxei8.v FEBA, (STATEP), INDICES154vsuxei8.v HGDC, (STATEP_C), INDICES155ret156SYM_FUNC_END(sha512_transform_zvknhb_zvkb)157158.section ".rodata"159.p2align 3160.type K512, @object161K512:162.dword 0x428a2f98d728ae22, 0x7137449123ef65cd163.dword 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc164.dword 0x3956c25bf348b538, 0x59f111f1b605d019165.dword 0x923f82a4af194f9b, 0xab1c5ed5da6d8118166.dword 0xd807aa98a3030242, 0x12835b0145706fbe167.dword 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2168.dword 0x72be5d74f27b896f, 0x80deb1fe3b1696b1169.dword 0x9bdc06a725c71235, 0xc19bf174cf692694170.dword 0xe49b69c19ef14ad2, 0xefbe4786384f25e3171.dword 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65172.dword 0x2de92c6f592b0275, 0x4a7484aa6ea6e483173.dword 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5174.dword 0x983e5152ee66dfab, 0xa831c66d2db43210175.dword 0xb00327c898fb213f, 0xbf597fc7beef0ee4176.dword 0xc6e00bf33da88fc2, 0xd5a79147930aa725177.dword 0x06ca6351e003826f, 0x142929670a0e6e70178.dword 0x27b70a8546d22ffc, 0x2e1b21385c26c926179.dword 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df180.dword 0x650a73548baf63de, 0x766a0abb3c77b2a8181.dword 0x81c2c92e47edaee6, 0x92722c851482353b182.dword 0xa2bfe8a14cf10364, 0xa81a664bbc423001183.dword 0xc24b8b70d0f89791, 0xc76c51a30654be30184.dword 0xd192e819d6ef5218, 0xd69906245565a910185.dword 0xf40e35855771202a, 0x106aa07032bbd1b8186.dword 0x19a4c116b8d2d0c8, 0x1e376c085141ab53187.dword 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8188.dword 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb189.dword 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3190.dword 0x748f82ee5defb2fc, 0x78a5636f43172f60191.dword 0x84c87814a1f0ab72, 0x8cc702081a6439ec192.dword 0x90befffa23631e28, 0xa4506cebde82bde9193.dword 0xbef9a3f7b2c67915, 0xc67178f2e372532b194.dword 0xca273eceea26619c, 0xd186b8c721c0c207195.dword 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178196.dword 0x06f067aa72176fba, 0x0a637dc5a2c898a6197.dword 0x113f9804bef90dae, 0x1b710b35131c471b198.dword 0x28db77f523047d84, 0x32caab7b40c72493199.dword 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c200.dword 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a201.dword 0x5fcb6fab3ad6faec, 0x6c44198c4a475817202.size K512, . - K512203204205