Path: blob/master/arch/riscv/crypto/sm3-riscv64-zvksh-zvkb.S
26424 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, Jerry Shih <jerry.shih@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 SM3 Secure Hash extension ('Zvksh')43// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')4445#include <linux/linkage.h>4647.text48.option arch, +zvksh, +zvkb4950#define STATEP a051#define DATA a152#define NUM_BLOCKS a25354#define STATE v0 // LMUL=255#define PREV_STATE v2 // LMUL=256#define W0 v4 // LMUL=257#define W1 v6 // LMUL=258#define VTMP v8 // LMUL=25960.macro sm3_8rounds i, w0, w161// Do 4 rounds using W_{0+i}..W_{7+i}.62vsm3c.vi STATE, \w0, \i + 063vslidedown.vi VTMP, \w0, 264vsm3c.vi STATE, VTMP, \i + 16566// Compute W_{4+i}..W_{11+i}.67vslidedown.vi VTMP, \w0, 468vslideup.vi VTMP, \w1, 46970// Do 4 rounds using W_{4+i}..W_{11+i}.71vsm3c.vi STATE, VTMP, \i + 272vslidedown.vi VTMP, VTMP, 273vsm3c.vi STATE, VTMP, \i + 37475.if \i < 2876// Compute W_{16+i}..W_{23+i}.77vsm3me.vv \w0, \w1, \w078.endif79// For the next 8 rounds, w0 and w1 are swapped.80.endm8182// void sm3_transform_zvksh_zvkb(u32 state[8], const u8 *data, int num_blocks);83SYM_FUNC_START(sm3_transform_zvksh_zvkb)8485// Load the state and endian-swap each 32-bit word.86vsetivli zero, 8, e32, m2, ta, ma87vle32.v STATE, (STATEP)88vrev8.v STATE, STATE8990.Lnext_block:91addi NUM_BLOCKS, NUM_BLOCKS, -19293// Save the previous state, as it's needed later.94vmv.v.v PREV_STATE, STATE9596// Load the next 512-bit message block into W0-W1.97vle32.v W0, (DATA)98addi DATA, DATA, 3299vle32.v W1, (DATA)100addi DATA, DATA, 32101102// Do the 64 rounds of SM3.103sm3_8rounds 0, W0, W1104sm3_8rounds 4, W1, W0105sm3_8rounds 8, W0, W1106sm3_8rounds 12, W1, W0107sm3_8rounds 16, W0, W1108sm3_8rounds 20, W1, W0109sm3_8rounds 24, W0, W1110sm3_8rounds 28, W1, W0111112// XOR in the previous state.113vxor.vv STATE, STATE, PREV_STATE114115// Repeat if more blocks remain.116bnez NUM_BLOCKS, .Lnext_block117118// Store the new state and return.119vrev8.v STATE, STATE120vse32.v STATE, (STATEP)121ret122SYM_FUNC_END(sm3_transform_zvksh_zvkb)123124125