RISC-V: provide some accelerated cryptography implementations using vector extensions
Subsystems (2)
v3 cover RISC-V: provide some accelerated cryptography implementations using vector extensions
From: Jerry Shih <[email protected]>
This series provides cryptographic implementations using the vector crypto extensions[1] including:
1. AES cipher
2. AES with CBC/CTR/ECB/XTS block modes
3. ChaCha20 stream cipher
4. GHASH for GCM
5. SHA-224/256 and SHA-384/512 hash
6. SM3 hash
7. SM4 cipher
This patch set is based on Heiko Stuebner's work at:
The implementations reuse the perl-asm scripts from OpenSSL[2] with some changes adapting for the kernel crypto framework.
The perl-asm scripts generate the RISC-V RVV 1.0 instructions and the opcodes of the vector crypto instructions into .S files. We will replace the vector crypto opcodes with asm mnemonics in the future. It needs lots of extensions checking for toolchains.
All changes pass the kernel run-time crypto self tests and the extra tests with vector-crypto-enabled qemu.
This series depend on:
1. kernel 6.6-rc7
2. support kernel-mode vector
3. vector crypto extensions detection
4. fix the error message:
alg: skcipher: skipping comparison tests for xts-aes-aesni because xts(ecb(aes-generic)) is unavailable
Here is a branch on github applying with all dependent patches:
Updated patches (on current order): 3, 4, 6, 7, 8, 9, 10, 11, 12 New patch: -
Unchanged patch: 1, 2, 5
Deleted patch: 6 in v2
Changelog v3:
- Use asm mnemonics for the instructions in RVV 1.0 extension.
- Use SYM_TYPED_FUNC_START for indirect-call asm symbols.
- Update aes xts_crypt() implementation.
- Update crypto function names with the prefix/suffix of riscv64 or the specific extensions to avoid the collision with functions in crypto/ or lib/crypto/.
Changelog v2:
- Do not turn on the RISC-V accelerated crypto kconfig options by default.
- Assume RISC-V vector extension could support unaligned access in kernel.
- Turn to use simd skcipher interface for AES-CBC/CTR/ECB/XTS and Chacha20.
- Rename crypto file and driver names to make the most important extension at first place.
Heiko Stuebner (2):
RISC-V: add helper function to read the vector VLEN
RISC-V: hook new crypto subdir into build-system
Jerry Shih (10):
RISC-V: crypto: add OpenSSL perl module for vector instructions
RISC-V: crypto: add Zvkned accelerated AES implementation
crypto: simd - Update walksize in simd skcipher
RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
RISC-V: crypto: add Zvkg accelerated GCM GHASH implementation
RISC-V: crypto: add Zvknha/b accelerated SHA224/256 implementations
RISC-V: crypto: add Zvknhb accelerated SHA384/512 implementations
RISC-V: crypto: add Zvksed accelerated SM4 implementation
RISC-V: crypto: add Zvksh accelerated SM3 implementation
RISC-V: crypto: add Zvkb accelerated ChaCha20 implementation
arch/riscv/Kbuild | 1 + arch/riscv/crypto/Kconfig | 110 ++ arch/riscv/crypto/Makefile | 68 + .../crypto/aes-riscv64-block-mode-glue.c | 494 +++++++ arch/riscv/crypto/aes-riscv64-glue.c | 137 ++ arch/riscv/crypto/aes-riscv64-glue.h | 18 + .../crypto/aes-riscv64-zvkned-zvbb-zvkg.pl | 949 +++++++++++++ arch/riscv/crypto/aes-riscv64-zvkned-zvkb.pl | 415 ++++++ arch/riscv/crypto/aes-riscv64-zvkned.pl | 1199 +++++++++++++++++ arch/riscv/crypto/chacha-riscv64-glue.c | 122 ++ arch/riscv/crypto/chacha-riscv64-zvkb.pl | 321 +++++ arch/riscv/crypto/ghash-riscv64-glue.c | 175 +++ arch/riscv/crypto/ghash-riscv64-zvkg.pl | 100 ++ arch/riscv/crypto/riscv.pm | 359 +++++ arch/riscv/crypto/sha256-riscv64-glue.c | 145 ++ .../sha256-riscv64-zvknha_or_zvknhb-zvkb.pl | 317 +++++ arch/riscv/crypto/sha512-riscv64-glue.c | 139 ++ .../crypto/sha512-riscv64-zvknhb-zvkb.pl | 265 ++++ arch/riscv/crypto/sm3-riscv64-glue.c | 124 ++ arch/riscv/crypto/sm3-riscv64-zvksh.pl | 227 ++++ arch/riscv/crypto/sm4-riscv64-glue.c | 121 ++ arch/riscv/crypto/sm4-riscv64-zvksed.pl | 268 ++++ arch/riscv/include/asm/vector.h | 11 + crypto/Kconfig | 3 + crypto/cryptd.c | 1 + crypto/simd.c | 1 + 26 files changed, 6090 insertions(+) create mode 100644 arch/riscv/crypto/Kconfig create mode 100644 arch/riscv/crypto/Makefile create mode 100644 arch/riscv/crypto/aes-riscv64-block-mode-glue.c create mode 100644 arch/riscv/crypto/aes-riscv64-glue.c create mode 100644 arch/riscv/crypto/aes-riscv64-glue.h create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned-zvbb-zvkg.pl create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned-zvkb.pl create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned.pl create mode 100644 arch/riscv/crypto/chacha-riscv64-glue.c create mode 100644 arch/riscv/crypto/chacha-riscv64-zvkb.pl create mode 100644 arch/riscv/crypto/ghash-riscv64-glue.c create mode 100644 arch/riscv/crypto/ghash-riscv64-zvkg.pl create mode 100644 arch/riscv/crypto/riscv.pm create mode 100644 arch/riscv/crypto/sha256-riscv64-glue.c create mode 100644 arch/riscv/crypto/sha256-riscv64-zvknha_or_zvknhb-zvkb.pl create mode 100644 arch/riscv/crypto/sha512-riscv64-glue.c create mode 100644 arch/riscv/crypto/sha512-riscv64-zvknhb-zvkb.pl create mode 100644 arch/riscv/crypto/sm3-riscv64-glue.c create mode 100644 arch/riscv/crypto/sm3-riscv64-zvksh.pl create mode 100644 arch/riscv/crypto/sm4-riscv64-glue.c create mode 100644 arch/riscv/crypto/sm4-riscv64-zvksed.pl
--
2.28.0
PATCH v3 1/12 RISC-V: add helper function to read the vector VLEN
From: Heiko Stuebner <[email protected]>
VLEN describes the length of each vector register and some instructions need specific minimal VLENs to work correctly.
The vector code already includes a variable riscv_v_vsize that contains the value of "32 vector registers with vlenb length" that gets filled during boot. vlenb is the value contained in the CSR_VLENB register and the value represents "VLEN / 8".
So add riscv_vector_vlen() to return the actual VLEN value for in-kernel users when they need to check the available VLEN.
--- arch/riscv/include/asm/vector.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/riscv/include/asm/vector.h +11 -0
@@ -244,4 +244,15 @@ void kernel_vector_allow_preemption(void);#define kernel_vector_allow_preemption () do {} while (0)#endif +/* +* Return the implementation's vlen value. +* +* riscv_v_vsize contains the value of "32 vector registers with vlenb length" +* so rebuild the vlen value in bits from it. +*/ +static inline int riscv_vector_vlen (void ) +{ +return riscv_v_vsize /32 *8 ; +} +#endif /* ! __ASM_RISCV_VECTOR_H */
Signature · 1 line
2.28.0
PATCH v3 2/12 RISC-V: hook new crypto subdir into build-system
In reply to Jerry ShihFrom: Heiko Stuebner <[email protected]>
Create a crypto subdirectory for added accelerated cryptography routines and hook it into the riscv Kbuild and the main crypto Kconfig.
--- arch/riscv/Kbuild | 1 + arch/riscv/crypto/Kconfig | 5 +++++ arch/riscv/crypto/Makefile | 4 ++++ crypto/Kconfig | 3 +++ 4 files changed, 13 insertions(+) create mode 100644 arch/riscv/crypto/Kconfig create mode 100644 arch/riscv/crypto/Makefile
arch/riscv/Kbuild +1 -0
@@ -2,6 +2,7 @@ obj-y += kernel/ mm/ net/ obj-$(CONFIG_BUILTIN_DTB) += boot/dts/ +obj-$(CONFIG_CRYPTO) += crypto/ obj-y += errata/ obj-$(CONFIG_KVM) += kvm/
arch/riscv/crypto/Kconfig new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +menu "Accelerated Cryptographic Algorithms for CPU (riscv)" + +endmenu
arch/riscv/crypto/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# linux/arch/riscv/crypto/Makefile +#
crypto/Kconfig +3 -0
@@ -1436,6 +1436,9 @@ endif if PPC source "arch/powerpc/crypto/Kconfig" endif +if RISCV +source "arch/riscv/crypto/Kconfig" +endif if S390 source "arch/s390/crypto/Kconfig" endif
Signature · 1 line
2.28.0
PATCH v3 3/12 RISC-V: crypto: add OpenSSL perl module for vector instructions
In reply to Jerry ShihThe OpenSSL has some RISC-V vector cryptography implementations which could be reused for kernel. These implementations use a number of perl helpers for handling vector-crypto-extension instructions. This patch take these perl helpers from OpenSSL(openssl/openssl#21923). The unused scalar crypto instructions in the original perl module are skipped.
---
Changelog v3:
- Only use opcodes for vector crypto extensions.
We use the asm mnemonics for all instructions in RVV 1.0 extension which are already supported in kernel.
--- arch/riscv/crypto/riscv.pm | 359 +++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 arch/riscv/crypto/riscv.pm
arch/riscv/crypto/riscv.pm new file +359 -0
@@ -0,0 +1,359 @@ +#! /usr/bin/env perl +# SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause +# +# This file is dual-licensed, meaning that you can use it under your +# choice of either of the following two licenses: +# +# Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. +# +# Licensed under the Apache License 2.0 (the "License"). You can obtain +# a copy in the file LICENSE in the source distribution or at +# https://www.openssl.org/source/license.html +# +# or +# +# Copyright (c) 2023, Christoph Müllner <[email protected]> +# Copyright (c) 2023, Jerry Shih <[email protected]> +# Copyright (c) 2023, Phoebe Chen <[email protected]> +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +use strict; +use warnings; + +# Set $have_stacktrace to 1 if we have Devel::StackTrace +my $have_stacktrace = 0; +if (eval {require Devel::StackTrace;1;}) { + $have_stacktrace = 1; +} + +my @regs = map("x$_",(0..31)); +# Mapping from the RISC-V psABI ABI mnemonic names to the register number. +my @regaliases = ('zero','ra','sp','gp','tp','t0','t1','t2','s0','s1', + map("a$_",(0..7)), + map("s$_",(2..11)), + map("t$_",(3..6)) +); + +my %reglookup; +@reglookup{@regs} = @regs; +@reglookup{@regaliases} = @regs; + +# Takes a register name, possibly an alias, and converts it to a register index +# from 0 to 31 +sub read_reg { + my $reg = lc shift; + if (!exists($reglookup{$reg})) { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unknown register ".$reg."\n".$trace); + } + my $regstr = $reglookup{$reg}; + if (!($regstr =~ /^x([0-9]+)$/)) { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Could not process register ".$reg."\n".$trace); + } + return $1; +} + +my @vregs = map("v$_",(0..31)); +my %vreglookup; +@vreglookup{@vregs} = @vregs; + +sub read_vreg { + my $vreg = lc shift; + if (!exists($vreglookup{$vreg})) { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unknown vector register ".$vreg."\n".$trace); + } + if (!($vreg =~ /^v([0-9]+)$/)) { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Could not process vector register ".$vreg."\n".$trace); + } + return $1; +} + +# Read the vm settings and convert to mask encoding. +sub read_mask_vreg { + my $vreg = shift; + # The default value is unmasked. + my $mask_bit = 1; + + if (defined($vreg)) { + my $reg_id = read_vreg $vreg; + if ($reg_id == 0) { + $mask_bit = 0; + } else { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("The ".$vreg." is not the mask register v0.\n".$trace); + } + } + return $mask_bit; +} + +# Vector crypto instructions + +## Zvbb and Zvkb instructions +## +## vandn (also in zvkb) +## vbrev +## vbrev8 (also in zvkb) +## vrev8 (also in zvkb) +## vclz +## vctz +## vcpop +## vrol (also in zvkb) +## vror (also in zvkb) +## vwsll + +sub vbrev8_v { + # vbrev8.v vd, vs2, vm + my $template = 0b010010_0_00000_01000_010_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($vd << 7)); +} + +sub vrev8_v { + # vrev8.v vd, vs2, vm + my $template = 0b010010_0_00000_01001_010_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($vd << 7)); +} + +sub vror_vi { + # vror.vi vd, vs2, uimm + my $template = 0b01010_0_1_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + my $uimm_i5 = $uimm >> 5; + my $uimm_i4_0 = $uimm & 0b11111; + + return ".word ".($template | ($uimm_i5 << 26) | ($vs2 << 20) | ($uimm_i4_0 << 15) | ($vd << 7)); +} + +sub vwsll_vv { + # vwsll.vv vd, vs2, vs1, vm + my $template = 0b110101_0_00000_00000_000_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($vs1 << 15) | ($vd << 7)); +} + +## Zvbc instructions + +sub vclmulh_vx { + # vclmulh.vx vd, vs2, rs1 + my $template = 0b0011011_00000_00000_110_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vclmul_vx_v0t { + # vclmul.vx vd, vs2, rs1, v0.t + my $template = 0b0011000_00000_00000_110_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vclmul_vx { + # vclmul.vx vd, vs2, rs1 + my $template = 0b0011001_00000_00000_110_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +## Zvkg instructions + +sub vghsh_vv { + # vghsh.vv vd, vs2, vs1 + my $template = 0b1011001_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vs1 << 15) | ($vd << 7)); +} + +sub vgmul_vv { + # vgmul.vv vd, vs2 + my $template = 0b1010001_00000_10001_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +## Zvkned instructions + +sub vaesdf_vs { + # vaesdf.vs vd, vs2 + my $template = 0b101001_1_00000_00001_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +sub vaesdm_vs { + # vaesdm.vs vd, vs2 + my $template = 0b101001_1_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +sub vaesef_vs { + # vaesef.vs vd, vs2 + my $template = 0b101001_1_00000_00011_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +sub vaesem_vs { + # vaesem.vs vd, vs2 + my $template = 0b101001_1_00000_00010_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +sub vaeskf1_vi { + # vaeskf1.vi vd, vs2, uimmm + my $template = 0b100010_1_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($uimm << 15) | ($vs2 << 20) | ($vd << 7)); +} + +sub vaeskf2_vi { + # vaeskf2.vi vd, vs2, uimm + my $template = 0b101010_1_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vaesz_vs { + # vaesz.vs vd, vs2 + my $template = 0b101001_1_00000_00111_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +## Zvknha and Zvknhb instructions + +sub vsha2ms_vv { + # vsha2ms.vv vd, vs2, vs1 + my $template = 0b1011011_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs2 << 20)| ($vs1 << 15 )| ($vd << 7)); +} + +sub vsha2ch_vv { + # vsha2ch.vv vd, vs2, vs1 + my $template = 0b101110_10000_00000_001_00000_01110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs2 << 20)| ($vs1 << 15 )| ($vd << 7)); +} + +sub vsha2cl_vv { + # vsha2cl.vv vd, vs2, vs1 + my $template = 0b101111_10000_00000_001_00000_01110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs2 << 20)| ($vs1 << 15 )| ($vd << 7)); +} + +## Zvksed instructions + +sub vsm4k_vi { + # vsm4k.vi vd, vs2, uimm + my $template = 0b1000011_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vsm4r_vs { + # vsm4r.vs vd, vs2 + my $template = 0b1010011_00000_10000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vd << 7)); +} + +## zvksh instructions + +sub vsm3c_vi { + # vsm3c.vi vd, vs2, uimm + my $template = 0b1010111_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15 ) | ($vd << 7)); +} + +sub vsm3me_vv { + # vsm3me.vv vd, vs2, vs1 + my $template = 0b1000001_00000_00000_010_00000_1110111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs2 << 20) | ($vs1 << 15 ) | ($vd << 7)); +} + +1;
Signature · 1 line
2.28.0
PATCH v3 4/12 RISC-V: crypto: add Zvkned accelerated AES implementation
In reply to Jerry ShihThe AES implementation using the Zvkned vector crypto extension from OpenSSL(openssl/openssl#21923).
---
Changelog v3:
- Rename aes_setkey() to aes_setkey_zvkned().
- Rename riscv64_aes_setkey() to riscv64_aes_setkey_zvkned().
- Use aes generic software key expanding everywhere.
- Remove rv64i_zvkned_set_encrypt_key().
We still need to provide the decryption expanding key for the SW fallback path which is not supported directly using zvkned extension. So, we turn to use the pure generic software key expanding everywhere to simplify the set_key flow.
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig AES_RISCV64 option by default.
- Turn to use crypto_aes_ctx structure for aes key.
- Use Zvkned extension for AES-128/256 key expanding.
- Export riscv64_aes_* symbols for other modules.
- Add asmlinkage qualifier for crypto asm function.
- Reorder structure riscv64_aes_alg_zvkned members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 11 + arch/riscv/crypto/Makefile | 11 + arch/riscv/crypto/aes-riscv64-glue.c | 137 +++++++ arch/riscv/crypto/aes-riscv64-glue.h | 18 + arch/riscv/crypto/aes-riscv64-zvkned.pl | 453 ++++++++++++++++++++++++ 5 files changed, 630 insertions(+) create mode 100644 arch/riscv/crypto/aes-riscv64-glue.c create mode 100644 arch/riscv/crypto/aes-riscv64-glue.h create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned.pl
arch/riscv/crypto/Kconfig +11 -0
@@ -2,4 +2,15 @@ menu "Accelerated Cryptographic Algorithms for CPU (riscv)" +config CRYPTO_AES_RISCV64 + tristate "Ciphers: AES" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_ALGAPI + select CRYPTO_LIB_AES + help + Block ciphers: AES cipher algorithms (FIPS-197) + + Architecture: riscv64 using: + - Zvkned vector crypto extension + endmenu
arch/riscv/crypto/Makefile +11 -0
@@ -2,3 +2,14 @@ # # linux/arch/riscv/crypto/Makefile # + +obj-$(CONFIG_CRYPTO_AES_RISCV64) += aes-riscv64.o +aes-riscv64-y := aes-riscv64-glue.o aes-riscv64-zvkned.o + +quiet_cmd_perlasm = PERLASM $@ + cmd_perlasm = $(PERL) $(<) void $(@) + +$(obj)/aes-riscv64-zvkned.S: $(src)/aes-riscv64-zvkned.pl + $(call cmd,perlasm) + +clean-files += aes-riscv64-zvkned.S
arch/riscv/crypto/aes-riscv64-glue.c new file +137 -0
137 lines not on this page · raw message
arch/riscv/crypto/aes-riscv64-glue.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef AES_RISCV64_GLUE_H +#define AES_RISCV64_GLUE_H + +#include <crypto/aes.h> +#include <linux/types.h> + +int riscv64_aes_setkey_zvkned (struct crypto_aes_ctx *ctx,const u8 *key, +unsigned int keylen); + +void riscv64_aes_encrypt_zvkned (const struct crypto_aes_ctx *ctx,u8 *dst, +const u8 *src); + +void riscv64_aes_decrypt_zvkned (const struct crypto_aes_ctx *ctx,u8 *dst, +const u8 *src); + +#endif /* AES_RISCV64_GLUE_H */
arch/riscv/crypto/aes-riscv64-zvkned.pl new file +453 -0
453 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 5/12 crypto: simd - Update `walksize` in simd skcipher
In reply to Jerry ShihThe walksize assignment is missed in simd skcipher.
--- crypto/cryptd.c | 1 + crypto/simd.c | 1 + 2 files changed, 2 insertions(+)
crypto/cryptd.c +1 -0
@@ -405,6 +405,7 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl, (alg->base.cra_flags &CRYPTO_ALG_INTERNAL ); inst->alg.ivsize =crypto_skcipher_alg_ivsize (alg); inst->alg.chunksize =crypto_skcipher_alg_chunksize (alg); + inst->alg.walksize =crypto_skcipher_alg_walksize (alg); inst->alg.min_keysize =crypto_skcipher_alg_min_keysize (alg); inst->alg.max_keysize =crypto_skcipher_alg_max_keysize (alg);
crypto/simd.c +1 -0
@@ -181,6 +181,7 @@ struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname, alg->ivsize = ialg->ivsize; alg->chunksize = ialg->chunksize; + alg->walksize = ialg->walksize; alg->min_keysize = ialg->min_keysize; alg->max_keysize = ialg->max_keysize;
Signature · 1 line
2.28.0
PATCH v3 6/12 RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
In reply to Jerry ShihPort the vector-crypto accelerated CBC, CTR, ECB and XTS block modes for AES cipher from OpenSSL(openssl/openssl#21923).
In addition, support XTS-AES-192 mode which is not existed in OpenSSL.
---
Changelog v3:
- Update extension checking conditions in riscv64_aes_block_mod_init().
- Add riscv64 prefix for all setkey, encrypt and decrypt functions.
- Update xts_crypt() implementation.
Use the similar approach as x86's aes-xts implementation.
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig AES_BLOCK_RISCV64 option by default.
- Update asm function for using aes key in crypto_aes_ctx structure.
- Turn to use simd skcipher interface for AES-CBC/CTR/ECB/XTS modes. We still have lots of discussions for kernel-vector implementation. Before the final version of kernel-vector, use simd skcipher interface to skip the fallback path for all aes modes in all kinds of contexts. If we could always enable kernel-vector in softirq in the future, we could make the original sync skcipher algorithm back.
- Refine aes-xts comments for head and tail blocks handling.
- Update VLEN constraint for aex-xts mode.
- Add asmlinkage qualifier for crypto asm function.
- Rename aes-riscv64-zvbb-zvkg-zvkned to aes-riscv64-zvkned-zvbb-zvkg.
- Rename aes-riscv64-zvkb-zvkned to aes-riscv64-zvkned-zvkb.
- Reorder structure riscv64_aes_algs_zvkned, riscv64_aes_alg_zvkned_zvkb and riscv64_aes_alg_zvkned_zvbb_zvkg members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 21 + arch/riscv/crypto/Makefile | 11 + .../crypto/aes-riscv64-block-mode-glue.c | 494 +++++++++ .../crypto/aes-riscv64-zvkned-zvbb-zvkg.pl | 949 ++++++++++++++++++ arch/riscv/crypto/aes-riscv64-zvkned-zvkb.pl | 415 ++++++++ arch/riscv/crypto/aes-riscv64-zvkned.pl | 746 ++++++++++++++ 6 files changed, 2636 insertions(+) create mode 100644 arch/riscv/crypto/aes-riscv64-block-mode-glue.c create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned-zvbb-zvkg.pl create mode 100644 arch/riscv/crypto/aes-riscv64-zvkned-zvkb.pl
arch/riscv/crypto/Kconfig +21 -0
@@ -13,4 +13,25 @@ config CRYPTO_AES_RISCV64 Architecture: riscv64 using: - Zvkned vector crypto extension +config CRYPTO_AES_BLOCK_RISCV64 + tristate "Ciphers: AES, modes: ECB/CBC/CTR/XTS" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_AES_RISCV64 + select CRYPTO_SIMD + select CRYPTO_SKCIPHER + help + Length-preserving ciphers: AES cipher algorithms (FIPS-197) + with block cipher modes: + - ECB (Electronic Codebook) mode (NIST SP 800-38A) + - CBC (Cipher Block Chaining) mode (NIST SP 800-38A) + - CTR (Counter) mode (NIST SP 800-38A) + - XTS (XOR Encrypt XOR Tweakable Block Cipher with Ciphertext + Stealing) mode (NIST SP 800-38E and IEEE 1619) + + Architecture: riscv64 using: + - Zvkned vector crypto extension + - Zvbb vector extension (XTS) + - Zvkb vector crypto extension (CTR/XTS) + - Zvkg vector crypto extension (XTS) + endmenu
arch/riscv/crypto/Makefile +11 -0
@@ -6,10 +6,21 @@ obj-$(CONFIG_CRYPTO_AES_RISCV64) += aes-riscv64.o aes-riscv64-y := aes-riscv64-glue.o aes-riscv64-zvkned.o +obj-$(CONFIG_CRYPTO_AES_BLOCK_RISCV64) += aes-block-riscv64.o +aes-block-riscv64-y := aes-riscv64-block-mode-glue.o aes-riscv64-zvkned-zvbb-zvkg.o aes-riscv64-zvkned-zvkb.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@) $(obj)/aes-riscv64-zvkned.S: $(src)/aes-riscv64-zvkned.pl $(call cmd,perlasm) +$(obj)/aes-riscv64-zvkned-zvbb-zvkg.S: $(src)/aes-riscv64-zvkned-zvbb-zvkg.pl + $(call cmd,perlasm) + +$(obj)/aes-riscv64-zvkned-zvkb.S: $(src)/aes-riscv64-zvkned-zvkb.pl + $(call cmd,perlasm) + clean-files += aes-riscv64-zvkned.S +clean-files += aes-riscv64-zvkned-zvbb-zvkg.S +clean-files += aes-riscv64-zvkned-zvkb.S
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +494 -0
494 lines not on this page · raw message
arch/riscv/crypto/aes-riscv64-zvkned-zvbb-zvkg.pl new file +949 -0
949 lines not on this page · raw message
arch/riscv/crypto/aes-riscv64-zvkned-zvkb.pl new file +415 -0
415 lines not on this page · raw message
arch/riscv/crypto/aes-riscv64-zvkned.pl +746 -0
752 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 7/12 RISC-V: crypto: add Zvkg accelerated GCM GHASH implementation
In reply to Jerry ShihAdd a gcm hash implementation using the Zvkg extension from OpenSSL (openssl/openssl#21923).
The perlasm here is different from the original implementation in OpenSSL. The OpenSSL assumes that the H is stored in little-endian. Thus, it needs to convert the H to big-endian for Zvkg instructions. In kernel, we have the big-endian H directly. There is no need for endian conversion.
---
Changelog v3:
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig GHASH_RISCV64 option by default.
- Add asmlinkage qualifier for crypto asm function.
- Update the ghash fallback path in ghash_blocks().
- Rename structure riscv64_ghash_context to riscv64_ghash_tfm_ctx.
- Fold ghash_update_zvkg() and ghash_final_zvkg().
- Reorder structure riscv64_ghash_alg_zvkg members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 10 ++ arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/ghash-riscv64-glue.c | 175 ++++++++++++++++++++++++ arch/riscv/crypto/ghash-riscv64-zvkg.pl | 100 ++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 arch/riscv/crypto/ghash-riscv64-glue.c create mode 100644 arch/riscv/crypto/ghash-riscv64-zvkg.pl
arch/riscv/crypto/Kconfig +10 -0
@@ -34,4 +34,14 @@ config CRYPTO_AES_BLOCK_RISCV64 - Zvkb vector crypto extension (CTR/XTS) - Zvkg vector crypto extension (XTS) +config CRYPTO_GHASH_RISCV64 + tristate "Hash functions: GHASH" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_GCM + help + GCM GHASH function (NIST SP 800-38D) + + Architecture: riscv64 using: + - Zvkg vector crypto extension + endmenu
arch/riscv/crypto/Makefile +7 -0
@@ -9,6 +9,9 @@ aes-riscv64-y := aes-riscv64-glue.o aes-riscv64-zvkned.o obj-$(CONFIG_CRYPTO_AES_BLOCK_RISCV64) += aes-block-riscv64.o aes-block-riscv64-y := aes-riscv64-block-mode-glue.o aes-riscv64-zvkned-zvbb-zvkg.o aes-riscv64-zvkned-zvkb.o +obj-$(CONFIG_CRYPTO_GHASH_RISCV64) += ghash-riscv64.o +ghash-riscv64-y := ghash-riscv64-glue.o ghash-riscv64-zvkg.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@)
@@ -21,6 +24,10 @@ $(obj)/aes-riscv64-zvkned-zvbb-zvkg.S: $(src)/aes-riscv64-zvkned-zvbb-zvkg.pl $(obj)/aes-riscv64-zvkned-zvkb.S: $(src)/aes-riscv64-zvkned-zvkb.pl $(call cmd,perlasm) +$(obj)/ghash-riscv64-zvkg.S: $(src)/ghash-riscv64-zvkg.pl + $(call cmd,perlasm) + clean-files += aes-riscv64-zvkned.S clean-files += aes-riscv64-zvkned-zvbb-zvkg.S clean-files += aes-riscv64-zvkned-zvkb.S +clean-files += ghash-riscv64-zvkg.S
arch/riscv/crypto/ghash-riscv64-glue.c new file +175 -0
175 lines not on this page · raw message
arch/riscv/crypto/ghash-riscv64-zvkg.pl new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 8/12 RISC-V: crypto: add Zvknha/b accelerated SHA224/256 implementations
In reply to Jerry ShihAdd SHA224 and 256 implementations using Zvknha or Zvknhb vector crypto extensions from OpenSSL(openssl/openssl#21923).
---
Changelog v3:
- Use SYM_TYPED_FUNC_START for sha256 indirect-call asm symbol.
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig SHA256_RISCV64 option by default.
- Add asmlinkage qualifier for crypto asm function.
- Rename sha256-riscv64-zvkb-zvknha_or_zvknhb to sha256-riscv64-zvknha_or_zvknhb-zvkb.
- Reorder structure sha256_algs members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 11 + arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/sha256-riscv64-glue.c | 145 ++++++++ .../sha256-riscv64-zvknha_or_zvknhb-zvkb.pl | 317 ++++++++++++++++++ 4 files changed, 480 insertions(+) create mode 100644 arch/riscv/crypto/sha256-riscv64-glue.c create mode 100644 arch/riscv/crypto/sha256-riscv64-zvknha_or_zvknhb-zvkb.pl
arch/riscv/crypto/Kconfig +11 -0
@@ -44,4 +44,15 @@ config CRYPTO_GHASH_RISCV64 Architecture: riscv64 using: - Zvkg vector crypto extension +config CRYPTO_SHA256_RISCV64 + tristate "Hash functions: SHA-224 and SHA-256" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_SHA256 + help + SHA-224 and SHA-256 secure hash algorithm (FIPS 180) + + Architecture: riscv64 using: + - Zvknha or Zvknhb vector crypto extensions + - Zvkb vector crypto extension + endmenu
arch/riscv/crypto/Makefile +7 -0
@@ -12,6 +12,9 @@ aes-block-riscv64-y := aes-riscv64-block-mode-glue.o aes-riscv64-zvkned-zvbb-zvk obj-$(CONFIG_CRYPTO_GHASH_RISCV64) += ghash-riscv64.o ghash-riscv64-y := ghash-riscv64-glue.o ghash-riscv64-zvkg.o +obj-$(CONFIG_CRYPTO_SHA256_RISCV64) += sha256-riscv64.o +sha256-riscv64-y := sha256-riscv64-glue.o sha256-riscv64-zvknha_or_zvknhb-zvkb.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@)
@@ -27,7 +30,11 @@ $(obj)/aes-riscv64-zvkned-zvkb.S: $(src)/aes-riscv64-zvkned-zvkb.pl $(obj)/ghash-riscv64-zvkg.S: $(src)/ghash-riscv64-zvkg.pl $(call cmd,perlasm) +$(obj)/sha256-riscv64-zvknha_or_zvknhb-zvkb.S: $(src)/sha256-riscv64-zvknha_or_zvknhb-zvkb.pl + $(call cmd,perlasm) + clean-files += aes-riscv64-zvkned.S clean-files += aes-riscv64-zvkned-zvbb-zvkg.S clean-files += aes-riscv64-zvkned-zvkb.S clean-files += ghash-riscv64-zvkg.S +clean-files += sha256-riscv64-zvknha_or_zvknhb-zvkb.S
arch/riscv/crypto/sha256-riscv64-glue.c new file +145 -0
145 lines not on this page · raw message
arch/riscv/crypto/sha256-riscv64-zvknha_or_zvknhb-zvkb.pl new file +317 -0
317 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 9/12 RISC-V: crypto: add Zvknhb accelerated SHA384/512 implementations
In reply to Jerry ShihAdd SHA384 and 512 implementations using Zvknhb vector crypto extension from OpenSSL(openssl/openssl#21923).
---
Changelog v3:
- Use SYM_TYPED_FUNC_START for sha512 indirect-call asm symbol.
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig SHA512_RISCV64 option by default.
- Add asmlinkage qualifier for crypto asm function.
- Rename sha512-riscv64-zvkb-zvknhb to sha512-riscv64-zvknhb-zvkb.
- Reorder structure sha512_algs members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 11 + arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/sha512-riscv64-glue.c | 139 +++++++++ .../crypto/sha512-riscv64-zvknhb-zvkb.pl | 265 ++++++++++++++++++ 4 files changed, 422 insertions(+) create mode 100644 arch/riscv/crypto/sha512-riscv64-glue.c create mode 100644 arch/riscv/crypto/sha512-riscv64-zvknhb-zvkb.pl
arch/riscv/crypto/Kconfig +11 -0
@@ -55,4 +55,15 @@ config CRYPTO_SHA256_RISCV64 - Zvknha or Zvknhb vector crypto extensions - Zvkb vector crypto extension +config CRYPTO_SHA512_RISCV64 + tristate "Hash functions: SHA-384 and SHA-512" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_SHA512 + help + SHA-384 and SHA-512 secure hash algorithm (FIPS 180) + + Architecture: riscv64 using: + - Zvknhb vector crypto extension + - Zvkb vector crypto extension + endmenu
arch/riscv/crypto/Makefile +7 -0
@@ -15,6 +15,9 @@ ghash-riscv64-y := ghash-riscv64-glue.o ghash-riscv64-zvkg.o obj-$(CONFIG_CRYPTO_SHA256_RISCV64) += sha256-riscv64.o sha256-riscv64-y := sha256-riscv64-glue.o sha256-riscv64-zvknha_or_zvknhb-zvkb.o +obj-$(CONFIG_CRYPTO_SHA512_RISCV64) += sha512-riscv64.o +sha512-riscv64-y := sha512-riscv64-glue.o sha512-riscv64-zvknhb-zvkb.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@)
@@ -33,8 +36,12 @@ $(obj)/ghash-riscv64-zvkg.S: $(src)/ghash-riscv64-zvkg.pl $(obj)/sha256-riscv64-zvknha_or_zvknhb-zvkb.S: $(src)/sha256-riscv64-zvknha_or_zvknhb-zvkb.pl $(call cmd,perlasm) +$(obj)/sha512-riscv64-zvknhb-zvkb.S: $(src)/sha512-riscv64-zvknhb-zvkb.pl + $(call cmd,perlasm) + clean-files += aes-riscv64-zvkned.S clean-files += aes-riscv64-zvkned-zvbb-zvkg.S clean-files += aes-riscv64-zvkned-zvkb.S clean-files += ghash-riscv64-zvkg.S clean-files += sha256-riscv64-zvknha_or_zvknhb-zvkb.S +clean-files += sha512-riscv64-zvknhb-zvkb.S
arch/riscv/crypto/sha512-riscv64-glue.c new file +139 -0
139 lines not on this page · raw message
arch/riscv/crypto/sha512-riscv64-zvknhb-zvkb.pl new file +265 -0
265 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 10/12 RISC-V: crypto: add Zvksed accelerated SM4 implementation
In reply to Jerry ShihAdd SM4 implementation using Zvksed vector crypto extension from OpenSSL (openssl/openssl#21923).
The perlasm here is different from the original implementation in OpenSSL. In OpenSSL, SM4 has the separated set_encrypt_key and set_decrypt_key functions. In kernel, these set_key functions are merged into a single one in order to skip the redundant key expanding instructions.
---
Changelog v3:
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig SM4_RISCV64 option by default.
- Add the missed static declaration for riscv64_sm4_zvksed_alg.
- Add asmlinkage qualifier for crypto asm function.
- Rename sm4-riscv64-zvkb-zvksed to sm4-riscv64-zvksed-zvkb.
- Reorder structure riscv64_sm4_zvksed_zvkb_alg members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 17 ++ arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/sm4-riscv64-glue.c | 121 +++++++++++ arch/riscv/crypto/sm4-riscv64-zvksed.pl | 268 ++++++++++++++++++++++++ 4 files changed, 413 insertions(+) create mode 100644 arch/riscv/crypto/sm4-riscv64-glue.c create mode 100644 arch/riscv/crypto/sm4-riscv64-zvksed.pl
arch/riscv/crypto/Kconfig +17 -0
@@ -66,4 +66,21 @@ config CRYPTO_SHA512_RISCV64 - Zvknhb vector crypto extension - Zvkb vector crypto extension +config CRYPTO_SM4_RISCV64 + tristate "Ciphers: SM4 (ShangMi 4)" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_ALGAPI + select CRYPTO_SM4 + help + SM4 cipher algorithms (OSCCA GB/T 32907-2016, + ISO/IEC 18033-3:2010/Amd 1:2021) + + SM4 (GBT.32907-2016) is a cryptographic standard issued by the + Organization of State Commercial Administration of China (OSCCA) + as an authorized cryptographic algorithms for the use within China. + + Architecture: riscv64 using: + - Zvksed vector crypto extension + - Zvkb vector crypto extension + endmenu
arch/riscv/crypto/Makefile +7 -0
@@ -18,6 +18,9 @@ sha256-riscv64-y := sha256-riscv64-glue.o sha256-riscv64-zvknha_or_zvknhb-zvkb.o obj-$(CONFIG_CRYPTO_SHA512_RISCV64) += sha512-riscv64.o sha512-riscv64-y := sha512-riscv64-glue.o sha512-riscv64-zvknhb-zvkb.o +obj-$(CONFIG_CRYPTO_SM4_RISCV64) += sm4-riscv64.o +sm4-riscv64-y := sm4-riscv64-glue.o sm4-riscv64-zvksed.o + quiet_cmd_perlasm = PERLASM $@ cmd_perlasm = $(PERL) $(<) void $(@)
@@ -39,9 +42,13 @@ $(obj)/sha256-riscv64-zvknha_or_zvknhb-zvkb.S: $(src)/sha256-riscv64-zvknha_or_z $(obj)/sha512-riscv64-zvknhb-zvkb.S: $(src)/sha512-riscv64-zvknhb-zvkb.pl $(call cmd,perlasm) +$(obj)/sm4-riscv64-zvksed.S: $(src)/sm4-riscv64-zvksed.pl + $(call cmd,perlasm) + clean-files += aes-riscv64-zvkned.S clean-files += aes-riscv64-zvkned-zvbb-zvkg.S clean-files += aes-riscv64-zvkned-zvkb.S clean-files += ghash-riscv64-zvkg.S clean-files += sha256-riscv64-zvknha_or_zvknhb-zvkb.S clean-files += sha512-riscv64-zvknhb-zvkb.S +clean-files += sm4-riscv64-zvksed.S
arch/riscv/crypto/sm4-riscv64-glue.c new file +121 -0
121 lines not on this page · raw message
arch/riscv/crypto/sm4-riscv64-zvksed.pl new file +268 -0
268 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 11/12 RISC-V: crypto: add Zvksh accelerated SM3 implementation
In reply to Jerry ShihAdd SM3 implementation using Zvksh vector crypto extension from OpenSSL (openssl/openssl#21923).
---
Changelog v3:
- Use SYM_TYPED_FUNC_START for sm3 indirect-call asm symbol.
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig SM3_RISCV64 option by default.
- Add asmlinkage qualifier for crypto asm function.
- Rename sm3-riscv64-zvkb-zvksh to sm3-riscv64-zvksh-zvkb.
- Reorder structure sm3_alg members initialization in the order declared.
--- arch/riscv/crypto/Kconfig | 12 ++ arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/sm3-riscv64-glue.c | 124 ++++++++++++++ arch/riscv/crypto/sm3-riscv64-zvksh.pl | 227 +++++++++++++++++++++++++ 4 files changed, 370 insertions(+) create mode 100644 arch/riscv/crypto/sm3-riscv64-glue.c create mode 100644 arch/riscv/crypto/sm3-riscv64-zvksh.pl
arch/riscv/crypto/Kconfig +12 -0
@@ -66,6 +66,18 @@ config CRYPTO_SHA512_RISCV64 - Zvknhb vector crypto extension - Zvkb vector crypto extension +config CRYPTO_SM3_RISCV64 + tristate "Hash functions: SM3 (ShangMi 3)" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_HASH + select CRYPTO_SM3 + help + SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012) + + Architecture: riscv64 using: + - Zvksh vector crypto extension + - Zvkb vector crypto extension + config CRYPTO_SM4_RISCV64 tristate "Ciphers: SM4 (ShangMi 4)" depends on 64BIT && RISCV_ISA_V
arch/riscv/crypto/Makefile +7 -0
@@ -18,6 +18,9 @@ sha256-riscv64-y := sha256-riscv64-glue.o sha256-riscv64-zvknha_or_zvknhb-zvkb.o obj-$(CONFIG_CRYPTO_SHA512_RISCV64) += sha512-riscv64.o sha512-riscv64-y := sha512-riscv64-glue.o sha512-riscv64-zvknhb-zvkb.o +obj-$(CONFIG_CRYPTO_SM3_RISCV64) += sm3-riscv64.o +sm3-riscv64-y := sm3-riscv64-glue.o sm3-riscv64-zvksh.o + obj-$(CONFIG_CRYPTO_SM4_RISCV64) += sm4-riscv64.o sm4-riscv64-y := sm4-riscv64-glue.o sm4-riscv64-zvksed.o
@@ -42,6 +45,9 @@ $(obj)/sha256-riscv64-zvknha_or_zvknhb-zvkb.S: $(src)/sha256-riscv64-zvknha_or_z $(obj)/sha512-riscv64-zvknhb-zvkb.S: $(src)/sha512-riscv64-zvknhb-zvkb.pl $(call cmd,perlasm) +$(obj)/sm3-riscv64-zvksh.S: $(src)/sm3-riscv64-zvksh.pl + $(call cmd,perlasm) + $(obj)/sm4-riscv64-zvksed.S: $(src)/sm4-riscv64-zvksed.pl $(call cmd,perlasm)
@@ -51,4 +57,5 @@ clean-files += aes-riscv64-zvkned-zvkb.S clean-files += ghash-riscv64-zvkg.S clean-files += sha256-riscv64-zvknha_or_zvknhb-zvkb.S clean-files += sha512-riscv64-zvknhb-zvkb.S +clean-files += sm3-riscv64-zvksh.S clean-files += sm4-riscv64-zvksed.S
arch/riscv/crypto/sm3-riscv64-glue.c new file +124 -0
124 lines not on this page · raw message
arch/riscv/crypto/sm3-riscv64-zvksh.pl new file +227 -0
227 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v3 12/12 RISC-V: crypto: add Zvkb accelerated ChaCha20 implementation
In reply to Jerry ShihAdd a ChaCha20 vector implementation from OpenSSL(openssl/openssl#21923).
---
Changelog v3:
- Rename kconfig CRYPTO_CHACHA20_RISCV64 to CRYPTO_CHACHA_RISCV64.
- Rename chacha20_encrypt() to riscv64_chacha20_encrypt().
- Use asm mnemonics for the instructions in RVV 1.0 extension.
Changelog v2:
- Do not turn on kconfig CHACHA20_RISCV64 option by default.
- Use simd skcipher interface.
- Add asmlinkage qualifier for crypto asm function.
- Reorder structure riscv64_chacha_alg_zvkb members initialization in the order declared.
- Use smaller iv buffer instead of whole state matrix as chacha20's input.
--- arch/riscv/crypto/Kconfig | 12 + arch/riscv/crypto/Makefile | 7 + arch/riscv/crypto/chacha-riscv64-glue.c | 122 +++++++++ arch/riscv/crypto/chacha-riscv64-zvkb.pl | 321 +++++++++++++++++++++++ 4 files changed, 462 insertions(+) create mode 100644 arch/riscv/crypto/chacha-riscv64-glue.c create mode 100644 arch/riscv/crypto/chacha-riscv64-zvkb.pl
arch/riscv/crypto/Kconfig +12 -0
@@ -34,6 +34,18 @@ config CRYPTO_AES_BLOCK_RISCV64 - Zvkb vector crypto extension (CTR/XTS) - Zvkg vector crypto extension (XTS) +config CRYPTO_CHACHA_RISCV64 + tristate "Ciphers: ChaCha" + depends on 64BIT && RISCV_ISA_V + select CRYPTO_SIMD + select CRYPTO_SKCIPHER + select CRYPTO_LIB_CHACHA_GENERIC + help + Length-preserving ciphers: ChaCha20 stream cipher algorithm + + Architecture: riscv64 using: + - Zvkb vector crypto extension + config CRYPTO_GHASH_RISCV64 tristate "Hash functions: GHASH" depends on 64BIT && RISCV_ISA_V
arch/riscv/crypto/Makefile +7 -0
@@ -9,6 +9,9 @@ aes-riscv64-y := aes-riscv64-glue.o aes-riscv64-zvkned.o obj-$(CONFIG_CRYPTO_AES_BLOCK_RISCV64) += aes-block-riscv64.o aes-block-riscv64-y := aes-riscv64-block-mode-glue.o aes-riscv64-zvkned-zvbb-zvkg.o aes-riscv64-zvkned-zvkb.o +obj-$(CONFIG_CRYPTO_CHACHA_RISCV64) += chacha-riscv64.o +chacha-riscv64-y := chacha-riscv64-glue.o chacha-riscv64-zvkb.o + obj-$(CONFIG_CRYPTO_GHASH_RISCV64) += ghash-riscv64.o ghash-riscv64-y := ghash-riscv64-glue.o ghash-riscv64-zvkg.o
@@ -36,6 +39,9 @@ $(obj)/aes-riscv64-zvkned-zvbb-zvkg.S: $(src)/aes-riscv64-zvkned-zvbb-zvkg.pl $(obj)/aes-riscv64-zvkned-zvkb.S: $(src)/aes-riscv64-zvkned-zvkb.pl $(call cmd,perlasm) +$(obj)/chacha-riscv64-zvkb.S: $(src)/chacha-riscv64-zvkb.pl + $(call cmd,perlasm) + $(obj)/ghash-riscv64-zvkg.S: $(src)/ghash-riscv64-zvkg.pl $(call cmd,perlasm)
@@ -54,6 +60,7 @@ $(obj)/sm4-riscv64-zvksed.S: $(src)/sm4-riscv64-zvksed.pl clean-files += aes-riscv64-zvkned.S clean-files += aes-riscv64-zvkned-zvbb-zvkg.S clean-files += aes-riscv64-zvkned-zvkb.S +clean-files += chacha-riscv64-zvkb.S clean-files += ghash-riscv64-zvkg.S clean-files += sha256-riscv64-zvknha_or_zvknhb-zvkb.S clean-files += sha512-riscv64-zvknhb-zvkb.S
arch/riscv/crypto/chacha-riscv64-glue.c new file +122 -0
122 lines not on this page · raw message
arch/riscv/crypto/chacha-riscv64-zvkb.pl new file +321 -0
321 lines not on this page · raw message
Signature · 1 line
2.28.0
Quoted 5 lines · Jerry Shih
This series depend on:
2. support kernel-mode vector3. vector crypto extensions detection
What's the status of getting these prerequisites merged?
- Eric
Quoted 10 lines · Eric Biggers
Quoted 5 lines · Jerry Shih
This series depend on:
2. support kernel-mode vector3. vector crypto extensions detection
What's the status of getting these prerequisites merged?
- Eric
The latest extension detection patch version is v5.
It's still under reviewing.
But I think the checking codes used in this crypto patch series will not change. We could just wait and rebase when it's merged.
The latest kernel-mode vector patch version is v3.
This patch doesn't work with qemu(hit kernel panic when using vector). It's not clear for the status. Could we still do the reviewing process for the gluing code and the crypto asm parts?
-Jerry
Hi Jerry,
Quoted 18 lines · Jerry Shih
Quoted 10 lines · Eric Biggers
Quoted 5 lines · Jerry Shih
This series depend on:
2. support kernel-mode vector3. vector crypto extensions detection
What's the status of getting these prerequisites merged?
- Eric
The latest extension detection patch version is v5.
It's still under reviewing.
But I think the checking codes used in this crypto patch series will not change. We could just wait and rebase when it's merged.
Quoted 5 lines
The latest kernel-mode vector patch version is v3.
This patch doesn't work with qemu(hit kernel panic when using vector). It's not clear for the status. Could we still do the reviewing process for the gluing code and the crypto asm parts?
I'm almost ready to give my Reviewed-by for this whole series. The problem is that it can't be merged until its prerequisites are merged.
Andy Chiu's last patchset "riscv: support kernel-mode Vector" was 2 months ago, but he also gave a talk at Plumbers about it more recently
(https://www.youtube.com/watch?v=eht3PccEn5o). So I assume he's still working on it. It sounds like he's also going to include support for preemption, and optimizations to memcpy, memset, memmove, and copy_{to,from}_user.
I think it would be a good idea to split out the basic support for kernel_vector_{begin,end} so that the users of them, as well as the preemption support, can be considered and merged separately. Maybe patch 1 of the series
(https://lore.kernel.org/r/[email protected]) is all that's needed initially?
Andy, what do you think?
- Eric
Palmer Dabbelt
maintainerQuoted 27 lines · On Tue, 05 Dec 2023 23:41:55 PST (-0800), [email protected] wrote:
Hi Jerry,
Quoted 18 lines · Jerry Shih
Quoted 10 lines · Eric Biggers
Quoted 5 lines · Jerry Shih
This series depend on:
2. support kernel-mode vector3. vector crypto extensions detection
What's the status of getting these prerequisites merged?
- Eric
The latest extension detection patch version is v5.
It's still under reviewing.
But I think the checking codes used in this crypto patch series will not change. We could just wait and rebase when it's merged.Quoted 5 lines
The latest kernel-mode vector patch version is v3.
This patch doesn't work with qemu(hit kernel panic when using vector). It's not clear for the status. Could we still do the reviewing process for the gluing code and the crypto asm parts?
Quoted 8 lines
I'm almost ready to give my Reviewed-by for this whole series. The problem is that it can't be merged until its prerequisites are merged.
Andy Chiu's last patchset "riscv: support kernel-mode Vector" was 2 months ago, but he also gave a talk at Plumbers about it more recently
(https://www.youtube.com/watch?v=eht3PccEn5o). So I assume he's still working on it. It sounds like he's also going to include support for preemption, and optimizations to memcpy, memset, memmove, and copy_{to,from}_user.
So I think we just got blocked on not knowing if turning on vector everywhere in the kernel was a good idea -- it's not what any other port does despite there having been some discussions floating around, but we never really figured out why. I can come up with some possible performance pathologies related to having vector on in many contexts, but it's all theory as there's not really any vector hardware that works upstream (though the K230 is starting to come along, so maybe that'll sort itself out).
Last we talked I think the general consensus is that we'd waited long enough, if nobody has a concrete objection we should just take it and see -- sure maybe there's some possible issues, but anything could have issues.
Quoted 5 lines
I think it would be a good idea to split out the basic support for kernel_vector_{begin,end} so that the users of them, as well as the preemption support, can be considered and merged separately. Maybe patch 1 of the series
(https://lore.kernel.org/r/[email protected]) is all that's needed initially?
I'm fine with that sort of approach too, it's certainly more in line with other ports to just restrict the kernel-mode vector support to explicitly enabled sections. Sure maybe there's other stuff to do in kernel vector land, but we can at least get something going.
Quoted 1 line
Andy, what do you think?
I'll wait on Andy to see, but I generally agree we should merge something for this cycle.
Andy: maybe just send a patch set with what you think is the best way to go? Then we have one target approach and we can get things moving.
Quoted 1 line
- Eric
Hi Palmer,
Quoted 58 lines · Palmer Dabbelt
Quoted 27 lines · On Tue, 05 Dec 2023 23:41:55 PST (-0800), [email protected] wrote:
Hi Jerry,
Quoted 18 lines · Jerry Shih
Quoted 10 lines · Eric Biggers
Quoted 5 lines · Jerry Shih
This series depend on:
2. support kernel-mode vector3. vector crypto extensions detection
What's the status of getting these prerequisites merged?
- Eric
The latest extension detection patch version is v5.
It's still under reviewing.
But I think the checking codes used in this crypto patch series will not change. We could just wait and rebase when it's merged.Quoted 5 lines
The latest kernel-mode vector patch version is v3.
This patch doesn't work with qemu(hit kernel panic when using vector). It's not clear for the status. Could we still do the reviewing process for the gluing code and the crypto asm parts?
Quoted 8 lines
I'm almost ready to give my Reviewed-by for this whole series. The problem is that it can't be merged until its prerequisites are merged.
Andy Chiu's last patchset "riscv: support kernel-mode Vector" was 2 months ago, but he also gave a talk at Plumbers about it more recently
(https://www.youtube.com/watch?v=eht3PccEn5o). So I assume he's still working on it. It sounds like he's also going to include support for preemption, and optimizations to memcpy, memset, memmove, and copy_{to,from}_user.So I think we just got blocked on not knowing if turning on vector everywhere in the kernel was a good idea -- it's not what any other port does despite there having been some discussions floating around, but we never really figured out why. I can come up with some possible performance pathologies related to having vector on in many contexts, but it's all theory as there's not really any vector hardware that works upstream (though the K230 is starting to come along, so maybe that'll sort itself out).
Last we talked I think the general consensus is that we'd waited long enough, if nobody has a concrete objection we should just take it and see -- sure maybe there's some possible issues, but anything could have issues.
Quoted 5 lines
I think it would be a good idea to split out the basic support for kernel_vector_{begin,end} so that the users of them, as well as the preemption support, can be considered and merged separately. Maybe patch 1 of the series
(https://lore.kernel.org/r/[email protected]) is all that's needed initially?
Quoted 4 lines
I'm fine with that sort of approach too, it's certainly more in line with other ports to just restrict the kernel-mode vector support to explicitly enabled sections. Sure maybe there's other stuff to do in kernel vector land, but we can at least get something going.
With the current approach of preempt_v we still need kernel_vector_begin/end to explicitly mark enabled sections. But indeed, preempt_v will make it easy to do function-wise, thread-wise enable if people need it.
Quoted 8 lines
Quoted 2 lines
Andy, what do you think?
I'll wait on Andy to see, but I generally agree we should merge something for this cycle.
Andy: maybe just send a patch set with what you think is the best way to go? Then we have one target approach and we can get things moving.
Yes, I think we can split. It will introduce some overhead on my side, but at least we can get some parts moving. I was preempted by some higher priority tasks. Luckily I am back now. Please expect v4 by next week, I hope it won't be too late for the cycle.
Quoted 2 lines
Quoted 2 lines
- Eric
Hi Jerry,
Quoted 3 lines · Jerry Shih
Changelog v2:
- Turn to use simd skcipher interface for AES-CBC/CTR/ECB/XTS and Chacha20.
If I understand correctly, the RISC-V kernel-mode vector support now seems to be heading down the path of supporting softirq context, as I had suggested originally. With patches 1-2 of Andy Chiu's latest patchset
"[v7, 00/10] riscv: support kernel-mode Vector"
(https://lore.kernel.org/linux-riscv/[email protected]).
applied, the kernel has basic support for kernel-mode vector, including in softirq context.
With that being the case, "skcipher" algorithms can just use the RISC-V vector unit unconditionally, given that skcipher only supports task and softirq context. Therefore, can you consider undoing your change that added fallbacks using the simd helper (crypto/simd.c)? Thanks!
- Eric
Hi Jerry,
Quoted 15 lines · Eric Biggers
Hi Jerry,
Quoted 3 lines · Jerry Shih
Changelog v2:
- Turn to use simd skcipher interface for AES-CBC/CTR/ECB/XTS and Chacha20.If I understand correctly, the RISC-V kernel-mode vector support now seems to be heading down the path of supporting softirq context, as I had suggested originally. With patches 1-2 of Andy Chiu's latest patchset
"[v7, 00/10] riscv: support kernel-mode Vector"
(https://lore.kernel.org/linux-riscv/[email protected]).
applied, the kernel has basic support for kernel-mode vector, including in softirq context.
Quoted 5 lines
With that being the case, "skcipher" algorithms can just use the RISC-V vector unit unconditionally, given that skcipher only supports task and softirq context. Therefore, can you consider undoing your change that added fallbacks using the simd helper (
crypto/simd.c)? Thanks!
I had a go at incorporating my suggestions into your patchset, and rebasing the patchset onto riscv/for-next plus the first two patches of "[v9, 00/10] riscv:
support kernel-mode Vector". You can get the result from branch "riscv-crypto" of https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git.
Everything seems to work (tested in QEMU, as usual).
Please consider using it when you send out v4; thanks! I can even send it out myself, if you want, but I assume you're still "owning" the patchset.
- Eric
Quoted 29 lines · Eric Biggers
Hi Jerry,
Quoted 15 lines · Eric Biggers
Hi Jerry,
Quoted 3 lines · Jerry Shih
Changelog v2:
- Turn to use simd skcipher interface for AES-CBC/CTR/ECB/XTS and Chacha20.If I understand correctly, the RISC-V kernel-mode vector support now seems to be heading down the path of supporting softirq context, as I had suggested originally. With patches 1-2 of Andy Chiu's latest patchset
"[v7, 00/10] riscv: support kernel-mode Vector"
(https://lore.kernel.org/linux-riscv/[email protected]).
applied, the kernel has basic support for kernel-mode vector, including in softirq context.Quoted 5 lines
With that being the case, "skcipher" algorithms can just use the RISC-V vector unit unconditionally, given that skcipher only supports task and softirq context. Therefore, can you consider undoing your change that added fallbacks using the simd helper (
crypto/simd.c)? Thanks!I had a go at incorporating my suggestions into your patchset, and rebasing the patchset onto riscv/for-next plus the first two patches of "[v9, 00/10] riscv:
support kernel-mode Vector". You can get the result from branch "riscv-crypto" of https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git.
Everything seems to work (tested in QEMU, as usual).
Quoted 4 lines
Please consider using it when you send out v4; thanks! I can even send it out myself, if you want, but I assume you're still "owning" the patchset.
- Eric
Thank you. I sent the v4 patch.
-Jerry