diff --git a/.github/workflows/aarch64-dit.yml b/.github/workflows/dit.yml similarity index 86% rename from .github/workflows/aarch64-dit.yml rename to .github/workflows/dit.yml index b1266aca..763d0cfe 100644 --- a/.github/workflows/aarch64-dit.yml +++ b/.github/workflows/dit.yml @@ -1,10 +1,10 @@ -name: aarch64-dit +name: dit on: pull_request: paths: - - ".github/workflows/aarch64-dit.yml" - - "aarch64-dit/**" + - ".github/workflows/dit.yml" + - "dit/**" - "Cargo.*" push: branches: master @@ -14,7 +14,7 @@ permissions: defaults: run: - working-directory: aarch64-dit + working-directory: dit env: CARGO_INCREMENTAL: 0 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 8a7f057d..caa1fee4 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -2,7 +2,6 @@ name: Publish to crates.io on: push: tags: [ - 'aarch64-dit-v*', 'blobby-v*', 'block-buffer-v*', 'block-padding-v*', @@ -13,6 +12,7 @@ on: 'dbl-v*', 'digest-io-async-v*', 'digest-io-v*', + 'dit-v*', 'hex-literal-v*', 'inout-v*', 'sponge-cursor-v*', diff --git a/Cargo.lock b/Cargo.lock index 092e4fbf..4bf53069 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,13 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "dit" +version = "0.0.0" +dependencies = [ + "cpufeatures 0.3.1", +] + [[package]] name = "errno" version = "0.3.14" diff --git a/Cargo.toml b/Cargo.toml index cda0076f..e62143a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "ctutils", "cpufeatures", "dbl", + "dit", "hex-literal", "inout", "sponge-cursor", @@ -17,8 +18,6 @@ members = [ "zeroize_derive", ] exclude = [ - # Works only on AArch64 - "aarch64-dit", # Results in circular dependencies "digest-io", "digest-io-async", @@ -40,7 +39,6 @@ cast_precision_loss = "warn" cast_sign_loss = "warn" checked_conversions = "warn" doc_markdown = "warn" -from_iter_instead_of_collect = "warn" implicit_saturating_sub = "warn" manual_assert = "warn" map_unwrap_or = "warn" diff --git a/README.md b/README.md index 9b767f9a..c566b7b4 100644 --- a/README.md +++ b/README.md @@ -8,23 +8,23 @@ This repository contains various utility crates used in the RustCrypto project. ## Crates -| Name | crates.io | Docs | Description | -|------|:---------:|:----:|-------------| -| [`aarch64-dit`] | [![crates.io](https://img.shields.io/crates/v/aarch64-dit.svg)](https://crates.io/crates/aarch64-dit) | [![Documentation](https://docs.rs/aarch64-dit/badge.svg)](https://docs.rs/aarch64-dit) | Wrappers for enabling/disabling the Data Independent Timing (DIT) feature on AArch64 CPUs | -| [`blobby`] | [![crates.io](https://img.shields.io/crates/v/blobby.svg)](https://crates.io/crates/blobby) | [![Documentation](https://docs.rs/blobby/badge.svg)](https://docs.rs/blobby) | Decoder of the simple de-duplicated binary blob storage format | -| [`block-buffer`] | [![crates.io](https://img.shields.io/crates/v/block-buffer.svg)](https://crates.io/crates/block-buffer) | [![Documentation](https://docs.rs/block-buffer/badge.svg)](https://docs.rs/block-buffer) | Fixed size buffer for block processing of data | +| Name | crates.io | Docs | Description | +|------|:---------------------------------------------------------------------------------------------------------:|:----:|-------------| +| [`blobby`] | [![crates.io](https://img.shields.io/crates/v/blobby.svg)](https://crates.io/crates/blobby) | [![Documentation](https://docs.rs/blobby/badge.svg)](https://docs.rs/blobby) | Decoder of the simple de-duplicated binary blob storage format | +| [`block-buffer`] | [![crates.io](https://img.shields.io/crates/v/block-buffer.svg)](https://crates.io/crates/block-buffer) | [![Documentation](https://docs.rs/block-buffer/badge.svg)](https://docs.rs/block-buffer) | Fixed size buffer for block processing of data | | [`block‑padding`] | [![crates.io](https://img.shields.io/crates/v/block-padding.svg)](https://crates.io/crates/block-padding) | [![Documentation](https://docs.rs/block-padding/badge.svg)](https://docs.rs/block-padding) | Padding and unpadding of messages divided into blocks | -| [`cmov`] | [![crates.io](https://img.shields.io/crates/v/cmov.svg)](https://crates.io/crates/cmov) | [![Documentation](https://docs.rs/cmov/badge.svg)](https://docs.rs/cmov) | Conditional move intrinsics | -| [`cpubits`] | [![crates.io](https://img.shields.io/crates/v/cpubits.svg)](https://crates.io/crates/cpubits) | [![Documentation](https://docs.rs/cpubits/badge.svg)](https://docs.rs/cpubits) | Compile-time detection heuristics for the optimal word size to use for the target CPU | -| [`cpufeatures`] | [![crates.io](https://img.shields.io/crates/v/cpufeatures.svg)](https://crates.io/crates/cpufeatures) | [![Documentation](https://docs.rs/cpufeatures/badge.svg)](https://docs.rs/cpufeatures) | Lightweight and efficient alternative to the `is_x86_feature_detected!` macro | -| [`ctutils`] | [![crates.io](https://img.shields.io/crates/v/ctutils.svg)](https://crates.io/crates/ctutils) | [![Documentation](https://docs.rs/ctutils/badge.svg)](https://docs.rs/ctutils) | Constant-time utility library with selection and equality testing support targeting cryptographic applications | -| [`dbl`] | [![crates.io](https://img.shields.io/crates/v/dbl.svg)](https://crates.io/crates/dbl) | [![Documentation](https://docs.rs/dbl/badge.svg)](https://docs.rs/dbl) | Double operation in Galois Field (GF) | -| [`digest-io`] | [![crates.io](https://img.shields.io/crates/v/digest-io.svg)](https://crates.io/crates/digest-io) | [![Documentation](https://docs.rs/digest-io/badge.svg)](https://docs.rs/digest-io) | `std::io`-compatibility wrappers for traits defined in the `digest` crate | -| [`hex-literal`] | [![crates.io](https://img.shields.io/crates/v/hex-literal.svg)](https://crates.io/crates/hex-literal) | [![Documentation](https://docs.rs/hex-literal/badge.svg)](https://docs.rs/hex-literal) | A macro for converting hexadecimal strings to a byte array at compile time | -| [`inout`] | [![crates.io](https://img.shields.io/crates/v/inout.svg)](https://crates.io/crates/inout) | [![Documentation](https://docs.rs/inout/badge.svg)](https://docs.rs/inout) | Custom reference types for code generic over in-place and buffer-to-buffer modes of operation | +| [`cmov`] | [![crates.io](https://img.shields.io/crates/v/cmov.svg)](https://crates.io/crates/cmov) | [![Documentation](https://docs.rs/cmov/badge.svg)](https://docs.rs/cmov) | Conditional move intrinsics | +| [`cpubits`] | [![crates.io](https://img.shields.io/crates/v/cpubits.svg)](https://crates.io/crates/cpubits) | [![Documentation](https://docs.rs/cpubits/badge.svg)](https://docs.rs/cpubits) | Compile-time detection heuristics for the optimal word size to use for the target CPU | +| [`cpufeatures`] | [![crates.io](https://img.shields.io/crates/v/cpufeatures.svg)](https://crates.io/crates/cpufeatures) | [![Documentation](https://docs.rs/cpufeatures/badge.svg)](https://docs.rs/cpufeatures) | Lightweight and efficient alternative to the `is_x86_feature_detected!` macro | +| [`ctutils`] | [![crates.io](https://img.shields.io/crates/v/ctutils.svg)](https://crates.io/crates/ctutils) | [![Documentation](https://docs.rs/ctutils/badge.svg)](https://docs.rs/ctutils) | Constant-time utility library with selection and equality testing support targeting cryptographic applications | +| [`dbl`] | [![crates.io](https://img.shields.io/crates/v/dbl.svg)](https://crates.io/crates/dbl) | [![Documentation](https://docs.rs/dbl/badge.svg)](https://docs.rs/dbl) | Double operation in Galois Field (GF) | +| [`digest-io`] | [![crates.io](https://img.shields.io/crates/v/digest-io.svg)](https://crates.io/crates/digest-io) | [![Documentation](https://docs.rs/digest-io/badge.svg)](https://docs.rs/digest-io) | `std::io`-compatibility wrappers for traits defined in the `digest` crate | +| [`dit`] | [![crates.io](https://img.shields.io/crates/v/dit.svg)](https://crates.io/crates/dit) | [![Documentation](https://docs.rs/dit/badge.svg)](https://docs.rs/dit) | Wrappers for enabling/disabling the Data Independent Timing (DIT) feature on AArch64 CPUs | +| [`hex-literal`] | [![crates.io](https://img.shields.io/crates/v/hex-literal.svg)](https://crates.io/crates/hex-literal) | [![Documentation](https://docs.rs/hex-literal/badge.svg)](https://docs.rs/hex-literal) | A macro for converting hexadecimal strings to a byte array at compile time | +| [`inout`] | [![crates.io](https://img.shields.io/crates/v/inout.svg)](https://crates.io/crates/inout) | [![Documentation](https://docs.rs/inout/badge.svg)](https://docs.rs/inout) | Custom reference types for code generic over in-place and buffer-to-buffer modes of operation | | [`sponge-cursor`] | [![crates.io](https://img.shields.io/crates/v/sponge-cursor.svg)](https://crates.io/crates/sponge-cursor) | [![Documentation](https://docs.rs/sponge-cursor/badge.svg)](https://docs.rs/sponge-cursor) | Cursor for sponge-based absorption and squeezing | -| [`wycheproof2blb`] | | | Utility for converting [Wycheproof] test vectors to the blobby format | -| [`zeroize`] | [![crates.io](https://img.shields.io/crates/v/zeroize.svg)](https://crates.io/crates/zeroize) | [![Documentation](https://docs.rs/zeroize/badge.svg)](https://docs.rs/zeroize) | Securely zero memory while avoiding compiler optimizations | +| [`wycheproof2blb`] | | | Utility for converting [Wycheproof] test vectors to the blobby format | +| [`zeroize`] | [![crates.io](https://img.shields.io/crates/v/zeroize.svg)](https://crates.io/crates/zeroize) | [![Documentation](https://docs.rs/zeroize/badge.svg)](https://docs.rs/zeroize) | Securely zero memory while avoiding compiler optimizations | ## License @@ -49,7 +49,7 @@ Unless you explicitly state otherwise, any contribution intentionally submitted [//]: # (crates) -[`aarch64-dit`]: ./aarch64-dit +[`dit`]: ./dit [`blobby`]: ./blobby [`block-buffer`]: ./block-buffer [`block‑padding`]: ./block-padding diff --git a/aarch64-dit/Cargo.lock b/aarch64-dit/Cargo.lock deleted file mode 100644 index bfe4e3fb..00000000 --- a/aarch64-dit/Cargo.lock +++ /dev/null @@ -1,23 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "aarch64-dit" -version = "0.2.0-pre" -dependencies = [ - "cpufeatures", -] - -[[package]] -name = "cpufeatures" -version = "0.3.0-pre" -dependencies = [ - "libc", -] - -[[package]] -name = "libc" -version = "0.2.169" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" diff --git a/aarch64-dit/CHANGELOG.md b/dit/CHANGELOG.md similarity index 100% rename from aarch64-dit/CHANGELOG.md rename to dit/CHANGELOG.md diff --git a/aarch64-dit/Cargo.toml b/dit/Cargo.toml similarity index 85% rename from aarch64-dit/Cargo.toml rename to dit/Cargo.toml index 0bc5ec98..2d4b51b0 100644 --- a/aarch64-dit/Cargo.toml +++ b/dit/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "aarch64-dit" -version = "0.2.0-pre" +name = "dit" +version = "0.0.0" authors = ["RustCrypto Developers"] edition = "2024" rust-version = "1.85" -documentation = "https://docs.rs/aarch64-dit" +documentation = "https://docs.rs/dit" readme = "README.md" repository = "https://github.com/RustCrypto/utils" license = "Apache-2.0 OR MIT" diff --git a/aarch64-dit/LICENSE-APACHE b/dit/LICENSE-APACHE similarity index 100% rename from aarch64-dit/LICENSE-APACHE rename to dit/LICENSE-APACHE diff --git a/aarch64-dit/LICENSE-MIT b/dit/LICENSE-MIT similarity index 94% rename from aarch64-dit/LICENSE-MIT rename to dit/LICENSE-MIT index 74d3cf92..0869eed8 100644 --- a/aarch64-dit/LICENSE-MIT +++ b/dit/LICENSE-MIT @@ -1,4 +1,4 @@ -Copyright (c) 2024-2025 The RustCrypto Project Developers +Copyright (c) 2024-2026 The RustCrypto Project Developers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated diff --git a/aarch64-dit/README.md b/dit/README.md similarity index 82% rename from aarch64-dit/README.md rename to dit/README.md index 2e6542c0..f8890cc4 100644 --- a/aarch64-dit/README.md +++ b/dit/README.md @@ -1,4 +1,4 @@ -# [RustCrypto]: AArch64 Data-Independent Timing (DIT) +# [RustCrypto]: Data-Independent Timing (DIT) [![Crate][crate-image]][crate-link] [![Docs][docs-image]][docs-link] @@ -36,14 +36,14 @@ additional terms or conditions. [//]: # (badges) -[crate-image]: https://img.shields.io/crates/v/aarch64-dit.svg -[crate-link]: https://crates.io/crates/aarch64-dit -[docs-image]: https://docs.rs/aarch64-dit/badge.svg -[docs-link]: https://docs.rs/aarch64-dit/ +[crate-image]: https://img.shields.io/crates/v/dit.svg +[crate-link]: https://crates.io/crates/dit +[docs-image]: https://docs.rs/dit/badge.svg +[docs-link]: https://docs.rs/dit/ [license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg [msrv-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg -[build-image]: https://github.com/RustCrypto/utils/actions/workflows/aarch64-dit.yml/badge.svg?branch=master -[build-link]: https://github.com/RustCrypto/utils/actions/workflows/aarch64-dit.yml?query=branch:master +[build-image]: https://github.com/RustCrypto/utils/actions/workflows/dit.yml/badge.svg?branch=master +[build-link]: https://github.com/RustCrypto/utils/actions/workflows/dit.yml?query=branch:master [//]: # (links) diff --git a/aarch64-dit/src/lib.rs b/dit/src/lib.rs similarity index 85% rename from aarch64-dit/src/lib.rs rename to dit/src/lib.rs index 32f47428..1fa98cab 100644 --- a/aarch64-dit/src/lib.rs +++ b/dit/src/lib.rs @@ -9,7 +9,7 @@ //! ## Usage //! //! ``` -//! use aarch64_dit::Dit; +//! use dit::Dit; //! //! let dit = Dit::init(); //! assert!(!dit.is_enabled()); @@ -17,21 +17,22 @@ //! assert!(dit.is_enabled()); //! ``` -#[cfg(not(target_arch = "aarch64"))] -compile_error!("This crate only builds on `aarch64` targets"); - +#[cfg(target_arch = "aarch64")] use core::arch::asm; +#[cfg(target_arch = "aarch64")] cpufeatures::new!(dit_supported, "dit"); /// Data-Independent Timing: support for enabling features of AArch64 CPUs which improve /// constant-time operation. pub struct Dit { + #[cfg(target_arch = "aarch64")] supported: dit_supported::InitToken, } impl Dit { /// Initialize Data-Independent Timing using runtime CPU feature detection. + #[cfg(target_arch = "aarch64")] pub fn init() -> Self { Self { supported: dit_supported::init(), @@ -41,6 +42,7 @@ impl Dit { /// Enable Data-Independent Timing (if available). /// /// Returns an RAII guard that will return DIT to its previous state when dropped. + #[cfg(target_arch = "aarch64")] #[must_use] pub fn enable(&self) -> Guard<'_> { let was_enabled = if self.is_supported() { @@ -57,20 +59,25 @@ impl Dit { /// Check if DIT has been enabled. pub fn is_enabled(&self) -> bool { + #[cfg(target_arch = "aarch64")] if self.is_supported() { - unsafe { get_dit_enabled() } - } else { - false + return unsafe { get_dit_enabled() }; } + + false } /// Check if DIT is supported by this CPU. pub fn is_supported(&self) -> bool { - self.supported.get() + #[cfg(target_arch = "aarch64")] + return self.supported.get(); + #[cfg(not(target_arch = "aarch64"))] + false } } /// RAII guard which returns DIT to its previous state when dropped. +#[cfg(target_arch = "aarch64")] pub struct Guard<'a> { /// DIT implementation. dit: &'a Dit, @@ -79,6 +86,7 @@ pub struct Guard<'a> { was_enabled: bool, } +#[cfg(target_arch = "aarch64")] impl Drop for Guard<'_> { fn drop(&mut self) { if self.dit.supported.get() { @@ -88,6 +96,7 @@ impl Drop for Guard<'_> { } /// Detect if DIT is enabled for the current thread by checking the processor state register. +#[cfg(target_arch = "aarch64")] #[target_feature(enable = "dit")] unsafe fn get_dit_enabled() -> bool { let mut dit: u64; @@ -104,6 +113,7 @@ unsafe fn get_dit_enabled() -> bool { /// Enable DIT for the current thread. /// /// Returns the previous DIT state prior to enabling DIT. +#[cfg(target_arch = "aarch64")] #[target_feature(enable = "dit")] unsafe fn set_dit_enabled() -> bool { unsafe { @@ -114,6 +124,7 @@ unsafe fn set_dit_enabled() -> bool { } /// Restore DIT state depending on the enabled bit. +#[cfg(target_arch = "aarch64")] #[target_feature(enable = "dit")] unsafe fn restore_dit(enabled: bool) { if !enabled { @@ -122,9 +133,10 @@ unsafe fn restore_dit(enabled: bool) { } } +#[cfg(target_arch = "aarch64")] #[cfg(test)] mod tests { - use super::{get_dit_enabled, restore_dit, set_dit_enabled, Dit}; + use super::{Dit, get_dit_enabled, restore_dit, set_dit_enabled}; cpufeatures::new!(dit_supported, "dit"); #[test]