Skip to content

Commit 6185849

Browse files
committed
Add AES support for RISC-V: RV64 scalar
1 parent 6501b99 commit 6185849

File tree

9 files changed

+1075
-1
lines changed

9 files changed

+1075
-1
lines changed

.github/workflows/aes.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,35 @@ jobs:
310310
- run: cross test --package aes --target ${{ matrix.target }} --features hazmat
311311
- run: cross test --package aes --target ${{ matrix.target }} --all-features
312312

313+
riscv:
314+
strategy:
315+
matrix:
316+
include:
317+
- target: riscv64gc-unknown-linux-gnu
318+
rustflags: ''
319+
rust: stable
320+
- target: riscv64gc-unknown-linux-gnu
321+
rustflags: ''
322+
rust: nightly
323+
- target: riscv64gc-unknown-linux-gnu
324+
rustflags: '-Ctarget-feature=+zkne,+zknd'
325+
rust: nightly
326+
runs-on: ubuntu-latest
327+
defaults:
328+
run:
329+
working-directory: .
330+
steps:
331+
- uses: actions/checkout@v4
332+
- uses: RustCrypto/actions/cargo-cache@master
333+
- uses: dtolnay/rust-toolchain@master
334+
with:
335+
toolchain: ${{ matrix.rust }}
336+
targets: ${{ matrix.target }}
337+
- uses: RustCrypto/actions/cross-install@master
338+
- run: cross test --package aes --target ${{ matrix.target }}
339+
env:
340+
RUSTFLAGS: "-Dwarning ${{ matrix.rustflags }}"
341+
313342
clippy:
314343
env:
315344
RUSTFLAGS: "-Dwarnings --cfg aes_compact"

aes/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ 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 = [
35+
"cfg(aes_compact)",
36+
"cfg(aes_force_soft)",
37+
"cfg(aes_avx256_disable)",
38+
"cfg(aes_avx512_disable)",
39+
"cfg(aes_riscv_zkned)",
40+
]
3541

3642
[package.metadata.docs.rs]
3743
all-features = true

aes/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
//! runtime. On other platforms the `aes` target feature must be enabled via
3636
//! RUSTFLAGS.
3737
//!
38+
//! ## RISC-V rv64 (scalar) {Zkne, ZKnd} extensions
39+
//!
40+
//! Support is available for the RISC-V rv64 scalar crypto extensions for AES. This
41+
//! is not currently autodetected at runtime. In order to enable, you need to
42+
//! enable the appropriate target features at compile time. For example:
43+
//! `RUSTFLAGS=-C target-feature=+zkne,+zknd`.
44+
//!
3845
//! ## `x86`/`x86_64` intrinsics (AES-NI and VAES)
3946
//! By default this crate uses runtime detection on `i686`/`x86_64` targets
4047
//! in order to determine if AES-NI and VAES are available, and if they are
@@ -123,6 +130,7 @@
123130
)]
124131
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
125132
#![warn(missing_docs, rust_2018_idioms)]
133+
#![cfg_attr(aes_riscv_zkned, feature(riscv_ext_intrinsics))]
126134

127135
#[cfg(feature = "hazmat")]
128136
pub mod hazmat;
@@ -137,6 +145,10 @@ cfg_if! {
137145
mod armv8;
138146
mod autodetect;
139147
pub use autodetect::*;
148+
mod soft;
149+
} else if #[cfg(all(target_arch = "riscv64", aes_riscv_zkned))] {
150+
mod riscv;
151+
pub use riscv::rv64::*;
140152
} else if #[cfg(all(
141153
any(target_arch = "x86", target_arch = "x86_64"),
142154
not(aes_force_soft)

aes/src/riscv.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! AES block cipher implementations for RISC-V using the Cryptography
2+
//! Extensions
3+
//!
4+
//! Supported targets: rv64 (scalar)
5+
//!
6+
//! NOTE: rv32 (scalar) is not currently implemented, primarily due to the
7+
//! difficulty in obtaining a suitable development environment (lack of distro
8+
//! support and lack of precompiled toolchains), the effort required for
9+
//! maintaining a test environment as 32-bit becomes less supported, and the
10+
//! overall scarcity of relevant hardware. If someone has a specific need for
11+
//! such an implementation, please open an issue.
12+
//!
13+
//! NOTE: These implementations are currently not enabled through
14+
//! auto-detection. In order to use this implementation, you must enable the
15+
//! appropriate target-features.
16+
//!
17+
//! Examining the module structure for this implementation should give you an
18+
//! idea of how to specify these features in your own code.
19+
//!
20+
//! NOTE: AES-128, AES-192, and AES-256 are supported.
21+
22+
#[cfg(all(target_arch = "riscv64", aes_riscv_zkned))]
23+
pub(crate) mod rv64;

0 commit comments

Comments
 (0)