Skip to content

Commit 9eef2a9

Browse files
committed
Add AES support for RISC-V: RV64 vector
1 parent d2afc69 commit 9eef2a9

File tree

15 files changed

+1308
-3
lines changed

15 files changed

+1308
-3
lines changed

.github/workflows/aes.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ jobs:
323323
- target: riscv64gc-unknown-linux-gnu
324324
rustflags: '-Ctarget-feature=+zkne,+zknd'
325325
rust: nightly
326+
- target: riscv64gc-unknown-linux-gnu
327+
rustflags: '-Ctarget-feature=+v,+zvkned --cfg=aes_zvkned'
328+
rust: nightly
329+
- target: riscv64gc-unknown-linux-gnu
330+
rustflags: '-Ctarget-feature=+zkne,+zknd,+v,+zvkned --cfg=aes_zvkned'
331+
rust: nightly
326332
runs-on: ubuntu-latest
327333
defaults:
328334
run:

aes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ check-cfg = [
3737
"cfg(aes_avx256_disable)",
3838
"cfg(aes_avx512_disable)",
3939
"cfg(riscv_zkned)",
40+
"cfg(riscv_zvkned)"
4041
]
4142

4243
[package.metadata.docs.rs]

aes/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@
4242
//! enable the appropriate target features at compile time. For example:
4343
//! `RUSTFLAGS=-C target-feature=+zkne,+zknd`.
4444
//!
45+
//! ## RISC-V rvv (vector) {Zvkned} extensions
46+
//!
47+
//! Support is available for the RISC-V vector crypto extensions for AES. This is
48+
//! not currently autodetected at runtime. In order to enable, you need to enable
49+
//! the appropriate target features at compile time. For example:
50+
//! `RUSTFLAGS=-C target-feature=+v,+zvkned`.
51+
//!
52+
//! NOTE: Hardware accelerated vector key-schedule routines for AES-192 are not
53+
//! available for the RISC-V vector crypto extensions. It is still possible to
54+
//! fall back to using the scalar key-schedule routines for AES-192 in this case
55+
//! if the appropriate target features are enabled. For example:
56+
//! `RUSTFLAGS=-C target-feature=+zkne,+zknd,+v,+zvkned`.
57+
//!
4558
//! ## `x86`/`x86_64` intrinsics (AES-NI and VAES)
4659
//! By default this crate uses runtime detection on `i686`/`x86_64` targets
4760
//! in order to determine if AES-NI and VAES are available, and if they are
@@ -159,6 +172,9 @@ cfg_if! {
159172
mod armv8;
160173
mod autodetect;
161174
pub use autodetect::*;
175+
} else if #[cfg(all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "v", riscv_zvkned))] {
176+
mod riscv;
177+
pub use riscv::rvv::*;
162178
} else if #[cfg(all(target_arch = "riscv64", riscv_zkned))] {
163179
mod riscv;
164180
pub use riscv::rv64::*;

aes/src/riscv.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
//! AES block cipher implementations for RISC-V using the Cryptography
22
//! Extensions
33
//!
4-
//! Supported targets: rv64 (scalar)
4+
//! Supported targets: rv64 (scalar), rvv
55
//!
66
//! NOTE: rv32 (scalar) is not currently implemented, primarily due to the
77
//! difficulty in obtaining a suitable development environment (lack of distro
88
//! support and lack of precompiled toolchains), the effort required for
99
//! maintaining a test environment as 32-bit becomes less supported, and the
1010
//! overall scarcity of relevant hardware. If someone has a specific need for
11-
//! such an implementation, please open an issue.
11+
//! such an implementation, please open an issue. Theoretically, the rvv
12+
//! implementation should work for riscv32, for a hypothetical rv32
13+
//! implementation satisfying the vector feature requirements.
1214
//!
1315
//! NOTE: These implementations are currently not enabled through
1416
//! auto-detection. In order to use this implementation, you must enable the
1517
//! appropriate target-features.
1618
//!
19+
//! Additionally, for the vector implementation, since the `zvkned`
20+
//! target-feature is not yet defined in Rust, you must pass
21+
//! `--cfg=riscv_zvkned` to the compiler (through `RUSTFLAGS` or some other
22+
//! means). However, you still must enable the `v` target-feature.
23+
//!
1724
//! Examining the module structure for this implementation should give you an
1825
//! idea of how to specify these features in your own code.
1926
//!
20-
//! NOTE: AES-128, AES-192, and AES-256 are supported.
27+
//! NOTE: AES-128, AES-192, and AES-256 are supported for both the scalar and
28+
//! vector implementations.
29+
//!
30+
//! However, key expansion is not vector-accelerated for the AES-192 case
31+
//! (because RISC-V does not provide vector instructions for this case). Users
32+
//! concerned with vector performance are advised to select AES-129 or AES-256
33+
//! instead. Nevertheless, the AES-192 vector implementation will still fall
34+
//! back to the scalar AES-192 key-schedule implementation, if the appropriate
35+
//! scalar target-features are enabled.
2136
2237
#[cfg(all(target_arch = "riscv64", riscv_zkned))]
2338
pub(crate) mod rv64;
39+
#[cfg(all(target_arch = "riscv64", riscv_zvkned,))]
40+
pub(crate) mod rvv;
2441

2542
use crate::Block;
2643

aes/src/riscv/rv64.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#![cfg_attr(all(riscv_zkned, riscv_zvkned),
2+
// When RVV crypto (Zvkned) and Scalar crypto (Zkn{ed}) is enabled, RVV will use
3+
// Scalar crypto AES-192 definitions. But the reset of the definitions from this
4+
// module will be unused, so we silence them once here.
5+
allow(unused))]
6+
17
//! AES block cipher implementation for RISC-V 64 using Scalar Cryptography Extensions: Zkne, Zknd
28
//!
39
//! RISC-V Scalar Cryptography Extension v1.0.1:

0 commit comments

Comments
 (0)