Skip to content

Commit 627354a

Browse files
committed
Add AES support for RISC-V: RV64 vector
1 parent b743abb commit 627354a

File tree

14 files changed

+1305
-4
lines changed

14 files changed

+1305
-4
lines changed

.github/workflows/aes.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ jobs:
320320
- target: riscv64gc-unknown-linux-gnu
321321
rustflags: '-Ctarget-feature=+zkne,+zknd'
322322
rust: nightly
323+
- target: riscv64gc-unknown-linux-gnu
324+
rustflags: '-Ctarget-feature=+v,+zvkned --cfg=aes_zvkned'
325+
rust: nightly
326+
- target: riscv64gc-unknown-linux-gnu
327+
rustflags: '-Ctarget-feature=+zkne,+zknd,+v,+zvkned --cfg=aes_zvkned'
328+
rust: nightly
323329
runs-on: ubuntu-latest
324330
defaults:
325331
run:

aes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ hazmat = [] # Expose cryptographically hazardous APIs
3131

3232
[lints.rust.unexpected_cfgs]
3333
level = "warn"
34-
check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)", "cfg(aes_avx256_disable)", "cfg(aes_avx512_disable)"]
34+
check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)", "cfg(aes_avx256_disable)", "cfg(aes_avx512_disable)", "cfg(aes_zvkned)"]
3535

3636
[package.metadata.docs.rs]
3737
all-features = true

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", aes_zvkned))] {
176+
mod riscv;
177+
pub use riscv::rvv::*;
162178
} else if #[cfg(all(target_arch = "riscv64", target_feature = "zkne", target_feature = "zknd"))] {
163179
mod riscv;
164180
pub use riscv::rv64::*;

aes/src/riscv.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
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 `--cfg aes_zvkned`
21+
//! to the compiler (through `RUSTFLAGS` or some other means). However, you
22+
//! 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(
2338
target_arch = "riscv64",
2439
target_feature = "zknd",
2540
target_feature = "zkne"
2641
))]
2742
pub(crate) mod rv64;
43+
#[cfg(all(
44+
any(target_arch = "riscv32", target_arch = "riscv64"),
45+
target_feature = "v",
46+
aes_zvkned,
47+
))]
48+
pub(crate) mod rvv;
2849

2950
use crate::Block;
3051

0 commit comments

Comments
 (0)