Skip to content

Commit

Permalink
Rework the backend project structure.
Browse files Browse the repository at this point in the history
This cleans up some old cruft.
  • Loading branch information
hasenbanck committed Aug 9, 2024
1 parent 70e8282 commit ec1aea4
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 797 deletions.
28 changes: 13 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ jobs:
- name: Linux x86_64
os: ubuntu-24.04
target: x86_64-unknown-linux-gnu
arguments: ""

- name: Linux x86
os: ubuntu-24.04
target: i686-unknown-linux-gnu
arguments: ""

- name: Linux riscv64gc
os: ubuntu-24.04
target: riscv64gc-unknown-linux-gnu
arguments: "--features=experimental_riscv"

- name: MacOS aarch64
os: macos-latest
target: aarch64-apple-darwin
arguments: ""

name: Clippy ${{ matrix.name }}
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -59,7 +63,7 @@ jobs:
cargo clippy --target ${{ matrix.target }} --no-default-features
# Check with default features
cargo clippy --target ${{ matrix.target }}
cargo clippy --target ${{ matrix.target }} ${{ matrix.arguments }}
test:
timeout-minutes: 30
Expand Down Expand Up @@ -98,37 +102,31 @@ jobs:
shell: bash
run: |
set -e
cargo test
cargo test --lib
- name: Tests (force_fallback)
- name: Tests (force_software)
shell: bash
run: |
set -e
cargo test --features=force_fallback
cargo test --lib --features=force_software
- name: Tests (force_no_runtime_detection)
- name: Tests (force_runtime_detection)
shell: bash
run: |
set -e
cargo test --features=force_fallback,force_no_runtime_detection
cargo test --lib --features=force_runtime_detection
- name: Tests no-std
shell: bash
run: |
set -e
cargo test --no-default-features
cargo test --lib --no-default-features
- name: Tests no-std (force_fallback)
- name: Tests no-std (force_software)
shell: bash
run: |
set -e
cargo test --no-default-features --features=force_fallback
- name: Tests no-std (force_no_runtime_detection)
shell: bash
run: |
set -e
cargo test --no-default-features --features=force_fallback,force_no_runtime_detection
cargo test --lib --no-default-features --features=force_software
verification:
timeout-minutes: 30
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ tls = ["std"]
experimental_riscv = []

### The following features are only used internally and are unstable ###
# Forces the compiler to always use the fallback (never using the hardware AES directly).
force_fallback = []
# Deactivates the runtime target feature detection. Combined with `force_fallback` this forced to always using the software AES.
force_no_runtime_detection = []
# Forces the compiler to enable the runtime detection.
force_runtime_detection = []
# Forces the compiler to enable the software backend.
force_software = []
# Enables some functionality used for the verification executable used in CI.
verification = ["std"]

Expand Down
16 changes: 7 additions & 9 deletions src/hardware/aarch64.rs → src/backend/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,19 +430,17 @@ pub unsafe fn aes_key_expansion<const L: usize, const N: usize>(key: [u8; L]) ->

#[cfg(all(
test,
not(any(
not(all(
target_arch = "aarch64",
target_feature = "neon",
target_feature = "aes",
)),
feature = "force_fallback"
))
all(
target_arch = "aarch64",
target_feature = "neon",
target_feature = "aes",
),
not(feature = "verification")
))]
mod tests {
use super::*;
use crate::constants::{AES128_KEY_COUNT, AES128_KEY_SIZE, AES_BLOCK_SIZE};
use crate::hardware::tests::{aes128_key_expansion_test, aes256_key_expansion_test};
use crate::tests::{aes128_key_expansion_test, aes256_key_expansion_test};

#[test]
fn test_aes128_key_expansion() {
Expand Down
35 changes: 35 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[cfg(all(target_arch = "aarch64", not(feature = "force_software")))]
pub(crate) mod aarch64;

#[cfg(all(
target_arch = "riscv64",
feature = "experimental_riscv",
not(feature = "force_software")
))]
pub(crate) mod riscv64;

#[cfg(all(
any(target_arch = "x86_64", target_arch = "x86"),
not(feature = "force_software")
))]
pub(crate) mod x86;

#[cfg(any(
not(any(
all(
any(target_arch = "x86_64", target_arch = "x86"),
target_feature = "sse2",
target_feature = "aes",
),
all(target_arch = "riscv64", feature = "experimental_riscv"),
all(
target_arch = "aarch64",
target_feature = "neon",
target_feature = "aes",
),
)),
feature = "force_runtime_detection",
feature = "force_software",
feature = "verification",
))]
pub(crate) mod soft;
30 changes: 19 additions & 11 deletions src/hardware/riscv64.rs → src/backend/riscv64.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use core::{
arch::asm,
cell::{Cell, RefCell},
};
use core::{arch::asm, cell::Cell};

