From 21994b60380e77147ff20fcd84ae2d5b1b51bd3a 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 | 71 ++++++++++------- crypto/fipsmodule/ec/p256_shared.h | 1 - src/aead/aes.rs | 119 ++++++++++++++++++----------- src/aead/aes_gcm.rs | 8 +- src/aead/chacha.rs | 52 ++++++++----- src/aead/chacha20_poly1305.rs | 6 +- src/aead/gcm.rs | 87 ++++++++++++--------- src/arithmetic/bigint.rs | 6 +- src/arithmetic/montgomery.rs | 67 +++++++++------- src/digest/sha2.rs | 9 ++- src/ec/suite_b/ops.rs | 2 +- src/ec/suite_b/ops/p256.rs | 10 +-- src/lib.rs | 3 + src/prefixed.rs | 13 ++-- 14 files changed, 274 insertions(+), 180 deletions(-) diff --git a/build.rs b/build.rs index 01ed161d14..ea6f8637ee 100644 --- a/build.rs +++ b/build.rs @@ -262,6 +262,35 @@ const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"]; const MACOS: &str = "macos"; 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. @@ -269,6 +298,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(), @@ -285,48 +316,30 @@ fn main() { &core_name_and_version ); + match find_asm_target(&get_target(is_git)) { + Some(_) => println!("cargo:rustc-cfg=no_perlasm"), + None => println!("cargo:rustc-cfg=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 = std::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` @@ -591,6 +604,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 0307b9ee12..eabd8c14d7 100644 --- a/src/aead/aes.rs +++ b/src/aead/aes.rs @@ -176,11 +176,14 @@ impl Key { }; match detect_implementation(cpu_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" + ) ))] // SAFETY: `aes_hw_set_encrypt_key` satisfies the `set_encrypt_key!` // contract for these target architectures. @@ -188,11 +191,14 @@ impl Key { set_encrypt_key!(aes_hw_set_encrypt_key, bytes, &mut key, cpu_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" + ) ))] // SAFETY: `vpaes_set_encrypt_key` satisfies the `set_encrypt_key!` // contract for these target architectures. @@ -213,19 +219,25 @@ impl Key { #[inline] pub fn encrypt_block(&self, a: Block, cpu_features: cpu::Features) -> Block { match detect_implementation(cpu_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" + ) ))] Implementation::HWAES => encrypt_block!(aes_hw_encrypt, a, self), - #[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" + ) ))] Implementation::VPAES_BSAES => encrypt_block!(vpaes_encrypt, a, self), @@ -248,11 +260,14 @@ impl Key { cpu_features: cpu::Features, ) { match detect_implementation(cpu_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" + ) ))] // SAFETY: // * self.inner was initialized with `aes_hw_set_encrypt_key` above, @@ -270,7 +285,10 @@ impl Key { ) }, - #[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") + ))] Implementation::VPAES_BSAES => { #[cfg(target_arch = "arm")] let in_out = { @@ -357,7 +375,7 @@ impl Key { [b0, b1, b2, b3, b4] } - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] + #[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "aarch64")))] #[must_use] pub fn is_aes_hw(&self, cpu_features: cpu::Features) -> bool { matches!(detect_implementation(cpu_features), Implementation::HWAES) @@ -437,20 +455,26 @@ pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN]; #[derive(Clone, Copy)] #[allow(clippy::upper_case_acronyms)] pub enum Implementation { - #[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" + ) ))] HWAES = 1, // On "arm" only, this indicates that the bsaes implementation may be used. - #[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" + ) ))] VPAES_BSAES = 2, @@ -459,36 +483,39 @@ pub enum Implementation { fn detect_implementation(cpu_features: cpu::Features) -> Implementation { // `cpu_features` is only used for specific platforms. - #[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + #[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) )))] let _cpu_features = cpu_features; - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] { if cpu::arm::AES.available(cpu_features) { return Implementation::HWAES; } } - #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))] { if cpu::intel::AES.available(cpu_features) { return Implementation::HWAES; } } - #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))] { if cpu::intel::SSSE3.available(cpu_features) { return Implementation::VPAES_BSAES; } } - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] { if cpu::arm::NEON.available(cpu_features) { return Implementation::VPAES_BSAES; diff --git a/src/aead/aes_gcm.rs b/src/aead/aes_gcm.rs index 781ec3d4af..9771dc3309 100644 --- a/src/aead/aes_gcm.rs +++ b/src/aead/aes_gcm.rs @@ -87,7 +87,7 @@ fn aes_gcm_seal( let mut ctr = Counter::one(nonce); let tag_iv = ctr.increment(); - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] let in_out = { if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() { in_out @@ -122,7 +122,7 @@ fn aes_gcm_seal( } }; - #[cfg(target_arch = "aarch64")] + #[cfg(all(perlasm, target_arch = "aarch64"))] let in_out = { if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() { in_out @@ -207,7 +207,7 @@ fn aes_gcm_open( let in_prefix_len = src.start; - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] let in_out = { if !aes_key.is_aes_hw(cpu_features) || !auth.is_avx() { in_out @@ -242,7 +242,7 @@ fn aes_gcm_open( } }; - #[cfg(target_arch = "aarch64")] + #[cfg(all(perlasm, target_arch = "aarch64"))] let in_out = { if !aes_key.is_aes_hw(cpu_features) || !auth.is_clmul() { in_out diff --git a/src/aead/chacha.rs b/src/aead/chacha.rs index a8771b4a9c..6c11f12145 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; @@ -88,11 +91,14 @@ impl Key { /// Only call this with `src` equal to `0..` or from `encrypt_within`. #[inline] fn encrypt_less_safe(&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( @@ -125,11 +131,14 @@ impl Key { } } - #[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; @@ -166,11 +175,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 154c31aafc..9c579dd27a 100644 --- a/src/aead/chacha20_poly1305.rs +++ b/src/aead/chacha20_poly1305.rs @@ -69,7 +69,7 @@ fn chacha20_poly1305_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 @@ -161,7 +161,7 @@ fn chacha20_poly1305_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 @@ -224,7 +224,7 @@ fn chacha20_poly1305_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 { diff --git a/src/aead/gcm.rs b/src/aead/gcm.rs index 5cd6c24784..b47b9e0db7 100644 --- a/src/aead/gcm.rs +++ b/src/aead/gcm.rs @@ -43,7 +43,7 @@ impl Key { let h_table = &mut key.h_table; match detect_implementation(cpu_features) { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] Implementation::CLMUL if has_avx_movbe(cpu_features) => { prefixed_extern! { fn gcm_init_avx(HTable: &mut HTable, h: &[u64; 2]); @@ -53,11 +53,14 @@ impl Key { } } - #[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" + ) ))] Implementation::CLMUL => { prefixed_extern! { @@ -68,7 +71,7 @@ impl Key { } } - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] Implementation::NEON => { prefixed_extern! { fn gcm_init_neon(Htable: &mut HTable, h: &[u64; 2]); @@ -216,17 +219,20 @@ impl<'key> Context<'key> { let h_table = &self.h_table; match detect_implementation(self.cpu_features) { - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] // SAFETY: gcm_ghash_avx satisfies the ghash! contract. Implementation::CLMUL if has_avx_movbe(self.cpu_features) => unsafe { ghash!(gcm_ghash_avx, xi, h_table, input, self.cpu_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" + ) ))] // SAFETY: gcm_ghash_clmul satisfies the ghash! contract on these // targets. @@ -234,7 +240,7 @@ impl<'key> Context<'key> { ghash!(gcm_ghash_clmul, xi, h_table, input, self.cpu_features); }, - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] // SAFETY: gcm_ghash_neon satisfies the ghash! contract on these // targets. Implementation::NEON => unsafe { @@ -254,11 +260,14 @@ impl<'key> Context<'key> { let h_table = &self.h_table; match detect_implementation(self.cpu_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" + ) ))] Implementation::CLMUL => { prefixed_extern! { @@ -269,7 +278,7 @@ impl<'key> Context<'key> { } } - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] Implementation::NEON => { prefixed_extern! { fn gcm_gmult_neon(xi: &mut Xi, Htable: &HTable); @@ -298,7 +307,7 @@ impl<'key> Context<'key> { f(self.Xi.0, self.cpu_features) } - #[cfg(target_arch = "x86_64")] + #[cfg(all(perlasm, target_arch = "x86_64"))] pub(super) fn is_avx(&self) -> bool { match detect_implementation(self.cpu_features) { Implementation::CLMUL => has_avx_movbe(self.cpu_features), @@ -306,7 +315,7 @@ impl<'key> Context<'key> { } } - #[cfg(target_arch = "aarch64")] + #[cfg(all(perlasm, target_arch = "aarch64"))] pub(super) fn is_clmul(&self) -> bool { matches!( detect_implementation(self.cpu_features), @@ -343,15 +352,18 @@ impl BitXorAssign for Xi { #[allow(clippy::upper_case_acronyms)] enum Implementation { - #[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" + ) ))] CLMUL, - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] NEON, Fallback, @@ -360,22 +372,25 @@ enum Implementation { #[inline] fn detect_implementation(cpu_features: cpu::Features) -> Implementation { // `cpu_features` is only used for specific platforms. - #[cfg(not(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "x86_64", - target_arch = "x86" + #[cfg(not(all( + perlasm, + any( + target_arch = "aarch64", + target_arch = "arm", + target_arch = "x86_64", + target_arch = "x86" + ) )))] let _cpu_features = cpu_features; - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] { if cpu::arm::PMULL.available(cpu_features) { return Implementation::CLMUL; } } - #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] + #[cfg(all(perlasm, any(target_arch = "x86_64", target_arch = "x86")))] { if cpu::intel::FXSR.available(cpu_features) && cpu::intel::PCLMULQDQ.available(cpu_features) { @@ -383,7 +398,7 @@ fn detect_implementation(cpu_features: cpu::Features) -> Implementation { } } - #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] + #[cfg(all(perlasm, any(target_arch = "aarch64", target_arch = "arm")))] { if cpu::arm::NEON.available(cpu_features) { return Implementation::NEON; 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 bddff100de..f270c3f406 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. #[allow(deprecated)] @@ -165,11 +168,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) { @@ -198,11 +204,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()); @@ -219,11 +228,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! { @@ -232,11 +244,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. @@ -274,7 +289,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/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 15be579828..f78fbbd26a 100644 --- a/src/ec/suite_b/ops.rs +++ b/src/ec/suite_b/ops.rs @@ -625,7 +625,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 6b0323cb41..f31c3bd5aa 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| { twin_mul_inefficient(&PRIVATE_KEY_OPS, g_scalar, p_scalar, p_xy) }, @@ -135,14 +135,14 @@ pub static PUBLIC_SCALAR_OPS: PublicScalarOps = PublicScalarOps { scalar_inv_to_mont_vartime: |s| PRIVATE_SCALAR_OPS.scalar_inv_to_mont(s), }; -#[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, p_xy: &(Elem, Elem)) -> Point { let scaled_g = point_mul_base_vartime(g_scalar); let scaled_p = PRIVATE_KEY_OPS.point_mul(p_scalar, p_xy); PRIVATE_KEY_OPS.common.point_sum(&scaled_g, &scaled_p) } -#[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) -> Point { prefixed_extern! { fn p256_point_mul_base_vartime(r: *mut Limb, // [3][COMMON_OPS.num_limbs] @@ -316,7 +316,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 05a65d0cae..c3535e00b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,3 +143,6 @@ mod sealed { // ``` pub trait Sealed {} } + +const _PERLASM_CONFIGURED: () = + assert!((cfg!(perlasm) && !cfg!(no_perlasm)) || (!cfg!(perlasm) && cfg!(no_perlasm))); 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.