Skip to contentSkip to conversation

crypto: riscv/aes - Implement scalar Zkn version for RV32

PATCH In review Ard Biesheuvel 2 participants 6 messages Jul 26, 2023 – Nov 1, 2023 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: riscv/aes - Implement scalar Zkn version for RV32

The generic AES implementation we rely on if no architecture specific one is available relies on lookup tables that are relatively large with respect to the typical L1 D-cache size, which not only affects performance, it may also result in timing variances that correlate with the encryption keys.

So we tend to avoid the generic code if we can, usually by using a driver that makes use of special AES instructions which supplant most of the logic of the table based implementation the AES algorithm.

The Zkn RISC-V extension provides another interesting take on this: it defines instructions operating on scalar registers that implement the table lookups without relying on tables in memory. Those tables carry 32-bit quantities, making them a natural fit for a 32-bit architecture. And given the use of scalars, we don't have to rely in in-kernel SIMD, which is a bonus.

So let's use the instructions to implement the core AES cipher for RV32.

Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Christoph Müllner <[email protected]>
Cc Heiko Stuebner <[email protected]>
---
 arch/riscv/crypto/Kconfig             |  12 ++
 arch/riscv/crypto/Makefile            |   3 +
 arch/riscv/crypto/aes-riscv32-glue.c  |  75 ++++++++++++
 arch/riscv/crypto/aes-riscv32-zkned.S | 119 ++++++++++++++++++++
 4 files changed, 209 insertions(+)
arch/riscv/crypto/Kconfig +12 -0
index 7542330916079447..fa3917859c2bbbc3 100644
@@ -2,6 +2,18 @@
 
 menu "Accelerated Cryptographic Algorithms for CPU (riscv)"
 
+config CRYPTO_AES_RISCV32
+	tristate "Scalar AES using the Zkn extension"
+	depends on !64BIT
+	select CRYPTO_LIB_AES
+	help
+	  Implement scalar AES using the RV32 instructions specified by the Zkn
+	  extension. These instructions replace the table lookups used by the
+	  generic implementation, making this implementation time invariant
+	  (provided that the instructions themselves are). It also reduces the
+	  D-cache footprint substantially, as the AES lookup tables are quite
+	  large.
+
 config CRYPTO_AES_RISCV
 	tristate "Ciphers: AES (RISCV)"
 	depends on 64BIT && RISCV_ISA_V
arch/riscv/crypto/Makefile +3 -0
index f7faba6c12c9d863..d073b18d2a0bbba3 100644
@@ -3,6 +3,9 @@
 # linux/arch/riscv/crypto/Makefile
 #
 
+obj-$(CONFIG_CRYPTO_AES_RISCV32) += aes-riscv32.o
+aes-riscv32-y := aes-riscv32-glue.o aes-riscv32-zkned.o
+
 obj-$(CONFIG_CRYPTO_AES_RISCV) += aes-riscv.o
 aes-riscv-y := aes-riscv-glue.o aes-riscv64-zvkned.o
 
arch/riscv/crypto/aes-riscv32-glue.c new file +75 -0

75 lines not on this page · raw message

arch/riscv/crypto/aes-riscv32-zkned.S new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 25 lines · Ard Biesheuvel

The generic AES implementation we rely on if no architecture specific one is available relies on lookup tables that are relatively large with respect to the typical L1 D-cache size, which not only affects performance, it may also result in timing variances that correlate with the encryption keys.

So we tend to avoid the generic code if we can, usually by using a driver that makes use of special AES instructions which supplant most of the logic of the table based implementation the AES algorithm.

The Zkn RISC-V extension provides another interesting take on this: it defines instructions operating on scalar registers that implement the table lookups without relying on tables in memory. Those tables carry 32-bit quantities, making them a natural fit for a 32-bit architecture. And given the use of scalars, we don't have to rely in in-kernel SIMD, which is a bonus.

