android-riscv64 icon indicating copy to clipboard operation
android-riscv64 copied to clipboard

kernel: crypto optimization

Open samitolvanen opened this issue 2 years ago • 12 comments

Linux doesn't have optimized crypto implementations for RISC-V. Storage encryption and dm/fs-verity are going to be unusably slow without optimized versions of AES and SHA-2 at least, preferably using Scalar Cryptography extensions or the upcoming Vector Cryptography extension. There's some potential overlap here with issue #36.

samitolvanen avatar Feb 07 '23 17:02 samitolvanen

Coincidentally a few relevant patches were posted to linux-riscv a couple of days ago: https://lore.kernel.org/linux-riscv/[email protected]/

samitolvanen avatar Feb 08 '23 21:02 samitolvanen

(interestingly, that also links to some openssl pull requests that might be useful for boringssl later...)

enh-google avatar Feb 08 '23 21:02 enh-google

(TIL there's also a CTS test you can't pass without AES instructions: cts/tests/tests/security/native/encryption/FileBasedEncryptionPolicyTest.cpp)

enh-google avatar Mar 16 '23 20:03 enh-google

AFAIK you can pass the test without AES instructions, but you can't use the Adiantum encryption mode unless the test is able to confirm that AES instructions really aren't available.

samitolvanen avatar Mar 16 '23 20:03 samitolvanen

The vector crypto patch series is now at v4, is based on the frozen vector crypto spec, and includes both SHA-256 and AES: https://lore.kernel.org/linux-riscv/[email protected]/

samitolvanen avatar Jul 17 '23 21:07 samitolvanen

RVI ratified the vector crypto extension last week, but apparently the original author of the kernel patch series decided to take a break from development and SiFive is planning on taking over: https://lore.kernel.org/linux-riscv/[email protected]/

samitolvanen avatar Oct 02 '23 19:10 samitolvanen

The new patch series from SiFive is [PATCH 00/12] RISC-V: provide some accelerated cryptography implementations using vector extensions, sent a few weeks ago. It generally seems to be in good shape, and it adds acceleration for all important algorithms (more or less). We are working through some details, such as how the use of the vector registers can be supported in softirq context, and whether the assembler can be used for the vector crypto instructions.

ebiggers avatar Nov 16 '23 18:11 ebiggers

The latest patchset is [PATCH v3 00/10] RISC-V crypto with reworked asm files. All prerequisites are upstream now, and we are aiming for the 6.9 kernel for the vector crypto patchset itself.

ebiggers avatar Jan 23 '24 00:01 ebiggers

RISC-V vector crypto optimizations were merged upstream for v6.9-rc1. See https://git.kernel.org/linus/67daf84203a02cf0 and https://git.kernel.org/linus/c150b809f7de2afd. The merged code covers all the basic use cases of kernel crypto. Future work will accelerate some remaining algorithms such as AES-GCM and AES-HCTR2. Fine-tuning may also be needed when this starts being tested on real RISC-V CPUs.

ebiggers avatar Mar 22 '24 20:03 ebiggers

nice!

being a kernel n00b, am i interpreting

+# This symbol indicates that the toolchain supports all v1.0 vector crypto
+# extensions, including Zvk*, Zvbb, and Zvbc.  LLVM added all of these at once.
+# binutils added all except Zvkb, then added Zvkb.  So we just check for Zvkb.
+config TOOLCHAIN_HAS_VECTOR_CRYPTO
+	def_bool $(as-instr, .option arch$(comma) +zvkb)
+	depends on AS_HAS_OPTION_ARCH

and

+config CRYPTO_AES_RISCV64
+	tristate "Ciphers: AES, modes: ECB, CBC, CTR, XTS"
+	depends on 64BIT && RISCV_ISA_V && TOOLCHAIN_HAS_VECTOR_CRYPTO

correctly as "we don't even need to modify the kernel configuration --- just having a working toolchain will be enough"? or do you have to meet the dependencies and manually configure this on?

enh-google avatar Mar 22 '24 20:03 enh-google

Unless specified otherwise, Kconfig options default to being off, so gki_defconfig will need updates to enable these options. (def_bool provides a default value for TOOLCHAIN_HAS_VECTOR_CRYPTO, but the algorithm options default to being off.)

SiFiveHolland avatar Mar 22 '24 21:03 SiFiveHolland

Android will need:

CONFIG_RISCV_ISA_V=y
CONFIG_CRYPTO_AES_RISCV64=y
CONFIG_CRYPTO_CHACHA_RISCV64=y
CONFIG_CRYPTO_GHASH_RISCV64=y
CONFIG_CRYPTO_SHA256_RISCV64=y
CONFIG_CRYPTO_SHA512_RISCV64=y

ebiggers avatar Mar 22 '24 22:03 ebiggers