Skip to content

Commit

Permalink
build.rs: Improve conditional compilation around PerlAsm.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nspin committed Mar 30, 2024
1 parent cc87515 commit e949d34
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 156 deletions.
18 changes: 15 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ const MACOS_ABI: &[&str] = &["ios", MACOS, "tvos"];
const MACOS: &str = "macos";
const WINDOWS: &str = "windows";

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.
Expand Down Expand Up @@ -324,9 +330,7 @@ fn ring_build_rs_main(c_root_dir: &Path, core_name_and_version: &str) {
force_warnings_into_errors,
};

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`
Expand Down Expand Up @@ -419,6 +423,8 @@ fn build_c_code(
core_name_and_version: &str,
) {
let (asm_srcs, obj_srcs) = if let Some(asm_target) = asm_target {
println!("cargo:rustc-cfg=perlasm");

let perlasm_src_dsts = perlasm_src_dsts(generated_dir, asm_target);

let asm_srcs = asm_srcs(perlasm_src_dsts);
Expand All @@ -435,6 +441,8 @@ fn build_c_code(
(asm_srcs, vec![])
}
} else {
println!("cargo:rustc-cfg=no_perlasm");

(vec![], vec![])
};

Expand Down Expand Up @@ -591,6 +599,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) {
Expand Down
1 change: 0 additions & 1 deletion crypto/fipsmodule/ec/p256_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
119 changes: 73 additions & 46 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,27 @@ 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"
)
))]
Implementation::HWAES => {
set_encrypt_key!(aes_hw_set_encrypt_key, bytes, key_bits, &mut 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::VPAES_BSAES => {
set_encrypt_key!(vpaes_set_encrypt_key, bytes, key_bits, &mut key)?
Expand All @@ -172,19 +178,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),

Expand All @@ -211,17 +223,23 @@ impl Key {
assert_eq!(in_out_len % BLOCK_LEN, 0);

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 => {
ctr32_encrypt_blocks!(aes_hw_ctr32_encrypt_blocks, in_out, src, &self.inner, ctr)
}

#[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 => {
// 8 blocks is the cut-off point where it's faster to use BSAES.
#[cfg(target_arch = "arm")]
Expand Down Expand Up @@ -277,7 +295,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)
Expand Down Expand Up @@ -357,20 +375,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,

Expand All @@ -379,36 +403,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")))]

Check warning on line 431 in src/aead/aes.rs

View check run for this annotation

Codecov / codecov/patch

src/aead/aes.rs#L431

Added line #L431 was not covered by tests
{
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")))]

Check warning on line 438 in src/aead/aes.rs

View check run for this annotation

Codecov / codecov/patch

src/aead/aes.rs#L438

Added line #L438 was not covered by tests
{
if cpu::arm::NEON.available(cpu_features) {
return Implementation::VPAES_BSAES;
Expand Down
8 changes: 4 additions & 4 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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
Expand Down Expand Up @@ -121,7 +121,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
Expand Down Expand Up @@ -206,7 +206,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
Expand Down Expand Up @@ -241,7 +241,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
Expand Down
52 changes: 32 additions & 20 deletions src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<usize>) {
#[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(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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] {
Expand Down
Loading

0 comments on commit e949d34

Please sign in to comment.