Path: blob/master/lib/crypto/riscv/sm3-riscv64-zvksh-zvkb.S
170891 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(struct sm3_block_state *state,83// const u8 *data, size_t nblocks);84SYM_FUNC_START(sm3_transform_zvksh_zvkb)8586// Load the state and endian-swap each 32-bit word.87vsetivli zero, 8, e32, m2, ta, ma88vle32.v STATE, (STATEP)89vrev8.v STATE, STATE9091.Lnext_block:92addi NUM_BLOCKS, NUM_BLOCKS, -19394// Save the previous state, as it's needed later.95vmv.v.v PREV_STATE, STATE9697// Load the next 512-bit message block into W0-W1.98vle32.v W0, (DATA)99addi DATA, DATA, 32100vle32.v W1, (DATA)101addi DATA, DATA, 32102103// Do the 64 rounds of SM3.104sm3_8rounds 0, W0, W1105sm3_8rounds 4, W1, W0106sm3_8rounds 8, W0, W1107sm3_8rounds 12, W1, W0108sm3_8rounds 16, W0, W1109sm3_8rounds 20, W1, W0110sm3_8rounds 24, W0, W1111sm3_8rounds 28, W1, W0112113// XOR in the previous state.114vxor.vv STATE, STATE, PREV_STATE115116// Repeat if more blocks remain.117bnez NUM_BLOCKS, .Lnext_block118119// Store the new state and return.120vrev8.v STATE, STATE121vse32.v STATE, (STATEP)122ret123SYM_FUNC_END(sm3_transform_zvksh_zvkb)124125126