From 31b9789cadba8164c73377b11b7a253c65a35a80 Mon Sep 17 00:00:00 2001 From: Nick Spinale Date: Fri, 26 Jan 2024 20:16:01 +0000 Subject: [PATCH] build.rs: Improve conditional compilation around PerlAsm. build.rs determines whether the target platform is supported by PerlAsm using both target_arch and target_os. Instances of conditional compilation in both src/ and crypto/ were using just target_arch to determine whether PerlAsm symbols are present, resulting in link-time build failures for certain targets, including, for example, aarch64-unknown-none. This commit fixes those instances of conditional compilation to align with the build script. I agree to license my contributions to each file under the terms given at the top of each file I changed. --- build.rs | 74 +++++++++++++++++++----------- crypto/fipsmodule/ec/p256_shared.h | 1 - src/aead/aes.rs | 56 ++++++++++++++-------- src/aead/aes/hw.rs | 5 +- src/aead/aes/vp.rs | 13 ++++-- src/aead/aes_gcm.rs | 71 +++++++++++++++++----------- src/aead/chacha.rs | 52 +++++++++++++-------- src/aead/chacha20_poly1305.rs | 8 ++-- src/aead/gcm.rs | 8 ++-- src/aead/gcm/clmul.rs | 5 +- src/aead/gcm/ffi.rs | 52 +++++++++++++-------- src/aead/gcm/neon.rs | 2 +- src/arithmetic/bigint.rs | 6 +-- src/arithmetic/montgomery.rs | 67 ++++++++++++++++----------- src/cpu.rs | 8 ++-- src/digest/sha2.rs | 9 ++-- src/ec/suite_b/ops.rs | 2 +- src/ec/suite_b/ops/p256.rs | 10 ++-- src/lib.rs | 16 +++++-- src/polyfill.rs | 2 +- src/prefixed.rs | 13 ++++-- 21 files changed, 296 insertions(+), 184 deletions(-) diff --git a/build.rs b/build.rs index e87e53dfff..295db9137d 100644 --- a/build.rs +++ b/build.rs @@ -259,6 +259,35 @@ const APPLE_ABI: &[&str] = &["ios", "macos", "tvos", "visionos", "watchos"]; const WINDOWS: &str = "windows"; +fn get_target(is_git: bool) -> Target { + let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); + + // Published builds are always built in release mode. + let is_debug = is_git && env::var("DEBUG").unwrap() != "false"; + + // During local development, force warnings in non-Rust code to be treated + // as errors. Since warnings are highly compiler-dependent and compilers + // don't maintain backward compatibility w.r.t. which warnings they issue, + // don't do this for packaged builds. + let force_warnings_into_errors = is_git; + + Target { + arch, + os, + env, + is_debug, + force_warnings_into_errors, + } +} + +fn find_asm_target(target: &Target) -> Option<&'static AsmTarget> { + ASM_TARGETS.iter().find(|asm_target| { + asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref()) + }) +} + fn main() { // Avoid assuming the working directory is the same is the $CARGO_MANIFEST_DIR so that toolchains // which may assume other working directories can still build this code. @@ -266,6 +295,8 @@ fn main() { env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR should always be set"), ); + let is_git = std::fs::metadata(c_root_dir.join(".git")).is_ok(); + // Keep in sync with `core_name_and_version!` in prefixed.rs. let core_name_and_version = [ &env::var("CARGO_PKG_NAME").unwrap(), @@ -282,48 +313,33 @@ fn main() { &core_name_and_version ); + println!("cargo:rustc-check-cfg=cfg(perlasm)"); + println!("cargo:rustc-check-cfg=cfg(no_perlasm)"); + + match find_asm_target(&get_target(is_git)) { + Some(_) => println!("cargo:rustc-cfg=perlasm"), + None => println!("cargo:rustc-cfg=no_perlasm"), + } + const RING_PREGENERATE_ASM: &str = "RING_PREGENERATE_ASM"; match env::var_os(RING_PREGENERATE_ASM).as_deref() { Some(s) if s == "1" => { pregenerate_asm_main(&c_root_dir, &core_name_and_version); } - None => ring_build_rs_main(&c_root_dir, &core_name_and_version), + None => ring_build_rs_main(&c_root_dir, &core_name_and_version, is_git), _ => { panic!("${} has an invalid value", RING_PREGENERATE_ASM); } } } -fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) { +fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str, is_git: bool) { let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = PathBuf::from(out_dir); - let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); - let env = env::var("CARGO_CFG_TARGET_ENV").unwrap(); - - let is_git = fs::metadata(c_root_dir.join(".git")).is_ok(); - - // Published builds are always built in release mode. - let is_debug = is_git && env::var("DEBUG").unwrap() != "false"; - - // During local development, force warnings in non-Rust code to be treated - // as errors. Since warnings are highly compiler-dependent and compilers - // don't maintain backward compatibility w.r.t. which warnings they issue, - // don't do this for packaged builds. - let force_warnings_into_errors = is_git; - - let target = Target { - arch, - os, - env, - is_debug, - force_warnings_into_errors, - }; + let target = get_target(is_git); - let asm_target = ASM_TARGETS.iter().find(|asm_target| { - asm_target.arch == target.arch && asm_target.oss.contains(&target.os.as_ref()) - }); + let asm_target = find_asm_target(&target); // If `.git` exists then assume this is the "local hacking" case where // we want to make it easy to build *ring* using `cargo build`/`cargo test` @@ -586,6 +602,10 @@ fn configure_cc(c: &mut cc::Build, target: &Target, c_root_dir: &Path, include_d if target.force_warnings_into_errors { c.warnings_into_errors(true); } + + if find_asm_target(target).is_none() { + let _ = c.define("OPENSSL_NO_ASM", "1"); + } } fn nasm(file: &Path, arch: &str, include_dir: &Path, out_dir: &Path, c_root_dir: &Path) { diff --git a/crypto/fipsmodule/ec/p256_shared.h b/crypto/fipsmodule/ec/p256_shared.h index 648619907a..4916f03429 100644 --- a/crypto/fipsmodule/ec/p256_shared.h +++ b/crypto/fipsmodule/ec/p256_shared.h @@ -24,7 +24,6 @@ #include "../bn/internal.h" #if !defined(OPENSSL_NO_ASM) && \ - (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \ !defined(OPENSSL_SMALL) # define OPENSSL_USE_NISTZ256 #endif diff --git a/src/aead/aes.rs b/src/aead/aes.rs index f3cd35be52..6924902c6c 100644 --- a/src/aead/aes.rs +++ b/src/aead/aes.rs @@ -32,7 +32,7 @@ pub(super) mod hw; pub(super) mod vp; cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] { pub(super) use ffi::AES_KEY; } else { use ffi::AES_KEY; @@ -41,14 +41,20 @@ cfg_if! { #[derive(Clone)] pub(super) enum Key { - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86") + ))] Hw(hw::Key), - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] Vp(vp::Key), @@ -61,16 +67,22 @@ impl Key { bytes: KeyBytes<'_>, cpu_features: cpu::Features, ) -> Result { - #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))] + #[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") + ))] if let Some(hw_features) = cpu_features.get_feature() { return Ok(Self::Hw(hw::Key::new(bytes, hw_features)?)); } - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) ))] if let Some(vp_features) = cpu_features.get_feature() { return Ok(Self::Vp(vp::Key::new(bytes, vp_features)?)); @@ -84,14 +96,20 @@ impl Key { #[inline] fn encrypt_block(&self, a: Block) -> Block { match self { - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86") + ))] Key::Hw(inner) => inner.encrypt_block(a), - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] Key::Vp(inner) => inner.encrypt_block(a), diff --git a/src/aead/aes/hw.rs b/src/aead/aes/hw.rs index c7b1e51de7..0b5de6c25c 100644 --- a/src/aead/aes/hw.rs +++ b/src/aead/aes/hw.rs @@ -12,7 +12,10 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -#![cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))] +#![cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") +))] use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, AES_KEY}; use crate::{cpu, error}; diff --git a/src/aead/aes/vp.rs b/src/aead/aes/vp.rs index 0893a9873c..94386618f0 100644 --- a/src/aead/aes/vp.rs +++ b/src/aead/aes/vp.rs @@ -12,11 +12,14 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -#![cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#![cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] use super::{Block, Counter, EncryptBlock, EncryptCtr32, Iv, KeyBytes, AES_KEY}; diff --git a/src/aead/aes_gcm.rs b/src/aead/aes_gcm.rs index feb4df1e28..bc529ce1fc 100644 --- a/src/aead/aes_gcm.rs +++ b/src/aead/aes_gcm.rs @@ -25,11 +25,14 @@ use core::ops::RangeFrom; #[cfg(target_arch = "x86_64")] use aes::EncryptCtr32 as _; -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] use cpu::GetFeature as _; @@ -47,16 +50,19 @@ impl Key { #[derive(Clone)] enum DynKey { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] AesHwClMulAvxMovbe(Combo), - #[cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))] + #[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") + ))] AesHwClMul(Combo), - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] Simd(Combo), - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "x86", target_arch = "x86_64")))] Simd(Combo), Fallback(Combo), @@ -64,7 +70,7 @@ enum DynKey { impl DynKey { fn new(key: aes::KeyBytes, cpu_features: cpu::Features) -> Result { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] if let (Some(aes), Some(gcm)) = (cpu_features.get_feature(), cpu_features.get_feature()) { let aes_key = aes::hw::Key::new(key, aes)?; let gcm_key_value = derive_gcm_key_value(&aes_key); @@ -72,7 +78,10 @@ impl DynKey { return Ok(Self::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key })); } - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86") + ))] if let (Some(aes), Some(gcm)) = (cpu_features.get_feature(), cpu_features.get_feature()) { let aes_key = aes::hw::Key::new(key, aes)?; let gcm_key_value = derive_gcm_key_value(&aes_key); @@ -80,7 +89,7 @@ impl DynKey { return Ok(Self::AesHwClMul(Combo { aes_key, gcm_key })); } - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] if let (Some(aes), Some(gcm)) = (cpu_features.get_feature(), cpu_features.get_feature()) { let aes_key = aes::vp::Key::new(key, aes)?; let gcm_key_value = derive_gcm_key_value(&aes_key); @@ -88,7 +97,7 @@ impl DynKey { return Ok(Self::Simd(Combo { aes_key, gcm_key })); } - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "x86", target_arch = "x86_64")))] if let Some(aes) = cpu_features.get_feature() { let aes_key = aes::vp::Key::new(key, aes)?; let gcm_key_value = derive_gcm_key_value(&aes_key); @@ -122,7 +131,7 @@ pub(super) fn seal( let tag_iv = ctr.increment(); match key { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] DynKey::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key }) => { use crate::c; let mut auth = gcm::Context::new(gcm_key, aad, in_out.len())?; @@ -165,7 +174,7 @@ pub(super) fn seal( seal_finish(aes_key, auth, remainder, ctr, tag_iv) } - #[cfg(target_arch = "aarch64")] + #[cfg(all(perlasm, target_arch = "aarch64"))] DynKey::AesHwClMul(Combo { aes_key, gcm_key }) => { use crate::bits::BitLength; @@ -206,11 +215,14 @@ pub(super) fn seal( #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] DynKey::AesHwClMul(c) => seal_strided(c, aad, in_out, ctr, tag_iv), - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) ))] DynKey::Simd(c) => seal_strided(c, aad, in_out, ctr, tag_iv), @@ -275,14 +287,14 @@ pub(super) fn open( src: RangeFrom, ) -> Result { // Check that `src` is in bounds. - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] let input = in_out.get(src.clone()).ok_or(error::Unspecified)?; let mut ctr = Counter::one(nonce); let tag_iv = ctr.increment(); match key { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] DynKey::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key }) => { use crate::c; @@ -344,7 +356,7 @@ pub(super) fn open( open_finish(aes_key, auth, in_out, src, ctr, tag_iv) } - #[cfg(target_arch = "aarch64")] + #[cfg(all(perlasm, target_arch = "aarch64"))] DynKey::AesHwClMul(Combo { aes_key, gcm_key }) => { use crate::bits::BitLength; @@ -390,11 +402,14 @@ pub(super) fn open( #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] DynKey::AesHwClMul(c) => open_strided(c, aad, in_out, src, ctr, tag_iv), - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) ))] DynKey::Simd(c) => open_strided(c, aad, in_out, src, ctr, tag_iv), diff --git a/src/aead/chacha.rs b/src/aead/chacha.rs index 53ee1811db..817626d4fe 100644 --- a/src/aead/chacha.rs +++ b/src/aead/chacha.rs @@ -17,11 +17,14 @@ use super::{quic::Sample, Nonce}; #[cfg(any( test, - not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )) ))] mod fallback; @@ -70,11 +73,14 @@ impl Key { /// Analogous to `slice::copy_within()`. #[inline(always)] pub fn encrypt_within(&self, counter: Counter, in_out: &mut [u8], src: RangeFrom) { - #[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + #[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] #[inline(always)] pub(super) fn ChaCha20_ctr32( @@ -113,11 +119,14 @@ impl Key { unsafe { ChaCha20_ctr32(output, input, in_out_len, key.words_less_safe(), &counter) } } - #[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + #[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )))] use fallback::ChaCha20_ctr32; @@ -154,11 +163,14 @@ impl Counter { /// the caller. #[cfg(any( test, - not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )) ))] fn into_words_less_safe(self) -> [u32; 4] { diff --git a/src/aead/chacha20_poly1305.rs b/src/aead/chacha20_poly1305.rs index e0f5e9e2c7..a186e10697 100644 --- a/src/aead/chacha20_poly1305.rs +++ b/src/aead/chacha20_poly1305.rs @@ -55,7 +55,7 @@ pub(super) fn seal( /// check. const _USIZE_BOUNDED_BY_U64: u64 = u64_from_usize(usize::MAX); - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] if has_integrated(cpu_features) { // XXX: BoringSSL uses `alignas(16)` on `key` instead of on the // structure, but Rust can't do that yet; see @@ -144,7 +144,7 @@ pub(super) fn open( // check. const _USIZE_BOUNDED_BY_U64: u64 = u64_from_usize(usize::MAX); - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] if has_integrated(cpu_features) { // XXX: BoringSSL uses `alignas(16)` on `key` instead of on the // structure, but Rust can't do that yet; see @@ -207,7 +207,7 @@ pub(super) fn open( Ok(finish(auth, aad.as_ref().len(), unprefixed_len)) } -#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] +#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] #[allow(clippy::needless_return)] #[inline(always)] fn has_integrated(cpu_features: cpu::Features) -> bool { @@ -231,7 +231,7 @@ fn finish(mut auth: poly1305::Context, aad_len: usize, in_out_len: usize) -> Tag auth.finish() } -#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] +#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] mod integrated { use super::super::TAG_LEN; diff --git a/src/aead/gcm.rs b/src/aead/gcm.rs index 7fcfd88d86..d9dce61383 100644 --- a/src/aead/gcm.rs +++ b/src/aead/gcm.rs @@ -24,7 +24,7 @@ use cfg_if::cfg_if; pub(super) use ffi::KeyValue; cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] { pub(super) use self::ffi::{HTable, Xi}; } else { use self::ffi::{HTable, Xi}; @@ -82,7 +82,7 @@ impl<'key, K: Gmult> Context<'key, K> { } } -#[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] +#[cfg(all(perlasm, target_arch = "aarch64", target_pointer_width = "64"))] impl Context<'_, K> { pub(super) fn in_out_whole_block_bits(&self) -> BitLength { use crate::polyfill::usize_from_u64; @@ -94,7 +94,7 @@ impl Context<'_, K> { } } -#[cfg(target_arch = "aarch64")] +#[cfg(all(perlasm, target_arch = "aarch64"))] /// Access to `inner` for the integrated AES-GCM implementations only. impl Context<'_, clmul::Key> { #[inline] @@ -103,7 +103,7 @@ impl Context<'_, clmul::Key> { } } -#[cfg(target_arch = "x86_64")] +#[cfg(all(perlasm, target_arch = "x86_64"))] impl Context<'_, clmulavxmovbe::Key> { /// Access to `inner` for the integrated AES-GCM implementations only. #[inline] diff --git a/src/aead/gcm/clmul.rs b/src/aead/gcm/clmul.rs index 848258a841..febd765a0f 100644 --- a/src/aead/gcm/clmul.rs +++ b/src/aead/gcm/clmul.rs @@ -12,7 +12,10 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -#![cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))] +#![cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") +))] use super::{ffi::KeyValue, Gmult, HTable, Xi}; use crate::cpu; diff --git a/src/aead/gcm/ffi.rs b/src/aead/gcm/ffi.rs index 6089800bea..4e1366299a 100644 --- a/src/aead/gcm/ffi.rs +++ b/src/aead/gcm/ffi.rs @@ -18,11 +18,14 @@ pub(in super::super) const BLOCK_LEN: usize = 16; pub(in super::super) type Block = [u8; BLOCK_LEN]; pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN]; -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] macro_rules! htable_new { ( $name:ident, $value:expr $(,)? ) => {{ @@ -34,11 +37,14 @@ macro_rules! htable_new { }}; } -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] macro_rules! gmult { ( $name:ident, $xi:expr, $h_table:expr $(,)? ) => {{ @@ -53,11 +59,14 @@ macro_rules! gmult { /// SAFETY: /// * The function `$name` must meet the contract of the `f` paramweter of /// `ghash()`. -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] macro_rules! ghash { ( $name:ident, $xi:expr, $h_table:expr, $input:expr $(,)? ) => {{ @@ -90,11 +99,14 @@ impl KeyValue { /// * `f` must read `len` bytes from `inp`; it may assume /// that `len` is a (non-zero) multiple of `BLOCK_LEN`. /// * `f` may inspect CPU features. -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) ))] impl HTable { pub(super) unsafe fn new( diff --git a/src/aead/gcm/neon.rs b/src/aead/gcm/neon.rs index f1dd07cf25..3b2b9e49ac 100644 --- a/src/aead/gcm/neon.rs +++ b/src/aead/gcm/neon.rs @@ -12,7 +12,7 @@ // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -#![cfg(any(target_arch = "aarch64", target_arch = "arm"))] +#![cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] use super::{Gmult, HTable, KeyValue, UpdateBlocks, Xi, BLOCK_LEN}; use crate::cpu; diff --git a/src/arithmetic/bigint.rs b/src/arithmetic/bigint.rs index b326c35e74..3d68990d57 100644 --- a/src/arithmetic/bigint.rs +++ b/src/arithmetic/bigint.rs @@ -104,7 +104,7 @@ fn from_montgomery_amm(limbs: BoxedLimbs, m: &Modulus) -> Elem Elem { #[inline] pub fn into_unencoded(self, m: &Modulus) -> Elem { @@ -399,7 +399,7 @@ pub(crate) fn elem_exp_vartime( acc } -#[cfg(not(target_arch = "x86_64"))] +#[cfg(not(all(perlasm, target_arch = "x86_64")))] pub fn elem_exp_consttime( base: Elem, exponent: &PrivateExponent, @@ -485,7 +485,7 @@ pub fn elem_exp_consttime( Ok(acc.into_unencoded(m)) } -#[cfg(target_arch = "x86_64")] +#[cfg(all(perlasm, target_arch = "x86_64"))] pub fn elem_exp_consttime( base: Elem, exponent: &PrivateExponent, diff --git a/src/arithmetic/montgomery.rs b/src/arithmetic/montgomery.rs index b0fb815fd5..2061b70226 100644 --- a/src/arithmetic/montgomery.rs +++ b/src/arithmetic/montgomery.rs @@ -125,11 +125,14 @@ unsafe fn mul_mont( unsafe { bn_mul_mont(r, a, b, n, n0, num_limbs) } } -#[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )))] // TODO: Stop calling this from C and un-export it. prefixed_export! { @@ -164,11 +167,14 @@ prefixed_export! { // we are using the platforms for which we don't have `bn_mul_mont` in assembly. #[cfg(any( feature = "alloc", - not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )) ))] pub(super) fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Limb], n0: &N0) { @@ -197,11 +203,14 @@ pub(super) fn limbs_from_mont_in_place(r: &mut [Limb], tmp: &mut [Limb], m: &[Li .unwrap() } -#[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )))] fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) { debug_assert_eq!(r.len(), 2 * a.len()); @@ -218,11 +227,14 @@ fn limbs_mul(r: &mut [Limb], a: &[Limb], b: &[Limb]) { #[cfg(any( test, - not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) )) ))] prefixed_extern! { @@ -231,11 +243,14 @@ prefixed_extern! { fn limbs_mul_add_limb(r: *mut Limb, a: *const Limb, b: Limb, num_limbs: c::size_t) -> Limb; } -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" +#[cfg(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) ))] prefixed_extern! { // `r` and/or 'a' and/or 'b' may alias. @@ -273,7 +288,7 @@ pub(super) fn limbs_mont_mul( } /// r = a * b -#[cfg(not(target_arch = "x86_64"))] +#[cfg(not(all(perlasm, target_arch = "x86_64")))] pub(super) fn limbs_mont_product( r: &mut [Limb], a: &[Limb], diff --git a/src/cpu.rs b/src/cpu.rs index 03821791ee..558f4f81c2 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -78,8 +78,8 @@ mod features { pub(crate) struct Features(NotSend); cfg_if::cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "arm", - target_arch = "x86", target_arch = "x86_64"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm", + target_arch = "x86", target_arch = "x86_64")))] { impl Features { // SAFETY: This must only be called after CPU features have been written // and synchronized. @@ -100,10 +100,10 @@ mod features { const _: () = assert!(size_of::() == 0); cfg_if::cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] { pub mod arm; use arm::featureflags::get_or_init as get_or_init_feature_flags; - } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { + } else if #[cfg(all(perlasm, any(target_arch = "x86", target_arch = "x86_64")))] { pub mod intel; use intel::featureflags::get_or_init as get_or_init_feature_flags; } else { diff --git a/src/digest/sha2.rs b/src/digest/sha2.rs index fe2f238f69..b58b31e3bf 100644 --- a/src/digest/sha2.rs +++ b/src/digest/sha2.rs @@ -29,7 +29,7 @@ pub(super) fn block_data_order_32( cpu_features: cpu::Features, ) { cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")))] { if let Some(num) = core::num::NonZeroUsize::new(data.len()) { // Assembly require CPU feature detection tohave been done. let _cpu_features = cpu_features; @@ -51,7 +51,7 @@ pub(super) fn block_data_order_64( cpu_features: cpu::Features, ) { cfg_if! { - if #[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))] { + if #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64")))] { if let Some(num) = core::num::NonZeroUsize::new(data.len()) { // Assembly require CPU feature detection tohave been done. let _cpu_features = cpu_features; @@ -404,7 +404,10 @@ impl Sha2 for Wrapping { ]; } -#[cfg(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64"))] +#[cfg(all( + perlasm, + any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86_64") +))] prefixed_extern! { fn sha256_block_data_order( state: &mut [Wrapping; CHAINING_WORDS], diff --git a/src/ec/suite_b/ops.rs b/src/ec/suite_b/ops.rs index 85d50f7ff3..9f2dd91566 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -646,7 +646,7 @@ mod tests { } // There is no `ecp_nistz256_neg` on other targets. - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] #[test] fn p256_elem_neg_test() { prefixed_extern! { diff --git a/src/ec/suite_b/ops/p256.rs b/src/ec/suite_b/ops/p256.rs index bb730bd7a9..8e5b667848 100644 --- a/src/ec/suite_b/ops/p256.rs +++ b/src/ec/suite_b/ops/p256.rs @@ -121,10 +121,10 @@ pub static PUBLIC_SCALAR_OPS: PublicScalarOps = PublicScalarOps { scalar_ops: &SCALAR_OPS, public_key_ops: &PUBLIC_KEY_OPS, - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] twin_mul: twin_mul_nistz256, - #[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))] + #[cfg(not(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64"))))] twin_mul: |g_scalar, p_scalar, p_xy, cpu| { twin_mul_inefficient(&PRIVATE_KEY_OPS, g_scalar, p_scalar, p_xy, cpu) }, @@ -135,7 +135,7 @@ pub static PUBLIC_SCALAR_OPS: PublicScalarOps = PublicScalarOps { scalar_inv_to_mont_vartime: |s, cpu| PRIVATE_SCALAR_OPS.scalar_inv_to_mont(s, cpu), }; -#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] +#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] fn twin_mul_nistz256( g_scalar: &Scalar, p_scalar: &Scalar, @@ -147,7 +147,7 @@ fn twin_mul_nistz256( PRIVATE_KEY_OPS.common.point_sum(&scaled_g, &scaled_p, cpu) } -#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] +#[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] fn point_mul_base_vartime(g_scalar: &Scalar, _cpu: cpu::Features) -> Point { prefixed_extern! { fn p256_point_mul_base_vartime(r: *mut Limb, // [3][COMMON_OPS.num_limbs] @@ -305,7 +305,7 @@ prefixed_extern! { #[cfg(test)] mod tests { - #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "x86_64")))] #[test] fn p256_point_mul_base_vartime_test() { use super::{super::tests::point_mul_base_tests, *}; diff --git a/src/lib.rs b/src/lib.rs index 830b0898ce..36a52bdc76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,11 +83,14 @@ clippy::cast_sign_loss )] #![cfg_attr( - not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" + not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )), allow(dead_code, unused_imports, unused_macros) )] @@ -159,3 +162,6 @@ mod sealed { // ``` pub trait Sealed {} } + +const _PERLASM_CONFIGURED: () = + assert!((cfg!(perlasm) && !cfg!(no_perlasm)) || (!cfg!(perlasm) && cfg!(no_perlasm))); diff --git a/src/polyfill.rs b/src/polyfill.rs index 4d5a0ec1f0..33942473ed 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -26,7 +26,7 @@ pub const fn usize_from_u32(x: u32) -> usize { x as usize } -#[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] +#[cfg(all(perlasm, target_arch = "aarch64", target_pointer_width = "64"))] #[allow(clippy::cast_possible_truncation)] pub fn usize_from_u64(x: u64) -> usize { x as usize diff --git a/src/prefixed.rs b/src/prefixed.rs index 14d69ed735..5a18082991 100644 --- a/src/prefixed.rs +++ b/src/prefixed.rs @@ -65,11 +65,14 @@ macro_rules! prefixed_extern { } #[deprecated = "`#[export_name]` creates problems and we will stop doing it."] -#[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86", - target_arch = "x86_64" +#[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86", + target_arch = "x86_64" + ) )))] macro_rules! prefixed_export { // A function.