Skip to content

Commit da215b0

Browse files
ebiggerspalmer-dabbelt
authored andcommitted
crypto: riscv - parallelize AES-CBC decryption
Since CBC decryption is parallelizable, make the RISC-V implementation of AES-CBC decryption process multiple blocks at a time, instead of processing the blocks one by one. This should improve performance. Signed-off-by: Eric Biggers <ebiggers@google.com> Link: https://lore.kernel.org/r/20240208060851.154129-1-ebiggers@kernel.org Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 028d1ae commit da215b0

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

arch/riscv/crypto/aes-riscv64-zvkned.S

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,25 @@ SYM_FUNC_END(aes_ecb_decrypt_zvkned)
139139
.endm
140140

141141
.macro aes_cbc_decrypt keylen
142+
srli LEN, LEN, 2 // Convert LEN from bytes to words
142143
vle32.v v16, (IVP) // Load IV
143144
1:
144-
vle32.v v17, (INP) // Load ciphertext block
145-
vmv.v.v v18, v17 // Save ciphertext block
146-
aes_decrypt v17, \keylen // Decrypt
147-
vxor.vv v17, v17, v16 // XOR with IV or prev ciphertext block
148-
vse32.v v17, (OUTP) // Store plaintext block
149-
vmv.v.v v16, v18 // Next "IV" is prev ciphertext block
150-
addi INP, INP, 16
151-
addi OUTP, OUTP, 16
152-
addi LEN, LEN, -16
145+
vsetvli t0, LEN, e32, m4, ta, ma
146+
vle32.v v20, (INP) // Load ciphertext blocks
147+
vslideup.vi v16, v20, 4 // Setup prev ciphertext blocks
148+
addi t1, t0, -4
149+
vslidedown.vx v24, v20, t1 // Save last ciphertext block
150+
aes_decrypt v20, \keylen // Decrypt the blocks
151+
vxor.vv v20, v20, v16 // XOR with prev ciphertext blocks
152+
vse32.v v20, (OUTP) // Store plaintext blocks
153+
vmv.v.v v16, v24 // Next "IV" is last ciphertext block
154+
slli t1, t0, 2 // Words to bytes
155+
add INP, INP, t1
156+
add OUTP, OUTP, t1
157+
sub LEN, LEN, t0
153158
bnez LEN, 1b
154159

160+
vsetivli zero, 4, e32, m1, ta, ma
155161
vse32.v v16, (IVP) // Store next IV
156162
ret
157163
.endm

0 commit comments

Comments
 (0)