From d64afb7a1f1d600d0ea967534a8bbd41d1b0325c Mon Sep 17 00:00:00 2001 From: Chen Xijun Date: Wed, 27 Jul 2022 15:21:15 +0800 Subject: [PATCH] Fix ring --- zeroidc/Cargo.lock | 2 -- zeroidc/Cargo.toml | 3 +++ zeroidc/vendor/ring/Cargo.toml | 2 +- zeroidc/vendor/ring/build.rs | 13 +++++++------ zeroidc/vendor/ring/src/cpu.rs | 25 ++++++++++++++++++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/zeroidc/Cargo.lock b/zeroidc/Cargo.lock index 45dff3e97..3e2575222 100644 --- a/zeroidc/Cargo.lock +++ b/zeroidc/Cargo.lock @@ -846,8 +846,6 @@ dependencies = [ [[package]] name = "ring" version = "0.16.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" dependencies = [ "cc", "libc", diff --git a/zeroidc/Cargo.toml b/zeroidc/Cargo.toml index 653be4cff..16731a509 100644 --- a/zeroidc/Cargo.toml +++ b/zeroidc/Cargo.toml @@ -21,3 +21,6 @@ thiserror = "1" [build-dependencies] cbindgen = "0.20" + +[patch.crates-io] +ring = { path = "vendor/ring" } \ No newline at end of file diff --git a/zeroidc/vendor/ring/Cargo.toml b/zeroidc/vendor/ring/Cargo.toml index 0c2424778..e5686d076 100644 --- a/zeroidc/vendor/ring/Cargo.toml +++ b/zeroidc/vendor/ring/Cargo.toml @@ -64,7 +64,7 @@ wasm32_c = [] version = "0.3.37" features = ["Crypto", "Window"] default-features = false -[target."cfg(any(target_arch = \"x86\",target_arch = \"x86_64\", all(any(target_arch = \"aarch64\", target_arch = \"arm\"), any(target_os = \"android\", target_os = \"fuchsia\", target_os = \"linux\"))))".dependencies.spin] +[target."cfg(any(target_arch = \"x86\",target_arch = \"x86_64\", all(any(target_arch = \"aarch64\", target_arch = \"arm\"), any(target_os = \"android\", target_os = \"fuchsia\", target_os = \"linux\", target_os = \"windows\"))))".dependencies.spin] version = "0.5.2" default-features = false [target."cfg(any(target_os = \"android\", target_os = \"linux\"))".dependencies.libc] diff --git a/zeroidc/vendor/ring/build.rs b/zeroidc/vendor/ring/build.rs index a5a8e1995..c4cbc1811 100644 --- a/zeroidc/vendor/ring/build.rs +++ b/zeroidc/vendor/ring/build.rs @@ -221,6 +221,7 @@ const ASM_TARGETS: &[(&str, Option<&str>, Option<&str>)] = &[ ("aarch64", Some("ios"), Some("ios64")), ("aarch64", Some("macos"), Some("ios64")), ("aarch64", None, Some("linux64")), + ("aarch64", Some(WINDOWS), Some("win64")), ("x86", Some(WINDOWS), Some("win32n")), ("x86", Some("ios"), Some("macosx")), ("x86", None, Some("elf")), @@ -384,7 +385,7 @@ fn build_c_code(target: &Target, pregenerated: PathBuf, out_dir: &Path) { // For Windows we also pregenerate the object files for non-Git builds so // the user doesn't need to install the assembler. On other platforms we // assume the C compiler also assembles. - if use_pregenerated && target.os == WINDOWS { + if use_pregenerated && (target.os == WINDOWS && target.arch != "aarch64") { // The pregenerated object files always use ".obj" as the extension, // even when the C/C++ compiler outputs files with the ".o" extension. asm_srcs = asm_srcs @@ -503,7 +504,7 @@ fn compile( let mut out_path = out_dir.join(p.file_name().unwrap()); assert!(out_path.set_extension(target.obj_ext)); if need_run(&p, &out_path, includes_modified) { - let cmd = if target.os != WINDOWS || ext != "asm" { + let cmd = if !(target.os == WINDOWS && target.arch != "aarch64") || ext != "asm" { cc(p, ext, target, warnings_are_errors, &out_path) } else { nasm(p, &target.arch, &out_path) @@ -675,7 +676,7 @@ fn perlasm_src_dsts( let mut src_dsts = srcs .iter() .filter(|p| is_perlasm(p)) - .map(|src| (src.clone(), asm_path(out_dir, src, os, perlasm_format))) + .map(|src| (src.clone(), asm_path(out_dir, src, arch, os, perlasm_format))) .collect::>(); // Some PerlAsm source files need to be run multiple times with different @@ -688,7 +689,7 @@ fn perlasm_src_dsts( let synthesized_path = PathBuf::from(synthesized); src_dsts.push(( concrete_path, - asm_path(out_dir, &synthesized_path, os, perlasm_format), + asm_path(out_dir, &synthesized_path, arch, os, perlasm_format), )) } }; @@ -710,11 +711,11 @@ fn is_perlasm(path: &PathBuf) -> bool { path.extension().unwrap().to_str().unwrap() == "pl" } -fn asm_path(out_dir: &Path, src: &Path, os: Option<&str>, perlasm_format: &str) -> PathBuf { +fn asm_path(out_dir: &Path, src: &Path, arch: &str, os: Option<&str>, perlasm_format: &str) -> PathBuf { let src_stem = src.file_stem().expect("source file without basename"); let dst_stem = src_stem.to_str().unwrap(); - let dst_extension = if os == Some("windows") { "asm" } else { "S" }; + let dst_extension = if os == Some("windows") && arch != "aarch64" { "asm" } else { "S" }; let dst_filename = format!("{}-{}.{}", dst_stem, perlasm_format, dst_extension); out_dir.join(dst_filename) } diff --git a/zeroidc/vendor/ring/src/cpu.rs b/zeroidc/vendor/ring/src/cpu.rs index e0895ae80..33e43f267 100644 --- a/zeroidc/vendor/ring/src/cpu.rs +++ b/zeroidc/vendor/ring/src/cpu.rs @@ -31,7 +31,7 @@ pub(crate) fn features() -> Features { target_arch = "x86_64", all( any(target_arch = "aarch64", target_arch = "arm"), - any(target_os = "android", target_os = "fuchsia", target_os = "linux") + any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows") ) ))] { @@ -49,7 +49,7 @@ pub(crate) fn features() -> Features { #[cfg(all( any(target_arch = "aarch64", target_arch = "arm"), - any(target_os = "android", target_os = "fuchsia", target_os = "linux") + any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows") ))] { arm::setup(); @@ -162,6 +162,25 @@ pub(crate) mod arm { } } + #[cfg(all(target_os = "windows", target_arch = "aarch64"))] + pub fn setup() { + // We do not need to check for the presence of NEON, as Armv8-A always has it + let mut features = NEON.mask; + + let result = unsafe { + winapi::um::processthreadsapi::IsProcessorFeaturePresent( + winapi::um::winnt::PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE, + ) + }; + + if result != 0 { + // These are all covered by one call in Windows + features |= AES.mask; + features |= PMULL.mask; + features |= SHA256.mask; + } + } + macro_rules! features { { $( @@ -237,7 +256,7 @@ pub(crate) mod arm { } #[cfg(all( - any(target_os = "android", target_os = "fuchsia", target_os = "linux"), + any(target_os = "android", target_os = "fuchsia", target_os = "linux", target_os = "windows"), any(target_arch = "arm", target_arch = "aarch64") ))] {