Path: blob/master/arch/riscv/crypto/sm4-riscv64-zvksed-zvkb.S
26442 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 SM4 Block Cipher extension ('Zvksed')43// - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')4445#include <linux/linkage.h>4647.text48.option arch, +zvksed, +zvkb4950// void sm4_expandkey_zksed_zvkb(const u8 user_key[16], u32 rkey_enc[32],51// u32 rkey_dec[32]);52SYM_FUNC_START(sm4_expandkey_zvksed_zvkb)53vsetivli zero, 4, e32, m1, ta, ma5455// Load the user key.56vle32.v v1, (a0)57vrev8.v v1, v15859// XOR the user key with the family key.60la t0, FAMILY_KEY61vle32.v v2, (t0)62vxor.vv v1, v1, v26364// Compute the round keys. Store them in forwards order in rkey_enc65// and in reverse order in rkey_dec.66addi a2, a2, 31*467li t0, -468.set i, 069.rept 870vsm4k.vi v1, v1, i71vse32.v v1, (a1) // Store to rkey_enc.72vsse32.v v1, (a2), t0 // Store to rkey_dec.73.if i < 774addi a1, a1, 1675addi a2, a2, -1676.endif77.set i, i + 178.endr7980ret81SYM_FUNC_END(sm4_expandkey_zvksed_zvkb)8283// void sm4_crypt_zvksed_zvkb(const u32 rkey[32], const u8 in[16], u8 out[16]);84SYM_FUNC_START(sm4_crypt_zvksed_zvkb)85vsetivli zero, 4, e32, m1, ta, ma8687// Load the input data.88vle32.v v1, (a1)89vrev8.v v1, v19091// Do the 32 rounds of SM4, 4 at a time.92.set i, 093.rept 894vle32.v v2, (a0)95vsm4r.vs v1, v296.if i < 797addi a0, a0, 1698.endif99.set i, i + 1100.endr101102// Store the output data (in reverse element order).103vrev8.v v1, v1104li t0, -4105addi a2, a2, 12106vsse32.v v1, (a2), t0107108ret109SYM_FUNC_END(sm4_crypt_zvksed_zvkb)110111.section ".rodata"112.p2align 2113.type FAMILY_KEY, @object114FAMILY_KEY:115.word 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC116.size FAMILY_KEY, . - FAMILY_KEY117118119