Path: blob/master/lib/crypto/riscv/chacha-riscv64-zvkb.S
54021 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, Jerry Shih <jerry.shih@sifive.com>14// Copyright 2024 Google LLC15// All rights reserved.16//17// Redistribution and use in source and binary forms, with or without18// modification, are permitted provided that the following conditions19// are met:20// 1. Redistributions of source code must retain the above copyright21// notice, this list of conditions and the following disclaimer.22// 2. Redistributions in binary form must reproduce the above copyright23// notice, this list of conditions and the following disclaimer in the24// documentation and/or other materials provided with the distribution.25//26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS27// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT28// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR29// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT30// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT32// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,33// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY34// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT35// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE36// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3738// The generated code of this file depends on the following RISC-V extensions:39// - RV64I40// - RISC-V Vector ('V') with VLEN >= 12841// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')4243#include <linux/linkage.h>4445.text46.option arch, +zvkb4748#define STATEP a049#define INP a150#define OUTP a251#define NBLOCKS a352#define NROUNDS a45354#define CONSTS0 a555#define CONSTS1 a656#define CONSTS2 a757#define CONSTS3 t058#define TMP t159#define VL t260#define STRIDE t361#define ROUND_CTR t462#define KEY0 t563// Avoid s0/fp to allow for unwinding64#define KEY1 s165#define KEY2 s266#define KEY3 s367#define KEY4 s468#define KEY5 s569#define KEY6 s670#define KEY7 s771#define COUNTER s872#define NONCE0 s973#define NONCE1 s1074#define NONCE2 s117576.macro chacha_round a0, b0, c0, d0, a1, b1, c1, d1, \77a2, b2, c2, d2, a3, b3, c3, d378// a += b; d ^= a; d = rol(d, 16);79vadd.vv \a0, \a0, \b080vadd.vv \a1, \a1, \b181vadd.vv \a2, \a2, \b282vadd.vv \a3, \a3, \b383vxor.vv \d0, \d0, \a084vxor.vv \d1, \d1, \a185vxor.vv \d2, \d2, \a286vxor.vv \d3, \d3, \a387vror.vi \d0, \d0, 32 - 1688vror.vi \d1, \d1, 32 - 1689vror.vi \d2, \d2, 32 - 1690vror.vi \d3, \d3, 32 - 169192// c += d; b ^= c; b = rol(b, 12);93vadd.vv \c0, \c0, \d094vadd.vv \c1, \c1, \d195vadd.vv \c2, \c2, \d296vadd.vv \c3, \c3, \d397vxor.vv \b0, \b0, \c098vxor.vv \b1, \b1, \c199vxor.vv \b2, \b2, \c2100vxor.vv \b3, \b3, \c3101vror.vi \b0, \b0, 32 - 12102vror.vi \b1, \b1, 32 - 12103vror.vi \b2, \b2, 32 - 12104vror.vi \b3, \b3, 32 - 12105106// a += b; d ^= a; d = rol(d, 8);107vadd.vv \a0, \a0, \b0108vadd.vv \a1, \a1, \b1109vadd.vv \a2, \a2, \b2110vadd.vv \a3, \a3, \b3111vxor.vv \d0, \d0, \a0112vxor.vv \d1, \d1, \a1113vxor.vv \d2, \d2, \a2114vxor.vv \d3, \d3, \a3115vror.vi \d0, \d0, 32 - 8116vror.vi \d1, \d1, 32 - 8117vror.vi \d2, \d2, 32 - 8118vror.vi \d3, \d3, 32 - 8119120// c += d; b ^= c; b = rol(b, 7);121vadd.vv \c0, \c0, \d0122vadd.vv \c1, \c1, \d1123vadd.vv \c2, \c2, \d2124vadd.vv \c3, \c3, \d3125vxor.vv \b0, \b0, \c0126vxor.vv \b1, \b1, \c1127vxor.vv \b2, \b2, \c2128vxor.vv \b3, \b3, \c3129vror.vi \b0, \b0, 32 - 7130vror.vi \b1, \b1, 32 - 7131vror.vi \b2, \b2, 32 - 7132vror.vi \b3, \b3, 32 - 7133.endm134135// void chacha_zvkb(struct chacha_state *state, const u8 *in, u8 *out,136// size_t nblocks, int nrounds);137//138// |nblocks| is the number of 64-byte blocks to process, and must be nonzero.139//140// |state| gives the ChaCha state matrix, including the 32-bit counter in141// state->x[12] following the RFC7539 convention; note that this differs from142// the original Salsa20 paper which uses a 64-bit counter in state->x[12..13].143// The updated 32-bit counter is written back to state->x[12] before returning.144SYM_FUNC_START(chacha_zvkb)145addi sp, sp, -96146sd s1, 8(sp)147sd s2, 16(sp)148sd s3, 24(sp)149sd s4, 32(sp)150sd s5, 40(sp)151sd s6, 48(sp)152sd s7, 56(sp)153sd s8, 64(sp)154sd s9, 72(sp)155sd s10, 80(sp)156sd s11, 88(sp)157158li STRIDE, 64159160// Set up the initial state matrix in scalar registers.161lw CONSTS0, 0(STATEP)162lw CONSTS1, 4(STATEP)163lw CONSTS2, 8(STATEP)164lw CONSTS3, 12(STATEP)165lw KEY0, 16(STATEP)166lw KEY1, 20(STATEP)167lw KEY2, 24(STATEP)168lw KEY3, 28(STATEP)169lw KEY4, 32(STATEP)170lw KEY5, 36(STATEP)171lw KEY6, 40(STATEP)172lw KEY7, 44(STATEP)173lw COUNTER, 48(STATEP)174lw NONCE0, 52(STATEP)175lw NONCE1, 56(STATEP)176lw NONCE2, 60(STATEP)177178.Lblock_loop:179// Set vl to the number of blocks to process in this iteration.180vsetvli VL, NBLOCKS, e32, m1, ta, ma181182// Set up the initial state matrix for the next VL blocks in v0-v15.183// v{i} holds the i'th 32-bit word of the state matrix for all blocks.184// Note that only the counter word, at index 12, differs across blocks.185vmv.v.x v0, CONSTS0186vmv.v.x v1, CONSTS1187vmv.v.x v2, CONSTS2188vmv.v.x v3, CONSTS3189vmv.v.x v4, KEY0190vmv.v.x v5, KEY1191vmv.v.x v6, KEY2192vmv.v.x v7, KEY3193vmv.v.x v8, KEY4194vmv.v.x v9, KEY5195vmv.v.x v10, KEY6196vmv.v.x v11, KEY7197vid.v v12198vadd.vx v12, v12, COUNTER199vmv.v.x v13, NONCE0200vmv.v.x v14, NONCE1201vmv.v.x v15, NONCE2202203// Load the first half of the input data for each block into v16-v23.204// v{16+i} holds the i'th 32-bit word for all blocks.205vlsseg8e32.v v16, (INP), STRIDE206207mv ROUND_CTR, NROUNDS208.Lnext_doubleround:209addi ROUND_CTR, ROUND_CTR, -2210// column round211chacha_round v0, v4, v8, v12, v1, v5, v9, v13, \212v2, v6, v10, v14, v3, v7, v11, v15213// diagonal round214chacha_round v0, v5, v10, v15, v1, v6, v11, v12, \215v2, v7, v8, v13, v3, v4, v9, v14216bnez ROUND_CTR, .Lnext_doubleround217218// Load the second half of the input data for each block into v24-v31.219// v{24+i} holds the {8+i}'th 32-bit word for all blocks.220addi TMP, INP, 32221vlsseg8e32.v v24, (TMP), STRIDE222223// Finalize the first half of the keystream for each block.224vadd.vx v0, v0, CONSTS0225vadd.vx v1, v1, CONSTS1226vadd.vx v2, v2, CONSTS2227vadd.vx v3, v3, CONSTS3228vadd.vx v4, v4, KEY0229vadd.vx v5, v5, KEY1230vadd.vx v6, v6, KEY2231vadd.vx v7, v7, KEY3232233// Encrypt/decrypt the first half of the data for each block.234vxor.vv v16, v16, v0235vxor.vv v17, v17, v1236vxor.vv v18, v18, v2237vxor.vv v19, v19, v3238vxor.vv v20, v20, v4239vxor.vv v21, v21, v5240vxor.vv v22, v22, v6241vxor.vv v23, v23, v7242243// Store the first half of the output data for each block.244vssseg8e32.v v16, (OUTP), STRIDE245246// Finalize the second half of the keystream for each block.247vadd.vx v8, v8, KEY4248vadd.vx v9, v9, KEY5249vadd.vx v10, v10, KEY6250vadd.vx v11, v11, KEY7251vid.v v0252vadd.vx v12, v12, COUNTER253vadd.vx v13, v13, NONCE0254vadd.vx v14, v14, NONCE1255vadd.vx v15, v15, NONCE2256vadd.vv v12, v12, v0257258// Encrypt/decrypt the second half of the data for each block.259vxor.vv v24, v24, v8260vxor.vv v25, v25, v9261vxor.vv v26, v26, v10262vxor.vv v27, v27, v11263vxor.vv v29, v29, v13264vxor.vv v28, v28, v12265vxor.vv v30, v30, v14266vxor.vv v31, v31, v15267268// Store the second half of the output data for each block.269addi TMP, OUTP, 32270vssseg8e32.v v24, (TMP), STRIDE271272// Update the counter, the remaining number of blocks, and the input and273// output pointers according to the number of blocks processed (VL).274add COUNTER, COUNTER, VL275sub NBLOCKS, NBLOCKS, VL276slli TMP, VL, 6277add OUTP, OUTP, TMP278add INP, INP, TMP279bnez NBLOCKS, .Lblock_loop280281sw COUNTER, 48(STATEP)282ld s1, 8(sp)283ld s2, 16(sp)284ld s3, 24(sp)285ld s4, 32(sp)286ld s5, 40(sp)287ld s6, 48(sp)288ld s7, 56(sp)289ld s8, 64(sp)290ld s9, 72(sp)291ld s10, 80(sp)292ld s11, 88(sp)293addi sp, sp, 96294ret295SYM_FUNC_END(chacha_zvkb)296297298