use crate::constants::{AES128_KEY_COUNT, AES128_KEY_SIZE, AES256_KEY_COUNT, AES256_KEY_SIZE};
use crate::constants::{AES128_KEY_COUNT, AES256_KEY_COUNT};

/// A random number generator based on the AES-128 block cipher that runs in CTR mode and has a
/// period of 64-bit.
Expand Down Expand Up @@ -69,6 +66,7 @@ impl Aes128Ctr64 {
}

#[inline(always)]
#[allow(unused_assignments)]
pub(crate) unsafe fn next_impl(&self) -> u128 {
// Increment the lower 64 bits.
let counter = self.counter.get();
Expand All @@ -80,7 +78,7 @@ impl Aes128Ctr64 {

// Initialize the state with the counter.
let mut state = counter;
let state_ptr = (&mut state).as_mut_ptr();
let state_ptr = state.as_mut_ptr();

asm!(
"vsetivli x0, 4, e32, m1, ta, ma",
Expand Down Expand Up @@ -208,6 +206,7 @@ impl Aes128Ctr128 {
}

#[inline(always)]
#[allow(unused_assignments)]
pub(crate) unsafe fn next_impl(&self) -> u128 {
// Increment the counter.
let counter = self.counter.get();
Expand Down Expand Up @@ -339,6 +338,7 @@ impl Aes256Ctr64 {
}

#[inline(always)]
#[allow(unused_assignments)]
pub(crate) unsafe fn next_impl(&self) -> u128 {
// Increment the lower 64 bits.
let counter = self.counter.get();
Expand All @@ -350,7 +350,7 @@ impl Aes256Ctr64 {

// Initialize the state with the counter.
let mut state = counter;
let state_ptr = (&mut state).as_mut_ptr();
let state_ptr = state.as_mut_ptr();

asm!(
"vsetivli x0, 4, e32, m1, ta, ma",
Expand Down Expand Up @@ -500,6 +500,7 @@ impl Aes256Ctr128 {
}

#[inline(always)]
#[allow(unused_assignments)]
pub(crate) unsafe fn next_impl(&self) -> u128 {
// Increment the counter.
let counter = self.counter.get();
Expand Down Expand Up @@ -585,10 +586,11 @@ impl Aes256Ctr128 {
}
}

#[allow(unused_assignments)]
unsafe fn aes128_key_expansion(key: u128) -> [u128; AES128_KEY_COUNT] {
let mut expanded_keys = [0u128; AES128_KEY_COUNT];
let key_ptr = &key as *const u128;
let mut expanded_ptr = (&mut expanded_keys).as_mut_ptr();
let mut expanded_ptr = expanded_keys.as_mut_ptr();

asm!(
"vsetivli x0, 4, e32, m4, ta, ma",
Expand Down Expand Up @@ -633,10 +635,11 @@ unsafe fn aes128_key_expansion(key: u128) -> [u128; AES128_KEY_COUNT] {
expanded_keys
}

#[allow(unused_assignments)]
unsafe fn aes256_key_expansion(key: [u128; 2]) -> [u128; AES256_KEY_COUNT] {
let mut expanded_keys = [0u128; AES256_KEY_COUNT];
let mut key_ptr = &key as *const u128;
let mut expanded_ptr = (&mut expanded_keys).as_mut_ptr();
let mut expanded_ptr = expanded_keys.as_mut_ptr();

asm!(
"vsetivli x0, 4, e32, m4, ta, ma",
Expand Down Expand Up @@ -695,11 +698,16 @@ unsafe fn aes256_key_expansion(key: [u128; 2]) -> [u128; AES256_KEY_COUNT] {
expanded_keys
}

#[cfg(all(test, not(feature = "force_fallback")))]
#[cfg(all(
test,
target_arch = "riscv64",
feature = "experimental_riscv",
not(feature = "verification")
))]
mod tests {
use super::*;
use crate::constants::{AES128_KEY_COUNT, AES128_KEY_SIZE, AES_BLOCK_SIZE};
use crate::hardware::tests::{aes128_key_expansion_test, aes256_key_expansion_test};
use crate::tests::{aes128_key_expansion_test, aes256_key_expansion_test};

#[test]
fn test_aes128_key_expansion() {
Expand Down
Loading

0 comments on commit ec1aea4

Please sign in to comment.