RISC-V: provide some accelerated cryptography implementations using vector extensions
Subsystems (2)
v2 cover RISC-V: provide some accelerated cryptography implementations using vector extensions
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 opcodes into .S files instead of asm mnemonics. The reason for using opcodes is because that we don't want to re-implement all crypto functions from OpenSSL. We will try to replace perl-asm with asm mnemonics in the future. It needs lots of extensions checking for toolchains. We already have RVV 1.0 in kernel, so we will replace the RVV opcodes with asm mnemonics at first.
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): 4, 7, 8, 9, 10, 11, 12, 13 New patch: 6,
Unchanged patch: 1, 2, 3, 5
Deleted patch: -
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 (11):
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
crypto: scatterwalk - Add scatterwalk_next() to get the next
scatterlist in scatter_walk
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 | 514 +++++++ arch/riscv/crypto/aes-riscv64-glue.c | 151 ++ 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 | 1339 +++++++++++++++++ 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 | 828 ++++++++++ arch/riscv/crypto/sha256-riscv64-glue.c | 145 ++ .../sha256-riscv64-zvknha_or_zvknhb-zvkb.pl | 318 ++++ arch/riscv/crypto/sha512-riscv64-glue.c | 139 ++ .../crypto/sha512-riscv64-zvknhb-zvkb.pl | 266 ++++ arch/riscv/crypto/sm3-riscv64-glue.c | 124 ++ arch/riscv/crypto/sm3-riscv64-zvksh.pl | 230 +++ 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 + include/crypto/scatterwalk.h | 9 +- 27 files changed, 6745 insertions(+), 2 deletions(-) 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 v2 1/13 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
Re: [PATCH v2 01/13] RISC-V: add helper function to read the vector VLEN
Quoted 10 lines · Jerry Shih
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_vsizethat contains the value of "32 vector registers with vlenb length" that gets filled during boot. vlenb is the value contained in theCSR_VLENBregister and the value represents "VLEN / 8".
Quoted 8 lines
So add
riscv_vector_vlen()to return the actual VLEN value for in-kernel users when they need to check the available VLEN.Signed-off-by Heiko Stuebner <[email protected]>Signed-off-by Jerry Shih <[email protected]>--- arch/riscv/include/asm/vector.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
- Eric
PATCH v2 2/13 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
Re: [PATCH v2 02/13] RISC-V: hook new crypto subdir into build-system
Quoted 7 lines · Jerry Shih
From: Heiko Stuebner <[email protected]>
Create a crypto subdirectory for added accelerated cryptography routines and hook it into the riscv
Kbuildand the main cryptoKconfig.Signed-off-by Heiko Stuebner <[email protected]>Signed-off-by Jerry Shih <[email protected]>
Quoted 8 lines
--- 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
- Eric
PATCH v2 3/13 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 and 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.
--- arch/riscv/crypto/riscv.pm | 828 +++++++++++++++++++++++++++++++++++++ 1 file changed, 828 insertions(+) create mode 100644 arch/riscv/crypto/riscv.pm
arch/riscv/crypto/riscv.pm new file +828 -0
@@ -0,0 +1,828 @@ +#! /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; +} + +# Read the sew setting(8, 16, 32 and 64) and convert to vsew encoding. +sub read_sew { + my $sew_setting = shift; + + if ($sew_setting eq "e8") { + return 0; + } elsif ($sew_setting eq "e16") { + return 1; + } elsif ($sew_setting eq "e32") { + return 2; + } elsif ($sew_setting eq "e64") { + return 3; + } else { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unsupported SEW setting:".$sew_setting."\n".$trace); + } +} + +# Read the LMUL settings and convert to vlmul encoding. +sub read_lmul { + my $lmul_setting = shift; + + if ($lmul_setting eq "mf8") { + return 5; + } elsif ($lmul_setting eq "mf4") { + return 6; + } elsif ($lmul_setting eq "mf2") { + return 7; + } elsif ($lmul_setting eq "m1") { + return 0; + } elsif ($lmul_setting eq "m2") { + return 1; + } elsif ($lmul_setting eq "m4") { + return 2; + } elsif ($lmul_setting eq "m8") { + return 3; + } else { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unsupported LMUL setting:".$lmul_setting."\n".$trace); + } +} + +# Read the tail policy settings and convert to vta encoding. +sub read_tail_policy { + my $tail_setting = shift; + + if ($tail_setting eq "ta") { + return 1; + } elsif ($tail_setting eq "tu") { + return 0; + } else { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unsupported tail policy setting:".$tail_setting."\n".$trace); + } +} + +# Read the mask policy settings and convert to vma encoding. +sub read_mask_policy { + my $mask_setting = shift; + + if ($mask_setting eq "ma") { + return 1; + } elsif ($mask_setting eq "mu") { + return 0; + } else { + my $trace = ""; + if ($have_stacktrace) { + $trace = Devel::StackTrace->new->as_string; + } + die("Unsupported mask policy setting:".$mask_setting."\n".$trace); + } +} + +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 instructions + +sub vadd_vv { + # vadd.vv vd, vs2, vs1, vm + my $template = 0b000000_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)); +} + +sub vadd_vx { + # vadd.vx vd, vs2, rs1, vm + my $template = 0b000000_0_00000_00000_100_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vsub_vv { + # vsub.vv vd, vs2, vs1, vm + my $template = 0b000010_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)); +} + +sub vsub_vx { + # vsub.vx vd, vs2, rs1, vm + my $template = 0b000010_0_00000_00000_100_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vid_v { + # vid.v vd + my $template = 0b0101001_00000_10001_010_00000_1010111; + my $vd = read_vreg shift; + return ".word ".($template | ($vd << 7)); +} + +sub viota_m { + # viota.m vd, vs2, vm + my $template = 0b010100_0_00000_10000_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 vle8_v { + # vle8.v vd, (rs1), vm + my $template = 0b000000_0_00000_00000_000_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($rs1 << 15) | ($vd << 7)); +} + +sub vle32_v { + # vle32.v vd, (rs1), vm + my $template = 0b000000_0_00000_00000_110_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($rs1 << 15) | ($vd << 7)); +} + +sub vle64_v { + # vle64.v vd, (rs1) + my $template = 0b0000001_00000_00000_111_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($rs1 << 15) | ($vd << 7)); +} + +sub vlse32_v { + # vlse32.v vd, (rs1), rs2 + my $template = 0b0000101_00000_00000_110_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($rs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vlsseg_nf_e32_v { + # vlsseg<nf>e32.v vd, (rs1), rs2 + my $template = 0b0000101_00000_00000_110_00000_0000111; + my $nf = shift; + $nf -= 1; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($nf << 29) | ($rs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vlse64_v { + # vlse64.v vd, (rs1), rs2 + my $template = 0b0000101_00000_00000_111_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($rs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vluxei8_v { + # vluxei8.v vd, (rs1), vs2, vm + my $template = 0b000001_0_00000_00000_000_00000_0000111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $vs2 = read_vreg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)); +} + +sub vmerge_vim { + # vmerge.vim vd, vs2, imm, v0 + my $template = 0b0101110_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $imm = shift; + return ".word ".($template | ($vs2 << 20) | ($imm << 15) | ($vd << 7)); +} + +sub vmerge_vvm { + # vmerge.vvm vd vs2 vs1 + my $template = 0b0101110_00000_00000_000_00000_1010111; + 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 vmseq_vi { + # vmseq.vi vd vs1, imm + my $template = 0b0110001_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs1 = read_vreg shift; + my $imm = shift; + return ".word ".($template | ($vs1 << 20) | ($imm << 15) | ($vd << 7)) +} + +sub vmsgtu_vx { + # vmsgtu.vx vd vs2, rs1, vm + my $template = 0b011110_0_00000_00000_100_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($rs1 << 15) | ($vd << 7)) +} + +sub vmv_v_i { + # vmv.v.i vd, imm + my $template = 0b0101111_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $imm = shift; + return ".word ".($template | ($imm << 15) | ($vd << 7)); +} + +sub vmv_v_x { + # vmv.v.x vd, rs1 + my $template = 0b0101111_00000_00000_100_00000_1010111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($rs1 << 15) | ($vd << 7)); +} + +sub vmv_v_v { + # vmv.v.v vd, vs1 + my $template = 0b0101111_00000_00000_000_00000_1010111; + my $vd = read_vreg shift; + my $vs1 = read_vreg shift; + return ".word ".($template | ($vs1 << 15) | ($vd << 7)); +} + +sub vor_vv_v0t { + # vor.vv vd, vs2, vs1, v0.t + my $template = 0b0010100_00000_00000_000_00000_1010111; + 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 vse8_v { + # vse8.v vd, (rs1), vm + my $template = 0b000000_0_00000_00000_000_00000_0100111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($rs1 << 15) | ($vd << 7)); +} + +sub vse32_v { + # vse32.v vd, (rs1), vm + my $template = 0b000000_0_00000_00000_110_00000_0100111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($rs1 << 15) | ($vd << 7)); +} + +sub vssseg_nf_e32_v { + # vssseg<nf>e32.v vs3, (rs1), rs2 + my $template = 0b0000101_00000_00000_110_00000_0100111; + my $nf = shift; + $nf -= 1; + my $vs3 = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($nf << 29) | ($rs2 << 20) | ($rs1 << 15) | ($vs3 << 7)); +} + +sub vsuxei8_v { + # vsuxei8.v vs3, (rs1), vs2, vm + my $template = 0b000001_0_00000_00000_000_00000_0100111; + my $vs3 = read_vreg shift; + my $rs1 = read_reg shift; + my $vs2 = read_vreg shift; + my $vm = read_mask_vreg shift; + return ".word ".($template | ($vm << 25) | ($vs2 << 20) | ($rs1 << 15) | ($vs3 << 7)); +} + +sub vse64_v { + # vse64.v vd, (rs1) + my $template = 0b0000001_00000_00000_111_00000_0100111; + my $vd = read_vreg shift; + my $rs1 = read_reg shift; + return ".word ".($template | ($rs1 << 15) | ($vd << 7)); +} + +sub vsetivli__x0_2_e64_m1_tu_mu { + # vsetivli x0, 2, e64, m1, tu, mu + return ".word 0xc1817057"; +} + +sub vsetivli__x0_4_e32_m1_tu_mu { + # vsetivli x0, 4, e32, m1, tu, mu + return ".word 0xc1027057"; +} + +sub vsetivli__x0_4_e64_m1_tu_mu { + # vsetivli x0, 4, e64, m1, tu, mu + return ".word 0xc1827057"; +} + +sub vsetivli__x0_8_e32_m1_tu_mu { + # vsetivli x0, 8, e32, m1, tu, mu + return ".word 0xc1047057"; +} + +sub vsetvli { + # vsetvli rd, rs1, vtypei + my $template = 0b0_00000000000_00000_111_00000_1010111; + my $rd = read_reg shift; + my $rs1 = read_reg shift; + my $sew = read_sew shift; + my $lmul = read_lmul shift; + my $tail_policy = read_tail_policy shift; + my $mask_policy = read_mask_policy shift; + my $vtypei = ($mask_policy << 7) | ($tail_policy << 6) | ($sew << 3) | $lmul; + + return ".word ".($template | ($vtypei << 20) | ($rs1 << 15) | ($rd << 7)); +} + +sub vsetivli { + # vsetvli rd, uimm, vtypei + my $template = 0b11_0000000000_00000_111_00000_1010111; + my $rd = read_reg shift; + my $uimm = shift; + my $sew = read_sew shift; + my $lmul = read_lmul shift; + my $tail_policy = read_tail_policy shift; + my $mask_policy = read_mask_policy shift; + my $vtypei = ($mask_policy << 7) | ($tail_policy << 6) | ($sew << 3) | $lmul; + + return ".word ".($template | ($vtypei << 20) | ($uimm << 15) | ($rd << 7)); +} + +sub vslidedown_vi { + # vslidedown.vi vd, vs2, uimm + my $template = 0b0011111_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vslidedown_vx { + # vslidedown.vx vd, vs2, rs1 + my $template = 0b0011111_00000_00000_100_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 vslideup_vi_v0t { + # vslideup.vi vd, vs2, uimm, v0.t + my $template = 0b0011100_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vslideup_vi { + # vslideup.vi vd, vs2, uimm + my $template = 0b0011101_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vsll_vi { + # vsll.vi vd, vs2, uimm, vm + my $template = 0b1001011_00000_00000_011_00000_1010111; + my $vd = read_vreg shift; + my $vs2 = read_vreg shift; + my $uimm = shift; + return ".word ".($template | ($vs2 << 20) | ($uimm << 15) | ($vd << 7)); +} + +sub vsrl_vx { + # vsrl.vx vd, vs2, rs1 + my $template = 0b1010001_00000_00000_100_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 vsse32_v { + # vse32.v vs3, (rs1), rs2 + my $template = 0b0000101_00000_00000_110_00000_0100111; + my $vs3 = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($rs2 << 20) | ($rs1 << 15) | ($vs3 << 7)); +} + +sub vsse64_v { + # vsse64.v vs3, (rs1), rs2 + my $template = 0b0000101_00000_00000_111_00000_0100111; + my $vs3 = read_vreg shift; + my $rs1 = read_reg shift; + my $rs2 = read_reg shift; + return ".word ".($template | ($rs2 << 20) | ($rs1 << 15) | ($vs3 << 7)); +} + +sub vxor_vv_v0t { + # vxor.vv vd, vs2, vs1, v0.t + my $template = 0b0010110_00000_00000_000_00000_1010111; + 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 vxor_vv { + # vxor.vv vd, vs2, vs1 + my $template = 0b0010111_00000_00000_000_00000_1010111; + 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 vzext_vf2 { + # vzext.vf2 vd, vs2, vm + my $template = 0b010010_0_00000_00110_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)); +} + +# 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 v2 4/13 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 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 | 151 ++++++ arch/riscv/crypto/aes-riscv64-glue.h | 18 + arch/riscv/crypto/aes-riscv64-zvkned.pl | 593 ++++++++++++++++++++++++ 5 files changed, 784 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 +151 -0
151 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 (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 +593 -0
593 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 14 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-glue.c new file +14
-0+int riscv64_aes_setkey (struct crypto_aes_ctx *ctx,const u8 *key, +unsigned int keylen) +{ +int ret; + + ret =aes_check_keylen (keylen); +if (ret <0 ) +return -EINVAL ; + +/* +* The RISC-V AES vector crypto key expanding doesn't support AES-192. +* Use the generic software key expanding for that case. +*/ +if ((keylen ==16 || keylen ==32 ) &&crypto_simd_usable ()) {
arch/riscv/crypto/aes-riscv64-glue.c line 55
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +10
-0+/* +* All zvkned-based functions use encryption expanding keys for both +* encryption and decryption. +*/ +kernel_vector_begin (); +rv64i_zvkned_set_encrypt_key (key, keylen, ctx); +kernel_vector_end (); + }else { + ret =aes_expandkey (ctx, key, keylen); + }
rv64i_zvkned_set_encrypt_key() does not initialize crypto_aes_ctx::key_dec. So, decryption results will be incorrect if !crypto_simd_usable() later.
arch/riscv/crypto/aes-riscv64-glue.c line 88
Quoted 2 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +2
-0+static int aes_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen)
It's best to avoid generic-sounding function names like this that could collide with functions in crypto/ or lib/crypto/. A better name for this function, for example, would be aes_setkey_zvkned().
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-zvkned.pl new file +0
-0
[...]
Quoted 1 line
+L_enc_128:
[...]
Quoted 1 line
+L_enc_192:
[...]
Quoted 1 line
+L_enc_256:
There's some severe source code duplication going on in the AES assembly, with the three AES variants having separate source code. You can just leave this as-is since this is what was merged into OpenSSL and we are borrowing that for now, but I do expect that we'll want to clean this up later.
- Eric
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 20 lines · Eric Biggers
Quoted 19 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-glue.c new file +19
-0+int riscv64_aes_setkey (struct crypto_aes_ctx *ctx,const u8 *key, +unsigned int keylen) +{ +int ret; + + ret =aes_check_keylen (keylen); +if (ret <0 ) +return -EINVAL ; + +/* +* The RISC-V AES vector crypto key expanding doesn't support AES-192. +* Use the generic software key expanding for that case. +*/ +if ((keylen ==16 || keylen ==32 ) &&crypto_simd_usable ()) { +/* +* All zvkned-based functions use encryption expanding keys for both +* encryption and decryption. +*/ +kernel_vector_begin ();
Quoted 8 lines
arch/riscv/crypto/aes-riscv64-glue.c line 55
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+rv64i_zvkned_set_encrypt_key (key, keylen, ctx); +kernel_vector_end (); + }else { + ret =aes_expandkey (ctx, key, keylen); + }
rv64i_zvkned_set_encrypt_key()does not initializecrypto_aes_ctx::key_dec. So, decryption results will be incorrect if !crypto_simd_usable() later.
Will we have the situation that crypto_simd_usable() condition is not consistent during the aes_setkey(), aes_enc/dec()? If yes, all accelerated(or HW specific) crypto algorithms should do the same implementations as the sw fallback path since the crypto_simd_usable() will change back and forth.
Quoted 6 lines
arch/riscv/crypto/aes-riscv64-glue.c line 88
Quoted 2 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +2
-0+static int aes_setkey (struct crypto_tfm *tfm,const u8 *key, +unsigned int keylen)It's best to avoid generic-sounding function names like this that could collide with functions in
crypto/orlib/crypto/. A better name for this function, for example, would beaes_setkey_zvkned().
Thx, I will fix that.
Quoted 8 lines
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-zvkned.pl new file +0
-0[...]
Quoted 1 line
+L_enc_128:
[...]
Quoted 8 lines
Quoted 1 line
+L_enc_192:
[...]
Quoted 1 line
+L_enc_256:
There's some severe source code duplication going on in the AES assembly, with the three AES variants having separate source code. You can just leave this as-is since this is what was merged into OpenSSL and we are borrowing that for now, but I do expect that we'll want to clean this up later.
Do we prefer the code with the branches instead of the specified implementation? We could make AES-128/192/256 together like:
@{[vaesz_vs $V24, $V1]}
@{[vaesem_vs $V24, $V2]}
@{[vaesem_vs $V24, $V3]}
@{[vaesem_vs $V24, $V4]}
@{[vaesem_vs $V24, $V5]}
@{[vaesem_vs $V24, $V6]}
@{[vaesem_vs $V24, $V7]}
@{[vaesem_vs $V24, $V8]}
@{[vaesem_vs $V24, $V9]}
@{[vaesem_vs $V24, $V10]}
beq $ROUND, $ROUND_11, 1f
@{[vaesem_vs $V24, $V11]}
@{[vaesem_vs $V24, $V12]}
beq $ROUND, $ROUND_13, 1f
@{[vaesem_vs $V24, $V13]}
@{[vaesem_vs $V24, $V14]}
1:
@{[vaesef_vs $V24, $V15]}But we will have the additional costs for the branches.
Quoted 1 line
- Eric
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 30 lines · Jerry Shih
Quoted 20 lines · Eric Biggers
Quoted 19 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-glue.c new file +19
-0+int riscv64_aes_setkey (struct crypto_aes_ctx *ctx,const u8 *key, +unsigned int keylen) +{ +int ret; + + ret =aes_check_keylen (keylen); +if (ret <0 ) +return -EINVAL ; + +/* +* The RISC-V AES vector crypto key expanding doesn't support AES-192. +* Use the generic software key expanding for that case. +*/ +if ((keylen ==16 || keylen ==32 ) &&crypto_simd_usable ()) { +/* +* All zvkned-based functions use encryption expanding keys for both +* encryption and decryption. +*/ +kernel_vector_begin ();Quoted 8 lines
arch/riscv/crypto/aes-riscv64-glue.c line 55
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+rv64i_zvkned_set_encrypt_key (key, keylen, ctx); +kernel_vector_end (); + }else { + ret =aes_expandkey (ctx, key, keylen); + }
rv64i_zvkned_set_encrypt_key()does not initializecrypto_aes_ctx::key_dec. So, decryption results will be incorrect if !crypto_simd_usable() later.
Quoted 4 lines
Will we have the situation that
crypto_simd_usable()condition is not consistent during theaes_setkey(), aes_enc/dec()? If yes, all accelerated(or HW specific) crypto algorithms should do the same implementations as the sw fallback path since thecrypto_simd_usable()will change back and forth.
Yes, the calls to one "crypto_cipher" can happen in different contexts. For example, crypto_simd_usable() can be true during setkey and false during decrypt, or vice versa.
If the RISC-V decryption code wants to use the regular key schedule (key_enc) instead of the "Equivalent Inverse Cipher key schedule" (key_dec), that's perfectly fine, but setkey still needs to initialize key_dec in case the fallback to aes_decrypt() gets taken.
Quoted 39 lines
Quoted 8 lines
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-zvkned.pl new file +0
-0[...]
Quoted 1 line
+L_enc_128:
[...]
Quoted 8 lines
Quoted 1 line
+L_enc_192:
[...]
Quoted 1 line
+L_enc_256:
There's some severe source code duplication going on in the AES assembly, with the three AES variants having separate source code. You can just leave this as-is since this is what was merged into OpenSSL and we are borrowing that for now, but I do expect that we'll want to clean this up later.
Do we prefer the code with the branches instead of the specified implementation? We could make AES-128/192/256 together like:
@{[vaesz_vs $V24, $V1]} @{[vaesem_vs $V24, $V2]} @{[vaesem_vs $V24, $V3]} @{[vaesem_vs $V24, $V4]} @{[vaesem_vs $V24, $V5]} @{[vaesem_vs $V24, $V6]} @{[vaesem_vs $V24, $V7]} @{[vaesem_vs $V24, $V8]} @{[vaesem_vs $V24, $V9]} @{[vaesem_vs $V24, $V10]} beq $ROUND, $ROUND_11, 1f @{[vaesem_vs $V24, $V11]} @{[vaesem_vs $V24, $V12]} beq $ROUND, $ROUND_13, 1f @{[vaesem_vs $V24, $V13]} @{[vaesem_vs $V24, $V14]} 1: @{[vaesef_vs $V24, $V15]}
Quoted 2 lines
But we will have the additional costs for the branches.
That needs to be decided on a case by case basis depending on the performance impact and how much binary code is saved. On some architectures, separate binary code for AES-{128,192,256} has been found to be worthwhile. However, that does *not* mean that they need to have separate source code. Take a look at how arch/x86/crypto/aes_ctrby8_avx-x86_64.S generates code for all the AES variants using macros, for example.
Anyway, I don't think you should bother making too many changes to the "perlasm" files. If we decide to make major cleanups I think we should just replace them with .S files (which already support macros).
- Eric
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
In reply to Jerry Shiharch/riscv/crypto/aes-riscv64-glue.c line 130
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+static inline bool check_aes_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 ; +}
I'm not keen on this construct, where you are checking vlen greater than 128 and the presence of Zvkned without checking for the presence of V itself. Can you use "has_vector()" in any places where you depend on the presence of vector please?
Also, there are potentially a lot of places in this drivers where you can replace "riscv_isa_extension_available()" with "riscv_has_extension_likely()". The latter is optimised with alternatives, so in places that are going to be evaluated frequently it may be beneficial for you.
Cheers,
Conor.
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 10 lines · Conor Dooley
arch/riscv/crypto/aes-riscv64-glue.c line 130
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+static inline bool check_aes_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 ; +}I'm not keen on this construct, where you are checking vlen greater than 128 and the presence of Zvkned without checking for the presence of V itself. Can you use "
has_vector()" in any places where you depend on the presence of vector please?
Shouldn't both of those things imply vector support already?
Quoted 5 lines
Also, there are potentially a lot of places in this drivers where you can replace "
riscv_isa_extension_available()" with "riscv_has_extension_likely()". The latter is optimised with alternatives, so in places that are going to be evaluated frequently it may be beneficial for you.
These extension checks are only executed in module_init functions, so they're not performance critical.
- Eric
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Conor Dooley
arch/riscv/crypto/aes-riscv64-glue.c line 130
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+static inline bool check_aes_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 ; +}I'm not keen on this construct, where you are checking vlen greater than 128 and the presence of Zvkned without checking for the presence of V itself. Can you use "
has_vector()" in any places where you depend on the presence of vector please?
Quoted 1 line
Shouldn't both of those things imply vector support already?
The vector crypto extensions imply V extension. Should we still need to check the V explicitly?
https://github.com/riscv/riscv-crypto/blob/main/doc/vector/riscv-crypto-spec-vector.adoc#1-extensions-overview
Quoted 8 lines
Quoted 5 lines
Also, there are potentially a lot of places in this drivers where you can replace "
riscv_isa_extension_available()" with "riscv_has_extension_likely()". The latter is optimised with alternatives, so in places that are going to be evaluated frequently it may be beneficial for you.These extension checks are only executed in
module_initfunctions, so they're not performance critical.
All riscv_isa_extension_available() calls in crypto drivers are called once in the module init calls. Should we still need that riscv_has_extension_likely() with a little more code size?
Quoted 1 line
- Eric
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 13 lines · Jerry Shih
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Conor Dooley
arch/riscv/crypto/aes-riscv64-glue.c line 130
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+static inline bool check_aes_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 ; +}I'm not keen on this construct, where you are checking vlen greater than 128 and the presence of Zvkned without checking for the presence of V itself. Can you use "
has_vector()" in any places where you depend on the presence of vector please?
Quoted 5 lines
Quoted 1 line
Shouldn't both of those things imply vector support already?
The vector crypto extensions imply
Vextension. Should we still need to check theVexplicitly?
https://github.com/riscv/riscv-crypto/blob/main/doc/vector/riscv-crypto-spec-vector.adoc#1-extensions-overview
The check for Zkvned is only for whether or not Zvkned has been provided in the DT or ACPI tables, it doesn't mean that the kernel supports the V extension. I could see something like a hypervisor that does not support vector parsing the "v" out of the DT or ACPI tables but not eliminating every single extension that may depend on vector support.
The latter check is, IMO, an implementation detail and also should not be used to imply that vector is supported.
Actually, Andy - questions for you. If the vsize is not homogeneous we do not support vector for userspace and we disable vector in hwcap, but riscv_v_size will have been set by riscv_fill_hwcap(). Is the disabling of vector propagated to other locations in the kernel that inform userspace, like hwprobe? I only skimmed the in-kernel vector patchset, but I could not see anything there that ensures homogeneity either. Should has_vector() calls start to fail if the vsize is not homogeneous? I feel like they should, but I might very well be missing something here.
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Also, there are potentially a lot of places in this drivers where you can replace "
riscv_isa_extension_available()" with "riscv_has_extension_likely()". The latter is optimised with alternatives, so in places that are going to be evaluated frequently it may be beneficial for you.These extension checks are only executed in
module_initfunctions, so they're not performance critical.
That's fine, they can continue as they are so.
Cheers,
Conor.
Re: [PATCH v2 04/13] RISC-V: crypto: add Zvkned accelerated AES implementation
Quoted 20 lines · Conor Dooley
Quoted 13 lines · Jerry Shih
Quoted 12 lines · Eric Biggers
Quoted 10 lines · Conor Dooley
arch/riscv/crypto/aes-riscv64-glue.c line 130
Quoted 5 lines
arch/riscv/crypto/aes-riscv64-glue.c new file +5
-0+static inline bool check_aes_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 ; +}I'm not keen on this construct, where you are checking vlen greater than 128 and the presence of Zvkned without checking for the presence of V itself. Can you use "
has_vector()" in any places where you depend on the presence of vector please?Quoted 5 lines
Quoted 1 line
Shouldn't both of those things imply vector support already?
The vector crypto extensions imply
Vextension. Should we still need to check theVexplicitly?
https://github.com/riscv/riscv-crypto/blob/main/doc/vector/riscv-crypto-spec-vector.adoc#1-extensions-overview
Quoted 8 lines
The check for Zkvned is only for whether or not Zvkned has been provided in the DT or ACPI tables, it doesn't mean that the kernel supports the V extension. I could see something like a hypervisor that does not support vector parsing the "v" out of the DT or ACPI tables but not eliminating every single extension that may depend on vector support.
The latter check is, IMO, an implementation detail and also should not be used to imply that vector is supported.
First, the RISC-V crypto files are only compiled when CONFIG_RISCV_ISA_V=y. So in those files, we know that the kernel supports V if the hardware does.
If the hardware can indeed declare extensions like Zvkned without declaring V, that sounds problematic. Would /proc/cpuinfo end up with the same misleading information in that case, in which case userspace would have the same problem too? I think that such misconfigurations are best handled centrally by having the low-level architecture code in the kernel clear all extensions that depend on missing extensions. IIRC there have been issues like this on x86, and that was the fix that was implemented. See arch/x86/kernel/cpu/cpuid-deps.c
- Eric
PATCH v2 5/13 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
Re: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
Quoted 25 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>--- 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 +0
-0
crypto/simd.c struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname, · line 186
Quoted 6 lines
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;
What are the consequences of this bug? I wonder if it actually matters? The "inner" algorithm is the one that actually gets used for the "walk", right?
- Eric
Re: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
Quoted 36 lines · Eric Biggers
Quoted 25 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>---
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 +0
-0crypto/simd.c struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname, · line 186
Quoted 6 lines
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;What are the consequences of this bug? I wonder if it actually matters? The "inner" algorithm is the one that actually gets used for the "walk", right?
Quoted 1 line
- Eric
Without this, we might still use chunksize or cra_blocksize as the walksize even though we setup with the larger walksize.
Here is the code for the walksize default value:
static int skcipher_prepare_alg(struct skcipher_alg *alg)
{
...
if (!alg->chunksize)
alg->chunksize = base->cra_blocksize;
if (!alg->walksize)
alg->walksize = alg->chunksize;And we already have the bigger walksize for x86 aes-xts.
.base = {
.cra_name = "__xts(aes)",
...
},
.walksize = 2 * AES_BLOCK_SIZE,The x86 aes-xts only uses one walk to handle the tail elements. It assumes that the walksize contains 2 aes blocks. If walksize is not set correctly, maybe some tail elements is not processed in simd-cipher mode for x86 aes-xts.
-Jerry
Re: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
Quoted 58 lines · Jerry Shih
Quoted 36 lines · Eric Biggers
Quoted 25 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>---
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 +0
-0crypto/simd.c struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname, · line 186
Quoted 6 lines
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;What are the consequences of this bug? I wonder if it actually matters? The "inner" algorithm is the one that actually gets used for the "walk", right?
Quoted 1 line
- Eric
Without this, we might still use chunksize or
cra_blocksizeas the walksize even though we setup with the larger walksize.Here is the code for the walksize default value:
static int skcipher_prepare_alg(struct skcipher_alg *alg) { ... if (!alg->chunksize) alg->chunksize = base->cra_blocksize; if (!alg->walksize) alg->walksize = alg->chunksize;And we already have the bigger walksize for x86 aes-xts.
.base = { .cra_name = "__xts(aes)", ... }, .walksize = 2 * AES_BLOCK_SIZE,
Quoted 3 lines
The x86 aes-xts only uses one
walkto handle the tail elements. It assumes that the walksize contains 2 aes blocks. If walksize is not set correctly, maybe some tail elements is not processed in simd-cipher mode for x86 aes-xts.
With the SIMD helper there are three "algorithms": the underlying algorithm, the cryptd algorithm, and the simd algorithm. This patch makes the "walksize" property be propagated from the underlying algorithm to the cryptd and simd algorithms. I don't see how that actually makes a difference, since the only place the skcipher_walk happens is on the underlying algorithm. So it uses the "walksize" from the underlying algorithm, right?
- Eric
Re: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
Quoted 63 lines · Eric Biggers
Quoted 58 lines · Jerry Shih
Quoted 36 lines · Eric Biggers
Quoted 25 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>---
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 +0
-0crypto/simd.c struct simd_skcipher_alg *simd_skcipher_create_compat(const char *algname, · line 186
Quoted 6 lines
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;What are the consequences of this bug? I wonder if it actually matters? The "inner" algorithm is the one that actually gets used for the "walk", right?
Quoted 1 line
- Eric
Without this, we might still use chunksize or
cra_blocksizeas the walksize even though we setup with the larger walksize.Here is the code for the walksize default value:
static int skcipher_prepare_alg(struct skcipher_alg *alg) { ... if (!alg->chunksize) alg->chunksize = base->cra_blocksize; if (!alg->walksize) alg->walksize = alg->chunksize;And we already have the bigger walksize for x86 aes-xts.
.base = { .cra_name = "__xts(aes)", ... }, .walksize = 2 * AES_BLOCK_SIZE,Quoted 3 lines
The x86 aes-xts only uses one
walkto handle the tail elements. It assumes that the walksize contains 2 aes blocks. If walksize is not set correctly, maybe some tail elements is not processed in simd-cipher mode for x86 aes-xts.
Quoted 8 lines
With the SIMD helper there are three "algorithms": the underlying algorithm, the cryptd algorithm, and the simd algorithm. This patch makes the "walksize" property be propagated from the underlying algorithm to the cryptd and simd algorithms. I don't see how that actually makes a difference, since the only place the
skcipher_walkhappens is on the underlying algorithm. So it uses the "walksize" from the underlying algorithm, right?- Eric
Yes, you are right.
I re-check the cryptd and simd cipher flow. They use the underlying algorithms. So, the actual walksize in the underlying algorithm is set by the user in skcipher_alg def.
The x86 aes-xts works correctly for both cryptd and simd-cipher case.
This patch becomes fixing the walksize display error in /proc/crypto.
The aes-xts skcipher_alg def:
...
.ivsize = AES_BLOCK_SIZE,
.chunksize = AES_BLOCK_SIZE,
.walksize = AES_BLOCK_SIZE * 8,
.base = {
.cra_flags = CRYPTO_ALG_INTERNAL,
.cra_name = "__xts(aes)",
.cra_driver_name = "__xts-aes-riscv64-zvkned-zvbb-zvkg",
...
},Without patch:
The original skcipher:
name : __xts(aes) driver : __xts-aes-riscv64-zvkned-zvbb-zvkg internal : yes async : no ... walksize : 128
The async skcipher registered by simd_register_skciphers_compat:
name : xts(aes) driver : xts-aes-riscv64-zvkned-zvbb-zvkg internal : no async : yes
...
walksize : 16
...
name : __xts(aes)
driver : cryptd(__xts-aes-riscv64-zvkned-zvbb-zvkg)
internal : yes
async : yes
...
walksize : 16
With patch:
name : xts(aes) driver : xts-aes-riscv64-zvkned-zvbb-zvkg internal : no async : yes
...
walksize : 128
...
name : __xts(aes)
driver : cryptd(__xts-aes-riscv64-zvkned-zvbb-zvkg)
internal : yes
async : yes
...
walksize : 128
Herbert Xu
maintainerRe: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
In reply to Jerry ShihQuoted 13 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>--- crypto/cryptd.c | 1 + crypto/simd.c | 1 + 2 files changed, 2 insertions(+)crypto/cryptd.c +0
-0
crypto/cryptd.c static int cryptd_create_skcipher(struct crypto_template *tmpl, · line 410
Quoted 6 lines
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);
Sorry but this patch doesn't apply any more now that we have lskcipher.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH v2 05/13] crypto: simd - Update `walksize` in simd skcipher
Quoted 9 lines · Herbert Xu
Quoted 8 lines · Jerry Shih
The
walksizeassignment is missed in simd skcipher.Signed-off-by Jerry Shih <[email protected]>---
crypto/cryptd.c| 1 +crypto/simd.c| 1 +2 files changed, 2 insertions(+)
Quoted 14 lines
crypto/cryptd.c static int cryptd_create_skcipher(struct crypto_template *tmpl, · line 410
Quoted 11 lines
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);Sorry but this patch doesn't apply any more now that we have lskcipher.
The lskcipher is merged in kernel 6.7. I will rebase the v3 series to 6.7 later.
Some dependent patches are not applicable to 6.7 now. I will check the status for the dependent patches.
-Jerry
PATCH v2 6/13 crypto: scatterwalk - Add scatterwalk_next() to get the next scatterlist in scatter_walk
In reply to Jerry ShihIn some situations, we might split the skcipher_request into several segments. When we try to move to next segment, we might use scatterwalk_ffwd() to get the corresponding scatterlist iterating from the head of scatterlist.
This helper function could just gather the information in skcipher_walk and move to next scatterlist directly.
--- include/crypto/scatterwalk.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
include/crypto/scatterwalk.h +7 -2
@@ -98,7 +98,12 @@ void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, unsigned int start, unsigned int nbytes, int out);struct scatterlist *scatterwalk_ffwd (struct scatterlist dst[2 ], -struct scatterlist *src, -unsigned int len); +struct scatterlist *src,unsigned int len); + +static inline struct scatterlist *scatterwalk_next (struct scatterlist dst[2 ], +struct scatter_walk *src) +{ +return scatterwalk_ffwd (dst, src->sg, src->offset - src->sg->offset); +}#endif /* _CRYPTO_SCATTERWALK_H */
Signature · 1 line
2.28.0
PATCH v2 7/13 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 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 | 514 ++++++++++ .../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, 2656 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 +514 -0
514 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
Re: [PATCH v2 07/13] RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 64
Quoted 3 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +3
-0+typedef void (*aes_xts_func )(const u8 *in,u8 *out,size_t length, +const struct crypto_aes_ctx *key,u8 *iv, +int update_iv);
There's no need for this indirection, because the function pointer can only have one value.
Note also that when Control Flow Integrity is enabled, assembly functions can only be called indirectly when they use SYM_TYPED_FUNC_START. That's another reason to avoid indirect calls that aren't actually necessary.
Review
Quoted 1 line
(quoted diff) +1
-0+ nbytes &= (~(AES_BLOCK_SIZE - 1));
Expressions like ~(n - 1) should not have another set of parentheses around them
Quoted 94 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +94
-0+static int xts_crypt (struct skcipher_request *req,aes_xts_func func) +{ +struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +const struct riscv64_aes_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct skcipher_request sub_req; +struct scatterlist sg_src[2 ], sg_dst[2 ]; +struct scatterlist *src, *dst; +struct skcipher_walk walk; +unsigned int walk_size =crypto_skcipher_walksize (tfm); +unsigned int tail_bytes; +unsigned int head_bytes; +unsigned int nbytes; +unsigned int update_iv =1 ; +int err; + +/* xts input size should be bigger than AES_BLOCK_SIZE */ +if (req->cryptlen <AES_BLOCK_SIZE ) +return -EINVAL ; + +/* +* We split xts-aes cryption into `head` and `tail` parts. +* The head block contains the input from the beginning which doesn't need +* `ciphertext stealing` method. +* The tail block contains at least two AES blocks including ciphertext +* stealing data from the end. +*/ +if (req->cryptlen <= walk_size) { +/* +* All data is in one `walk`. We could handle it within one AES-XTS call in +* the end. +*/ + tail_bytes = req->cryptlen; + head_bytes =0 ; + }else { +if (req->cryptlen & (AES_BLOCK_SIZE -1 )) { +/* +* with ciphertext stealing +* +* Find the largest tail size which is small than `walk` size while the +* head part still fits AES block boundary. +*/ + tail_bytes = req->cryptlen & (AES_BLOCK_SIZE -1 ); + tail_bytes = walk_size + tail_bytes -AES_BLOCK_SIZE ; + head_bytes = req->cryptlen - tail_bytes; + }else { +/* no ciphertext stealing */ + tail_bytes =0 ; + head_bytes = req->cryptlen; + } + } + +riscv64_aes_encrypt_zvkned (&ctx->ctx2, req->iv, req->iv); + +if (head_bytes && tail_bytes) { +/* If we have to parts, setup new request for head part only. */ +skcipher_request_set_tfm (&sub_req, tfm); +skcipher_request_set_callback ( + &sub_req,skcipher_request_flags (req),NULL ,NULL ); +skcipher_request_set_crypt (&sub_req, req->src, req->dst, + head_bytes, req->iv); + req = &sub_req; + } + +if (head_bytes) { + err =skcipher_walk_virt (&walk, req, false); +while ((nbytes = walk.nbytes)) { +if (nbytes == walk.total) + update_iv = (tail_bytes >0 ); + + nbytes &= (~(AES_BLOCK_SIZE -1 )); +kernel_vector_begin (); +func (walk.src.virt.addr, walk.dst.virt.addr, nbytes, + &ctx->ctx1, req->iv, update_iv); +kernel_vector_end (); + + err =skcipher_walk_done (&walk, walk.nbytes - nbytes); + } +if (err || !tail_bytes) +return err; + +/* +* Setup new request for tail part. +* We use `scatterwalk_next()` to find the next scatterlist from last +* walk instead of iterating from the beginning. +*/ + dst = src =scatterwalk_next (sg_src, &walk.in); +if (req->dst != req->src) + dst =scatterwalk_next (sg_dst, &walk.out); +skcipher_request_set_crypt (req, src, dst, tail_bytes, req->iv); + } + +/* tail */ + err =skcipher_walk_virt (&walk, req, false); +if (err)
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 342
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +10
-0+return err; +if (walk.nbytes != tail_bytes) +return -EINVAL ; +kernel_vector_begin (); +func (walk.src.virt.addr, walk.dst.virt.addr, walk.nbytes, &ctx->ctx1, + req->iv,0 ); +kernel_vector_end (); + +return skcipher_walk_done (&walk,0 ); +}
Did you consider writing xts_crypt() the way that arm64 and x86 do it? The above seems to reinvent sort of the same thing from first principles. I'm wondering if you should just copy the existing approach for now. Then there would be no need to add the scatterwalk_next() function, and also the handling of inputs that don't need ciphertext stealing would be a bit more streamlined.
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +10
-0+static int __init riscv64_aes_block_mod_init (void ) +{ +int ret = -ENODEV ; + +if (riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 &&riscv_vector_vlen () <=2048 ) { + ret =simd_register_skciphers_compat ( + riscv64_aes_algs_zvkned, +ARRAY_SIZE (riscv64_aes_algs_zvkned), + riscv64_aes_simd_algs_zvkned);
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 464
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +10
-0+if (ret) +return ret; + +if (riscv_isa_extension_available (NULL ,ZVBB )) { + ret =simd_register_skciphers_compat ( + riscv64_aes_alg_zvkned_zvkb, +ARRAY_SIZE (riscv64_aes_alg_zvkned_zvkb), + riscv64_aes_simd_alg_zvkned_zvkb); +if (ret) + goto unregister_zvkned;
This makes the registration of the zvkned-zvkb algorithm conditional on zvbb, not zvkb. Shouldn't the extension checks actually look like:
ZVKNED
ZVKB
ZVBB && ZVKG- Eric
Re: [PATCH v2 07/13] RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
Quoted 11 lines · Eric Biggers
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 64
Quoted 3 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +3
-0+typedef void (*aes_xts_func )(const u8 *in,u8 *out,size_t length, +const struct crypto_aes_ctx *key,u8 *iv, +int update_iv);There's no need for this indirection, because the function pointer can only have one value.
Note also that when Control Flow Integrity is enabled, assembly functions can only be called indirectly when they use
SYM_TYPED_FUNC_START. That's another reason to avoid indirect calls that aren't actually necessary.
We have two function pointers for encryption and decryption.
static int xts_encrypt(struct skcipher_request *req)
{
return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt);
}
static int xts_decrypt(struct skcipher_request *req)
{
return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt);
}The enc and dec path could be folded together into xts_crypt(), but we will have additional branches for enc/decryption path if we don't want to have the indirect calls. Use SYM_TYPED_FUNC_START in asm might be better.
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ nbytes &= (~(AES_BLOCK_SIZE - 1));Expressions like ~(n - 1) should not have another set of parentheses around them
Fixed.
Quoted 102 lines
Quoted 102 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +102
-0+static int xts_crypt (struct skcipher_request *req,aes_xts_func func) +{ +struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +const struct riscv64_aes_xts_ctx *ctx =crypto_skcipher_ctx (tfm); +struct skcipher_request sub_req; +struct scatterlist sg_src[2 ], sg_dst[2 ]; +struct scatterlist *src, *dst; +struct skcipher_walk walk; +unsigned int walk_size =crypto_skcipher_walksize (tfm); +unsigned int tail_bytes; +unsigned int head_bytes; +unsigned int nbytes; +unsigned int update_iv =1 ; +int err; + +/* xts input size should be bigger than AES_BLOCK_SIZE */ +if (req->cryptlen <AES_BLOCK_SIZE ) +return -EINVAL ; + +/* +* We split xts-aes cryption into `head` and `tail` parts. +* The head block contains the input from the beginning which doesn't need +* `ciphertext stealing` method. +* The tail block contains at least two AES blocks including ciphertext +* stealing data from the end. +*/ +if (req->cryptlen <= walk_size) { +/* +* All data is in one `walk`. We could handle it within one AES-XTS call in +* the end. +*/ + tail_bytes = req->cryptlen; + head_bytes =0 ; + }else { +if (req->cryptlen & (AES_BLOCK_SIZE -1 )) { +/* +* with ciphertext stealing +* +* Find the largest tail size which is small than `walk` size while the +* head part still fits AES block boundary. +*/ + tail_bytes = req->cryptlen & (AES_BLOCK_SIZE -1 ); + tail_bytes = walk_size + tail_bytes -AES_BLOCK_SIZE ; + head_bytes = req->cryptlen - tail_bytes; + }else { +/* no ciphertext stealing */ + tail_bytes =0 ; + head_bytes = req->cryptlen; + } + } + +riscv64_aes_encrypt_zvkned (&ctx->ctx2, req->iv, req->iv); + +if (head_bytes && tail_bytes) { +/* If we have to parts, setup new request for head part only. */ +skcipher_request_set_tfm (&sub_req, tfm); +skcipher_request_set_callback ( + &sub_req,skcipher_request_flags (req),NULL ,NULL ); +skcipher_request_set_crypt (&sub_req, req->src, req->dst, + head_bytes, req->iv); + req = &sub_req; + } + +if (head_bytes) { + err =skcipher_walk_virt (&walk, req, false); +while ((nbytes = walk.nbytes)) { +if (nbytes == walk.total) + update_iv = (tail_bytes >0 ); + + nbytes &= (~(AES_BLOCK_SIZE -1 )); +kernel_vector_begin (); +func (walk.src.virt.addr, walk.dst.virt.addr, nbytes, + &ctx->ctx1, req->iv, update_iv); +kernel_vector_end (); + + err =skcipher_walk_done (&walk, walk.nbytes - nbytes); + } +if (err || !tail_bytes) +return err; + +/* +* Setup new request for tail part. +* We use `scatterwalk_next()` to find the next scatterlist from last +* walk instead of iterating from the beginning. +*/ + dst = src =scatterwalk_next (sg_src, &walk.in); +if (req->dst != req->src) + dst =scatterwalk_next (sg_dst, &walk.out); +skcipher_request_set_crypt (req, src, dst, tail_bytes, req->iv); + } + +/* tail */ + err =skcipher_walk_virt (&walk, req, false); +if (err) +return err; +if (walk.nbytes != tail_bytes) +return -EINVAL ; +kernel_vector_begin (); +func (walk.src.virt.addr, walk.dst.virt.addr, walk.nbytes, &ctx->ctx1, + req->iv,0 ); +kernel_vector_end (); +
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return skcipher_walk_done(&walk, 0); +}Did you consider writing
xts_crypt()the way that arm64 and x86 do it? The above seems to reinvent sort of the same thing from first principles. I'm wondering if you should just copy the existing approach for now. Then there would be no need to add thescatterwalk_next()function, and also the handling of inputs that don't need ciphertext stealing would be a bit more streamlined.
I will check the arm and x86's implementations.
But the scatterwalk_next() proposed in this series does the same thing as the call scatterwalk_ffwd() in arm and x86's implementations.
The scatterwalk_ffwd() iterates from the beginning of scatterlist(O(n)), but the scatterwalk_next() is just iterates from the end point of the last used scatterlist(O(1)).
Quoted 24 lines
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +10
-0+static int __init riscv64_aes_block_mod_init (void ) +{ +int ret = -ENODEV ; + +if (riscv_isa_extension_available (NULL ,ZVKNED ) && +riscv_vector_vlen () >=128 &&riscv_vector_vlen () <=2048 ) { + ret =simd_register_skciphers_compat ( + riscv64_aes_algs_zvkned, +ARRAY_SIZE (riscv64_aes_algs_zvkned), + riscv64_aes_simd_algs_zvkned);arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 464
Quoted 10 lines
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +10
-0+if (ret) +return ret; + +if (riscv_isa_extension_available (NULL ,ZVBB )) { + ret =simd_register_skciphers_compat ( + riscv64_aes_alg_zvkned_zvkb, +ARRAY_SIZE (riscv64_aes_alg_zvkned_zvkb), + riscv64_aes_simd_alg_zvkned_zvkb); +if (ret) + goto unregister_zvkned;This makes the registration of the zvkned-zvkb algorithm conditional on zvbb, not zvkb. Shouldn't the extension checks actually look like:
Quoted 3 lines
ZVKNED
ZVKB
ZVBB && ZVKGFixed.
But we will have the conditions like:
if(ZVKNED) {
reg_cipher_1();
if(ZVKB) {
reg_cipher_2();
}
if (ZVBB && ZVKG) {
reg_cipher_3();
}
}Quoted 1 line
- Eric
Re: [PATCH v2 07/13] RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
Quoted 23 lines · Jerry Shih
Quoted 11 lines · Eric Biggers
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 64
Quoted 3 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +3
-0+typedef void (*aes_xts_func )(const u8 *in,u8 *out,size_t length, +const struct crypto_aes_ctx *key,u8 *iv, +int update_iv);There's no need for this indirection, because the function pointer can only have one value.
Note also that when Control Flow Integrity is enabled, assembly functions can only be called indirectly when they use
SYM_TYPED_FUNC_START. That's another reason to avoid indirect calls that aren't actually necessary.We have two function pointers for encryption and decryption.
static int xts_encrypt(struct skcipher_request *req) { return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt); } static int xts_decrypt(struct skcipher_request *req) { return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt); }
Quoted 4 lines
The enc and dec path could be folded together into
xts_crypt(), but we will have additional branches for enc/decryption path if we don't want to have the indirect calls. UseSYM_TYPED_FUNC_STARTin asm might be better.
Right. Normal branches are still more efficient and straightforward than indirect calls, though, and they don't need any special considerations for CFI. So I'd just add a 'bool encrypt' or 'bool decrypt' argument to xts_crypt(), and make xts_crypt() call the appropriate assembly function based on that.
Quoted 6 lines
Quoted 5 lines
Did you consider writing
xts_crypt()the way that arm64 and x86 do it? The above seems to reinvent sort of the same thing from first principles. I'm wondering if you should just copy the existing approach for now. Then there would be no need to add thescatterwalk_next()function, and also the handling of inputs that don't need ciphertext stealing would be a bit more streamlined.
Quoted 6 lines
I will check the arm and x86's implementations.
But thescatterwalk_next()proposed in this series does the same thing as the callscatterwalk_ffwd()in arm and x86's implementations.
Thescatterwalk_ffwd()iterates from the beginning of scatterlist(O(n)), but thescatterwalk_next()is just iterates from the end point of the last used scatterlist(O(1)).
Sure, but your scatterwalk_next() only matters when there are multiple scatterlist entries and the AES-XTS message length isn't a multiple of the AES block size. That's not an important case, so there's little need to micro-optimize it. The case that actually matters for AES-XTS is a single-entry scatterlist containing a whole number of AES blocks.
- Eric
Re: [PATCH v2 07/13] RISC-V: crypto: add accelerated AES-CBC/CTR/ECB/XTS implementations
Quoted 29 lines · Eric Biggers
Quoted 23 lines · Jerry Shih
Quoted 11 lines · Eric Biggers
arch/riscv/crypto/aes-riscv64-block-mode-glue.c line 64
Quoted 3 lines · Jerry Shih
arch/riscv/crypto/aes-riscv64-block-mode-glue.c new file +3
-0+typedef void (*aes_xts_func )(const u8 *in,u8 *out,size_t length, +const struct crypto_aes_ctx *key,u8 *iv, +int update_iv);There's no need for this indirection, because the function pointer can only have one value.
Note also that when Control Flow Integrity is enabled, assembly functions can only be called indirectly when they use
SYM_TYPED_FUNC_START. That's another reason to avoid indirect calls that aren't actually necessary.We have two function pointers for encryption and decryption.
static int xts_encrypt(struct skcipher_request *req) { return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt); } static int xts_decrypt(struct skcipher_request *req) { return xts_crypt(req, rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt); }Quoted 4 lines
The enc and dec path could be folded together into
xts_crypt(), but we will have additional branches for enc/decryption path if we don't want to have the indirect calls. UseSYM_TYPED_FUNC_STARTin asm might be better.
Quoted 4 lines
Right. Normal branches are still more efficient and straightforward than indirect calls, though, and they don't need any special considerations for CFI. So I'd just add a 'bool encrypt' or 'bool decrypt' argument to
xts_crypt(), and makexts_crypt()call the appropriate assembly function based on that.
Fixed.
The xts_crypt() now has an additional bool argument for enc/decryption.
Quoted 13 lines
Quoted 6 lines
Quoted 5 lines
Did you consider writing
xts_crypt()the way that arm64 and x86 do it? The above seems to reinvent sort of the same thing from first principles. I'm wondering if you should just copy the existing approach for now. Then there would be no need to add thescatterwalk_next()function, and also the handling of inputs that don't need ciphertext stealing would be a bit more streamlined.Quoted 6 lines
I will check the arm and x86's implementations.
But thescatterwalk_next()proposed in this series does the same thing as the callscatterwalk_ffwd()in arm and x86's implementations.
Thescatterwalk_ffwd()iterates from the beginning of scatterlist(O(n)), but thescatterwalk_next()is just iterates from the end point of the last used scatterlist(O(1)).
Quoted 5 lines
Sure, but your
scatterwalk_next()only matters when there are multiple scatterlist entries and the AES-XTS message length isn't a multiple of the AES block size. That's not an important case, so there's little need to micro-optimize it. The case that actually matters for AES-XTS is a single-entry scatterlist containing a whole number of AES blocks.
The v3 patch will remove the scatterwalk_next() and use scatterwalk_ffwd() instead.
-Jerry
PATCH v2 8/13 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 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 v2 9/13 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 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 | 318 ++++++++++++++++++ 4 files changed, 481 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 +318 -0
318 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v2 09/13] RISC-V: crypto: add Zvknha/b accelerated SHA224/256 implementations
arch/riscv/crypto/sha256-riscv64-glue.c line 29
Quoted 9 lines · Jerry Shih
arch/riscv/crypto/sha256-riscv64-glue.c new file +9
-0+/* +* sha256 using zvkb and zvknha/b vector crypto extension +* +* This asm function will just take the first 256-bit as the sha256 state from +* the pointer to `struct sha256_state`. +*/ +asmlinkage void +sha256_block_data_order_zvkb_zvknha_or_zvknhb (struct sha256_state *digest, +const u8 *data,int num_blks);
The SHA-2 and SM3 assembly functions are potentially being called using indirect calls, depending on whether the compiler optimizes out the indirect call that exists in the code or not. These assembly functions also are not defined using SYM_TYPED_FUNC_START. This is not compatible with Control Flow Integrity (CONFIG_CFI_CLANG); these indirect calls might generate CFI failures.
I recommend using wrapper functions to avoid this issue, like what is done in arch/arm64/crypto/sha2-ce-glue.c.
- Eric
Re: [PATCH v2 09/13] RISC-V: crypto: add Zvknha/b accelerated SHA224/256 implementations
Quoted 17 lines · Eric Biggers
arch/riscv/crypto/sha256-riscv64-glue.c line 29
Quoted 9 lines · Jerry Shih
arch/riscv/crypto/sha256-riscv64-glue.c new file +9
-0+/* +* sha256 using zvkb and zvknha/b vector crypto extension +* +* This asm function will just take the first 256-bit as the sha256 state from +* the pointer to `struct sha256_state`. +*/ +asmlinkage void +sha256_block_data_order_zvkb_zvknha_or_zvknhb (struct sha256_state *digest, +const u8 *data,int num_blks);The SHA-2 and SM3 assembly functions are potentially being called using indirect calls, depending on whether the compiler optimizes out the indirect call that exists in the code or not. These assembly functions also are not defined using
SYM_TYPED_FUNC_START. This is not compatible with Control Flow Integrity (CONFIG_CFI_CLANG); these indirect calls might generate CFI failures.
Quoted 4 lines
I recommend using wrapper functions to avoid this issue, like what is done in
arch/arm64/crypto/sha2-ce-glue.c.- Eric
Here is the previous review comment for the assembly function wrapper:
Quoted 12 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+asmlinkage void sha256_block_data_order_zvbb_zvknha(u32 *digest, const void *data, + unsigned int num_blks); + +static void __sha256_block_data_order(struct sha256_state *sst, u8 const *src, + int blocks) +{ + sha256_block_data_order_zvbb_zvknha(sst->state, src, blocks); +}Having a double-underscored function wrap around a non-underscored one like this isn't conventional for Linux kernel code. IIRC some of the other crypto code happens to do this, but it really is supposed to be the other way around.
Quoted 4 lines
I think you should just declare the assembly function to take a 'struct
sha256_state', with a comment mentioning that only the 'u32 state[8]' at the beginning is actually used. That's whatarch/x86/crypto/sha256_ssse3_glue.cdoes, for example. Then,__sha256_block_data_order()would be unneeded.
Do you mean that we need the wrapper functions back for both SHA-* and SM3? If yes, we also don't need to check the state offset like:
BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
Could we just use the SYM_TYPED_FUNC_START in asm directly without the wrappers?
-Jerry
Re: [PATCH v2 09/13] RISC-V: crypto: add Zvknha/b accelerated SHA224/256 implementations
Quoted 41 lines · Jerry Shih
Quoted 17 lines · Eric Biggers
arch/riscv/crypto/sha256-riscv64-glue.c line 29
Quoted 9 lines · Jerry Shih
arch/riscv/crypto/sha256-riscv64-glue.c new file +9
-0+/* +* sha256 using zvkb and zvknha/b vector crypto extension +* +* This asm function will just take the first 256-bit as the sha256 state from +* the pointer to `struct sha256_state`. +*/ +asmlinkage void +sha256_block_data_order_zvkb_zvknha_or_zvknhb (struct sha256_state *digest, +const u8 *data,int num_blks);The SHA-2 and SM3 assembly functions are potentially being called using indirect calls, depending on whether the compiler optimizes out the indirect call that exists in the code or not. These assembly functions also are not defined using
SYM_TYPED_FUNC_START. This is not compatible with Control Flow Integrity (CONFIG_CFI_CLANG); these indirect calls might generate CFI failures.Quoted 4 lines
I recommend using wrapper functions to avoid this issue, like what is done in
arch/arm64/crypto/sha2-ce-glue.c.- Eric
Here is the previous review comment for the assembly function wrapper:
Quoted 12 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+asmlinkage void sha256_block_data_order_zvbb_zvknha(u32 *digest, const void *data, + unsigned int num_blks); + +static void __sha256_block_data_order(struct sha256_state *sst, u8 const *src, + int blocks) +{ + sha256_block_data_order_zvbb_zvknha(sst->state, src, blocks); +}Having a double-underscored function wrap around a non-underscored one like this isn't conventional for Linux kernel code. IIRC some of the other crypto code happens to do this, but it really is supposed to be the other way around.
Quoted 4 lines
I think you should just declare the assembly function to take a 'struct
sha256_state', with a comment mentioning that only the 'u32 state[8]' at the beginning is actually used. That's whatarch/x86/crypto/sha256_ssse3_glue.cdoes, for example. Then,__sha256_block_data_order()would be unneeded.
Quoted 6 lines
Do you mean that we need the wrapper functions back for both SHA-* and SM3? If yes, we also don't need to check the state offset like:
BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);Could we just use the
SYM_TYPED_FUNC_STARTin asm directly without the wrappers?
Sorry, I forgot that I had recommended against wrapper functions earlier. I didn't realize that SYM_TYPED_FUNC_START was missing. Yes, you can also do it without wrapper functions if you add SYM_TYPED_FUNC_START to the assembly.
- Eric
PATCH v2 10/13 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 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 | 266 ++++++++++++++++++ 4 files changed, 423 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 +266 -0
266 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH v2 11/13 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 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 v2 12/13 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 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 | 230 +++++++++++++++++++++++++ 4 files changed, 373 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 +230 -0
230 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v2 12/13] RISC-V: crypto: add Zvksh accelerated SM3 implementation
Quoted 1 line · Jerry Shih
+static int
__initriscv64_riscv64_sm3_mod_init(void)
There's an extra "_riscv64" in this function name.
- Eric
Re: [PATCH v2 12/13] RISC-V: crypto: add Zvksh accelerated SM3 implementation
Quoted 6 lines · Eric Biggers
Quoted 1 line · Jerry Shih
+static int
__initriscv64_riscv64_sm3_mod_init(void)There's an extra "
_riscv64" in this function name.- Eric
Fixed.
PATCH v2 13/13 RISC-V: crypto: add Zvkb accelerated ChaCha20 implementation
In reply to Jerry ShihAdd a ChaCha20 vector implementation from OpenSSL(openssl/openssl#21923).
---
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_CHACHA20_RISCV64 + tristate "Ciphers: ChaCha20" + 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_CHACHA20_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
Re: [PATCH v2 13/13] RISC-V: crypto: add Zvkb accelerated ChaCha20 implementation
Quoted 1 line · Jerry Shih
+config
CRYPTO_CHACHA20_RISCV64
Can you call this kconfig option just CRYPTO_CHACHA_RISCV64? I.e. drop the "20". The ChaCha family of ciphers includes more than just ChaCha20.
The other architectures do use "CHACHA20" in their equivalent option, even when they implement XChaCha12 too. But that's for historical reasons -- we didn't want to break anything by renaming the kconfig options. For a new option we should use the more general name from the beginning, even if initially only ChaCha20 is implemented (which is fine).
Quoted 1 line
+static int
chacha20_encrypt(struct skcipher_request *req)
riscv64_chacha_crypt(), please. chacha20_encrypt() is dangerously close to being the same name as chacha20_crypt() which already exists in crypto/chacha.h.
arch/riscv/crypto/chacha-riscv64-glue.c line 96
Quoted 5 lines
arch/riscv/crypto/chacha-riscv64-glue.c new file +5
-0+static inline bool check_chacha20_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKB ) && +riscv_vector_vlen () >=128 ; +}
Just to double check: your intent is to simply require VLEN >= 128 for all the RISC-V vector crypto code, even when some might work with a shorter VLEN? I don't see anything in chacha-riscv64-zvkb.pl that assumes VLEN >= 128, for example. I think it would even work with VLEN == 32.
I think requiring VLEN >= 128 anyway makes sense so that we don't have to worry about validating the code with shorter VLEN. And "application processors" are supposed to have VLEN >= 128. But I just wanted to make sure this is what you intended too.
- Eric
Re: [PATCH v2 13/13] RISC-V: crypto: add Zvkb accelerated ChaCha20 implementation
Quoted 11 lines · Eric Biggers
Quoted 1 line · Jerry Shih
+config
CRYPTO_CHACHA20_RISCV64Can you call this kconfig option just
CRYPTO_CHACHA_RISCV64? I.e. drop the "20". The ChaCha family of ciphers includes more than just ChaCha20.The other architectures do use "CHACHA20" in their equivalent option, even when they implement XChaCha12 too. But that's for historical reasons -- we didn't want to break anything by renaming the kconfig options. For a new option we should use the more general name from the beginning, even if initially only ChaCha20 is implemented (which is fine).
I will use CRYPTO_CHACHA_RISCV64 instead.
Quoted 4 lines
Quoted 1 line
+static int
chacha20_encrypt(struct skcipher_request *req)
riscv64_chacha_crypt(), please.chacha20_encrypt()is dangerously close to being the same name aschacha20_crypt()which already exists incrypto/chacha.h.
The function will will have additional prefix/suffix.
Quoted 10 lines
arch/riscv/crypto/chacha-riscv64-glue.c line 96
Quoted 5 lines
arch/riscv/crypto/chacha-riscv64-glue.c new file +5
-0+static inline bool check_chacha20_ext (void ) +{ +return riscv_isa_extension_available (NULL ,ZVKB ) && +riscv_vector_vlen () >=128 ; +}Just to double check: your intent is to simply require VLEN >= 128 for all the RISC-V vector crypto code, even when some might work with a shorter VLEN? I don't see anything in chacha-riscv64-zvkb.pl that assumes VLEN >= 128, for example. I think it would even work with VLEN == 32.
Yes, the chacha algorithm here only needs the VLEN>=32. But I think we will not get benefits with that kind of hw.
Quoted 4 lines
I think requiring VLEN >= 128 anyway makes sense so that we don't have to worry about validating the code with shorter VLEN. And "application processors" are supposed to have VLEN >= 128. But I just wanted to make sure this is what you intended too.
The standard "V" extension assumes VLEN>=128. I just follow that assumption.
https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#183-v-vector-extension-for-application-processors
-Jerry