So let's use the instructions to implement the core AES cipher for RV32.

Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Christoph Müllner <[email protected]>
Cc Heiko Stuebner <[email protected]>
Quoted 6 lines
---
 arch/riscv/crypto/Kconfig             |  12 ++
 arch/riscv/crypto/Makefile            |   3 +
 arch/riscv/crypto/aes-riscv32-glue.c  |  75 ++++++++++++
 arch/riscv/crypto/aes-riscv32-zkned.S | 119 ++++++++++++++++++++
 4 files changed, 209 insertions(+)

Hi Ard:

Any chance you could postpone this til after I've finished removing crypto_cipher?

Thanks,

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
#2 lore raw
Quoted 34 lines · Herbert Xu
Quoted 25 lines · Ard Biesheuvel

The generic AES implementation we rely on if no architecture specific one is available relies on lookup tables that are relatively large with respect to the typical L1 D-cache size, which not only affects performance, it may also result in timing variances that correlate with the encryption keys.

So we tend to avoid the generic code if we can, usually by using a driver that makes use of special AES instructions which supplant most of the logic of the table based implementation the AES algorithm.

The Zkn RISC-V extension provides another interesting take on this: it defines instructions operating on scalar registers that implement the table lookups without relying on tables in memory. Those tables carry 32-bit quantities, making them a natural fit for a 32-bit architecture. And given the use of scalars, we don't have to rely in in-kernel SIMD, which is a bonus.

So let's use the instructions to implement the core AES cipher for RV32.

Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Christoph Müllner <[email protected]>
Cc Heiko Stuebner <[email protected]>
Quoted 6 lines
---
 arch/riscv/crypto/Kconfig             |  12 ++
 arch/riscv/crypto/Makefile            |   3 +
 arch/riscv/crypto/aes-riscv32-glue.c  |  75 ++++++++++++
 arch/riscv/crypto/aes-riscv32-zkned.S | 119 ++++++++++++++++++++
 4 files changed, 209 insertions(+)
Quoted 5 lines

Hi Ard:

Any chance you could postpone this til after I've finished removing crypto_cipher?

That's fine with me. Do you have an ETA on that? Need any help?

I have implemented the scalar 64-bit counterpart as well in the mean time

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw
Quoted 3 lines · Ard Biesheuvel
Quoted 1 line · Herbert Xu

...

Quoted 9 lines
Quoted 5 lines

Hi Ard:

Any chance you could postpone this til after I've finished removing crypto_cipher?

That's fine with me. Do you have an ETA on that? Need any help?

I have implemented the scalar 64-bit counterpart as well in the mean time

Is this still happening?

A message that is not in the archive (sent off-list or not yet received): mhng-ff1fe914-36e9-42e8-88ac-44c7f6976e3d@palmer-ri-x1c9

maintainer
#5 lore raw
Quoted 4 lines · Palmer Dabbelt

I don't really know much about the crypto stuff, but looks like there's still a "struct crypto_cipher" in my trees. Am I still supposed to be waiting on something?

Yes lskcipher has now been added. Please don't add any new code through the old crypto_cipher interface.

Thanks,

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

A message that is not in the archive (sent off-list or not yet received): 20231027-stage-cable-022844c2567d@spud

#7 lore raw
Quoted 21 lines · Conor Dooley
Quoted 19 lines · Palmer Dabbelt
Quoted 17 lines · Ard Biesheuvel
Quoted 3 lines · Ard Biesheuvel
Quoted 1 line · Herbert Xu

...

Quoted 9 lines
Quoted 5 lines

Hi Ard:

Any chance you could postpone this til after I've finished removing crypto_cipher?

That's fine with me. Do you have an ETA on that? Need any help?

I have implemented the scalar 64-bit counterpart as well in the mean time

Is this still happening?

Quoted 9 lines
Quoted 3 lines

I don't really know much about the crypto stuff, but looks like there's still a "struct crypto_cipher" in my trees. Am I still supposed to be waiting on something?

Regardless of crypto_cipher structs, this needs whatever series that actually implements Zkn detection from DT/ACPI to be merged first, as otherwise the definitions that iscv_isa_extension_available() depends on don't exist.

Please disregard this patch. I have an updated version already, but I'll need to rebase it once the prerequisites are in place.

https://git.kernel.org/pub/scm/linux/kernel/git/ardb/linux.git/log/?h=riscv-scalar-aes