From 22e9beefd10d35c2148fbf19fddb24da59978204 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 6 Feb 2025 13:08:33 -0500 Subject: [PATCH 01/40] Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8 --- .github/workflows/bindgen.yml | 2 + .github/workflows/build.yml | 15 + Cargo.lock | 10 + rust-symcrypt/Cargo.toml | 1 + scripts/generate-all-bindings.ps1 | 3 +- symcrypt-bindgen/src/main.rs | 2 +- symcrypt-sys/Cargo.toml | 11 +- symcrypt-sys/build/jitterentropy.rs | 73 +++ symcrypt-sys/{build.rs => build/main.rs} | 27 +- symcrypt-sys/build/static_link.rs | 411 +++++++++++++++ symcrypt-sys/build/triple.rs | 31 ++ symcrypt-sys/inc/buildInfo.h | 6 +- .../src/bindings/aarch64_pc_windows_msvc.rs | 281 +++++++++- .../src/bindings/x86_64_pc_windows_msvc.rs | 494 +++++++++++++++++- 14 files changed, 1303 insertions(+), 64 deletions(-) create mode 100644 symcrypt-sys/build/jitterentropy.rs rename symcrypt-sys/{build.rs => build/main.rs} (81%) create mode 100644 symcrypt-sys/build/static_link.rs create mode 100644 symcrypt-sys/build/triple.rs diff --git a/.github/workflows/bindgen.yml b/.github/workflows/bindgen.yml index 9b448ece..1783dc40 100644 --- a/.github/workflows/bindgen.yml +++ b/.github/workflows/bindgen.yml @@ -41,6 +41,8 @@ jobs: sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu } + - uses: Swatinem/rust-cache@v2 + - name: Generate bindings shell: pwsh run: | diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5ccd4ed4..2af0e0fd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,6 +7,7 @@ on: env: CARGO_TERM_COLOR: always + CARGO_HACK_DEPTH: 2 jobs: build: @@ -38,6 +39,11 @@ jobs: steps: - uses: actions/checkout@v4 + with: + submodules: true + + - name: Init jitterentropy submodule + run: git -C symcrypt-sys/symcrypt submodule update --init -- 3rdparty/jitterentropy-library - name: Install host target shell: pwsh @@ -48,6 +54,10 @@ jobs: sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu } + - uses: taiki-e/install-action@cargo-hack + + - uses: Swatinem/rust-cache@v2 + - name: Download SymCrypt shell: pwsh run: | @@ -83,3 +93,8 @@ jobs: if: matrix.run-tests shell: pwsh run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run hack test + if: matrix.run-tests + shell: pwsh + run: cargo hack test --depth $env:CARGO_HACK_DEPTH --feature-powerset --locked --target ${{ matrix.target }} diff --git a/Cargo.lock b/Cargo.lock index afc44e85..22ad5e62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,15 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +[[package]] +name = "cc" +version = "1.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229" +dependencies = [ + "shlex", +] + [[package]] name = "cexpr" version = "0.6.0" @@ -395,6 +404,7 @@ dependencies = [ name = "symcrypt-sys" version = "0.4.0" dependencies = [ + "cc", "libc", ] diff --git a/rust-symcrypt/Cargo.toml b/rust-symcrypt/Cargo.toml index 6cb1fd6d..cd2e5d79 100644 --- a/rust-symcrypt/Cargo.toml +++ b/rust-symcrypt/Cargo.toml @@ -24,6 +24,7 @@ default = [] md5 = [] sha1 = [] pkcs1-encrypt-decrypt = [] +dynamic = ["symcrypt-sys/dynamic"] [dev-dependencies] hex = "0.4.3" diff --git a/scripts/generate-all-bindings.ps1 b/scripts/generate-all-bindings.ps1 index 40fc153d..24d61175 100644 --- a/scripts/generate-all-bindings.ps1 +++ b/scripts/generate-all-bindings.ps1 @@ -13,7 +13,8 @@ # # - Enter the WSL shell and run the following commands: # sudo apt update && sudo apt upgrade -# sudo apt install -y clang libclang-dev rustup +# sudo apt install -y clang libclang-dev +# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # to install rust for WSL # sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu # for cross-compilation # # rustup update diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index 0479305a..0d813bbe 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -152,7 +152,7 @@ fn get_rust_version_from_cargo_metadata() -> String { fn fix_bindings_for_windows(triple: &str, bindings_file: &str) { if triple.contains("windows") { println!("Fixing bindings for Windows"); - let link_str = "#[link(name = \"symcrypt\", kind = \"dylib\")]"; + let link_str = r#"#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))]"#; let regex_exp1 = regex::Regex::new(r"pub static \w+: \[SYMCRYPT_OID; \d+usize\];").unwrap(); let regex_exp2 = regex::Regex::new(r"pub static \w+: PCSYMCRYPT_\w+;").unwrap(); let bindings_content = diff --git a/symcrypt-sys/Cargo.toml b/symcrypt-sys/Cargo.toml index ad71cf50..28f399ea 100644 --- a/symcrypt-sys/Cargo.toml +++ b/symcrypt-sys/Cargo.toml @@ -6,13 +6,20 @@ license = "MIT OR Apache-2.0" description = "Rust/C Bindings for SymCrypt" edition.workspace = true rust-version.workspace = true -build = "build.rs" +build = "build/main.rs" homepage = "https://github.com/microsoft/SymCrypt" repository = "https://github.com/microsoft/rust-symcrypt" readme = "README.md" -exclude = ["symcrypt/*", "inc/*"] +exclude = ["symcrypt/*", "inc/*"] # FIXME: update this before merging to main branch +#links = "symcrypt" # FIXME: uncomment this before merging to main branch # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +dynamic = [] + [dependencies] libc = "0.2.0" + +[build-dependencies] +cc = { version = "1.2.10" } # FIXME: enable parallel feature before merging to main branch diff --git a/symcrypt-sys/build/jitterentropy.rs b/symcrypt-sys/build/jitterentropy.rs new file mode 100644 index 00000000..52ab018b --- /dev/null +++ b/symcrypt-sys/build/jitterentropy.rs @@ -0,0 +1,73 @@ +use super::triple::Triple; + +const LIB_NAME: &str = "jitterentropy"; + +pub fn compile_and_link_jitterentropy(triple: Triple) { + println!("Compiling jitterentropy..."); + let cargo_toml_dir = + std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set"); + let jitterentropy_dir = format!("{cargo_toml_dir}/symcrypt/3rdparty/jitterentropy-library"); + + let mut cc = cc::Build::new(); + cc.target(triple.to_triple()) + .warnings(false) + .include(&jitterentropy_dir) + .include(format!("{jitterentropy_dir}/src")); + + // Add the source files + let src_files = std::fs::read_dir(format!("{jitterentropy_dir}/src")) + .expect("Failed to read src directory") + .filter_map(|entry| { + let entry = entry.expect("Failed to read directory entry"); + let path = entry.path(); + if path.extension().and_then(|s| s.to_str()) == Some("c") { + Some(path) + } else { + None + } + }); + cc.files(src_files); + + // Set compiler flags. Warnings are commented out. + cc.flag_if_supported("-fwrapv") + .flag_if_supported("--param ssp-buffer-size=4") + .flag_if_supported("-fvisibility=hidden") + .flag_if_supported("-fPIE") + //.flag_if_supported("-Wcast-align") + //.flag_if_supported("-Wmissing-field-initializers") + //.flag_if_supported("-Wshadow") + //.flag_if_supported("-Wswitch-enum") + //.flag_if_supported("-Wextra") + //.flag_if_supported("-Wall") + //.flag_if_supported("-pedantic") + .flag_if_supported("-fPIC") + .flag_if_supported("-O0") + //.flag_if_supported("-Wconversion") + ; + + if gcc_version_ge_490() { + cc.flag_if_supported("-fstack-protector-strong"); + } else { + cc.flag_if_supported("-fstack-protector-all"); + } + + cc.compile(LIB_NAME); + println!("cargo:rustc-link-lib=static={LIB_NAME}"); +} + +fn gcc_version_ge_490() -> bool { + if let Ok(output) = std::process::Command::new("gcc") + .arg("-dumpversion") + .output() + { + if let Ok(version) = String::from_utf8(output.stdout) { + let parts: Vec<&str> = version.trim().split('.').collect(); + if parts.len() >= 2 { + if let (Ok(major), Ok(minor)) = (parts[0].parse::(), parts[1].parse::()) { + return major > 4 || (major == 4 && minor >= 9); + } + } + } + } + false +} diff --git a/symcrypt-sys/build.rs b/symcrypt-sys/build/main.rs similarity index 81% rename from symcrypt-sys/build.rs rename to symcrypt-sys/build/main.rs index 309d8254..f67c866a 100644 --- a/symcrypt-sys/build.rs +++ b/symcrypt-sys/build/main.rs @@ -1,13 +1,30 @@ -#[cfg(target_os = "windows")] -use std::env; +#[cfg(not(feature = "dynamic"))] +pub mod static_link; -fn main() { +#[cfg(not(feature = "dynamic"))] +pub mod triple; + +#[cfg(not(feature = "dynamic"))] +pub mod jitterentropy; + +fn main() -> std::io::Result<()> { + #[cfg(feature = "dynamic")] + link_symcrypt_dynamically()?; + + #[cfg(not(feature = "dynamic"))] + static_link::compile_and_link_symcrypt()?; + + Ok(()) +} + +#[cfg(feature = "dynamic")] +fn link_symcrypt_dynamically() -> std::io::Result<()> { #[cfg(target_os = "windows")] { // Look for the .lib file during link time. We are searching the Windows/System32 path which is set as a current default to match // the long term placement of a Windows shipped symcrypt.dll - let lib_path = env::var("SYMCRYPT_LIB_PATH") + let lib_path = std::env::var("SYMCRYPT_LIB_PATH") .unwrap_or_else(|_| panic!("SYMCRYPT_LIB_PATH environment variable not set, for more information please see: https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt#quick-start-guide")); println!("cargo:rustc-link-search=native={}", lib_path); @@ -41,4 +58,6 @@ fn main() { // Note: This process is a band-aid. Long-term, our long term solution is to package manage SymCrypt for a subset of // Linux distros. } + + Ok(()) } diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs new file mode 100644 index 00000000..39823928 --- /dev/null +++ b/symcrypt-sys/build/static_link.rs @@ -0,0 +1,411 @@ +use super::jitterentropy::compile_and_link_jitterentropy; +use super::triple::Triple; + +const LIB_NAME: &str = "symcrypt"; + +pub fn compile_and_link_symcrypt() -> std::io::Result<()> { + // based on SymCrypt/lib/CMakeLists.txt + + let options = SymCryptOptions::new(); + println!("Build config: {:?}", options); + + const ADDITIONAL_DEPENDENCIES: &[&str] = &[ + #[cfg(windows)] + "bcrypt", + ]; + println!("cargo:rerun-if-changed=upstream"); + println!("Compiling SymCrypt..."); + + compile_symcrypt_static(LIB_NAME, &options)?; + println!("cargo:rustc-link-lib=static={LIB_NAME}"); + + for dep in ADDITIONAL_DEPENDENCIES { + println!("cargo:rustc-link-lib=dylib={dep}"); + } + + if options.need_jitterentropy() { + compile_and_link_jitterentropy(options.triple()); + } + + Ok(()) +} + +#[derive(Debug)] +struct SymCryptOptions { + triple: Triple, + symcrypt_use_asm: bool, + //symcrypt_fips_build: bool, +} +impl SymCryptOptions { + fn new() -> Self { + Self { + triple: Triple::get_target_triple(), + symcrypt_use_asm: false, + //symcrypt_fips_build: false, + } + } + fn use_asm(&self) -> bool { + self.symcrypt_use_asm + } + fn triple(&self) -> Triple { + self.triple.clone() + } + fn need_jitterentropy(&self) -> bool { + matches!( + self.triple, + Triple::x86_64_unknown_linux_gnu | Triple::aarch64_unknown_linux_gnu + ) + } + + fn preconfigure_cc(&self) -> cc::Build { + let mut cc = cc::Build::new(); + cc.target(self.triple.to_triple()) + .include("inc") + .include("symcrypt/inc") + .include("symcrypt/lib") + .warnings(false); + + if !self.symcrypt_use_asm { + cc.define("SYMCRYPT_IGNORE_PLATFORM", None); + } + + match self.triple { + Triple::x86_64_pc_windows_msvc => { + cc.asm_flag("/DSYMCRYPT_MASM"); + } + Triple::aarch64_pc_windows_msvc => { + cc.define("_ARM64_", None); + } + Triple::x86_64_unknown_linux_gnu => { + cc.include("symcrypt/modules/linux/common"); + cc.flag("-mpclmul"); + cc.flag("-Wno-incompatible-pointer-types"); // Should we create parent Enum for Windows / Linux? + /* + cc.flag("-mpclmul") + .flag("-mssse3") + .flag("-mxsave") + .flag("-maes") + .flag("-msha") + .flag("-mrdrnd") + .flag("-mrdseed"); + */ + } + Triple::aarch64_unknown_linux_gnu => { + cc.include("symcrypt/modules/linux/common"); + cc.flag("-Wno-incompatible-pointer-types"); + } + } + + if self.need_jitterentropy() { + cc.include("symcrypt/3rdparty/jitterentropy-library"); + } + + cc + } +} + +const SOURCE_DIR: &str = "symcrypt/lib"; +const CMAKE_SOURCES_COMMON: &str = " +3des.c +a_dispatch.c +aes-asm.c +aes-c.c +aes-default-bc.c +aes-default.c +aes-key.c +aes-neon.c +aes-selftest.c +aes-xmm.c +aes-ymm.c +aescmac.c +aesCtrDrbg.c +aeskw.c +AesTables.c +blockciphermodes.c +ccm.c +chacha20_poly1305.c +chacha20.c +cpuid_notry.c +cpuid_um.c +cpuid.c +crt.c +DesTables.c +desx.c +dh.c +dl_internal_groups.c +dlgroup.c +dlkey.c +dsa.c +ec_dh.c +ec_dispatch.c +ec_dsa.c +ec_internal_curves.c +ec_montgomery.c +ec_mul.c +ec_short_weierstrass.c +ec_twisted_edwards.c +eckey.c +ecpoint.c +ecurve.c +equal.c +FatalIntercept.c +fdef_general.c +fdef_int.c +fdef_mod.c +fdef369_mod.c +fips_selftest.c +gcm.c +gen_int.c +ghash.c +hash.c +hkdf_selftest.c +hkdf.c +hmac.c +hmacmd5.c +hmacsha1.c +hmacsha224.c +hmacsha256.c +hmacsha384.c +hmacsha512.c +hmacsha512_224.c +hmacsha512_256.c +hmacsha3_224.c +hmacsha3_256.c +hmacsha3_384.c +hmacsha3_512.c +kmac.c +libmain.c +lms.c +marvin32.c +md2.c +md4.c +md5.c +mldsa.c +mldsa_primitives.c +mlkem.c +mlkem_primitives.c +modexp.c +paddingPkcs7.c +parhash.c +pbkdf2_hmacsha1.c +pbkdf2_hmacsha256.c +pbkdf2.c +poly1305.c +primes.c +rc2.c +rc4.c +rdrand.c +rdseed.c +recoding.c +rsa_enc.c +rsa_padding.c +rsakey.c +ScsTable.c +scsTools.c +selftest.c +service_indicator.c +session.c +sha1.c +sha256.c +sha256Par.c +sha256Par-ymm.c +sha256-xmm.c +sha256-ymm.c +sha512.c +sha512Par.c +sha512Par-ymm.c +sha512-ymm.c +sha3.c +sha3_224.c +sha3_256.c +sha3_384.c +sha3_512.c +shake.c +sp800_108_hmacsha1.c +sp800_108_hmacsha256.c +sp800_108_hmacsha512.c +sp800_108.c +srtp_kdf.c +srtp_kdf_selftest.c +ssh_kdf.c +ssh_kdf_sha256.c +ssh_kdf_sha512.c +sskdf.c +sskdf_selftest.c +tlsCbcVerify.c +tlsprf_selftest.c +tlsprf.c +xmss.c +xtsaes.c +"; + +// only for x86_64_unknown_linux_gnu +const SPECIAL_FLAGS: &str = r#" +set_source_files_properties(aes-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mvaes;-mvpclmulqdq") +set_source_files_properties(sha256Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") +set_source_files_properties(sha512Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") +set_source_files_properties(sha256-xmm.c PROPERTIES COMPILE_OPTIONS "-mssse3") +set_source_files_properties(sha256-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") +set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") +"#; + +fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io::Result<()> { + let (already_compiled_files, intermediates) = compile_intermediates(&options); + + let mut base_files: Vec<&'static str> = CMAKE_SOURCES_COMMON + .lines() + .filter(|line| { + let line = line.trim(); + !(line.is_empty() || line.starts_with("#") || already_compiled_files.contains(&line)) + }) + .collect(); + + base_files.push("env_generic.c"); // symcrypt_generic + + let mut module_files = vec![]; + + match options.triple() { + Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { + base_files.push("env_windowsUserModeWin7.c"); + base_files.push("env_windowsUserModeWin8_1.c"); + base_files.push("IEEE802_11SaeCustom.c"); + module_files.push("symcrypt/modules/windows/user/module.c"); + } + Triple::x86_64_unknown_linux_gnu => { + base_files.push("linux/intrinsics.c"); + base_files.push("env_posixUserMode.c"); + + // generic + module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); + module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); + module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); + + // Enable integrity verification if compiling for AMD64 or ARM64 or ARM + module_files.push("symcrypt/modules/linux/common/integrity.c"); + + // symcrypt_module_linux_common + module_files.push("symcrypt/modules/linux/common/module.c"); + module_files.push("symcrypt/modules/linux/common/rng.c"); + } + Triple::aarch64_unknown_linux_gnu => { + base_files.push("env_linuxUserMode.c"); + + // generic + module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); + module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); + module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); + module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); + + // Enable integrity verification if compiling for AMD64 or ARM64 or ARM + module_files.push("symcrypt/modules/linux/common/integrity.c"); + + // symcrypt_module_linux_common + module_files.push("symcrypt/modules/linux/common/module.c"); + module_files.push("symcrypt/modules/linux/common/rng.c"); + } + } + + let asm_files = match options.triple() { + Triple::x86_64_pc_windows_msvc => vec![ + "aesasm.asm", + "fdef_asm.asm", + "fdef_mulx.asm", + "fdef369_asm.asm", + "sha256xmm_asm.asm", + "sha256ymm_asm.asm", + "sha512ymm_asm.asm", + "sha512ymm_avx512vl_asm.asm", + "wipe.asm", + ], + Triple::aarch64_pc_windows_msvc => vec!["fdef_asm.asm", "fdef369_asm.asm", "wipe.asm"], + Triple::x86_64_unknown_linux_gnu => vec![ + "aesasm-gas.asm", + "fdef_asm-gas.asm", + "fdef369_asm-gas.asm", + "fdef_mulx-gas.asm", + "wipe-gas.asm", + "sha256xmm_asm-gas.asm", + "sha256ymm_asm-gas.asm", + "sha512ymm_asm-gas.asm", + "sha512ymm_avx512vl_asm-gas.asm", + ], + Triple::aarch64_unknown_linux_gnu => { + vec!["fdef_asm-gas.asm", "fdef369_asm-gas.asm", "wipe-gas.asm"] + } + }; + + let mut cc = options.preconfigure_cc(); + cc.objects(intermediates); + + for file in base_files { + cc.file(format!("{SOURCE_DIR}/{file}")); + } + + if options.use_asm() { + for file in asm_files { + cc.file(format!( + "{SOURCE_DIR}/asm/{}/{file}", + options.triple.to_triple() + )); + } + } + cc.files(module_files); + + println!("Files to compile: {}", cc.get_files().count()); + cc.compile(lib_name); + + Ok(()) +} + +fn compile_intermediates( + symcrypt_options: &SymCryptOptions, +) -> (Vec<&'static str>, Vec) { + let mut files = vec![]; + let mut intermediates = vec![]; + + if symcrypt_options.triple() != Triple::x86_64_unknown_linux_gnu { + return (files, intermediates); + } + + for line in SPECIAL_FLAGS.lines() { + if line.trim().is_empty() || line.trim().starts_with("#") { + continue; + } + + let line = line + .strip_prefix("set_source_files_properties(") + .unwrap() + .strip_suffix(")") + .unwrap(); + + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() < 4 { + continue; + } + + let file = parts[0]; + println!("Compiling {file} with custom options: {}", parts[3]); + + let options = parts[3] + .trim_matches('"') + .split(';') + .filter(|s| !s.is_empty()); + + let mut cc = symcrypt_options.preconfigure_cc(); + cc.file(format!("{SOURCE_DIR}/{file}")); + for option in options { + cc.flag(option); + } + let mut result = cc.compile_intermediates(); + + files.push(file); + intermediates.append(&mut result); + } + + (files, intermediates) +} diff --git a/symcrypt-sys/build/triple.rs b/symcrypt-sys/build/triple.rs new file mode 100644 index 00000000..689ae4c3 --- /dev/null +++ b/symcrypt-sys/build/triple.rs @@ -0,0 +1,31 @@ +#[allow(non_camel_case_types)] +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Triple { + x86_64_pc_windows_msvc, + aarch64_pc_windows_msvc, + x86_64_unknown_linux_gnu, + aarch64_unknown_linux_gnu, +} + +impl Triple { + pub fn get_target_triple() -> Self { + let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); + let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + + match (target_os.as_str(), target_arch.as_str()) { + ("windows", "x86_64") => Triple::x86_64_pc_windows_msvc, + ("windows", "aarch64") => Triple::aarch64_pc_windows_msvc, + ("linux", "x86_64") => Triple::x86_64_unknown_linux_gnu, + ("linux", "aarch64") => Triple::aarch64_unknown_linux_gnu, + _ => panic!("unsupported target. OS: {target_os}, Arch: {target_arch}"), + } + } + pub fn to_triple(&self) -> &'static str { + match self { + Triple::x86_64_pc_windows_msvc => "x86_64-pc-windows-msvc", + Triple::aarch64_pc_windows_msvc => "aarch64-pc-windows-msvc", + Triple::x86_64_unknown_linux_gnu => "x86_64-unknown-linux-gnu", + Triple::aarch64_unknown_linux_gnu => "aarch64-unknown-linux-gnu", + } + } +} diff --git a/symcrypt-sys/inc/buildInfo.h b/symcrypt-sys/inc/buildInfo.h index fb984057..388b7a87 100644 --- a/symcrypt-sys/inc/buildInfo.h +++ b/symcrypt-sys/inc/buildInfo.h @@ -2,7 +2,7 @@ #define _SYMCRYPT_STRING_INT(a) #a #define _SYMCRYPT_STRING(a) _SYMCRYPT_STRING_INT(a) -#define SYMCRYPT_BUILD_INFO_BRANCH "" -#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-28T01:44:15+01:00_53be637" +#define SYMCRYPT_BUILD_INFO_BRANCH "main" +#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-27T19:44:15-05:00_53be637" #define SYMCRYPT_BUILD_INFO_VERSION _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_API) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_MINOR) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_PATCH) -#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-01-31T15:53:40" +#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-02-05T15:08:23" diff --git a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs index ee1327b4..a5b34264 100644 --- a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs @@ -1227,35 +1227,35 @@ impl Default for _SYMCRYPT_OID { } pub type SYMCRYPT_OID = _SYMCRYPT_OID; pub type PCSYMCRYPT_OID = *const SYMCRYPT_OID; -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptMd5OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha1OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha256OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha384OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha512OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_256OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_384OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_512OidList: [SYMCRYPT_OID; 2usize]; } @@ -2013,6 +2013,235 @@ pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPAN #[repr(C)] #[repr(align(16))] #[derive(Debug, Default, Copy, Clone)] +<<<<<<< HEAD +pub struct _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA384_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +pub type PSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +pub type PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA384_STATE { + pub hash: SYMCRYPT_SHA384_STATE, + pub pKey: PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_STATE> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), + 240usize, + "Size of _SYMCRYPT_HMAC_SHA384_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA384_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::hash" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, + 224usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::pKey" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 232usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::magic" + ); +} +impl Default for _SYMCRYPT_HMAC_SHA384_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_HMAC_SHA384_STATE = _SYMCRYPT_HMAC_SHA384_STATE; +pub type PSYMCRYPT_HMAC_SHA384_STATE = *mut _SYMCRYPT_HMAC_SHA384_STATE; +pub type PCSYMCRYPT_HMAC_SHA384_STATE = *const SYMCRYPT_HMAC_SHA384_STATE; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA512_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +pub type PSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +pub type PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_STATE { + pub hash: SYMCRYPT_SHA512_STATE, + pub pKey: PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_STATE> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), + 240usize, + "Size of _SYMCRYPT_HMAC_SHA512_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::hash" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, + 224usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::pKey" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 232usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::magic" + ); +} +impl Default for _SYMCRYPT_HMAC_SHA512_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_HMAC_SHA512_STATE = _SYMCRYPT_HMAC_SHA512_STATE; +pub type PSYMCRYPT_HMAC_SHA512_STATE = *mut _SYMCRYPT_HMAC_SHA512_STATE; +pub type PCSYMCRYPT_HMAC_SHA512_STATE = *const SYMCRYPT_HMAC_SHA512_STATE; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +======= +>>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) pub struct _SYMCRYPT_HMAC_SHA512_256_EXPANDED_KEY { pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, @@ -4492,7 +4721,7 @@ extern "C" { extern "C" { pub fn SymCryptMd5StateCopy(pSrc: PCSYMCRYPT_MD5_STATE, pDst: PSYMCRYPT_MD5_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptMd5Algorithm: PCSYMCRYPT_HASH; } @@ -4511,7 +4740,7 @@ extern "C" { extern "C" { pub fn SymCryptSha1StateCopy(pSrc: PCSYMCRYPT_SHA1_STATE, pDst: PSYMCRYPT_SHA1_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha1Algorithm: PCSYMCRYPT_HASH; } @@ -4530,7 +4759,7 @@ extern "C" { extern "C" { pub fn SymCryptSha256StateCopy(pSrc: PCSYMCRYPT_SHA256_STATE, pDst: PSYMCRYPT_SHA256_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha256Algorithm: PCSYMCRYPT_HASH; } @@ -4549,7 +4778,7 @@ extern "C" { extern "C" { pub fn SymCryptSha384StateCopy(pSrc: PCSYMCRYPT_SHA384_STATE, pDst: PSYMCRYPT_SHA384_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha384Algorithm: PCSYMCRYPT_HASH; } @@ -4568,7 +4797,7 @@ extern "C" { extern "C" { pub fn SymCryptSha512StateCopy(pSrc: PCSYMCRYPT_SHA512_STATE, pDst: PSYMCRYPT_SHA512_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha512Algorithm: PCSYMCRYPT_HASH; } @@ -4590,7 +4819,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_256_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_256Algorithm: PCSYMCRYPT_HASH; } @@ -4612,7 +4841,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_384_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_384Algorithm: PCSYMCRYPT_HASH; } @@ -4634,7 +4863,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_512_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_512Algorithm: PCSYMCRYPT_HASH; } @@ -4672,7 +4901,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacMd5Result(pState: PSYMCRYPT_HMAC_MD5_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacMd5Algorithm: PCSYMCRYPT_MAC; } @@ -4714,7 +4943,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha1Result(pState: PSYMCRYPT_HMAC_SHA1_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha1Algorithm: PCSYMCRYPT_MAC; } @@ -4756,7 +4985,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha256Result(pState: PSYMCRYPT_HMAC_SHA256_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha256Algorithm: PCSYMCRYPT_MAC; } @@ -4798,7 +5027,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha384Result(pState: PSYMCRYPT_HMAC_SHA384_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha384Algorithm: PCSYMCRYPT_MAC; } @@ -4840,7 +5069,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha512Result(pState: PSYMCRYPT_HMAC_SHA512_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha512Algorithm: PCSYMCRYPT_MAC; } @@ -4899,7 +5128,7 @@ extern "C" { cbData: SIZE_T, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptAesBlockCipher: PCSYMCRYPT_BLOCKCIPHER; } @@ -5327,19 +5556,19 @@ extern "C" { extern "C" { pub fn SymCryptEcurveSizeofFieldElement(pCurve: PCSYMCRYPT_ECURVE) -> UINT32; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP256: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP384: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP521: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsCurve25519: PCSYMCRYPT_ECURVE_PARAMS; } diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index 07970b27..a3a62591 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -62,7 +62,79 @@ pub type PCUINT64 = *const UINT64; pub type PVOID = *mut ::std::os::raw::c_void; pub type PCVOID = *const ::std::os::raw::c_void; pub type BOOLEAN = BYTE; -pub type __m128i = [::std::os::raw::c_longlong; 2usize]; +#[repr(C)] +#[repr(align(16))] +#[derive(Copy, Clone)] +pub union __m128i { + pub m128i_i8: [::std::os::raw::c_char; 16usize], + pub m128i_i16: [::std::os::raw::c_short; 8usize], + pub m128i_i32: [::std::os::raw::c_int; 4usize], + pub m128i_i64: [::std::os::raw::c_longlong; 2usize], + pub m128i_u8: [::std::os::raw::c_uchar; 16usize], + pub m128i_u16: [::std::os::raw::c_ushort; 8usize], + pub m128i_u32: [::std::os::raw::c_uint; 4usize], + pub m128i_u64: [::std::os::raw::c_ulonglong; 2usize], +} +#[test] +fn bindgen_test_layout___m128i() { + const UNINIT: ::std::mem::MaybeUninit<__m128i> = ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!(::std::mem::size_of::<__m128i>(), 16usize, "Size of __m128i"); + assert_eq!( + ::std::mem::align_of::<__m128i>(), + 16usize, + "Alignment of __m128i" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_i8) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_i8" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_i16) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_i16" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_i32) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_i32" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_i64) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_i64" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_u8) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_u8" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_u16) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_u16" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_u32) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_u32" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).m128i_u64) as usize - ptr as usize }, + 0usize, + "Offset of field: __m128i::m128i_u64" + ); +} +impl Default for __m128i { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} pub type SYMCRYPT_BLOCKCIPHER = _SYMCRYPT_BLOCKCIPHER; pub type PCSYMCRYPT_BLOCKCIPHER = *const SYMCRYPT_BLOCKCIPHER; #[repr(C)] @@ -804,6 +876,8 @@ pub struct _SYMCRYPT_SHA512_224_STATE { #[test] fn bindgen_test_layout__SYMCRYPT_SHA512_224_STATE() { const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_SHA512_224_STATE> = +<<<<<<< HEAD +======= ::std::mem::MaybeUninit::uninit(); let ptr = UNINIT.as_ptr(); assert_eq!( @@ -935,6 +1009,141 @@ pub struct _SYMCRYPT_KECCAK_STATE { pub squeezeMode: BOOLEAN, } #[test] +fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_KECCAK_STATE> = +>>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_SHA512_224_STATE>(), + 224usize, + "Size of _SYMCRYPT_SHA512_224_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_SHA512_224_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_SHA512_224_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).bytesInBuffer) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::bytesInBuffer" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 8usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::magic" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dataLengthL) as usize - ptr as usize }, + 16usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::dataLengthL" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dataLengthH) as usize - ptr as usize }, + 24usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::dataLengthH" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, + 32usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::buffer" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chain) as usize - ptr as usize }, + 160usize, + "Offset of field: _SYMCRYPT_SHA512_224_STATE::chain" + ); +} +impl Default for _SYMCRYPT_SHA512_224_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_SHA512_224_STATE = _SYMCRYPT_SHA512_224_STATE; +#[repr(C)] +#[repr(align(16))] +<<<<<<< HEAD +#[derive(Debug, Copy, Clone)] +pub struct _SYMCRYPT_SHA512_256_STATE { + pub bytesInBuffer: UINT32, + pub magic: SIZE_T, + pub dataLengthL: UINT64, + pub dataLengthH: UINT64, + pub buffer: [BYTE; 128usize], + pub chain: SYMCRYPT_SHA512_CHAINING_STATE, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_SHA512_256_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_SHA512_256_STATE> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_SHA512_256_STATE>(), + 224usize, + "Size of _SYMCRYPT_SHA512_256_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_SHA512_256_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_SHA512_256_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).bytesInBuffer) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::bytesInBuffer" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 8usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::magic" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dataLengthL) as usize - ptr as usize }, + 16usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::dataLengthL" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).dataLengthH) as usize - ptr as usize }, + 24usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::dataLengthH" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, + 32usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::buffer" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).chain) as usize - ptr as usize }, + 160usize, + "Offset of field: _SYMCRYPT_SHA512_256_STATE::chain" + ); +} +impl Default for _SYMCRYPT_SHA512_256_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_SHA512_256_STATE = _SYMCRYPT_SHA512_256_STATE; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +pub struct _SYMCRYPT_KECCAK_STATE { + pub state: [UINT64; 25usize], + pub inputBlockSize: UINT32, + pub stateIndex: UINT32, + pub paddingValue: UINT8, + pub squeezeMode: BOOLEAN, +} +#[test] fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_KECCAK_STATE> = ::std::mem::MaybeUninit::uninit(); @@ -978,6 +1187,8 @@ fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { pub type SYMCRYPT_KECCAK_STATE = _SYMCRYPT_KECCAK_STATE; #[repr(C)] #[repr(align(16))] +======= +>>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) #[derive(Debug, Default, Copy, Clone)] pub struct _SYMCRYPT_SHA3_224_STATE { pub ks: SYMCRYPT_KECCAK_STATE, @@ -1228,35 +1439,35 @@ impl Default for _SYMCRYPT_OID { } pub type SYMCRYPT_OID = _SYMCRYPT_OID; pub type PCSYMCRYPT_OID = *const SYMCRYPT_OID; -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptMd5OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha1OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha256OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha384OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha512OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_256OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_384OidList: [SYMCRYPT_OID; 2usize]; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_512OidList: [SYMCRYPT_OID; 2usize]; } @@ -2014,6 +2225,235 @@ pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPAN #[repr(C)] #[repr(align(16))] #[derive(Debug, Default, Copy, Clone)] +<<<<<<< HEAD +pub struct _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA384_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +pub type PSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +pub type PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA384_STATE { + pub hash: SYMCRYPT_SHA384_STATE, + pub pKey: PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_STATE> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), + 240usize, + "Size of _SYMCRYPT_HMAC_SHA384_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA384_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::hash" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, + 224usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::pKey" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 232usize, + "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::magic" + ); +} +impl Default for _SYMCRYPT_HMAC_SHA384_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_HMAC_SHA384_STATE = _SYMCRYPT_HMAC_SHA384_STATE; +pub type PSYMCRYPT_HMAC_SHA384_STATE = *mut _SYMCRYPT_HMAC_SHA384_STATE; +pub type PCSYMCRYPT_HMAC_SHA384_STATE = *const SYMCRYPT_HMAC_SHA384_STATE; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA512_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +pub type PSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +pub type PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_STATE { + pub hash: SYMCRYPT_SHA512_STATE, + pub pKey: PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_STATE() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_STATE> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), + 240usize, + "Size of _SYMCRYPT_HMAC_SHA512_STATE" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_STATE" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::hash" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, + 224usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::pKey" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 232usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::magic" + ); +} +impl Default for _SYMCRYPT_HMAC_SHA512_STATE { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +pub type SYMCRYPT_HMAC_SHA512_STATE = _SYMCRYPT_HMAC_SHA512_STATE; +pub type PSYMCRYPT_HMAC_SHA512_STATE = *mut _SYMCRYPT_HMAC_SHA512_STATE; +pub type PCSYMCRYPT_HMAC_SHA512_STATE = *const SYMCRYPT_HMAC_SHA512_STATE; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +pub struct _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY { + pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, + pub magic: SIZE_T, +} +#[test] +fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY() { + const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY> = + ::std::mem::MaybeUninit::uninit(); + let ptr = UNINIT.as_ptr(); + assert_eq!( + ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), + 144usize, + "Size of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" + ); + assert_eq!( + ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), + 16usize, + "Alignment of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, + 0usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::innerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, + 64usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::outerState" + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, + 128usize, + "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::magic" + ); +} +pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Default, Copy, Clone)] +======= +>>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) pub struct _SYMCRYPT_HMAC_SHA512_256_EXPANDED_KEY { pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, @@ -4226,7 +4666,7 @@ extern "C" { extern "C" { pub fn SymCryptMd5StateCopy(pSrc: PCSYMCRYPT_MD5_STATE, pDst: PSYMCRYPT_MD5_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptMd5Algorithm: PCSYMCRYPT_HASH; } @@ -4245,7 +4685,7 @@ extern "C" { extern "C" { pub fn SymCryptSha1StateCopy(pSrc: PCSYMCRYPT_SHA1_STATE, pDst: PSYMCRYPT_SHA1_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha1Algorithm: PCSYMCRYPT_HASH; } @@ -4264,7 +4704,7 @@ extern "C" { extern "C" { pub fn SymCryptSha256StateCopy(pSrc: PCSYMCRYPT_SHA256_STATE, pDst: PSYMCRYPT_SHA256_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha256Algorithm: PCSYMCRYPT_HASH; } @@ -4283,7 +4723,7 @@ extern "C" { extern "C" { pub fn SymCryptSha384StateCopy(pSrc: PCSYMCRYPT_SHA384_STATE, pDst: PSYMCRYPT_SHA384_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha384Algorithm: PCSYMCRYPT_HASH; } @@ -4302,7 +4742,7 @@ extern "C" { extern "C" { pub fn SymCryptSha512StateCopy(pSrc: PCSYMCRYPT_SHA512_STATE, pDst: PSYMCRYPT_SHA512_STATE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha512Algorithm: PCSYMCRYPT_HASH; } @@ -4324,7 +4764,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_256_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_256Algorithm: PCSYMCRYPT_HASH; } @@ -4346,7 +4786,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_384_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_384Algorithm: PCSYMCRYPT_HASH; } @@ -4368,7 +4808,7 @@ extern "C" { pDst: PSYMCRYPT_SHA3_512_STATE, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptSha3_512Algorithm: PCSYMCRYPT_HASH; } @@ -4406,7 +4846,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacMd5Result(pState: PSYMCRYPT_HMAC_MD5_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacMd5Algorithm: PCSYMCRYPT_MAC; } @@ -4448,7 +4888,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha1Result(pState: PSYMCRYPT_HMAC_SHA1_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha1Algorithm: PCSYMCRYPT_MAC; } @@ -4490,7 +4930,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha256Result(pState: PSYMCRYPT_HMAC_SHA256_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha256Algorithm: PCSYMCRYPT_MAC; } @@ -4532,7 +4972,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha384Result(pState: PSYMCRYPT_HMAC_SHA384_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha384Algorithm: PCSYMCRYPT_MAC; } @@ -4574,7 +5014,7 @@ extern "C" { extern "C" { pub fn SymCryptHmacSha512Result(pState: PSYMCRYPT_HMAC_SHA512_STATE, pbResult: PBYTE); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptHmacSha512Algorithm: PCSYMCRYPT_MAC; } @@ -4633,7 +5073,7 @@ extern "C" { cbData: SIZE_T, ); } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptAesBlockCipher: PCSYMCRYPT_BLOCKCIPHER; } @@ -5061,19 +5501,19 @@ extern "C" { extern "C" { pub fn SymCryptEcurveSizeofFieldElement(pCurve: PCSYMCRYPT_ECURVE) -> UINT32; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP256: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP384: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsNistP521: PCSYMCRYPT_ECURVE_PARAMS; } -#[link(name = "symcrypt", kind = "dylib")] +#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))] extern "C" { pub static SymCryptEcurveParamsCurve25519: PCSYMCRYPT_ECURVE_PARAMS; } From 1d3b1f5fe514daac79e02402461fa6781a063d42 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 6 Feb 2025 15:30:39 -0500 Subject: [PATCH 02/40] regenerate bindings --- symcrypt-sys/build/static_link.rs | 20 +- symcrypt-sys/inc/buildInfo.h | 16 +- symcrypt-sys/inc/symcrypt_internal_shared.inc | 66 ++-- .../src/bindings/aarch64_pc_windows_msvc.rs | 229 ----------- .../src/bindings/x86_64_pc_windows_msvc.rs | 368 ------------------ 5 files changed, 51 insertions(+), 648 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 39823928..cb061434 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -79,16 +79,16 @@ impl SymCryptOptions { Triple::x86_64_unknown_linux_gnu => { cc.include("symcrypt/modules/linux/common"); cc.flag("-mpclmul"); - cc.flag("-Wno-incompatible-pointer-types"); // Should we create parent Enum for Windows / Linux? - /* - cc.flag("-mpclmul") - .flag("-mssse3") - .flag("-mxsave") - .flag("-maes") - .flag("-msha") - .flag("-mrdrnd") - .flag("-mrdseed"); - */ + cc.flag("-Wno-incompatible-pointer-types"); // Should we create parent Enum for Windows / Linux? + /* + cc.flag("-mpclmul") + .flag("-mssse3") + .flag("-mxsave") + .flag("-maes") + .flag("-msha") + .flag("-mrdrnd") + .flag("-mrdseed"); + */ } Triple::aarch64_unknown_linux_gnu => { cc.include("symcrypt/modules/linux/common"); diff --git a/symcrypt-sys/inc/buildInfo.h b/symcrypt-sys/inc/buildInfo.h index 388b7a87..e8125f3e 100644 --- a/symcrypt-sys/inc/buildInfo.h +++ b/symcrypt-sys/inc/buildInfo.h @@ -1,8 +1,8 @@ -#include "symcrypt_internal_shared.inc" - -#define _SYMCRYPT_STRING_INT(a) #a -#define _SYMCRYPT_STRING(a) _SYMCRYPT_STRING_INT(a) -#define SYMCRYPT_BUILD_INFO_BRANCH "main" -#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-27T19:44:15-05:00_53be637" -#define SYMCRYPT_BUILD_INFO_VERSION _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_API) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_MINOR) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_PATCH) -#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-02-05T15:08:23" +#include "symcrypt_internal_shared.inc" + +#define _SYMCRYPT_STRING_INT(a) #a +#define _SYMCRYPT_STRING(a) _SYMCRYPT_STRING_INT(a) +#define SYMCRYPT_BUILD_INFO_BRANCH "main" +#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-27T19:44:15-05:00_53be637" +#define SYMCRYPT_BUILD_INFO_VERSION _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_API) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_MINOR) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_PATCH) +#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-02-06T15:27:36" diff --git a/symcrypt-sys/inc/symcrypt_internal_shared.inc b/symcrypt-sys/inc/symcrypt_internal_shared.inc index f89b662f..f60f9924 100644 --- a/symcrypt-sys/inc/symcrypt_internal_shared.inc +++ b/symcrypt-sys/inc/symcrypt_internal_shared.inc @@ -1,33 +1,33 @@ -// -// symcrypt_internal_shared.inc -// Copyright (c) Microsoft Corporation. Licensed under the MIT license. -// -// This is the file that contains the SymCrypt version information and defines SYMCRYPT_DEBUG. -// It is included in both C and ASM such that the values are the same on both sides. -// We use the C preprocessor to set ASM constants, as we already need to use the C preprocessor for -// symcryptasm processing (see scripts/symcryptasm_processor.py). -// -// In previous releases we had a numbering system with major/minor version number. -// This worked well with the sequential servicing imposed by SourceDepot. -// With the switch to Git this no longer works due to having multiple branches. -// We move to having the version here only specify the API and minor version number -// These will NOT be changed for every build. The API version only changes when there are -// breaking changes to the API in symcrypt.h. (Note: symcrypt_low_level.h is not stable and can change -// at any time.) The minor version is changed at regular intervals, but not necessarily at -// every build of the library. -// -// Separate from these numbers the build system includes information about the branch, -// last commit, build time, etc. -// -// The API numbering starts at 100 to avoid number conflicts with the old system. -// - -#define SYMCRYPT_CODE_VERSION_API 103 -#define SYMCRYPT_CODE_VERSION_MINOR 8 -#define SYMCRYPT_CODE_VERSION_PATCH 0 - -#if defined(DBG) -#define SYMCRYPT_DEBUG 1 -#else -#define SYMCRYPT_DEBUG 0 -#endif +// +// symcrypt_internal_shared.inc +// Copyright (c) Microsoft Corporation. Licensed under the MIT license. +// +// This is the file that contains the SymCrypt version information and defines SYMCRYPT_DEBUG. +// It is included in both C and ASM such that the values are the same on both sides. +// We use the C preprocessor to set ASM constants, as we already need to use the C preprocessor for +// symcryptasm processing (see scripts/symcryptasm_processor.py). +// +// In previous releases we had a numbering system with major/minor version number. +// This worked well with the sequential servicing imposed by SourceDepot. +// With the switch to Git this no longer works due to having multiple branches. +// We move to having the version here only specify the API and minor version number +// These will NOT be changed for every build. The API version only changes when there are +// breaking changes to the API in symcrypt.h. (Note: symcrypt_low_level.h is not stable and can change +// at any time.) The minor version is changed at regular intervals, but not necessarily at +// every build of the library. +// +// Separate from these numbers the build system includes information about the branch, +// last commit, build time, etc. +// +// The API numbering starts at 100 to avoid number conflicts with the old system. +// + +#define SYMCRYPT_CODE_VERSION_API 103 +#define SYMCRYPT_CODE_VERSION_MINOR 8 +#define SYMCRYPT_CODE_VERSION_PATCH 0 + +#if defined(DBG) +#define SYMCRYPT_DEBUG 1 +#else +#define SYMCRYPT_DEBUG 0 +#endif diff --git a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs index a5b34264..c0214865 100644 --- a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs @@ -2013,235 +2013,6 @@ pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPAN #[repr(C)] #[repr(align(16))] #[derive(Debug, Default, Copy, Clone)] -<<<<<<< HEAD -pub struct _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA384_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -pub type PSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -pub type PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA384_STATE { - pub hash: SYMCRYPT_SHA384_STATE, - pub pKey: PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_STATE> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), - 240usize, - "Size of _SYMCRYPT_HMAC_SHA384_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA384_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::hash" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, - 224usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::pKey" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 232usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::magic" - ); -} -impl Default for _SYMCRYPT_HMAC_SHA384_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_HMAC_SHA384_STATE = _SYMCRYPT_HMAC_SHA384_STATE; -pub type PSYMCRYPT_HMAC_SHA384_STATE = *mut _SYMCRYPT_HMAC_SHA384_STATE; -pub type PCSYMCRYPT_HMAC_SHA384_STATE = *const SYMCRYPT_HMAC_SHA384_STATE; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA512_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -pub type PSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -pub type PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_STATE { - pub hash: SYMCRYPT_SHA512_STATE, - pub pKey: PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_STATE> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), - 240usize, - "Size of _SYMCRYPT_HMAC_SHA512_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::hash" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, - 224usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::pKey" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 232usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::magic" - ); -} -impl Default for _SYMCRYPT_HMAC_SHA512_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_HMAC_SHA512_STATE = _SYMCRYPT_HMAC_SHA512_STATE; -pub type PSYMCRYPT_HMAC_SHA512_STATE = *mut _SYMCRYPT_HMAC_SHA512_STATE; -pub type PCSYMCRYPT_HMAC_SHA512_STATE = *const SYMCRYPT_HMAC_SHA512_STATE; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -======= ->>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) pub struct _SYMCRYPT_HMAC_SHA512_256_EXPANDED_KEY { pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index a3a62591..58a7ddc5 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -876,8 +876,6 @@ pub struct _SYMCRYPT_SHA512_224_STATE { #[test] fn bindgen_test_layout__SYMCRYPT_SHA512_224_STATE() { const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_SHA512_224_STATE> = -<<<<<<< HEAD -======= ::std::mem::MaybeUninit::uninit(); let ptr = UNINIT.as_ptr(); assert_eq!( @@ -1009,141 +1007,6 @@ pub struct _SYMCRYPT_KECCAK_STATE { pub squeezeMode: BOOLEAN, } #[test] -fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_KECCAK_STATE> = ->>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_SHA512_224_STATE>(), - 224usize, - "Size of _SYMCRYPT_SHA512_224_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_SHA512_224_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_SHA512_224_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).bytesInBuffer) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::bytesInBuffer" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 8usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::magic" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).dataLengthL) as usize - ptr as usize }, - 16usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::dataLengthL" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).dataLengthH) as usize - ptr as usize }, - 24usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::dataLengthH" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, - 32usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::buffer" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).chain) as usize - ptr as usize }, - 160usize, - "Offset of field: _SYMCRYPT_SHA512_224_STATE::chain" - ); -} -impl Default for _SYMCRYPT_SHA512_224_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_SHA512_224_STATE = _SYMCRYPT_SHA512_224_STATE; -#[repr(C)] -#[repr(align(16))] -<<<<<<< HEAD -#[derive(Debug, Copy, Clone)] -pub struct _SYMCRYPT_SHA512_256_STATE { - pub bytesInBuffer: UINT32, - pub magic: SIZE_T, - pub dataLengthL: UINT64, - pub dataLengthH: UINT64, - pub buffer: [BYTE; 128usize], - pub chain: SYMCRYPT_SHA512_CHAINING_STATE, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_SHA512_256_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_SHA512_256_STATE> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_SHA512_256_STATE>(), - 224usize, - "Size of _SYMCRYPT_SHA512_256_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_SHA512_256_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_SHA512_256_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).bytesInBuffer) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::bytesInBuffer" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 8usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::magic" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).dataLengthL) as usize - ptr as usize }, - 16usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::dataLengthL" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).dataLengthH) as usize - ptr as usize }, - 24usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::dataLengthH" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, - 32usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::buffer" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).chain) as usize - ptr as usize }, - 160usize, - "Offset of field: _SYMCRYPT_SHA512_256_STATE::chain" - ); -} -impl Default for _SYMCRYPT_SHA512_256_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_SHA512_256_STATE = _SYMCRYPT_SHA512_256_STATE; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -pub struct _SYMCRYPT_KECCAK_STATE { - pub state: [UINT64; 25usize], - pub inputBlockSize: UINT32, - pub stateIndex: UINT32, - pub paddingValue: UINT8, - pub squeezeMode: BOOLEAN, -} -#[test] fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_KECCAK_STATE> = ::std::mem::MaybeUninit::uninit(); @@ -1187,8 +1050,6 @@ fn bindgen_test_layout__SYMCRYPT_KECCAK_STATE() { pub type SYMCRYPT_KECCAK_STATE = _SYMCRYPT_KECCAK_STATE; #[repr(C)] #[repr(align(16))] -======= ->>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) #[derive(Debug, Default, Copy, Clone)] pub struct _SYMCRYPT_SHA3_224_STATE { pub ks: SYMCRYPT_KECCAK_STATE, @@ -2225,235 +2086,6 @@ pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPAN #[repr(C)] #[repr(align(16))] #[derive(Debug, Default, Copy, Clone)] -<<<<<<< HEAD -pub struct _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA384_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -pub type PSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -pub type PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA384_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA384_STATE { - pub hash: SYMCRYPT_SHA384_STATE, - pub pKey: PCSYMCRYPT_HMAC_SHA384_EXPANDED_KEY, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA384_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA384_STATE> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), - 240usize, - "Size of _SYMCRYPT_HMAC_SHA384_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA384_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA384_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::hash" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, - 224usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::pKey" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 232usize, - "Offset of field: _SYMCRYPT_HMAC_SHA384_STATE::magic" - ); -} -impl Default for _SYMCRYPT_HMAC_SHA384_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_HMAC_SHA384_STATE = _SYMCRYPT_HMAC_SHA384_STATE; -pub type PSYMCRYPT_HMAC_SHA384_STATE = *mut _SYMCRYPT_HMAC_SHA384_STATE; -pub type PCSYMCRYPT_HMAC_SHA384_STATE = *const SYMCRYPT_HMAC_SHA384_STATE; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA512_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -pub type PSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *mut _SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -pub type PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY = *const SYMCRYPT_HMAC_SHA512_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_STATE { - pub hash: SYMCRYPT_SHA512_STATE, - pub pKey: PCSYMCRYPT_HMAC_SHA512_EXPANDED_KEY, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_STATE() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_STATE> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), - 240usize, - "Size of _SYMCRYPT_HMAC_SHA512_STATE" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_STATE>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_STATE" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).hash) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::hash" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).pKey) as usize - ptr as usize }, - 224usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::pKey" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 232usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_STATE::magic" - ); -} -impl Default for _SYMCRYPT_HMAC_SHA512_STATE { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} -pub type SYMCRYPT_HMAC_SHA512_STATE = _SYMCRYPT_HMAC_SHA512_STATE; -pub type PSYMCRYPT_HMAC_SHA512_STATE = *mut _SYMCRYPT_HMAC_SHA512_STATE; -pub type PCSYMCRYPT_HMAC_SHA512_STATE = *const SYMCRYPT_HMAC_SHA512_STATE; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -pub struct _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY { - pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, - pub magic: SIZE_T, -} -#[test] -fn bindgen_test_layout__SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY() { - const UNINIT: ::std::mem::MaybeUninit<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY> = - ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!( - ::std::mem::size_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), - 144usize, - "Size of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" - ); - assert_eq!( - ::std::mem::align_of::<_SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY>(), - 16usize, - "Alignment of _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).innerState) as usize - ptr as usize }, - 0usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::innerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).outerState) as usize - ptr as usize }, - 64usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::outerState" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).magic) as usize - ptr as usize }, - 128usize, - "Offset of field: _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY::magic" - ); -} -pub type SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY = _SYMCRYPT_HMAC_SHA512_224_EXPANDED_KEY; -#[repr(C)] -#[repr(align(16))] -#[derive(Debug, Default, Copy, Clone)] -======= ->>>>>>> 5f23fa5 (Merged changes from u/vaiz/static2 and user/nnmkhang/static_103.8) pub struct _SYMCRYPT_HMAC_SHA512_256_EXPANDED_KEY { pub innerState: SYMCRYPT_SHA512_CHAINING_STATE, pub outerState: SYMCRYPT_SHA512_CHAINING_STATE, From 1f98ce6a4926bf6fb710b5e7c88cc1398b335913 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 6 Feb 2025 15:32:11 -0500 Subject: [PATCH 03/40] Cargo Fmt --- symcrypt-bindgen/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index 0d813bbe..c68fb6cb 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -152,7 +152,8 @@ fn get_rust_version_from_cargo_metadata() -> String { fn fix_bindings_for_windows(triple: &str, bindings_file: &str) { if triple.contains("windows") { println!("Fixing bindings for Windows"); - let link_str = r#"#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))]"#; + let link_str = + r#"#[cfg_attr(feature = "dynamic", link(name = "symcrypt", kind = "dylib"))]"#; let regex_exp1 = regex::Regex::new(r"pub static \w+: \[SYMCRYPT_OID; \d+usize\];").unwrap(); let regex_exp2 = regex::Regex::new(r"pub static \w+: PCSYMCRYPT_\w+;").unwrap(); let bindings_content = From 94fcdba4c629d5ad2173d16e1abf5d6acc2189df Mon Sep 17 00:00:00 2001 From: Vaiz <4908982+Vaiz@users.noreply.github.com> Date: Fri, 7 Feb 2025 17:42:58 +0100 Subject: [PATCH 04/40] minor fixes for static linking (#82) * update bindings * add condition for hack * fix path * manually test static build --- .github/workflows/build.yml | 19 +++-- symcrypt-sys/build/static_link.rs | 2 +- symcrypt-sys/inc/buildInfo.h | 6 +- .../src/bindings/x86_64_pc_windows_msvc.rs | 74 +------------------ 4 files changed, 14 insertions(+), 87 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2af0e0fd..b5b821fd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ on: env: CARGO_TERM_COLOR: always - CARGO_HACK_DEPTH: 2 + FEATURES_TO_TEST: md5,sha1,pkcs1-encrypt-decrypt jobs: build: @@ -54,8 +54,6 @@ jobs: sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu } - - uses: taiki-e/install-action@cargo-hack - - uses: Swatinem/rust-cache@v2 - name: Download SymCrypt @@ -84,17 +82,18 @@ jobs: - name: Release build run: cargo build --release --locked --verbose --target ${{ matrix.target }} - - name: Run tests (Debug) + - name: Run tests (Debug, dynamic) if: matrix.run-tests - shell: pwsh run: cargo test --locked --verbose --all-features --target ${{ matrix.target }} - - name: Run tests (Release) + - name: Run tests (Release, dynamic) if: matrix.run-tests - shell: pwsh run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} - - name: Run hack test + - name: Run test (Debug, static) if: matrix.run-tests - shell: pwsh - run: cargo hack test --depth $env:CARGO_HACK_DEPTH --feature-powerset --locked --target ${{ matrix.target }} + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} + + - name: Run test (Release, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index cb061434..b7fc20d6 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -291,7 +291,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io module_files.push("symcrypt/modules/linux/common/rng.c"); } Triple::aarch64_unknown_linux_gnu => { - base_files.push("env_linuxUserMode.c"); + base_files.push("env_posixUserMode.c"); // generic module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); diff --git a/symcrypt-sys/inc/buildInfo.h b/symcrypt-sys/inc/buildInfo.h index e8125f3e..c93659e2 100644 --- a/symcrypt-sys/inc/buildInfo.h +++ b/symcrypt-sys/inc/buildInfo.h @@ -2,7 +2,7 @@ #define _SYMCRYPT_STRING_INT(a) #a #define _SYMCRYPT_STRING(a) _SYMCRYPT_STRING_INT(a) -#define SYMCRYPT_BUILD_INFO_BRANCH "main" -#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-27T19:44:15-05:00_53be637" +#define SYMCRYPT_BUILD_INFO_BRANCH "" +#define SYMCRYPT_BUILD_INFO_COMMIT "2025-01-28T01:44:15+01:00_53be637" #define SYMCRYPT_BUILD_INFO_VERSION _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_API) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_MINOR) "." _SYMCRYPT_STRING(SYMCRYPT_CODE_VERSION_PATCH) -#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-02-06T15:27:36" +#define SYMCRYPT_BUILD_INFO_TIMESTAMP "2025-02-07T14:10:46" diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index 58a7ddc5..d115bf07 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -62,79 +62,7 @@ pub type PCUINT64 = *const UINT64; pub type PVOID = *mut ::std::os::raw::c_void; pub type PCVOID = *const ::std::os::raw::c_void; pub type BOOLEAN = BYTE; -#[repr(C)] -#[repr(align(16))] -#[derive(Copy, Clone)] -pub union __m128i { - pub m128i_i8: [::std::os::raw::c_char; 16usize], - pub m128i_i16: [::std::os::raw::c_short; 8usize], - pub m128i_i32: [::std::os::raw::c_int; 4usize], - pub m128i_i64: [::std::os::raw::c_longlong; 2usize], - pub m128i_u8: [::std::os::raw::c_uchar; 16usize], - pub m128i_u16: [::std::os::raw::c_ushort; 8usize], - pub m128i_u32: [::std::os::raw::c_uint; 4usize], - pub m128i_u64: [::std::os::raw::c_ulonglong; 2usize], -} -#[test] -fn bindgen_test_layout___m128i() { - const UNINIT: ::std::mem::MaybeUninit<__m128i> = ::std::mem::MaybeUninit::uninit(); - let ptr = UNINIT.as_ptr(); - assert_eq!(::std::mem::size_of::<__m128i>(), 16usize, "Size of __m128i"); - assert_eq!( - ::std::mem::align_of::<__m128i>(), - 16usize, - "Alignment of __m128i" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_i8) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_i8" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_i16) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_i16" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_i32) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_i32" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_i64) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_i64" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_u8) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_u8" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_u16) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_u16" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_u32) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_u32" - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).m128i_u64) as usize - ptr as usize }, - 0usize, - "Offset of field: __m128i::m128i_u64" - ); -} -impl Default for __m128i { - fn default() -> Self { - let mut s = ::std::mem::MaybeUninit::::uninit(); - unsafe { - ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); - s.assume_init() - } - } -} +pub type __m128i = [::std::os::raw::c_longlong; 2usize]; pub type SYMCRYPT_BLOCKCIPHER = _SYMCRYPT_BLOCKCIPHER; pub type PCSYMCRYPT_BLOCKCIPHER = *const SYMCRYPT_BLOCKCIPHER; #[repr(C)] From 9bbc251ceebfa5db1b743d9e58cc7485229f89d2 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Mon, 10 Feb 2025 09:44:32 -0500 Subject: [PATCH 05/40] small comments --- symcrypt-sys/build/main.rs | 6 ++++++ symcrypt-sys/build/static_link.rs | 1 + 2 files changed, 7 insertions(+) diff --git a/symcrypt-sys/build/main.rs b/symcrypt-sys/build/main.rs index f67c866a..55434d7a 100644 --- a/symcrypt-sys/build/main.rs +++ b/symcrypt-sys/build/main.rs @@ -24,6 +24,8 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { // Look for the .lib file during link time. We are searching the Windows/System32 path which is set as a current default to match // the long term placement of a Windows shipped symcrypt.dll + // TODO: Update this info + let lib_path = std::env::var("SYMCRYPT_LIB_PATH") .unwrap_or_else(|_| panic!("SYMCRYPT_LIB_PATH environment variable not set, for more information please see: https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt#quick-start-guide")); println!("cargo:rustc-link-search=native={}", lib_path); @@ -47,6 +49,10 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { #[cfg(target_os = "linux")] { + + + //TODO: update this info + // Note: Linux support is based off of the Azure Linux distro. // This has been tested on Ubuntu 22.04.03 LTS on WSL and has confirmed working but support for other distros // aside from Azure Linux is not guaranteed so YMMV. diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index b7fc20d6..630c20a3 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -9,6 +9,7 @@ pub fn compile_and_link_symcrypt() -> std::io::Result<()> { let options = SymCryptOptions::new(); println!("Build config: {:?}", options); + // Required Windows bcrypt dependency for BCryptGenRandom const ADDITIONAL_DEPENDENCIES: &[&str] = &[ #[cfg(windows)] "bcrypt", From 0a335468e8b71f7ed58c2c880e7173c68c064b3e Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Mon, 10 Feb 2025 11:05:23 -0500 Subject: [PATCH 06/40] Adding custom impl for symcrypt random --- rust-symcrypt/src/lib.rs | 10 ++-- symcrypt-sys/build/static_link.rs | 26 ++++++++-- symcrypt-sys/inc/test.c | 80 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 symcrypt-sys/inc/test.c diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 5ea0b00e..bf7281da 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -29,10 +29,12 @@ fn symcrypt_init() { /// Takes in a a buffer called `buff` and fills it with random bytes. This function cannot fail. pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_init(); - unsafe { - // SAFETY: FFI calls - symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); - } + + // Commenting out for custom random testing. In the future; dynamic will call SymCryptRandom and static will call custom impl. + // unsafe { + // // SAFETY: FFI calls + // symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); + // } } /// `NumberFormat` is an enum that contains a friendly representation of endianess diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 630c20a3..761d36c7 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -278,18 +278,34 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io // generic module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); + + // Not needed for non fips. + // module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); + // module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); + // module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); + + // Required for SYMCRYPT_ENVIRONMENT_POSIX_USERMODE, could move this to custom test.c file. module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); + + // Required for SymCryptCallBackAlloc/Free module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); + // Custom test.c file that contains SymCryptRandom, SymCryptModuleInit and SymCryptCallBackRandom + module_files.push("inc/test.c"); + + // Enable integrity verification if compiling for AMD64 or ARM64 or ARM module_files.push("symcrypt/modules/linux/common/integrity.c"); // symcrypt_module_linux_common - module_files.push("symcrypt/modules/linux/common/module.c"); - module_files.push("symcrypt/modules/linux/common/rng.c"); + + // Not required since we're creating our own random. + + // module.c mainly exposes SymCryptModuleInit which we've copied to test.c + // module_files.push("symcrypt/modules/linux/common/module.c"); + + // rng.c is not required since we're creating our own random, this file exposes SymCryptRandom, which we should just be able to replace + // module_files.push("symcrypt/modules/linux/common/rng.c"); } Triple::aarch64_unknown_linux_gnu => { base_files.push("env_posixUserMode.c"); diff --git a/symcrypt-sys/inc/test.c b/symcrypt-sys/inc/test.c new file mode 100644 index 00000000..c37a6ffd --- /dev/null +++ b/symcrypt-sys/inc/test.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#include "wrapper.h" +#include + + +VOID SYMCRYPT_CALL SymCryptModuleInit( UINT32 api, UINT32 minor ) +{ + if( api != SYMCRYPT_CODE_VERSION_API || + (api == SYMCRYPT_CODE_VERSION_API && minor > SYMCRYPT_CODE_VERSION_MINOR) ) + { + SymCryptFatal( 'vers' ); + } +} + +SYMCRYPT_ERROR +SYMCRYPT_CALL +SymCryptCallbackRandom( PBYTE pbBuffer, SIZE_T cbBuffer ) +{ + customRand( pbBuffer, cbBuffer ); + return SYMCRYPT_NO_ERROR; +} + + + + +VOID +SYMCRYPT_CALL +customRand( PBYTE pbRandom, SIZE_T cbRandom) { + SIZE_T result; + result = getrandom( pbRandom, cbRandom, 0 ); + if (result != cbRandom ) + { + // If the entropy pool has been initialized and the request size is small + // (buflen <= 256), then getrandom() will not fail with EINTR, + // but we check anyway as it's not safe to continue if we don't + // receive the right amount of entropy. + SymCryptFatal( 'rngs' ); + } +} + +// // +// // rngsecureurandom.c +// // Defines secure entropy functions using urandom as the source +// // +// // Copyright (c) Microsoft Corporation. Licensed under the MIT license. +// // + +// #include "precomp.h" +// #include + +// // Nothing to init +// VOID +// SYMCRYPT_CALL +// SymCryptEntropySecureInit(void){} + +// // Nothing to uninit +// VOID +// SYMCRYPT_CALL +// SymCryptEntropySecureUninit(void){} + +// // urandom is our secure entropy source. +// VOID +// SYMCRYPT_CALL +// SymCryptEntropySecureGet( _Out_writes_( cbResult ) PBYTE pbResult, SIZE_T cbResult ) +// { +// SIZE_T result; +// result = getrandom( pbResult, cbResult, 0 ); +// if (result != cbResult ) +// { +// // If the entropy pool has been initialized and the request size is small +// // (buflen <= 256), then getrandom() will not fail with EINTR, +// // but we check anyway as it's not safe to continue if we don't +// // receive the right amount of entropy. +// SymCryptFatal( 'rngs' ); +// } +// } \ No newline at end of file From fcd36d35b24b662c49944a078f9efb4b46580609 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Mon, 10 Feb 2025 18:02:19 -0500 Subject: [PATCH 07/40] Remove most reliant code for static linking --- rust-symcrypt/src/lib.rs | 18 ++++-- symcrypt-bindgen/src/main.rs | 1 + symcrypt-sys/build/static_link.rs | 46 +++++---------- symcrypt-sys/inc/test.c | 95 ++++++++++++++++--------------- 4 files changed, 77 insertions(+), 83 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index bf7281da..b49cfc95 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -17,12 +17,20 @@ fn symcrypt_init() { static INIT: Once = Once::new(); unsafe { // SAFETY: FFI calls, blocking from being run again. + #[cfg(feature = "dynamic")] INIT.call_once(|| { symcrypt_sys::SymCryptModuleInit( symcrypt_sys::SYMCRYPT_CODE_VERSION_API, symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, ) }); + + #[cfg(not(feature = "dynamic"))] + println!("static mode"); + // INIT.call_once(|| { + // symcrypt_sys::SymCryptInit() + // }); + } } @@ -30,11 +38,11 @@ fn symcrypt_init() { pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_init(); - // Commenting out for custom random testing. In the future; dynamic will call SymCryptRandom and static will call custom impl. - // unsafe { - // // SAFETY: FFI calls - // symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); - // } + //Commenting out for custom random testing. In the future; dynamic will call SymCryptRandom and static will call custom impl. + unsafe { + // SAFETY: FFI calls + symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); + } } /// `NumberFormat` is an enum that contains a friendly representation of endianess diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index c68fb6cb..506cec3f 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -57,6 +57,7 @@ fn main() { // INIT FUNCTIONS .allowlist_function("SymCryptModuleInit") + // TODO: Add symcryptinit() for static linking. .allowlist_var("^(SYMCRYPT_CODE_VERSION.*)$") // HASH FUNCTIONS .allowlist_function("^SymCrypt(?:Sha3_(?:256|384|512)|Sha(?:256|384|512|1)|Md5)(?:Init|Append|Result|StateCopy)?$") diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 761d36c7..e55b99df 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -265,6 +265,12 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io let mut module_files = vec![]; + + /// TODO: + /// remove env_usermode_win7 + /// remove module.c from windows branch + /// create "test.c" for windows branch / combine windows callbacks etc to test.c + /// add symcryptinit for static linking match options.triple() { Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { base_files.push("env_windowsUserModeWin7.c"); @@ -275,54 +281,32 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io Triple::x86_64_unknown_linux_gnu => { base_files.push("linux/intrinsics.c"); base_files.push("env_posixUserMode.c"); - - // generic - module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); - - // Not needed for non fips. - // module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); - // module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); - // module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); - - // Required for SYMCRYPT_ENVIRONMENT_POSIX_USERMODE, could move this to custom test.c file. - module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); - - // Required for SymCryptCallBackAlloc/Free - module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); - - // Custom test.c file that contains SymCryptRandom, SymCryptModuleInit and SymCryptCallBackRandom module_files.push("inc/test.c"); - - - // Enable integrity verification if compiling for AMD64 or ARM64 or ARM - module_files.push("symcrypt/modules/linux/common/integrity.c"); - - // symcrypt_module_linux_common - - // Not required since we're creating our own random. - - // module.c mainly exposes SymCryptModuleInit which we've copied to test.c - // module_files.push("symcrypt/modules/linux/common/module.c"); - - // rng.c is not required since we're creating our own random, this file exposes SymCryptRandom, which we should just be able to replace - // module_files.push("symcrypt/modules/linux/common/rng.c"); } Triple::aarch64_unknown_linux_gnu => { base_files.push("env_posixUserMode.c"); // generic + // not needed for generic non fips. module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); + + // Required for SYMCRYPT_ENVIRONMENT_POSIX_USERMODE, could move this to custom test.c file. module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); + + // Required for SymCryptCallBackAlloc/Free module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); // Enable integrity verification if compiling for AMD64 or ARM64 or ARM - module_files.push("symcrypt/modules/linux/common/integrity.c"); + module_files.push("symcrypt/modules/linux/common/integrity.c"); //not needed // symcrypt_module_linux_common + + // module.c mainly exposes SymCryptModuleInit which we've copied to test.c module_files.push("symcrypt/modules/linux/common/module.c"); + // rng.c is not required since we're creating our own random, this file exposes SymCryptRandom, which we should just be able to replace module_files.push("symcrypt/modules/linux/common/rng.c"); } } diff --git a/symcrypt-sys/inc/test.c b/symcrypt-sys/inc/test.c index c37a6ffd..be7ec8f0 100644 --- a/symcrypt-sys/inc/test.c +++ b/symcrypt-sys/inc/test.c @@ -5,31 +5,69 @@ #include "wrapper.h" #include +#include +SYMCRYPT_ENVIRONMENT_POSIX_USERMODE +// add SymCryptINit() for static link -VOID SYMCRYPT_CALL SymCryptModuleInit( UINT32 api, UINT32 minor ) +PVOID +SYMCRYPT_CALL +SymCryptCallbackAlloc( SIZE_T nBytes ) { - if( api != SYMCRYPT_CODE_VERSION_API || - (api == SYMCRYPT_CODE_VERSION_API && minor > SYMCRYPT_CODE_VERSION_MINOR) ) - { - SymCryptFatal( 'vers' ); - } + // aligned_alloc requires size to be integer multiple of alignment + SIZE_T cbAllocation = (nBytes + (SYMCRYPT_ASYM_ALIGN_VALUE - 1)) & ~(SYMCRYPT_ASYM_ALIGN_VALUE - 1); + + return aligned_alloc(SYMCRYPT_ASYM_ALIGN_VALUE, cbAllocation); } +VOID +SYMCRYPT_CALL +SymCryptCallbackFree( VOID * pMem ) +{ + free( pMem ); +} + +// from linux docs + +// RETURN VALUE top +// On success, getrandom() returns the number of bytes that were +// copied to the buffer buf. This may be less than the number of +// bytes requested via buflen if either GRND_RANDOM was specified in +// flags and insufficient entropy was present in the random source or +// the system call was interrupted by a signal. + +// On error, -1 is returned, and errno is set to indicate the error. SYMCRYPT_ERROR SYMCRYPT_CALL -SymCryptCallbackRandom( PBYTE pbBuffer, SIZE_T cbBuffer ) +SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) { - customRand( pbBuffer, cbBuffer ); + size_t total_received = 0; + ssize_t result; + + while (total_received < cbBuffer) { + result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); + + if (result < 0) { + if (errno == EINTR) { + // Interrupted by a signal, retry the call + continue; + } + //return SYMCRYPT_INTERNAL_ERROR; // Other errors, fail + SymCryptFatal( 'vers' ); + + } + + total_received += (size_t)result; + } + return SYMCRYPT_NO_ERROR; } - VOID SYMCRYPT_CALL -customRand( PBYTE pbRandom, SIZE_T cbRandom) { +SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { SIZE_T result; result = getrandom( pbRandom, cbRandom, 0 ); if (result != cbRandom ) @@ -41,40 +79,3 @@ customRand( PBYTE pbRandom, SIZE_T cbRandom) { SymCryptFatal( 'rngs' ); } } - -// // -// // rngsecureurandom.c -// // Defines secure entropy functions using urandom as the source -// // -// // Copyright (c) Microsoft Corporation. Licensed under the MIT license. -// // - -// #include "precomp.h" -// #include - -// // Nothing to init -// VOID -// SYMCRYPT_CALL -// SymCryptEntropySecureInit(void){} - -// // Nothing to uninit -// VOID -// SYMCRYPT_CALL -// SymCryptEntropySecureUninit(void){} - -// // urandom is our secure entropy source. -// VOID -// SYMCRYPT_CALL -// SymCryptEntropySecureGet( _Out_writes_( cbResult ) PBYTE pbResult, SIZE_T cbResult ) -// { -// SIZE_T result; -// result = getrandom( pbResult, cbResult, 0 ); -// if (result != cbResult ) -// { -// // If the entropy pool has been initialized and the request size is small -// // (buflen <= 256), then getrandom() will not fail with EINTR, -// // but we check anyway as it's not safe to continue if we don't -// // receive the right amount of entropy. -// SymCryptFatal( 'rngs' ); -// } -// } \ No newline at end of file From d6a38d86ad0122a84ae74e722b426003a0a0fca3 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Tue, 11 Feb 2025 15:23:26 -0500 Subject: [PATCH 08/40] generate new binding for SymCryptInit --- rust-symcrypt/src/lib.rs | 10 ++++++++-- symcrypt-bindgen/src/main.rs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index b49cfc95..4eb1595f 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -37,10 +37,16 @@ fn symcrypt_init() { /// Takes in a a buffer called `buff` and fills it with random bytes. This function cannot fail. pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_init(); - - //Commenting out for custom random testing. In the future; dynamic will call SymCryptRandom and static will call custom impl. + unsafe { // SAFETY: FFI calls + + #[cfg(feature = "dynamic")] + // If calling dynamically, we will use SymCryptRandom that is provided by the SymCrypt library. + symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); + + #[cfg(not(feature = "dynamic"))] + // If calling statically, we will use a custom random implementation. symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); } } diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index 506cec3f..db77465d 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -57,7 +57,7 @@ fn main() { // INIT FUNCTIONS .allowlist_function("SymCryptModuleInit") - // TODO: Add symcryptinit() for static linking. + .allowlist_function("SymCryptInit") .allowlist_var("^(SYMCRYPT_CODE_VERSION.*)$") // HASH FUNCTIONS .allowlist_function("^SymCrypt(?:Sha3_(?:256|384|512)|Sha(?:256|384|512|1)|Md5)(?:Init|Append|Result|StateCopy)?$") From 23f576d17e05515af3f76df38b162f60baed768e Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Tue, 11 Feb 2025 17:30:59 -0500 Subject: [PATCH 09/40] Adding default static lib impl for windows --- rust-symcrypt/src/lib.rs | 33 ++++++--- symcrypt-sys/build/jitterentropy.rs | 73 ------------------- symcrypt-sys/build/main.rs | 5 +- symcrypt-sys/build/static_link.rs | 50 +------------ .../inc/{test.c => static_LinuxDefault.c} | 13 +--- symcrypt-sys/inc/static_WindowsDefault.c | 58 +++++++++++++++ .../src/bindings/aarch64_pc_windows_msvc.rs | 3 + .../src/bindings/aarch64_unknown_linux_gnu.rs | 3 + .../src/bindings/x86_64_pc_windows_msvc.rs | 3 + .../src/bindings/x86_64_unknown_linux_gnu.rs | 3 + 10 files changed, 98 insertions(+), 146 deletions(-) delete mode 100644 symcrypt-sys/build/jitterentropy.rs rename symcrypt-sys/inc/{test.c => static_LinuxDefault.c} (91%) create mode 100644 symcrypt-sys/inc/static_WindowsDefault.c diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 4eb1595f..d210d402 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -18,19 +18,23 @@ fn symcrypt_init() { unsafe { // SAFETY: FFI calls, blocking from being run again. #[cfg(feature = "dynamic")] - INIT.call_once(|| { - symcrypt_sys::SymCryptModuleInit( - symcrypt_sys::SYMCRYPT_CODE_VERSION_API, - symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, - ) - }); + { + println!("dynamic mode"); + INIT.call_once(|| { + symcrypt_sys::SymCryptModuleInit( + symcrypt_sys::SYMCRYPT_CODE_VERSION_API, + symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, + ) + }); + } #[cfg(not(feature = "dynamic"))] - println!("static mode"); - // INIT.call_once(|| { - // symcrypt_sys::SymCryptInit() - // }); - + { + println!("static mode"); + INIT.call_once(|| { + symcrypt_sys::SymCryptInit() + }); + } } } @@ -46,8 +50,13 @@ pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); #[cfg(not(feature = "dynamic"))] - // If calling statically, we will use a custom random implementation. symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); + + + // TODO: Investigate if we should use getrandom here instead of SymCryptRandom + // potentially use rand::get_random() here as to not have a mismatch. Although it is possible to expose a version + // of SymCryptRandom in static mode, There could be a potential issue with mixing function definitions between + // the dynamic and static modes. } } diff --git a/symcrypt-sys/build/jitterentropy.rs b/symcrypt-sys/build/jitterentropy.rs deleted file mode 100644 index 52ab018b..00000000 --- a/symcrypt-sys/build/jitterentropy.rs +++ /dev/null @@ -1,73 +0,0 @@ -use super::triple::Triple; - -const LIB_NAME: &str = "jitterentropy"; - -pub fn compile_and_link_jitterentropy(triple: Triple) { - println!("Compiling jitterentropy..."); - let cargo_toml_dir = - std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set"); - let jitterentropy_dir = format!("{cargo_toml_dir}/symcrypt/3rdparty/jitterentropy-library"); - - let mut cc = cc::Build::new(); - cc.target(triple.to_triple()) - .warnings(false) - .include(&jitterentropy_dir) - .include(format!("{jitterentropy_dir}/src")); - - // Add the source files - let src_files = std::fs::read_dir(format!("{jitterentropy_dir}/src")) - .expect("Failed to read src directory") - .filter_map(|entry| { - let entry = entry.expect("Failed to read directory entry"); - let path = entry.path(); - if path.extension().and_then(|s| s.to_str()) == Some("c") { - Some(path) - } else { - None - } - }); - cc.files(src_files); - - // Set compiler flags. Warnings are commented out. - cc.flag_if_supported("-fwrapv") - .flag_if_supported("--param ssp-buffer-size=4") - .flag_if_supported("-fvisibility=hidden") - .flag_if_supported("-fPIE") - //.flag_if_supported("-Wcast-align") - //.flag_if_supported("-Wmissing-field-initializers") - //.flag_if_supported("-Wshadow") - //.flag_if_supported("-Wswitch-enum") - //.flag_if_supported("-Wextra") - //.flag_if_supported("-Wall") - //.flag_if_supported("-pedantic") - .flag_if_supported("-fPIC") - .flag_if_supported("-O0") - //.flag_if_supported("-Wconversion") - ; - - if gcc_version_ge_490() { - cc.flag_if_supported("-fstack-protector-strong"); - } else { - cc.flag_if_supported("-fstack-protector-all"); - } - - cc.compile(LIB_NAME); - println!("cargo:rustc-link-lib=static={LIB_NAME}"); -} - -fn gcc_version_ge_490() -> bool { - if let Ok(output) = std::process::Command::new("gcc") - .arg("-dumpversion") - .output() - { - if let Ok(version) = String::from_utf8(output.stdout) { - let parts: Vec<&str> = version.trim().split('.').collect(); - if parts.len() >= 2 { - if let (Ok(major), Ok(minor)) = (parts[0].parse::(), parts[1].parse::()) { - return major > 4 || (major == 4 && minor >= 9); - } - } - } - } - false -} diff --git a/symcrypt-sys/build/main.rs b/symcrypt-sys/build/main.rs index 55434d7a..a2fc8510 100644 --- a/symcrypt-sys/build/main.rs +++ b/symcrypt-sys/build/main.rs @@ -4,9 +4,6 @@ pub mod static_link; #[cfg(not(feature = "dynamic"))] pub mod triple; -#[cfg(not(feature = "dynamic"))] -pub mod jitterentropy; - fn main() -> std::io::Result<()> { #[cfg(feature = "dynamic")] link_symcrypt_dynamically()?; @@ -25,7 +22,7 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { // the long term placement of a Windows shipped symcrypt.dll // TODO: Update this info - + let lib_path = std::env::var("SYMCRYPT_LIB_PATH") .unwrap_or_else(|_| panic!("SYMCRYPT_LIB_PATH environment variable not set, for more information please see: https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt#quick-start-guide")); println!("cargo:rustc-link-search=native={}", lib_path); diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index e55b99df..1ce7a456 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -1,4 +1,3 @@ -use super::jitterentropy::compile_and_link_jitterentropy; use super::triple::Triple; const LIB_NAME: &str = "symcrypt"; @@ -24,10 +23,6 @@ pub fn compile_and_link_symcrypt() -> std::io::Result<()> { println!("cargo:rustc-link-lib=dylib={dep}"); } - if options.need_jitterentropy() { - compile_and_link_jitterentropy(options.triple()); - } - Ok(()) } @@ -51,12 +46,6 @@ impl SymCryptOptions { fn triple(&self) -> Triple { self.triple.clone() } - fn need_jitterentropy(&self) -> bool { - matches!( - self.triple, - Triple::x86_64_unknown_linux_gnu | Triple::aarch64_unknown_linux_gnu - ) - } fn preconfigure_cc(&self) -> cc::Build { let mut cc = cc::Build::new(); @@ -97,10 +86,6 @@ impl SymCryptOptions { } } - if self.need_jitterentropy() { - cc.include("symcrypt/3rdparty/jitterentropy-library"); - } - cc } } @@ -266,48 +251,21 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io let mut module_files = vec![]; - /// TODO: - /// remove env_usermode_win7 - /// remove module.c from windows branch - /// create "test.c" for windows branch / combine windows callbacks etc to test.c - /// add symcryptinit for static linking match options.triple() { Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { - base_files.push("env_windowsUserModeWin7.c"); base_files.push("env_windowsUserModeWin8_1.c"); base_files.push("IEEE802_11SaeCustom.c"); - module_files.push("symcrypt/modules/windows/user/module.c"); + //module_files.push("symcrypt/modules/windows/user/module.c"); + module_files.push("inc/static_WindowsDefault.c"); } Triple::x86_64_unknown_linux_gnu => { base_files.push("linux/intrinsics.c"); base_files.push("env_posixUserMode.c"); - module_files.push("inc/test.c"); + module_files.push("inc/static_LinuxDefault.c"); } Triple::aarch64_unknown_linux_gnu => { base_files.push("env_posixUserMode.c"); - - // generic - // not needed for generic non fips. - module_files.push("symcrypt/modules/linux/generic/statusindicator.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngfipsjitter.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngforkdetection.c"); - module_files.push("symcrypt/modules/linux/common/optional/rngsecureurandom.c"); - - // Required for SYMCRYPT_ENVIRONMENT_POSIX_USERMODE, could move this to custom test.c file. - module_files.push("symcrypt/modules/linux/common/optional/module_linuxUserMode.c"); - - // Required for SymCryptCallBackAlloc/Free - module_files.push("symcrypt/modules/linux/common/callbacks_pthread.c"); - - // Enable integrity verification if compiling for AMD64 or ARM64 or ARM - module_files.push("symcrypt/modules/linux/common/integrity.c"); //not needed - - // symcrypt_module_linux_common - - // module.c mainly exposes SymCryptModuleInit which we've copied to test.c - module_files.push("symcrypt/modules/linux/common/module.c"); - // rng.c is not required since we're creating our own random, this file exposes SymCryptRandom, which we should just be able to replace - module_files.push("symcrypt/modules/linux/common/rng.c"); + module_files.push("inc/static_LinuxDefault.c"); } } diff --git a/symcrypt-sys/inc/test.c b/symcrypt-sys/inc/static_LinuxDefault.c similarity index 91% rename from symcrypt-sys/inc/test.c rename to symcrypt-sys/inc/static_LinuxDefault.c index be7ec8f0..d5f47111 100644 --- a/symcrypt-sys/inc/test.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -8,7 +8,6 @@ #include SYMCRYPT_ENVIRONMENT_POSIX_USERMODE -// add SymCryptINit() for static link PVOID SYMCRYPT_CALL @@ -27,15 +26,13 @@ SymCryptCallbackFree( VOID * pMem ) free( pMem ); } -// from linux docs - +// From Linux docs on getrandom: // RETURN VALUE top // On success, getrandom() returns the number of bytes that were // copied to the buffer buf. This may be less than the number of // bytes requested via buflen if either GRND_RANDOM was specified in // flags and insufficient entropy was present in the random source or // the system call was interrupted by a signal. - // On error, -1 is returned, and errno is set to indicate the error. SYMCRYPT_ERROR SYMCRYPT_CALL @@ -46,25 +43,19 @@ SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) while (total_received < cbBuffer) { result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); - if (result < 0) { if (errno == EINTR) { - // Interrupted by a signal, retry the call + // Buffer is not yet full, continue to get more entropy continue; } //return SYMCRYPT_INTERNAL_ERROR; // Other errors, fail SymCryptFatal( 'vers' ); - } - total_received += (size_t)result; } - return SYMCRYPT_NO_ERROR; } - - VOID SYMCRYPT_CALL SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { diff --git a/symcrypt-sys/inc/static_WindowsDefault.c b/symcrypt-sys/inc/static_WindowsDefault.c new file mode 100644 index 00000000..ff865ae9 --- /dev/null +++ b/symcrypt-sys/inc/static_WindowsDefault.c @@ -0,0 +1,58 @@ +// +// module.c +// Main file for SymCrypt Windows user-mode module, symcrypt.dll +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT license. +// + +#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) + +// Ensure that windows.h doesn't re-define the status_* symbols +#define WIN32_NO_STATUS +#include +#include +#include +#include +#include + +SYMCRYPT_ENVIRONMENT_WINDOWS_USERMODE_LATEST; + +#define SYMCRYPT_FIPS_STATUS_INDICATOR + +PVOID +SYMCRYPT_CALL +SymCryptCallbackAlloc( SIZE_T nBytes ) +{ + return _aligned_malloc( nBytes, SYMCRYPT_ASYM_ALIGN_VALUE ); +} + +VOID +SYMCRYPT_CALL +SymCryptCallbackFree(PVOID ptr) +{ + _aligned_free( ptr ); +} + +VOID +SYMCRYPT_CALL +SymCryptRandom( + _Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer, + SIZE_T cbBuffer ) +{ + NTSTATUS status = BCryptGenRandom( BCRYPT_RNG_ALG_HANDLE, pbBuffer, (ULONG) cbBuffer, 0 ); + if (!NT_SUCCESS(status)) + { + SymCryptFatal(status); + } +} + +SYMCRYPT_ERROR +SYMCRYPT_CALL +SymCryptCallbackRandom( + _Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer, + SIZE_T cbBuffer ) +{ + NTSTATUS status = BCryptGenRandom( BCRYPT_RNG_ALG_HANDLE, pbBuffer, (ULONG) cbBuffer, 0 ); + + return NT_SUCCESS( status ) ? SYMCRYPT_NO_ERROR : SYMCRYPT_EXTERNAL_FAILURE; +} \ No newline at end of file diff --git a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs index c0214865..ea0c4a77 100644 --- a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs @@ -4474,6 +4474,9 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +extern "C" { + pub fn SymCryptInit(); +} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs index b1c9e4df..9bb38bbf 100644 --- a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs @@ -4198,6 +4198,9 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +extern "C" { + pub fn SymCryptInit(); +} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index d115bf07..037d6a13 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -4208,6 +4208,9 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +extern "C" { + pub fn SymCryptInit(); +} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs index 8bda4666..3da8f817 100644 --- a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs @@ -4198,6 +4198,9 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +extern "C" { + pub fn SymCryptInit(); +} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } From 70dc0637e1166d692bd8b2c27fb1ac21645f4d27 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Tue, 11 Feb 2025 17:44:36 -0500 Subject: [PATCH 10/40] Cargo Fmt and adding some comments --- rust-symcrypt/src/lib.rs | 9 +++------ symcrypt-sys/build/main.rs | 6 ++---- symcrypt-sys/build/static_link.rs | 1 - symcrypt-sys/inc/static_LinuxDefault.c | 7 +++++++ symcrypt-sys/inc/static_WindowsDefault.c | 8 +++----- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index d210d402..47dcd385 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -31,9 +31,7 @@ fn symcrypt_init() { #[cfg(not(feature = "dynamic"))] { println!("static mode"); - INIT.call_once(|| { - symcrypt_sys::SymCryptInit() - }); + INIT.call_once(|| symcrypt_sys::SymCryptInit()); } } } @@ -44,7 +42,7 @@ pub fn symcrypt_random(buff: &mut [u8]) { unsafe { // SAFETY: FFI calls - + #[cfg(feature = "dynamic")] // If calling dynamically, we will use SymCryptRandom that is provided by the SymCrypt library. symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); @@ -52,9 +50,8 @@ pub fn symcrypt_random(buff: &mut [u8]) { #[cfg(not(feature = "dynamic"))] symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); - // TODO: Investigate if we should use getrandom here instead of SymCryptRandom - // potentially use rand::get_random() here as to not have a mismatch. Although it is possible to expose a version + // potentially use rand::get_random() here as to not have a mismatch. Although it is possible to expose a version // of SymCryptRandom in static mode, There could be a potential issue with mixing function definitions between // the dynamic and static modes. } diff --git a/symcrypt-sys/build/main.rs b/symcrypt-sys/build/main.rs index a2fc8510..051fb8e0 100644 --- a/symcrypt-sys/build/main.rs +++ b/symcrypt-sys/build/main.rs @@ -21,7 +21,7 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { // Look for the .lib file during link time. We are searching the Windows/System32 path which is set as a current default to match // the long term placement of a Windows shipped symcrypt.dll - // TODO: Update this info + // TODO: Update this info let lib_path = std::env::var("SYMCRYPT_LIB_PATH") .unwrap_or_else(|_| panic!("SYMCRYPT_LIB_PATH environment variable not set, for more information please see: https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt#quick-start-guide")); @@ -46,9 +46,7 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { #[cfg(target_os = "linux")] { - - - //TODO: update this info + //TODO: update this info // Note: Linux support is based off of the Azure Linux distro. // This has been tested on Ubuntu 22.04.03 LTS on WSL and has confirmed working but support for other distros diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 1ce7a456..3f32f942 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -250,7 +250,6 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io let mut module_files = vec![]; - match options.triple() { Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { base_files.push("env_windowsUserModeWin8_1.c"); diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index d5f47111..648c9a33 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -1,3 +1,10 @@ +// +// static_LinuxDefault.c +// Default implementation for Linux static shared object. +// +// Copyright (c) Microsoft Corporation. Licensed under the MIT license. +// + #include #include #include diff --git a/symcrypt-sys/inc/static_WindowsDefault.c b/symcrypt-sys/inc/static_WindowsDefault.c index ff865ae9..f63738b6 100644 --- a/symcrypt-sys/inc/static_WindowsDefault.c +++ b/symcrypt-sys/inc/static_WindowsDefault.c @@ -1,6 +1,6 @@ // -// module.c -// Main file for SymCrypt Windows user-mode module, symcrypt.dll +// static_WindowsDefault.c +// Default implementation for Windows static shared object. // // Copyright (c) Microsoft Corporation. Licensed under the MIT license. // @@ -17,8 +17,6 @@ SYMCRYPT_ENVIRONMENT_WINDOWS_USERMODE_LATEST; -#define SYMCRYPT_FIPS_STATUS_INDICATOR - PVOID SYMCRYPT_CALL SymCryptCallbackAlloc( SIZE_T nBytes ) @@ -55,4 +53,4 @@ SymCryptCallbackRandom( NTSTATUS status = BCryptGenRandom( BCRYPT_RNG_ALG_HANDLE, pbBuffer, (ULONG) cbBuffer, 0 ); return NT_SUCCESS( status ) ? SYMCRYPT_NO_ERROR : SYMCRYPT_EXTERNAL_FAILURE; -} \ No newline at end of file +} From 712b19cb53110951254cae2aa251e0d9844269e5 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 12:45:27 -0500 Subject: [PATCH 11/40] Removing symcryptinit from being exposes to test, updating some documentation and removing jitter from build scripts --- .github/workflows/build.yml | 3 -- rust-symcrypt/src/lib.rs | 38 +++++------------- scripts/generate-all-bindings.ps1 | 1 - symcrypt-bindgen/src/main.rs | 2 +- symcrypt-sys/build/main.rs | 23 +++-------- symcrypt-sys/inc/static_LinuxDefault.c | 49 +++++++++++++++--------- symcrypt-sys/inc/static_WindowsDefault.c | 8 ++++ 7 files changed, 54 insertions(+), 70 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b5b821fd..f24b7fd4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,9 +41,6 @@ jobs: - uses: actions/checkout@v4 with: submodules: true - - - name: Init jitterentropy submodule - run: git -C symcrypt-sys/symcrypt submodule update --init -- 3rdparty/jitterentropy-library - name: Install host target shell: pwsh diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 47dcd385..069fc9cd 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -17,22 +17,13 @@ fn symcrypt_init() { static INIT: Once = Once::new(); unsafe { // SAFETY: FFI calls, blocking from being run again. - #[cfg(feature = "dynamic")] - { - println!("dynamic mode"); - INIT.call_once(|| { - symcrypt_sys::SymCryptModuleInit( - symcrypt_sys::SYMCRYPT_CODE_VERSION_API, - symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, - ) - }); - } - - #[cfg(not(feature = "dynamic"))] - { - println!("static mode"); - INIT.call_once(|| symcrypt_sys::SymCryptInit()); - } + // Under the covers, the default static lib implementation exposes SymCryptModuleInit, but calls SymCryptInit for static linking + INIT.call_once(|| { + symcrypt_sys::SymCryptModuleInit( + symcrypt_sys::SYMCRYPT_CODE_VERSION_API, + symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, + ) + }); } } @@ -41,19 +32,10 @@ pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_init(); unsafe { - // SAFETY: FFI calls - - #[cfg(feature = "dynamic")] - // If calling dynamically, we will use SymCryptRandom that is provided by the SymCrypt library. + // SAFETY: FFI call + // Under the covers, the default static lib implementation calls BCryptGenRandom on windows and sys/getrandom on linux + // but calls SymCryptRandom for dynamic linking symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); - - #[cfg(not(feature = "dynamic"))] - symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); - - // TODO: Investigate if we should use getrandom here instead of SymCryptRandom - // potentially use rand::get_random() here as to not have a mismatch. Although it is possible to expose a version - // of SymCryptRandom in static mode, There could be a potential issue with mixing function definitions between - // the dynamic and static modes. } } diff --git a/scripts/generate-all-bindings.ps1 b/scripts/generate-all-bindings.ps1 index 24d61175..93388865 100644 --- a/scripts/generate-all-bindings.ps1 +++ b/scripts/generate-all-bindings.ps1 @@ -35,7 +35,6 @@ $PSNativeCommandUseErrorActionPreference = $True Push-Location "$PSScriptRoot/.." # Move to the root of the project git submodule update --init -git -C symcrypt-sys/symcrypt/3rdparty/jitterentropy-library submodule update --init python3 "./symcrypt-sys/symcrypt/scripts/version.py" --build-info mv -Force "./symcrypt-sys/symcrypt/inc/buildInfo.h" "./symcrypt-sys/inc/" diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index db77465d..eb054ae7 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -57,7 +57,7 @@ fn main() { // INIT FUNCTIONS .allowlist_function("SymCryptModuleInit") - .allowlist_function("SymCryptInit") + //.allowlist_function("SymCryptInit") .allowlist_var("^(SYMCRYPT_CODE_VERSION.*)$") // HASH FUNCTIONS .allowlist_function("^SymCrypt(?:Sha3_(?:256|384|512)|Sha(?:256|384|512|1)|Md5)(?:Init|Append|Result|StateCopy)?$") diff --git a/symcrypt-sys/build/main.rs b/symcrypt-sys/build/main.rs index 051fb8e0..9e872db5 100644 --- a/symcrypt-sys/build/main.rs +++ b/symcrypt-sys/build/main.rs @@ -18,11 +18,7 @@ fn main() -> std::io::Result<()> { fn link_symcrypt_dynamically() -> std::io::Result<()> { #[cfg(target_os = "windows")] { - // Look for the .lib file during link time. We are searching the Windows/System32 path which is set as a current default to match - // the long term placement of a Windows shipped symcrypt.dll - - // TODO: Update this info - + // Look for the .lib file during link time. We are searching the PATH for symcrypt.dll let lib_path = std::env::var("SYMCRYPT_LIB_PATH") .unwrap_or_else(|_| panic!("SYMCRYPT_LIB_PATH environment variable not set, for more information please see: https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt#quick-start-guide")); println!("cargo:rustc-link-search=native={}", lib_path); @@ -37,27 +33,18 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { // 5. The directories that are listed in the PATH environment variable. // For more info please see: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order - - // For the least invasive usage, we suggest putting the symcrypt.dll inside of same folder as the .exe file. - - // Note: This process is a band-aid. Long-term SymCrypt will be shipped with Windows which will make this process much more - // streamlined. } #[cfg(target_os = "linux")] { - //TODO: update this info - // Note: Linux support is based off of the Azure Linux distro. - // This has been tested on Ubuntu 22.04.03 LTS on WSL and has confirmed working but support for other distros - // aside from Azure Linux is not guaranteed so YMMV. println!("cargo:rustc-link-lib=dylib=symcrypt"); // the "lib" prefix for libsymcrypt is implied on Linux - // You must put the included symcrypt.so files in your usr/lib/x86_64-linux-gnu/ path. - // This is where the Linux ld linker will look for the symcrypt.so files. + // If you are using AL3, you can get the required symcrypt.so via tdnf + // If you are using Ubuntu, you can get the required symcrypt.so via PMC. Please see the quick start guide for more information. - // Note: This process is a band-aid. Long-term, our long term solution is to package manage SymCrypt for a subset of - // Linux distros. + // If you are using a different Linux distro, you will need to configure your distro's LD linker to find the required symcrypt.so files. + // As an example, on Ubuntu you can place your symcrypt.so files in your usr/lib/x86_64-linux-gnu/ path. } Ok(()) diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index 648c9a33..44affc10 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -16,6 +16,14 @@ SYMCRYPT_ENVIRONMENT_POSIX_USERMODE +VOID +SYMCRYPT_CALL +SymCryptModuleInit( + _In_ UINT32 api, + _In_ UINT32 minor) { + SymCryptInit(); +} + PVOID SYMCRYPT_CALL SymCryptCallbackAlloc( SIZE_T nBytes ) @@ -33,14 +41,6 @@ SymCryptCallbackFree( VOID * pMem ) free( pMem ); } -// From Linux docs on getrandom: -// RETURN VALUE top -// On success, getrandom() returns the number of bytes that were -// copied to the buffer buf. This may be less than the number of -// bytes requested via buflen if either GRND_RANDOM was specified in -// flags and insufficient entropy was present in the random source or -// the system call was interrupted by a signal. -// On error, -1 is returned, and errno is set to indicate the error. SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) @@ -55,25 +55,36 @@ SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) // Buffer is not yet full, continue to get more entropy continue; } - //return SYMCRYPT_INTERNAL_ERROR; // Other errors, fail - SymCryptFatal( 'vers' ); + return SYMCRYPT_EXTERNAL_FAILURE } total_received += (size_t)result; } return SYMCRYPT_NO_ERROR; } +// From Linux docs on getrandom: +// RETURN VALUE top +// On success, getrandom() returns the number of bytes that were +// copied to the buffer buf. This may be less than the number of +// bytes requested via buflen if either GRND_RANDOM was specified in +// flags and insufficient entropy was present in the random source or +// the system call was interrupted by a signal. +// On error, -1 is returned, and errno is set to indicate the error. VOID SYMCRYPT_CALL SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { - SIZE_T result; - result = getrandom( pbRandom, cbRandom, 0 ); - if (result != cbRandom ) - { - // If the entropy pool has been initialized and the request size is small - // (buflen <= 256), then getrandom() will not fail with EINTR, - // but we check anyway as it's not safe to continue if we don't - // receive the right amount of entropy. - SymCryptFatal( 'rngs' ); + size_t total_received = 0; + ssize_t result; + + while (total_received < cbBuffer) { + result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); + if (result < 0) { + if (errno == EINTR) { + // Buffer is not yet full, continue to get more entropy + continue; + } + SymCryptFatal("rngs"); + } + total_received += (size_t)result; } } diff --git a/symcrypt-sys/inc/static_WindowsDefault.c b/symcrypt-sys/inc/static_WindowsDefault.c index f63738b6..c898e40b 100644 --- a/symcrypt-sys/inc/static_WindowsDefault.c +++ b/symcrypt-sys/inc/static_WindowsDefault.c @@ -17,6 +17,14 @@ SYMCRYPT_ENVIRONMENT_WINDOWS_USERMODE_LATEST; +VOID +SYMCRYPT_CALL +SymCryptModuleInit( + _In_ UINT32 api, + _In_ UINT32 minor) { + SymCryptInit(); +} + PVOID SYMCRYPT_CALL SymCryptCallbackAlloc( SIZE_T nBytes ) From 492d48592301b7bb00216877e54f235ce6475610 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 12:54:22 -0500 Subject: [PATCH 12/40] Updating bindings to not expose SymCryptInit() --- symcrypt-bindgen/src/main.rs | 1 - symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs | 3 --- symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs | 3 --- symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs | 3 --- symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs | 3 --- 5 files changed, 13 deletions(-) diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index eb054ae7..c68fb6cb 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -57,7 +57,6 @@ fn main() { // INIT FUNCTIONS .allowlist_function("SymCryptModuleInit") - //.allowlist_function("SymCryptInit") .allowlist_var("^(SYMCRYPT_CODE_VERSION.*)$") // HASH FUNCTIONS .allowlist_function("^SymCrypt(?:Sha3_(?:256|384|512)|Sha(?:256|384|512|1)|Md5)(?:Init|Append|Result|StateCopy)?$") diff --git a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs index ea0c4a77..c0214865 100644 --- a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs @@ -4474,9 +4474,6 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } -extern "C" { - pub fn SymCryptInit(); -} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs index 9bb38bbf..b1c9e4df 100644 --- a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs @@ -4198,9 +4198,6 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } -extern "C" { - pub fn SymCryptInit(); -} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index 037d6a13..d115bf07 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -4208,9 +4208,6 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } -extern "C" { - pub fn SymCryptInit(); -} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } diff --git a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs index 3da8f817..8bda4666 100644 --- a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs @@ -4198,9 +4198,6 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } -extern "C" { - pub fn SymCryptInit(); -} extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } From 013c5af8c0042a806ad5375616fc826b17ca1014 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 13:00:15 -0500 Subject: [PATCH 13/40] cargo fmt --- symcrypt-sys/build/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/symcrypt-sys/build/main.rs b/symcrypt-sys/build/main.rs index 9e872db5..66acc7d9 100644 --- a/symcrypt-sys/build/main.rs +++ b/symcrypt-sys/build/main.rs @@ -37,10 +37,9 @@ fn link_symcrypt_dynamically() -> std::io::Result<()> { #[cfg(target_os = "linux")] { - println!("cargo:rustc-link-lib=dylib=symcrypt"); // the "lib" prefix for libsymcrypt is implied on Linux - // If you are using AL3, you can get the required symcrypt.so via tdnf + // If you are using AL3, you can get the required symcrypt.so via tdnf // If you are using Ubuntu, you can get the required symcrypt.so via PMC. Please see the quick start guide for more information. // If you are using a different Linux distro, you will need to configure your distro's LD linker to find the required symcrypt.so files. From 3f7521c4fcc20ed51cd55e8e0277df8fa4f75276 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 13:20:12 -0500 Subject: [PATCH 14/40] Fixing linux build breaks --- symcrypt-sys/inc/static_LinuxDefault.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index 44affc10..12e1ac22 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -14,7 +14,7 @@ #include #include -SYMCRYPT_ENVIRONMENT_POSIX_USERMODE +SYMCRYPT_ENVIRONMENT_POSIX_USERMODE; VOID SYMCRYPT_CALL @@ -55,7 +55,7 @@ SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) // Buffer is not yet full, continue to get more entropy continue; } - return SYMCRYPT_EXTERNAL_FAILURE + return SYMCRYPT_EXTERNAL_FAILURE; } total_received += (size_t)result; } @@ -76,14 +76,14 @@ SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { size_t total_received = 0; ssize_t result; - while (total_received < cbBuffer) { - result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); + while (total_received < cbRandom) { + result = getrandom(pbRandom + total_received, cbRandom - total_received, 0); if (result < 0) { if (errno == EINTR) { // Buffer is not yet full, continue to get more entropy continue; } - SymCryptFatal("rngs"); + SymCryptFatal(' rngs '); } total_received += (size_t)result; } From 121697925c91b2d0c4ffcb0f6ce05a79dd1e464f Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 16:06:16 -0500 Subject: [PATCH 15/40] remove comment --- symcrypt-sys/build/static_link.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 3f32f942..6225f335 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -254,7 +254,6 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { base_files.push("env_windowsUserModeWin8_1.c"); base_files.push("IEEE802_11SaeCustom.c"); - //module_files.push("symcrypt/modules/windows/user/module.c"); module_files.push("inc/static_WindowsDefault.c"); } Triple::x86_64_unknown_linux_gnu => { From 5d79db1df2b5dbb1f91f0aa6c2bb6a70443e1e0c Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 17:59:15 -0500 Subject: [PATCH 16/40] Adding comments, and making minor improvements to the build system --- symcrypt-sys/build/static_link.rs | 140 ++++++++++++++++------- symcrypt-sys/build/triple.rs | 3 + symcrypt-sys/inc/static_LinuxDefault.c | 3 + symcrypt-sys/inc/static_WindowsDefault.c | 3 + 4 files changed, 106 insertions(+), 43 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 6225f335..671bef48 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -1,10 +1,17 @@ use super::triple::Triple; +use std::collections::HashSet; const LIB_NAME: &str = "symcrypt"; +/// Compiles and links the SymCrypt library statically. +/// This is the entery point for building SymCrypt statically. +/// +/// - Determines the build configuration based on target architecture. +/// - Calls `compile_symcrypt_static()` to actually build SymCrypt. +/// - Outputs necessary metadata for Cargo (`cargo:rustc-link-lib=...`). +/// +/// Based on SymCrypt's `CMakeLists.txt`, but adapted for Rust. pub fn compile_and_link_symcrypt() -> std::io::Result<()> { - // based on SymCrypt/lib/CMakeLists.txt - let options = SymCryptOptions::new(); println!("Build config: {:?}", options); @@ -16,9 +23,11 @@ pub fn compile_and_link_symcrypt() -> std::io::Result<()> { println!("cargo:rerun-if-changed=upstream"); println!("Compiling SymCrypt..."); + // Compile and Build SymCrypt with provided SymCryptOptions compile_symcrypt_static(LIB_NAME, &options)?; println!("cargo:rustc-link-lib=static={LIB_NAME}"); + // Link additional dependencies for dep in ADDITIONAL_DEPENDENCIES { println!("cargo:rustc-link-lib=dylib={dep}"); } @@ -26,18 +35,27 @@ pub fn compile_and_link_symcrypt() -> std::io::Result<()> { Ok(()) } +// TODO: update -symcrypt_fips_build comment + +/// Holds configuration options for compiling SymCrypt. +/// +/// - `triple`: The target triple +/// - `symcrypt_use_asm`: Whether to enable assembly optimizations. +/// - `symcrypt_fips_build`: Whether to build in FIPS mode PLACEHOLDER +/// - `preconfiged_cc`: Returns a Pre-configured `cc` object for the target triple. #[derive(Debug)] struct SymCryptOptions { triple: Triple, symcrypt_use_asm: bool, - //symcrypt_fips_build: bool, + //symcrypt_fips_build: bool, // TODO: Determine if we should expose FIPS build option? } + impl SymCryptOptions { fn new() -> Self { Self { triple: Triple::get_target_triple(), - symcrypt_use_asm: false, - //symcrypt_fips_build: false, + symcrypt_use_asm: false, // FIXME: Turn this to true when we get ASM checked in + //symcrypt_fips_build: false, // TODO: Determine if we should expose FIPS build option? } } fn use_asm(&self) -> bool { @@ -47,42 +65,44 @@ impl SymCryptOptions { self.triple.clone() } + // Returns a cc object that has been preconfigured for the target triple fn preconfigure_cc(&self) -> cc::Build { let mut cc = cc::Build::new(); cc.target(self.triple.to_triple()) .include("inc") .include("symcrypt/inc") .include("symcrypt/lib") - .warnings(false); + .warnings(false); // Ignore noisy warnings from SymCrypt if !self.symcrypt_use_asm { cc.define("SYMCRYPT_IGNORE_PLATFORM", None); } + // Set specific flags for each target match self.triple { Triple::x86_64_pc_windows_msvc => { - cc.asm_flag("/DSYMCRYPT_MASM"); + cc.asm_flag("/DSYMCRYPT_MASM"); // TODO: figure out what these flags do } Triple::aarch64_pc_windows_msvc => { cc.define("_ARM64_", None); } Triple::x86_64_unknown_linux_gnu => { - cc.include("symcrypt/modules/linux/common"); + cc.include("symcrypt/modules/linux/common"); // TODO: remove? cc.flag("-mpclmul"); - cc.flag("-Wno-incompatible-pointer-types"); // Should we create parent Enum for Windows / Linux? - /* - cc.flag("-mpclmul") - .flag("-mssse3") - .flag("-mxsave") - .flag("-maes") - .flag("-msha") - .flag("-mrdrnd") - .flag("-mrdseed"); - */ + cc.flag("-Wno-incompatible-pointer-types"); + /* + cc.flag("-mpclmul") + .flag("-mssse3") + .flag("-mxsave") + .flag("-maes") + .flag("-msha") + .flag("-mrdrnd") + .flag("-mrdseed"); + */ } Triple::aarch64_unknown_linux_gnu => { - cc.include("symcrypt/modules/linux/common"); - cc.flag("-Wno-incompatible-pointer-types"); + cc.include("symcrypt/modules/linux/common"); // TODO: remove? + cc.flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors } } @@ -225,29 +245,25 @@ xmss.c xtsaes.c "; -// only for x86_64_unknown_linux_gnu -const SPECIAL_FLAGS: &str = r#" -set_source_files_properties(aes-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mvaes;-mvpclmulqdq") -set_source_files_properties(sha256Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") -set_source_files_properties(sha512Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") -set_source_files_properties(sha256-xmm.c PROPERTIES COMPILE_OPTIONS "-mssse3") -set_source_files_properties(sha256-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") -set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") -"#; - fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io::Result<()> { - let (already_compiled_files, intermediates) = compile_intermediates(&options); + // Compile intermediates required this is currently only required for x86_64_unknown_linux_gnu + let (already_compiled_files, intermediates) = compile_symcrypt_intermediates(&options); + // Convert already compiled files to a HashSet for faster lookups + let already_compiled_set: HashSet<&str> = already_compiled_files.iter().cloned().collect(); + + // Prepares list of files to be compiled, excluding already compiled files for x86_64_unknown_linux_gnu let mut base_files: Vec<&'static str> = CMAKE_SOURCES_COMMON .lines() + .map(str::trim) // Trim once instead of inside filter .filter(|line| { - let line = line.trim(); - !(line.is_empty() || line.starts_with("#") || already_compiled_files.contains(&line)) + !line.is_empty() && !line.starts_with("#") && !already_compiled_set.contains(line) }) .collect(); base_files.push("env_generic.c"); // symcrypt_generic + // Add module-specific files for each target let mut module_files = vec![]; match options.triple() { @@ -257,7 +273,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io module_files.push("inc/static_WindowsDefault.c"); } Triple::x86_64_unknown_linux_gnu => { - base_files.push("linux/intrinsics.c"); + base_files.push("linux/intrinsics.c"); // TODO: Confirm that its Only required for Linux x86_64 base_files.push("env_posixUserMode.c"); module_files.push("inc/static_LinuxDefault.c"); } @@ -267,6 +283,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io } } + // Add assembly pre generated ASM files to be compiled let asm_files = match options.triple() { Triple::x86_64_pc_windows_msvc => vec![ "aesasm.asm", @@ -296,50 +313,82 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io } }; + // Pre-Configure the cc compiler based on the target triple let mut cc = options.preconfigure_cc(); + + // Add in the intermediates that were previously compiled, will be empty for most targets cc.objects(intermediates); + // Add base files to be compiled for file in base_files { cc.file(format!("{SOURCE_DIR}/{file}")); } + // Add assembly files to be compiled if options.use_asm() { for file in asm_files { cc.file(format!( - "{SOURCE_DIR}/asm/{}/{file}", + "{SOURCE_DIR}/asm/{}/{file}", // TODO: replace with right file path when ASM checked in. options.triple.to_triple() )); } } + + // Add module-specific files to be compiled cc.files(module_files); println!("Files to compile: {}", cc.get_files().count()); + + // Compiles all files and returns the compiled library cc.compile(lib_name); Ok(()) } -fn compile_intermediates( +// Special compile files for x86_64_unknown_linux_gnu +const X86_64_LINUX_CUSTOM_COMPILE_FILES: &str = r#" +set_source_files_properties(aes-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mvaes;-mvpclmulqdq") +set_source_files_properties(sha256Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") +set_source_files_properties(sha512Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") +set_source_files_properties(sha256-xmm.c PROPERTIES COMPILE_OPTIONS "-mssse3") +set_source_files_properties(sha256-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") +set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") +"#; + +/// Compiles the SymCrypt custom intermediates +/// +/// Currently this is only required for x86_64_unknown_linux_gnu, +/// but can be modified to include other targets as needed. +/// +/// If the target is not `x86_64_unknown_linux_gnu`, it returns empty vectors +fn compile_symcrypt_intermediates( symcrypt_options: &SymCryptOptions, ) -> (Vec<&'static str>, Vec) { let mut files = vec![]; let mut intermediates = vec![]; + // Only compile intermediates for x86_64_unknown_linux_gnu. + // Can modify with additional targets as needed. if symcrypt_options.triple() != Triple::x86_64_unknown_linux_gnu { - return (files, intermediates); + return (files, intermediates); // No intermediates to compile } - for line in SPECIAL_FLAGS.lines() { + // Fetch preconfigured cc based on the target triple. + let mut cc = symcrypt_options.preconfigure_cc(); + + for line in X86_64_LINUX_CUSTOM_COMPILE_FILES.lines() { if line.trim().is_empty() || line.trim().starts_with("#") { continue; } let line = line .strip_prefix("set_source_files_properties(") - .unwrap() + .expect("Malformed input: missing prefix") .strip_suffix(")") - .unwrap(); + .expect("Malformed input: missing suffix"); + // Example of parts: + // [aes-ymm.c, PROPERTIES, COMPILE_OPTIONS, "-mavx;-mavx2;-mvaes;-mvpclmulqdq"] let parts: Vec<&str> = line.split_whitespace().collect(); if parts.len() < 4 { continue; @@ -348,21 +397,26 @@ fn compile_intermediates( let file = parts[0]; println!("Compiling {file} with custom options: {}", parts[3]); + // Isolate the compile options let options = parts[3] .trim_matches('"') .split(';') .filter(|s| !s.is_empty()); - let mut cc = symcrypt_options.preconfigure_cc(); + // Push intermediates to the cc object to be compiled cc.file(format!("{SOURCE_DIR}/{file}")); for option in options { cc.flag(option); } - let mut result = cc.compile_intermediates(); + // Add the file to the list of files to be replaced by compiled intermediates. files.push(file); - intermediates.append(&mut result); } + // Use cc's compile_intermediates() to batch generate intermediate files without linking + let mut result = cc.compile_intermediates(); + intermediates.append(&mut result); + + // Return files to be replaced by intermediates. (files, intermediates) } diff --git a/symcrypt-sys/build/triple.rs b/symcrypt-sys/build/triple.rs index 689ae4c3..590a45a3 100644 --- a/symcrypt-sys/build/triple.rs +++ b/symcrypt-sys/build/triple.rs @@ -1,5 +1,7 @@ #[allow(non_camel_case_types)] #[derive(Debug, PartialEq, Eq, Clone)] + +/// The `Triple` enum represents the target architecture and operating system. pub enum Triple { x86_64_pc_windows_msvc, aarch64_pc_windows_msvc, @@ -9,6 +11,7 @@ pub enum Triple { impl Triple { pub fn get_target_triple() -> Self { + // Extract target OS and architecture from environment variables let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index 12e1ac22..d8dec495 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -16,6 +16,9 @@ SYMCRYPT_ENVIRONMENT_POSIX_USERMODE; +// Exposing SymCryptModuleInit and under the covers calling SymCryptInit. +// Doing this allows us to use less #[cfg] attributes in the Rust code, and does not expose +// SymCryptInit to the public symcrypt-sys API. VOID SYMCRYPT_CALL SymCryptModuleInit( diff --git a/symcrypt-sys/inc/static_WindowsDefault.c b/symcrypt-sys/inc/static_WindowsDefault.c index c898e40b..b531da9c 100644 --- a/symcrypt-sys/inc/static_WindowsDefault.c +++ b/symcrypt-sys/inc/static_WindowsDefault.c @@ -17,6 +17,9 @@ SYMCRYPT_ENVIRONMENT_WINDOWS_USERMODE_LATEST; +// Exposing SymCryptModuleInit and under the covers calling SymCryptInit. +// Doing this allows us to use less #[cfg] attributes in the Rust code, and does not expose +// SymCryptInit to the public symcrypt-sys API. VOID SYMCRYPT_CALL SymCryptModuleInit( From 623a6600c6d9ccdab27e390b860dcbee16092bbf Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 18:00:55 -0500 Subject: [PATCH 17/40] Fixing spacing --- symcrypt-sys/inc/static_LinuxDefault.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index d8dec495..36b65aef 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -86,7 +86,7 @@ SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { // Buffer is not yet full, continue to get more entropy continue; } - SymCryptFatal(' rngs '); + SymCryptFatal( 'rngs' ); } total_received += (size_t)result; } From fdcc63865ebcd5c8a6ce2f08f6c0f285de757591 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 12 Feb 2025 18:15:49 -0500 Subject: [PATCH 18/40] cargo fmt --- symcrypt-sys/build/static_link.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 671bef48..58b23b6f 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -315,7 +315,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io // Pre-Configure the cc compiler based on the target triple let mut cc = options.preconfigure_cc(); - + // Add in the intermediates that were previously compiled, will be empty for most targets cc.objects(intermediates); @@ -355,11 +355,11 @@ set_source_files_properties(sha256-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") "#; -/// Compiles the SymCrypt custom intermediates -/// +/// Compiles the SymCrypt custom intermediates +/// /// Currently this is only required for x86_64_unknown_linux_gnu, /// but can be modified to include other targets as needed. -/// +/// /// If the target is not `x86_64_unknown_linux_gnu`, it returns empty vectors fn compile_symcrypt_intermediates( symcrypt_options: &SymCryptOptions, From 94808cd5af2494062b02574ec19d3a12f7ed068e Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 14:44:38 -0500 Subject: [PATCH 19/40] Adding missing flags, updating some comments --- symcrypt-sys/build/static_link.rs | 44 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 58b23b6f..300a9ffd 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -75,33 +75,47 @@ impl SymCryptOptions { .warnings(false); // Ignore noisy warnings from SymCrypt if !self.symcrypt_use_asm { - cc.define("SYMCRYPT_IGNORE_PLATFORM", None); + cc.define("SYMCRYPT_IGNORE_PLATFORM", None); // TODO: Fix when we get ASM } + // Set min C version to C11. + cc.flag("-std=c11"); + // Set specific flags for each target match self.triple { Triple::x86_64_pc_windows_msvc => { - cc.asm_flag("/DSYMCRYPT_MASM"); // TODO: figure out what these flags do + cc.define("_AMD64_", None); + // cc.asm_flag("/DSYMCRYPT_MASM"); // MASM assembly TODO: enable for ASM + cc.flag("/MP"); // Multi-threaded compilation + cc.flag("/Zp8"); // Structure packing alignment + cc.flag("/WX"); // Treat warnings as errors + cc.flag("/guard:cf"); // Control Flow Guard + cc.flag("/dynamicbase"); // Enable ASLR + cc.flag("/EHsc"); // Exception handling } Triple::aarch64_pc_windows_msvc => { cc.define("_ARM64_", None); } Triple::x86_64_unknown_linux_gnu => { - cc.include("symcrypt/modules/linux/common"); // TODO: remove? cc.flag("-mpclmul"); - cc.flag("-Wno-incompatible-pointer-types"); - /* - cc.flag("-mpclmul") - .flag("-mssse3") - .flag("-mxsave") - .flag("-maes") - .flag("-msha") - .flag("-mrdrnd") - .flag("-mrdseed"); - */ + cc.flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors + // From SymCrypt-Platforms.cmake + cc.flag("-mssse3"); + cc.flag("-mxsave"); + cc.flag("-maes"); + cc.flag("-msha"); + cc.flag("-mrdrnd"); + cc.flag("-mrdseed"); + cc.flag("-mbmi2"); + cc.flag("-fstack-protector-strong"); + cc.flag("-Wstack-protector"); + cc.flag("-fno-plt"); + cc.flag("-fno-builtin-bcmp"); + cc.flag("-fno-unroll-loops"); } Triple::aarch64_unknown_linux_gnu => { - cc.include("symcrypt/modules/linux/common"); // TODO: remove? + cc.flag("-march=armv8-a+simd+crypto"); // Enable a baseline of features for the compiler to support everywhere. + cc.flag("-flax-vector-conversions"); // Setting -flax-vector-conversions to build Arm64 intrinsics code with GCC. cc.flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors } } @@ -273,7 +287,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io module_files.push("inc/static_WindowsDefault.c"); } Triple::x86_64_unknown_linux_gnu => { - base_files.push("linux/intrinsics.c"); // TODO: Confirm that its Only required for Linux x86_64 + base_files.push("linux/intrinsics.c"); // Only needed for x86_64_unknown_linux_gnu base_files.push("env_posixUserMode.c"); module_files.push("inc/static_LinuxDefault.c"); } From 8a0f14272198bd4cafff95af37696fe5090f2b4f Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 15:01:59 -0500 Subject: [PATCH 20/40] fix linux build breaks --- symcrypt-sys/build/static_link.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 300a9ffd..1cf30c76 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -78,9 +78,15 @@ impl SymCryptOptions { cc.define("SYMCRYPT_IGNORE_PLATFORM", None); // TODO: Fix when we get ASM } - // Set min C version to C11. - cc.flag("-std=c11"); - + // Set C11 standard for Windows and GNU targets. + match self.triple { + Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { + cc.flag("-std=c11"); + } + Triple::aarch64_unknown_linux_gnu | Triple::x86_64_unknown_linux_gnu => { + cc.flag("-std=gnu11"); + } + } // Set specific flags for each target match self.triple { Triple::x86_64_pc_windows_msvc => { From 3f7afcbf059315cd23770a3ed5322db5559d25a6 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 15:05:14 -0500 Subject: [PATCH 21/40] removing c11 check --- symcrypt-sys/build/static_link.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 1cf30c76..57900e19 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -77,16 +77,7 @@ impl SymCryptOptions { if !self.symcrypt_use_asm { cc.define("SYMCRYPT_IGNORE_PLATFORM", None); // TODO: Fix when we get ASM } - - // Set C11 standard for Windows and GNU targets. - match self.triple { - Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { - cc.flag("-std=c11"); - } - Triple::aarch64_unknown_linux_gnu | Triple::x86_64_unknown_linux_gnu => { - cc.flag("-std=gnu11"); - } - } + // Set specific flags for each target match self.triple { Triple::x86_64_pc_windows_msvc => { From 159bb4cc98b4aec3321c19280e7ac030fa324f54 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 15:48:28 -0500 Subject: [PATCH 22/40] cargo fmt --- symcrypt-sys/build/static_link.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 57900e19..6f85f5f3 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -77,7 +77,7 @@ impl SymCryptOptions { if !self.symcrypt_use_asm { cc.define("SYMCRYPT_IGNORE_PLATFORM", None); // TODO: Fix when we get ASM } - + // Set specific flags for each target match self.triple { Triple::x86_64_pc_windows_msvc => { From 8d5c9a2e9b80daa3d6953ac70a5ae11d40a7a77b Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 18:41:52 -0500 Subject: [PATCH 23/40] Update to new bindings, add regex to conditionally compile init and random, update default static files --- rust-symcrypt/src/lib.rs | 29 +++++++++++-- symcrypt-bindgen/src/main.rs | 52 ++++++++++++++++++++++++ symcrypt-sys/build/static_link.rs | 4 +- symcrypt-sys/inc/static_LinuxDefault.c | 44 ++++---------------- symcrypt-sys/inc/static_WindowsDefault.c | 24 ----------- 5 files changed, 87 insertions(+), 66 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 069fc9cd..92146355 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -15,27 +15,50 @@ pub mod rsa; fn symcrypt_init() { // Subsequent calls to `symcrypt_init()` after the first will not be invoked per .call_once docs https://doc.rust-lang.org/std/sync/struct.Once.html static INIT: Once = Once::new(); + + // `symcrypt_init` calls `SymCryptModuleInit` or `SymCryptInit` depending on the feature flag + // We have also set feature flags on the bindings themselves to only expose the functions we need. + // This is to try and eliminate footguns like calling SymCryptModuleInit on a statically linked module. unsafe { // SAFETY: FFI calls, blocking from being run again. - // Under the covers, the default static lib implementation exposes SymCryptModuleInit, but calls SymCryptInit for static linking + + #[cfg(feature = "dynamic")] INIT.call_once(|| { symcrypt_sys::SymCryptModuleInit( symcrypt_sys::SYMCRYPT_CODE_VERSION_API, symcrypt_sys::SYMCRYPT_CODE_VERSION_MINOR, ) }); + + #[cfg(not(feature = "dynamic"))] + INIT.call_once(|| { + symcrypt_sys::SymCryptInit(); + }); + // call symcrypt Init, only expose this for static linking. Add a regex to add a cfg feature to the bindings for this. } } /// Takes in a a buffer called `buff` and fills it with random bytes. This function cannot fail. +/// +/// If calling `symcrypt_random` with a dynamically linked module, `SymCryptRandom` will be called. +/// +/// If calling `symcrypt_random` with a statically linked module, `SymCryptCallbackRandom` will be called. pub fn symcrypt_random(buff: &mut [u8]) { symcrypt_init(); + // `symcrypt_random` calls `SymCryptRandom` or `SymCryptCallbackRandom` depending on the feature flag + // We have also set feature flags on the bindings themselves to only expose the functions we need. + // This is to try and eliminate footguns like calling SymCryptRandom on a statically linked module. unsafe { // SAFETY: FFI call - // Under the covers, the default static lib implementation calls BCryptGenRandom on windows and sys/getrandom on linux - // but calls SymCryptRandom for dynamic linking + + // Call SymCryptRandom for dynamic linking + #[cfg(feature = "dynamic")] symcrypt_sys::SymCryptRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); + + // Call SymCryptCallbackRandom for static linking + #[cfg(not(feature = "dynamic"))] + symcrypt_sys::SymCryptCallbackRandom(buff.as_mut_ptr(), buff.len() as symcrypt_sys::SIZE_T); } } diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index c68fb6cb..6eb746b9 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -57,6 +57,7 @@ fn main() { // INIT FUNCTIONS .allowlist_function("SymCryptModuleInit") + .allowlist_function("SymCryptInit") .allowlist_var("^(SYMCRYPT_CODE_VERSION.*)$") // HASH FUNCTIONS .allowlist_function("^SymCrypt(?:Sha3_(?:256|384|512)|Sha(?:256|384|512|1)|Md5)(?:Init|Append|Result|StateCopy)?$") @@ -106,6 +107,7 @@ fn main() { // UTILITY FUNCTIONS .allowlist_function("SymCryptWipe") .allowlist_function("SymCryptRandom") + .allowlist_function("SymCryptCallbackRandom") .allowlist_function("SymCryptLoadMsbFirstUint64") .allowlist_function("SymCryptStoreMsbFirstUint64") @@ -118,6 +120,10 @@ fn main() { .write_to_file(&bindings_file) .expect("Couldn't write bindings!"); + // For dynamic linking, we expose SymCryptModuleInit, for static linking, we expose SymCryptInit. + fix_symcrypt_bindings(&bindings_file); + + // For dynamic linking, we need to add a link attribute to the bindings. fix_bindings_for_windows(triple, &bindings_file); } @@ -179,3 +185,49 @@ fn fix_bindings_for_windows(triple: &str, bindings_file: &str) { .expect("Unable to write bindings file"); } } + +#[allow(clippy::collapsible_if)] +fn fix_symcrypt_bindings(bindings_file: &str) { + println!("Fixing bindings to expose SymCryptInit or SymCryptModuleInit and SymCryptRandom"); + + let bindings_content = + std::fs::read_to_string(bindings_file).expect("Unable to read bindings file"); + + let mut out_content = Vec::new(); + let lines: Vec<&str> = bindings_content.lines().collect(); + let mut i = 0; + + // With Dynamic, we want to expose SymCryptModuleInit and SymCryptRandom + // With Static, we want to expose SymCryptInit and SymCryptCallbackRandom + while i < lines.len() { + if lines[i].trim() == "extern \"C\" {" { + if i + 1 < lines.len() { + let next_line = lines[i + 1].trim(); + + let cfg_attr = match next_line { + line if line == "pub fn SymCryptInit();" + || line.starts_with("pub fn SymCryptCallbackRandom(") => + { + "#[cfg(not(feature = \"dynamic\"))]" + } + line if line.starts_with("pub fn SymCryptModuleInit(") + || line.starts_with("pub fn SymCryptRandom(") => + { + "#[cfg(feature = \"dynamic\")]" + } + _ => "", + }; + + if !cfg_attr.is_empty() { + out_content.push(cfg_attr.to_string()); + } + } + } + + out_content.push(lines[i].to_string()); + i += 1; + } + + // Write the modified content back + std::fs::write(bindings_file, out_content.join("\n")).expect("Unable to write bindings file"); +} diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 6f85f5f3..4867f146 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -87,7 +87,7 @@ impl SymCryptOptions { cc.flag("/Zp8"); // Structure packing alignment cc.flag("/WX"); // Treat warnings as errors cc.flag("/guard:cf"); // Control Flow Guard - cc.flag("/dynamicbase"); // Enable ASLR + // cc.flag("/dynamicbase"); // Enable ASLR cc.flag("/EHsc"); // Exception handling } Triple::aarch64_pc_windows_msvc => { @@ -258,7 +258,7 @@ xtsaes.c fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io::Result<()> { // Compile intermediates required this is currently only required for x86_64_unknown_linux_gnu - let (already_compiled_files, intermediates) = compile_symcrypt_intermediates(&options); + let (already_compiled_files, intermediates) = compile_symcrypt_intermediates(options); // Convert already compiled files to a HashSet for faster lookups let already_compiled_set: HashSet<&str> = already_compiled_files.iter().cloned().collect(); diff --git a/symcrypt-sys/inc/static_LinuxDefault.c b/symcrypt-sys/inc/static_LinuxDefault.c index 36b65aef..7f0a9bad 100644 --- a/symcrypt-sys/inc/static_LinuxDefault.c +++ b/symcrypt-sys/inc/static_LinuxDefault.c @@ -16,17 +16,6 @@ SYMCRYPT_ENVIRONMENT_POSIX_USERMODE; -// Exposing SymCryptModuleInit and under the covers calling SymCryptInit. -// Doing this allows us to use less #[cfg] attributes in the Rust code, and does not expose -// SymCryptInit to the public symcrypt-sys API. -VOID -SYMCRYPT_CALL -SymCryptModuleInit( - _In_ UINT32 api, - _In_ UINT32 minor) { - SymCryptInit(); -} - PVOID SYMCRYPT_CALL SymCryptCallbackAlloc( SIZE_T nBytes ) @@ -44,27 +33,6 @@ SymCryptCallbackFree( VOID * pMem ) free( pMem ); } -SYMCRYPT_ERROR -SYMCRYPT_CALL -SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) -{ - size_t total_received = 0; - ssize_t result; - - while (total_received < cbBuffer) { - result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); - if (result < 0) { - if (errno == EINTR) { - // Buffer is not yet full, continue to get more entropy - continue; - } - return SYMCRYPT_EXTERNAL_FAILURE; - } - total_received += (size_t)result; - } - return SYMCRYPT_NO_ERROR; -} - // From Linux docs on getrandom: // RETURN VALUE top // On success, getrandom() returns the number of bytes that were @@ -73,21 +41,23 @@ SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) // flags and insufficient entropy was present in the random source or // the system call was interrupted by a signal. // On error, -1 is returned, and errno is set to indicate the error. -VOID +SYMCRYPT_ERROR SYMCRYPT_CALL -SymCryptRandom( PBYTE pbRandom, SIZE_T cbRandom) { +SymCryptCallbackRandom(unsigned char *pbBuffer, size_t cbBuffer) +{ size_t total_received = 0; ssize_t result; - while (total_received < cbRandom) { - result = getrandom(pbRandom + total_received, cbRandom - total_received, 0); + while (total_received < cbBuffer) { + result = getrandom(pbBuffer + total_received, cbBuffer - total_received, 0); if (result < 0) { if (errno == EINTR) { // Buffer is not yet full, continue to get more entropy continue; } - SymCryptFatal( 'rngs' ); + return SYMCRYPT_EXTERNAL_FAILURE; } total_received += (size_t)result; } + return SYMCRYPT_NO_ERROR; } diff --git a/symcrypt-sys/inc/static_WindowsDefault.c b/symcrypt-sys/inc/static_WindowsDefault.c index b531da9c..6d9e618c 100644 --- a/symcrypt-sys/inc/static_WindowsDefault.c +++ b/symcrypt-sys/inc/static_WindowsDefault.c @@ -17,17 +17,6 @@ SYMCRYPT_ENVIRONMENT_WINDOWS_USERMODE_LATEST; -// Exposing SymCryptModuleInit and under the covers calling SymCryptInit. -// Doing this allows us to use less #[cfg] attributes in the Rust code, and does not expose -// SymCryptInit to the public symcrypt-sys API. -VOID -SYMCRYPT_CALL -SymCryptModuleInit( - _In_ UINT32 api, - _In_ UINT32 minor) { - SymCryptInit(); -} - PVOID SYMCRYPT_CALL SymCryptCallbackAlloc( SIZE_T nBytes ) @@ -42,19 +31,6 @@ SymCryptCallbackFree(PVOID ptr) _aligned_free( ptr ); } -VOID -SYMCRYPT_CALL -SymCryptRandom( - _Out_writes_bytes_( cbBuffer ) PBYTE pbBuffer, - SIZE_T cbBuffer ) -{ - NTSTATUS status = BCryptGenRandom( BCRYPT_RNG_ALG_HANDLE, pbBuffer, (ULONG) cbBuffer, 0 ); - if (!NT_SUCCESS(status)) - { - SymCryptFatal(status); - } -} - SYMCRYPT_ERROR SYMCRYPT_CALL SymCryptCallbackRandom( From 036df15f93b72559894183b554835a323d3adbad Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 18:47:02 -0500 Subject: [PATCH 24/40] Adding bindings from the github artifacts --- symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs | 10 ++++++++++ .../src/bindings/aarch64_unknown_linux_gnu.rs | 12 +++++++++++- symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs | 10 ++++++++++ .../src/bindings/x86_64_unknown_linux_gnu.rs | 12 +++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs index c0214865..4e013c7e 100644 --- a/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/aarch64_pc_windows_msvc.rs @@ -4474,6 +4474,11 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptInit(); +} +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } @@ -5078,9 +5083,14 @@ extern "C" { extern "C" { pub fn SymCryptHkdfSelfTest(); } +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptRandom(pbRandom: PBYTE, cbRandom: SIZE_T); } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptCallbackRandom(pbBuffer: PBYTE, cbBuffer: SIZE_T) -> SYMCRYPT_ERROR; +} pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_LSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 1; pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_MSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 2; pub type _SYMCRYPT_NUMBER_FORMAT = ::std::os::raw::c_int; diff --git a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs index b1c9e4df..211eb2c1 100644 --- a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs @@ -4198,6 +4198,11 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptInit(); +} +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } @@ -4788,9 +4793,14 @@ extern "C" { extern "C" { pub fn SymCryptHkdfSelfTest(); } +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptRandom(pbRandom: PBYTE, cbRandom: SIZE_T); } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptCallbackRandom(pbBuffer: PBYTE, cbBuffer: SIZE_T) -> SYMCRYPT_ERROR; +} pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_LSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 1; pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_MSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 2; pub type _SYMCRYPT_NUMBER_FORMAT = ::std::os::raw::c_uint; @@ -5224,4 +5234,4 @@ extern "C" { pbAgreedSecret: PBYTE, cbAgreedSecret: SIZE_T, ) -> SYMCRYPT_ERROR; -} +} \ No newline at end of file diff --git a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs index d115bf07..bbbedfb9 100644 --- a/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs +++ b/symcrypt-sys/src/bindings/x86_64_pc_windows_msvc.rs @@ -4208,6 +4208,11 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptInit(); +} +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } @@ -4812,9 +4817,14 @@ extern "C" { extern "C" { pub fn SymCryptHkdfSelfTest(); } +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptRandom(pbRandom: PBYTE, cbRandom: SIZE_T); } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptCallbackRandom(pbBuffer: PBYTE, cbBuffer: SIZE_T) -> SYMCRYPT_ERROR; +} pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_LSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 1; pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_MSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 2; pub type _SYMCRYPT_NUMBER_FORMAT = ::std::os::raw::c_int; diff --git a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs index 8bda4666..bd188ae6 100644 --- a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs @@ -4198,6 +4198,11 @@ extern "C" { extern "C" { pub fn SymCryptStoreMsbFirstUint64(src: UINT64, pbDst: PBYTE, cbDst: SIZE_T) -> SYMCRYPT_ERROR; } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptInit(); +} +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptModuleInit(api: UINT32, minor: UINT32); } @@ -4788,9 +4793,14 @@ extern "C" { extern "C" { pub fn SymCryptHkdfSelfTest(); } +#[cfg(feature = "dynamic")] extern "C" { pub fn SymCryptRandom(pbRandom: PBYTE, cbRandom: SIZE_T); } +#[cfg(not(feature = "dynamic"))] +extern "C" { + pub fn SymCryptCallbackRandom(pbBuffer: PBYTE, cbBuffer: SIZE_T) -> SYMCRYPT_ERROR; +} pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_LSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 1; pub const _SYMCRYPT_NUMBER_FORMAT_SYMCRYPT_NUMBER_FORMAT_MSB_FIRST: _SYMCRYPT_NUMBER_FORMAT = 2; pub type _SYMCRYPT_NUMBER_FORMAT = ::std::os::raw::c_uint; @@ -5224,4 +5234,4 @@ extern "C" { pbAgreedSecret: PBYTE, cbAgreedSecret: SIZE_T, ) -> SYMCRYPT_ERROR; -} +} \ No newline at end of file From ad228d633357354d3a84fb05b1041af5ffd46a51 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 18:51:25 -0500 Subject: [PATCH 25/40] Adding new line for linux builds --- symcrypt-bindgen/src/main.rs | 5 +++++ symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs | 2 +- symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index 6eb746b9..8a9451ab 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -228,6 +228,11 @@ fn fix_symcrypt_bindings(bindings_file: &str) { i += 1; } + // Append newline for linux bindings + if !out_content.last().unwrap_or(&String::new()).ends_with('\n') { + out_content.push("".to_string()); + } + // Write the modified content back std::fs::write(bindings_file, out_content.join("\n")).expect("Unable to write bindings file"); } diff --git a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs index 211eb2c1..ff90d93e 100644 --- a/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/aarch64_unknown_linux_gnu.rs @@ -5234,4 +5234,4 @@ extern "C" { pbAgreedSecret: PBYTE, cbAgreedSecret: SIZE_T, ) -> SYMCRYPT_ERROR; -} \ No newline at end of file +} diff --git a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs index bd188ae6..cb35fa3c 100644 --- a/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs +++ b/symcrypt-sys/src/bindings/x86_64_unknown_linux_gnu.rs @@ -5234,4 +5234,4 @@ extern "C" { pbAgreedSecret: PBYTE, cbAgreedSecret: SIZE_T, ) -> SYMCRYPT_ERROR; -} \ No newline at end of file +} From d2e80dd7a6b8fd9eed460705cb85d3a9488cfc6d Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 13 Feb 2025 18:53:27 -0500 Subject: [PATCH 26/40] cargo fmt --- symcrypt-bindgen/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/symcrypt-bindgen/src/main.rs b/symcrypt-bindgen/src/main.rs index 8a9451ab..1f12bcd7 100644 --- a/symcrypt-bindgen/src/main.rs +++ b/symcrypt-bindgen/src/main.rs @@ -230,7 +230,7 @@ fn fix_symcrypt_bindings(bindings_file: &str) { // Append newline for linux bindings if !out_content.last().unwrap_or(&String::new()).ends_with('\n') { - out_content.push("".to_string()); + out_content.push("".to_string()); } // Write the modified content back From 4af76204b16a38e7126600165417f4c8c13c00dc Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Fri, 14 Feb 2025 17:48:30 -0500 Subject: [PATCH 27/40] Addressing Max's comments, doing some cleanup --- symcrypt-sys/build/static_link.rs | 142 ++++++++++++++++++------------ 1 file changed, 87 insertions(+), 55 deletions(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 4867f146..4391452e 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -78,42 +78,75 @@ impl SymCryptOptions { cc.define("SYMCRYPT_IGNORE_PLATFORM", None); // TODO: Fix when we get ASM } - // Set specific flags for each target + // Set specific flags for operating system + + match self.triple { + // Target all Windows targets + Triple::x86_64_pc_windows_msvc | Triple::aarch64_pc_windows_msvc => { + // From SymCrypt-Platforms.cmake + cc.flag("/MP") // Multi-threaded compilation + .flag("/Zp8") // Structure packing alignment + .flag("/WX") // Treat warnings as errors + .flag("/guard:cf") // Control Flow Guard + .flag("/wd5105") // Disable warning caused by Windows SDK headers + .flag("/EHsc"); // Exception handling + // .flag("/dynamicbase"); // Enabling ASLR produces lots of warnings + + // From lib/CmakeLists.txt + // cc.asm_flag("/DSYMCRYPT_MASM"); // TODO: enable for ASM + } + + // Target all Linux targets + Triple::x86_64_unknown_linux_gnu | Triple::aarch64_unknown_linux_gnu => { + // From lib/CmakeLists.txt + // Stack Protection ON by default for linux + cc.flag("-fstack-protector-strong") + .flag("-Wstack-protector") + .flag("--param=ssp-buffer-size=4") + .flag("-fstack-clash-protection") + .flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors + + // From lib/CmakeLists.txt + // cc.flag("-x assembler-with-cpp"); // TODO: enable for ASM + + // From SymCrypt-Platforms.cmake + cc.flag("-Wno-unknown-pragmas") + .flag("-Werror") + .flag("-Wno-deprecated-declarations") + .flag("-Wno-deprecated") + .flag("-g") + .flag("-Wno-multichar") + .flag("-fPIC") // PIC is enabled by default on Linux + .flag("-fno-plt") + .flag("-fno-builtin-bcmp") + .flag("-fno-unroll-loops"); + } + } + + // Set specific flags for each triple match self.triple { Triple::x86_64_pc_windows_msvc => { - cc.define("_AMD64_", None); - // cc.asm_flag("/DSYMCRYPT_MASM"); // MASM assembly TODO: enable for ASM - cc.flag("/MP"); // Multi-threaded compilation - cc.flag("/Zp8"); // Structure packing alignment - cc.flag("/WX"); // Treat warnings as errors - cc.flag("/guard:cf"); // Control Flow Guard - // cc.flag("/dynamicbase"); // Enable ASLR - cc.flag("/EHsc"); // Exception handling + // From SymCrypt-Platforms.cmake + cc.define("_AMD64_", None).flag("/Gz"); // Set default to __stdcall, only for X86 } Triple::aarch64_pc_windows_msvc => { cc.define("_ARM64_", None); } Triple::x86_64_unknown_linux_gnu => { - cc.flag("-mpclmul"); - cc.flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors - // From SymCrypt-Platforms.cmake - cc.flag("-mssse3"); - cc.flag("-mxsave"); - cc.flag("-maes"); - cc.flag("-msha"); - cc.flag("-mrdrnd"); - cc.flag("-mrdseed"); - cc.flag("-mbmi2"); - cc.flag("-fstack-protector-strong"); - cc.flag("-Wstack-protector"); - cc.flag("-fno-plt"); - cc.flag("-fno-builtin-bcmp"); - cc.flag("-fno-unroll-loops"); + // From SymCrypt-Platforms.cmake + // Only for x86_64_unknown_linux_gnu + cc.flag("-mssse3") + .flag("-mxsave") + .flag("-maes") + .flag("-mpclmul") + .flag("-msha") + .flag("-mrdrnd") + .flag("-mrdseed"); } Triple::aarch64_unknown_linux_gnu => { - cc.flag("-march=armv8-a+simd+crypto"); // Enable a baseline of features for the compiler to support everywhere. - cc.flag("-flax-vector-conversions"); // Setting -flax-vector-conversions to build Arm64 intrinsics code with GCC. - cc.flag("-Wno-incompatible-pointer-types"); // Ignore noisy SymCrypt errors + // From SymCrypt-Platforms.cmake + cc.flag("-march=armv8-a+simd+crypto") // Enable a baseline of features for the compiler to support everywhere. + .flag("-flax-vector-conversions"); // Setting -flax-vector-conversions to build Arm64 intrinsics code with GCC. } } @@ -122,7 +155,7 @@ impl SymCryptOptions { } const SOURCE_DIR: &str = "symcrypt/lib"; -const CMAKE_SOURCES_COMMON: &str = " +const SOURCES_COMMON: &str = " 3des.c a_dispatch.c aes-asm.c @@ -264,7 +297,7 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io let already_compiled_set: HashSet<&str> = already_compiled_files.iter().cloned().collect(); // Prepares list of files to be compiled, excluding already compiled files for x86_64_unknown_linux_gnu - let mut base_files: Vec<&'static str> = CMAKE_SOURCES_COMMON + let mut base_files: Vec<&'static str> = SOURCES_COMMON .lines() .map(str::trim) // Trim once instead of inside filter .filter(|line| { @@ -295,19 +328,22 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io } // Add assembly pre generated ASM files to be compiled + // ASM files come from lib/CMakeLists.txt let asm_files = match options.triple() { Triple::x86_64_pc_windows_msvc => vec![ - "aesasm.asm", - "fdef_asm.asm", - "fdef_mulx.asm", - "fdef369_asm.asm", - "sha256xmm_asm.asm", - "sha256ymm_asm.asm", - "sha512ymm_asm.asm", - "sha512ymm_avx512vl_asm.asm", - "wipe.asm", + "aesasm-gas.asm", + "fdef_asm-gas.asm", + "fdef369_asm-gas.asm", + "fdef_mulx-gas.asm", + "wipe-gas.asm", + "sha256xmm_asm-gas.asm", + "sha256ymm_asm-gas.asm", + "sha512ymm_asm-gas.asm", + "sha512ymm_avx512vl_asm-gas.asm", ], - Triple::aarch64_pc_windows_msvc => vec!["fdef_asm.asm", "fdef369_asm.asm", "wipe.asm"], + Triple::aarch64_pc_windows_msvc => { + vec!["fdef_asm-gas.asm", "fdef369_asm-gas.asm", "wipe-gas.asm"] + } Triple::x86_64_unknown_linux_gnu => vec![ "aesasm-gas.asm", "fdef_asm-gas.asm", @@ -358,14 +394,16 @@ fn compile_symcrypt_static(lib_name: &str, options: &SymCryptOptions) -> std::io // Special compile files for x86_64_unknown_linux_gnu const X86_64_LINUX_CUSTOM_COMPILE_FILES: &str = r#" -set_source_files_properties(aes-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mvaes;-mvpclmulqdq") -set_source_files_properties(sha256Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") -set_source_files_properties(sha512Par-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2") -set_source_files_properties(sha256-xmm.c PROPERTIES COMPILE_OPTIONS "-mssse3") -set_source_files_properties(sha256-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") -set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") +aes-ymm.c "-mavx;-mavx2;-mvaes;-mvpclmulqdq" +sha256Par-ymm.c "-mavx;-mavx2" +sha512Par-ymm.c "-mavx;-mavx2" +sha256-xmm.c "-mssse3" +sha256-ymm.c "-mavx;-mavx2;-mbmi2" +sha512-ymm.c "-mavx;-mavx2;-mbmi2" "#; +//set_source_files_properties(sha512-ymm.c PROPERTIES COMPILE_OPTIONS "-mavx;-mavx2;-mbmi2") + /// Compiles the SymCrypt custom intermediates /// /// Currently this is only required for x86_64_unknown_linux_gnu, @@ -392,24 +430,18 @@ fn compile_symcrypt_intermediates( continue; } - let line = line - .strip_prefix("set_source_files_properties(") - .expect("Malformed input: missing prefix") - .strip_suffix(")") - .expect("Malformed input: missing suffix"); - // Example of parts: - // [aes-ymm.c, PROPERTIES, COMPILE_OPTIONS, "-mavx;-mavx2;-mvaes;-mvpclmulqdq"] + // [aes-ymm.c, "-mavx;-mavx2;-mvaes;-mvpclmulqdq"] let parts: Vec<&str> = line.split_whitespace().collect(); - if parts.len() < 4 { + if parts.len() < 2 { continue; } let file = parts[0]; - println!("Compiling {file} with custom options: {}", parts[3]); + println!("Compiling {file} with custom options: {}", parts[1]); // Isolate the compile options - let options = parts[3] + let options = parts[1] .trim_matches('"') .split(';') .filter(|s| !s.is_empty()); From b9a6cedd83709026f3a1e74d502698bf2ac24dc4 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Mon, 24 Feb 2025 13:06:41 -0500 Subject: [PATCH 28/40] Adding Sam's comments --- rust-symcrypt/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 92146355..833be95c 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -34,11 +34,12 @@ fn symcrypt_init() { INIT.call_once(|| { symcrypt_sys::SymCryptInit(); }); - // call symcrypt Init, only expose this for static linking. Add a regex to add a cfg feature to the bindings for this. } } -/// Takes in a a buffer called `buff` and fills it with random bytes. This function cannot fail. +/// Takes in a buffer called `buff` and fills it with random bytes. This function +/// is never expected to fail, but failure (due to OS dependencies) will crash the application. +/// There is no recoverable failure mode. /// /// If calling `symcrypt_random` with a dynamically linked module, `SymCryptRandom` will be called. /// From cc288818cc288de0e8b4d635fb2bbd3cef514991 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Mon, 24 Feb 2025 13:11:42 -0500 Subject: [PATCH 29/40] Cargo fmt + cargo clippy --- rust-symcrypt/src/lib.rs | 2 +- rust-symcrypt/src/rsa/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust-symcrypt/src/lib.rs b/rust-symcrypt/src/lib.rs index 833be95c..70607f32 100644 --- a/rust-symcrypt/src/lib.rs +++ b/rust-symcrypt/src/lib.rs @@ -37,7 +37,7 @@ fn symcrypt_init() { } } -/// Takes in a buffer called `buff` and fills it with random bytes. This function +/// Takes in a buffer called `buff` and fills it with random bytes. This function /// is never expected to fail, but failure (due to OS dependencies) will crash the application. /// There is no recoverable failure mode. /// diff --git a/rust-symcrypt/src/rsa/mod.rs b/rust-symcrypt/src/rsa/mod.rs index 9819cbb8..4d1fba8f 100644 --- a/rust-symcrypt/src/rsa/mod.rs +++ b/rust-symcrypt/src/rsa/mod.rs @@ -549,7 +549,7 @@ mod test { .rev() .enumerate() .fold(0, |v, (byte_offset, byte)| { - v | (*byte as u64) << (8 * byte_offset) + v | ((*byte as u64) << (8 * byte_offset)) }); assert_eq!(pub_exp_exported, pub_exp); From 099516f28aeff1022a786d5ebf47c07810e4a98d Mon Sep 17 00:00:00 2001 From: Khang Date: Tue, 25 Feb 2025 13:27:41 -0500 Subject: [PATCH 30/40] User/nnmkhang/update workflows (#86) * Seperating build workflow for windows and linux, adding runner for arm64 on linux --- .github/workflows/build_linux.yml | 77 ++++++++ .../{build.yml => build_windows.yml} | 172 ++++++++---------- 2 files changed, 153 insertions(+), 96 deletions(-) create mode 100644 .github/workflows/build_linux.yml rename .github/workflows/{build.yml => build_windows.yml} (57%) diff --git a/.github/workflows/build_linux.yml b/.github/workflows/build_linux.yml new file mode 100644 index 00000000..eb00eb76 --- /dev/null +++ b/.github/workflows/build_linux.yml @@ -0,0 +1,77 @@ +name: Build Linux + +on: + push: + branches: [ "main" ] + pull_request: + +env: + CARGO_TERM_COLOR: always + FEATURES_TO_TEST: md5,sha1,pkcs1-encrypt-decrypt + +jobs: + build: + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + runs-on: ubuntu-latest + run-tests: true + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04-arm + runs-on: ubuntu-24.04-arm + run-tests: true + + runs-on: ${{ matrix.os }} + name: ${{ matrix.target }} + env: + CARGO_BUILD_TARGET: ${{ matrix.target }} + + steps: + - uses: actions/checkout@v4 # Checks out SymCrypt based on Github submodule + with: + submodules: true + + - uses: Swatinem/rust-cache@v2 + + # Download SymCrypt via PMC + - name: Install SymCrypt via PMC + shell: bash + run: | + curl -sSL -O https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb + sudo dpkg -i packages-microsoft-prod.deb + sudo apt-get update + sudo apt-get install -y symcrypt + + - name: Install host target + shell: pwsh + run: | + rustup target add ${{ matrix.target }} + if ("${{ matrix.target }}" -match "aarch64-unknown-linux-gnu") { + sudo apt update + sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu + } + + - name: Debug build + run: cargo build --locked --verbose --target ${{ matrix.target }} + + - name: Release build + run: cargo build --release --locked --verbose --target ${{ matrix.target }} + + - name: Run tests (Debug, dynamic) + if: matrix.run-tests + run: cargo test --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run tests (Release, dynamic) + if: matrix.run-tests + run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run test (Debug, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} + + - name: Run test (Release, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} diff --git a/.github/workflows/build.yml b/.github/workflows/build_windows.yml similarity index 57% rename from .github/workflows/build.yml rename to .github/workflows/build_windows.yml index f24b7fd4..74181f4b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build_windows.yml @@ -1,96 +1,76 @@ -name: Build - -on: - push: - branches: [ "main" ] - pull_request: - -env: - CARGO_TERM_COLOR: always - FEATURES_TO_TEST: md5,sha1,pkcs1-encrypt-decrypt - -jobs: - build: - strategy: - fail-fast: false - matrix: - include: - - target: x86_64-pc-windows-msvc - os: windows-latest - symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-amd64-release-103.8.0-53be637d.zip" - run-tests: true - - target: aarch64-pc-windows-msvc - os: windows-latest - symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-arm64-release-103.8.0-53be637d.zip" - run-tests: false # Windows doesn't support ARM64 emulation - - target: x86_64-unknown-linux-gnu - symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-linux-generic-amd64-release-103.8.0-53be637.tar.gz" - os: ubuntu-latest - run-tests: true - - target: aarch64-unknown-linux-gnu - os: ubuntu-latest - symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-linux-generic-arm64-release-103.8.0-53be637.tar.gz" - run-tests: false - - runs-on: ${{ matrix.os }} - name: ${{ matrix.target }} - env: - CARGO_BUILD_TARGET: ${{ matrix.target }} - - steps: - - uses: actions/checkout@v4 - with: - submodules: true - - - name: Install host target - shell: pwsh - run: | - rustup target add ${{ matrix.target }} - if ("${{ matrix.target }}" -match "aarch64-unknown-linux-gnu") { - sudo apt update - sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu - } - - - uses: Swatinem/rust-cache@v2 - - - name: Download SymCrypt - shell: pwsh - run: | - $dir = $(Get-Item .).FullName - if ("${{ matrix.symcrypt }}" -match "\.tar\.gz$") { - Invoke-WebRequest -Uri ${{ matrix.symcrypt }} -OutFile symcrypt.tar.gz - New-Item -ItemType Directory -Force -Path symcrypt - tar -xzf symcrypt.tar.gz -C symcrypt - echo "$dir/symcrypt/lib" >> $env:GITHUB_PATH - echo "LD_LIBRARY_PATH=$dir/symcrypt/lib:$env:LD_LIBRARY_PATH" >> $env:GITHUB_ENV - echo "LIBRARY_PATH=$dir/symcrypt/lib:$env:LIBRARY_PATH" >> $env:GITHUB_ENV - echo "SYMCRYPT_LIB_PATH=$dir/symcrypt/lib" >> $env:GITHUB_ENV - } else { - Invoke-WebRequest -Uri ${{ matrix.symcrypt }} -OutFile symcrypt.zip - New-Item -ItemType Directory -Force -Path symcrypt - Expand-Archive -Path symcrypt.zip -DestinationPath symcrypt - echo "$dir/symcrypt/dll" >> $env:GITHUB_PATH - echo "SYMCRYPT_LIB_PATH=$dir/symcrypt/dll" >> $env:GITHUB_ENV - } - - - name: Debug build - run: cargo build --locked --verbose --target ${{ matrix.target }} - - - name: Release build - run: cargo build --release --locked --verbose --target ${{ matrix.target }} - - - name: Run tests (Debug, dynamic) - if: matrix.run-tests - run: cargo test --locked --verbose --all-features --target ${{ matrix.target }} - - - name: Run tests (Release, dynamic) - if: matrix.run-tests - run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} - - - name: Run test (Debug, static) - if: matrix.run-tests - run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} - - - name: Run test (Release, static) - if: matrix.run-tests - run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} +name: Build Windows + +on: + push: + branches: [ "main" ] + pull_request: + +env: + CARGO_TERM_COLOR: always + FEATURES_TO_TEST: md5,sha1,pkcs1-encrypt-decrypt + +jobs: + build: + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-pc-windows-msvc + os: windows-latest + runs-on: windows-latest + symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-amd64-release-103.8.0-53be637d.zip" + run-tests: true + - target: aarch64-pc-windows-msvc + os: windows-latest + runs-on: windows-latest + symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-arm64-release-103.8.0-53be637d.zip" + run-tests: false # Windows doesn't support ARM64 emulation + runs-on: ${{ matrix.os }} + name: ${{ matrix.target }} + env: + CARGO_BUILD_TARGET: ${{ matrix.target }} + + steps: + - uses: actions/checkout@v4 # Checks out SymCrypt based on Github submodule + with: + submodules: true + + - uses: Swatinem/rust-cache@v2 + + # Install host architecture, required for cross-compilation since there is no arm64-windows-msvc runner + - name: Install host target + shell: pwsh + run: | + rustup target add ${{ matrix.target }} + + - name: Download SymCrypt and Set Environment Variables + shell: pwsh + run: | + Invoke-WebRequest -Uri ${{ matrix.symcrypt }} -OutFile symcrypt.zip + New-Item -ItemType Directory -Force -Path symcrypt + Expand-Archive -Path symcrypt.zip -DestinationPath symcrypt + echo "$env:GITHUB_WORKSPACE\symcrypt\dll" >> $env:GITHUB_PATH + echo "SYMCRYPT_LIB_PATH=$env:GITHUB_WORKSPACE\symcrypt\dll" >> $env:GITHUB_ENV + echo "PATH=$env:GITHUB_WORKSPACE\symcrypt\dll;$env:PATH" >> $env:GITHUB_ENV + + - name: Debug build + run: cargo build --locked --verbose --target ${{ matrix.target }} + + - name: Release build + run: cargo build --release --locked --verbose --target ${{ matrix.target }} + + - name: Run tests (Debug, dynamic) + if: matrix.run-tests + run: cargo test --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run tests (Release, dynamic) + if: matrix.run-tests + run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run test (Debug, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} + + - name: Run test (Release, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} \ No newline at end of file From 48f46830abd6c8a5ca3462e4678328532df7ca9b Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Tue, 25 Feb 2025 13:29:02 -0500 Subject: [PATCH 31/40] newline --- .github/workflows/build_windows.yml | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 .github/workflows/build_windows.yml diff --git a/.github/workflows/build_windows.yml b/.github/workflows/build_windows.yml new file mode 100644 index 00000000..fd711e3b --- /dev/null +++ b/.github/workflows/build_windows.yml @@ -0,0 +1,76 @@ +name: Build Windows + +on: + push: + branches: [ "main" ] + pull_request: + +env: + CARGO_TERM_COLOR: always + FEATURES_TO_TEST: md5,sha1,pkcs1-encrypt-decrypt + +jobs: + build: + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-pc-windows-msvc + os: windows-latest + runs-on: windows-latest + symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-amd64-release-103.8.0-53be637d.zip" + run-tests: true + - target: aarch64-pc-windows-msvc + os: windows-latest + runs-on: windows-latest + symcrypt: "https://github.com/microsoft/SymCrypt/releases/download/v103.8.0/symcrypt-windows-arm64-release-103.8.0-53be637d.zip" + run-tests: false # Windows doesn't support ARM64 emulation + runs-on: ${{ matrix.os }} + name: ${{ matrix.target }} + env: + CARGO_BUILD_TARGET: ${{ matrix.target }} + + steps: + - uses: actions/checkout@v4 # Checks out SymCrypt based on Github submodule + with: + submodules: true + + - uses: Swatinem/rust-cache@v2 + + # Install host architecture, required for cross-compilation since there is no arm64-windows-msvc runner + - name: Install host target + shell: pwsh + run: | + rustup target add ${{ matrix.target }} + + - name: Download SymCrypt and Set Environment Variables + shell: pwsh + run: | + Invoke-WebRequest -Uri ${{ matrix.symcrypt }} -OutFile symcrypt.zip + New-Item -ItemType Directory -Force -Path symcrypt + Expand-Archive -Path symcrypt.zip -DestinationPath symcrypt + echo "$env:GITHUB_WORKSPACE\symcrypt\dll" >> $env:GITHUB_PATH + echo "SYMCRYPT_LIB_PATH=$env:GITHUB_WORKSPACE\symcrypt\dll" >> $env:GITHUB_ENV + echo "PATH=$env:GITHUB_WORKSPACE\symcrypt\dll;$env:PATH" >> $env:GITHUB_ENV + + - name: Debug build + run: cargo build --locked --verbose --target ${{ matrix.target }} + + - name: Release build + run: cargo build --release --locked --verbose --target ${{ matrix.target }} + + - name: Run tests (Debug, dynamic) + if: matrix.run-tests + run: cargo test --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run tests (Release, dynamic) + if: matrix.run-tests + run: cargo test --release --locked --verbose --all-features --target ${{ matrix.target }} + + - name: Run test (Debug, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} + + - name: Run test (Release, static) + if: matrix.run-tests + run: cargo test --features ${{ env.FEATURES_TO_TEST }} --locked --target ${{ matrix.target }} From 518ae4a0a7eba6f4df087bfbb61027b79aea5c83 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Tue, 25 Feb 2025 14:47:21 -0500 Subject: [PATCH 32/40] Prepping Cargo.toml and updating docs for version bump to 0.6.0 --- Cargo.lock | 4 +-- rust-symcrypt/Cargo.toml | 4 +-- rust-symcrypt/INSTALL.md | 19 +++++------ rust-symcrypt/README.md | 68 ++++++++++++++++++++++++++++------------ symcrypt-sys/Cargo.toml | 2 +- symcrypt-sys/README.md | 2 +- 6 files changed, 64 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22ad5e62..f33a0e30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -381,7 +381,7 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "symcrypt" -version = "0.5.1" +version = "0.6.0" dependencies = [ "hex", "lazy_static", @@ -402,7 +402,7 @@ dependencies = [ [[package]] name = "symcrypt-sys" -version = "0.4.0" +version = "0.5.0" dependencies = [ "cc", "libc", diff --git a/rust-symcrypt/Cargo.toml b/rust-symcrypt/Cargo.toml index cd2e5d79..13faa0a8 100644 --- a/rust-symcrypt/Cargo.toml +++ b/rust-symcrypt/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "symcrypt" authors = ["nnmkhang"] -version = "0.5.1" +version = "0.6.0" license = "MIT OR Apache-2.0" description = "Friendly and Idiomatic Wrappers for SymCrypt" edition.workspace = true @@ -15,7 +15,7 @@ readme = "README.md" [dependencies] # uses '../symcrypt-sys' when compiled locally, and uses # crates.io versioning when published -symcrypt-sys = {path = "../symcrypt-sys", version = "0.4.0"} +symcrypt-sys = {path = "../symcrypt-sys", version = "0.5.0"} libc = "0.2.0" lazy_static = "1.4.0" diff --git a/rust-symcrypt/INSTALL.md b/rust-symcrypt/INSTALL.md index bc0c47fe..629a4ba6 100644 --- a/rust-symcrypt/INSTALL.md +++ b/rust-symcrypt/INSTALL.md @@ -1,21 +1,18 @@ -# Detailed Build and Install +# Detailed Build and Install for Dynamic Linking -This page provides more detailed installation instructions +This page provides more detailed installation instructions for dynamic linking on Windows and Linux. -## Installation -For ease of use, the recommended usage is to obtain these binaries from the official SymCrypt [Repo](https://github.com/microsoft/SymCrypt/releases/tag/v103.4.2). +The `symcrypt` crate is a wrapper on top of the `SymCrypt` library, and requires access to the `SymCrypt` library during the build and execution stage for dynamic linking. For ease of use, the recommended way to configure your `SymCrypt` library dependancy is to obtain the required binaries from the official [SymCrypt Repo](https://github.com/microsoft/SymCrypt/releases/tag/v103.8.0). -**Note:** If you wish to build your own version please follow the [Build Instructions](https://github.com/microsoft/SymCrypt/blob/main/BUILD.md) that are provided by SymCrypt to install SymCrypt for your target architecture. - -Once SymCrypt is installed on your machine, we must configure your machine so that the SymCrypt crate's build script can easily find `symcrypt.dll` and `symcrypt.lib` which are needed on Windows, or the `libsymcrypt.so*` files which are needed for Linux. +However, If you wish to build your own version of the underlying `SymCrypt` library please follow the [Build Instructions](https://github.com/microsoft/SymCrypt/blob/main/BUILD.md) that are provided by SymCrypt to install SymCrypt for your target architecture. ### Windows Install -The `symcrypt.lib` can be found in the the following path after SymCrypt has been downloaded and unzipped. +The `symcrypt.lib` can be found in the the following path after `SymCrypt` has been downloaded and unzipped. `C:\Your-Path-To-SymCrypt-Release-Download\dll\` -The SymCrypt crate needs to link against the SymCrypt import library during build. +The SymCrypt crate needs to link against the `SymCrypt` import library during build. To do so you must set the `SYMCRYPT_LIB_PATH` environment variable. You can do this by using the following command: @@ -40,9 +37,13 @@ Here are 2 recommended options to ensure your `symcrypt.dll` is found by Windows 1. Put the `symcrypt.dll` in the same folder as your output `.exe` file. If you are doing development (not release), the common path will be: `C:\your-project\target\debug\`. 2. Permanently add the `symcrypt.dll` path into your System PATH environment variable. Doing this will ensure that any project that uses the SymCrypt crate will be able to access `symcrypt.lib` +**NOTE:** By setting the `SYMCRYPT_LIB_PATH` via `setx SYMCRYPT_LIB_PATH ""`; `symcrypt.dll` will already be on the `PATH` and you will not have to do any additional configuration for program. + ### Linux Install After installing and unzipping SymCrypt on a Linux distro, the required `libsymcrypt.so*` files can be found in the following path: `~/Your-Path-To-SymCrypt-Release-Download/lib/` The symcrypt crate needs to be able to link with these libs during build/run time. In order to mimic the installation path for other libraries, you must place the `libsymcrypt.so*` files into linker load path. The way that this is set will vary between distros. On most distros it set via the environment variable `$LD_LIBRARY_PATH`. + +**Note:** While the `symcrypt` crate has only been tested on `Ubuntu`, working with other distros should be similar. The goal is to place the `libsymcrypt.so*` files in a location where the your Linux distro can find the required libs at build/run time. The path may be different depending on your flavour of Linux, and architecture. diff --git a/rust-symcrypt/README.md b/rust-symcrypt/README.md index 3468c3bd..b54a24a8 100644 --- a/rust-symcrypt/README.md +++ b/rust-symcrypt/README.md @@ -4,18 +4,18 @@ This crate provides friendly and idiomatic Rust wrappers over [SymCrypt](https:/ This crate has a dependency on `symcrypt-sys`, which utilizes `bindgen` to create Rust/C FFI bindings. -**`symcrypt` version `0.5.1` is based off of `SymCrypt v103.4.2`.**. You must use a version that is greater than or equal to `SymCrypt v103.4.2`. +**`symcrypt` version `0.6.0` is based off of `SymCrypt v103.8.0`.** You must use a version that is greater than or equal to `SymCrypt v103.8.0`. To view a detailed list of changes please see the [releases page](https://github.com/microsoft/rust-symcrypt/releases/). ### Supported Configurations -| Operating Environment | Architecture | Dynamic Linking | -| --------------------- | ----------------- | ----------- | -| Windows user mode | AMD64, ARM64 | ✅ | -| Ubuntu | AMD64, ARM64 | ✅ | -| Azure Linux 3 | AMD64, ARM64 | ✅ | +| Operating Environment | Architecture | Dynamic Linking | Static Linking | +| --------------------- | ----------------- | --------------- | -------------- | +| Windows user mode | AMD64, ARM64 | ✅ | ✅ | +| Ubuntu | AMD64, ARM64 | ✅ | ✅ | +| Azure Linux 3 | AMD64, ARM64 | ✅ | ✅ | ## Supported APIs @@ -65,7 +65,28 @@ To enable either `Md5` or `Sha1`, or `Pkcs1 Encrypt/Decrypt` pass the `md5` or ` ## Quick Start Guide -`symcrypt` requires the `SymCrypt` library to be present at both build time and run time. +As of version `0.6.0`, the `symcrypt` crate can take advantage of both static and dynamic linking. + +--- + +## Static Linking: + +**NOTE: Static linking is highly experimental and should not be used in production and or release builds. If you are Microsoft employee please contact the SymCrypt team for more info.** + +Static linking works by building the `SymCrypt` library from source and static linking to lib that is produced. + +Static linking is enabled by default and does not require any additional configuration. You can skip to the `Usage` section below. + +--- +## Dynamic Linking: + +To enable Dynamic linking, you must enable the `dynamic` feature flag. + +```cargo +[dependencies] +symcrypt = {vesrion = "0.6.0", features = ["dynamic"]} +hex = "0.4.3" +``` ### Windows: Download the latest `symcrypt.dll` and `symcrypt.lib` for your corresponding CPU architecture from the [SymCrypt Releases Page](https://github.com/microsoft/SymCrypt/releases) and place them somewhere accessible on your machine. @@ -76,7 +97,7 @@ Set the required `SYMCRYPT_LIB_PATH` environment variable. You can do this by us You will need to restart `terminal` / `cmd` after setting the environment variable. -For more information please see the `INSTALL.md` file on the [`rust-symcrypt`](https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt) page. +For more information please see `INSTALL.md`. ### Linux: @@ -86,18 +107,16 @@ SymCrypt is pre-installed on Azure Linux 3 machines. Please ensure that you have #### Other distros: -For Ubuntu, you can install SymCrypt via package manager by connecting to PMC. +For Ubuntu, you can install SymCrypt via package manager by connecting to PMC ( Example shown for Ubuntu `24.04` ): -1. [Connect to PMC](https://learn.microsoft.com/en-us/linux/packages) -2. `sudo apt-get install symcrypt` +1. `curl -sSL -O https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb` +2. `sudo dpkg -i packages-microsoft-prod.deb` +3. `sudo apt-get update` +4. `sudo apt-get install symcrypt` -Alternatively, you can manually install the lib files: +For more info on connecting to PMC please see: [Connecting to PMC](https://learn.microsoft.com/en-us/linux/packages) -Download the latest `libsymcrypt.so*` files for your corresponding CPU architecture from the [SymCrypt Releases Page](https://github.com/microsoft/SymCrypt/releases) and place them in your machines `$LD_LIBRARY_PATH`. - -For more information please see the `INSTALL.md` file on the [`rust-symcrypt`](https://github.com/microsoft/rust-symcrypt/tree/main/rust-symcrypt) page - -**Note:** This path may be different depending on your flavour of Linux, and architecture. The goal is to place the `libsymcrypt.so*` files in a location where the your Linux distro can find the required libs at build/run time. +If you want to try connecting with another flavour of Linux, or for more info please see `INSTALL.md` --- @@ -108,15 +127,24 @@ There are unit tests attached to each file that show how to use each function. I ### Instructions: -add symcrypt to your `Cargo.toml` file. +Add symcrypt to your `Cargo.toml` file. + +If static linking: +```cargo +[dependencies] +symcrypt = "0.6.0" +hex = "0.4.3" +``` + +If dynamic linking: ```cargo [dependencies] -symcrypt = "0.5.1" +symcrypt = {vesrion = "0.6.0", features = ["dynamic"]} hex = "0.4.3" ``` -include symcrypt in your code +Include symcrypt in your code ```rust use symcrypt::hash::sha256; diff --git a/symcrypt-sys/Cargo.toml b/symcrypt-sys/Cargo.toml index 28f399ea..5ac4880b 100644 --- a/symcrypt-sys/Cargo.toml +++ b/symcrypt-sys/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "symcrypt-sys" authors = ["Microsoft"] -version = "0.4.0" +version = "0.5.0" license = "MIT OR Apache-2.0" description = "Rust/C Bindings for SymCrypt" edition.workspace = true diff --git a/symcrypt-sys/README.md b/symcrypt-sys/README.md index 0cd79b66..58ce29de 100644 --- a/symcrypt-sys/README.md +++ b/symcrypt-sys/README.md @@ -20,7 +20,7 @@ You must also configure your system to pick up the SymCrypt lib on your machine, In your `Cargo.toml` ```Rust -symcrypt-sys = "0.4.0" +symcrypt-sys = "0.5.0" ``` Then you can call the underlying SymCrypt code directly via the FFIs. ```Rust From b1fc149c4359c65ae3c1ef64f9f9500bd28fbc68 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 26 Feb 2025 18:13:31 -0500 Subject: [PATCH 33/40] WIP --- rust-symcrypt/README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/rust-symcrypt/README.md b/rust-symcrypt/README.md index b54a24a8..61e33622 100644 --- a/rust-symcrypt/README.md +++ b/rust-symcrypt/README.md @@ -10,12 +10,15 @@ To view a detailed list of changes please see the [releases page](https://github ### Supported Configurations - | Operating Environment | Architecture | Dynamic Linking | Static Linking | | --------------------- | ----------------- | --------------- | -------------- | -| Windows user mode | AMD64, ARM64 | ✅ | ✅ | -| Ubuntu | AMD64, ARM64 | ✅ | ✅ | -| Azure Linux 3 | AMD64, ARM64 | ✅ | ✅ | +| Windows user mode | AMD64, ARM64 | ✅ | ✅ ** | +| Ubuntu | AMD64, ARM64 | ✅ | ✅ ** | +| Azure Linux 3 | AMD64, ARM64 | ✅ | ✅ ** | + +**Note:** ** Static linking is exper,emta; + + ## Supported APIs From eadf174c13879c02217b7c0c40fc9f3539653e7e Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 5 Mar 2025 16:22:07 -0500 Subject: [PATCH 34/40] updating documentation --- .github/workflows/clippy.yml | 2 ++ rust-symcrypt/README.md | 41 +++++++++++------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 763ba9e0..88a4b90f 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -22,6 +22,8 @@ jobs: steps: - name: Checkout sources uses: actions/checkout@v4 + with: + submodules: true - name: Install Rust toolchain uses: dtolnay/rust-toolchain@stable diff --git a/rust-symcrypt/README.md b/rust-symcrypt/README.md index 61e33622..445e5de8 100644 --- a/rust-symcrypt/README.md +++ b/rust-symcrypt/README.md @@ -1,5 +1,4 @@ # SymCrypt Rust Wrapper - This crate provides friendly and idiomatic Rust wrappers over [SymCrypt](https://github.com/microsoft/SymCrypt), an open-source cryptographic library. This crate has a dependency on `symcrypt-sys`, which utilizes `bindgen` to create Rust/C FFI bindings. @@ -12,13 +11,13 @@ To view a detailed list of changes please see the [releases page](https://github ### Supported Configurations | Operating Environment | Architecture | Dynamic Linking | Static Linking | | --------------------- | ----------------- | --------------- | -------------- | -| Windows user mode | AMD64, ARM64 | ✅ | ✅ ** | -| Ubuntu | AMD64, ARM64 | ✅ | ✅ ** | -| Azure Linux 3 | AMD64, ARM64 | ✅ | ✅ ** | - -**Note:** ** Static linking is exper,emta; +| Windows user mode | AMD64, ARM64 | ✅ | ✅ ⚠️ | +| Ubuntu | AMD64, ARM64 | ✅ | ✅ ⚠️ | +| Azure Linux 3 | AMD64, ARM64 | ✅ | ✅ ⚠️ | +**Note:** ⚠️ Static linking is meant to be used for rapid development and testing. Static linking does not offer FIPS and is **not to be used in Microsoft production or release builds.** For more information please see the `Quick Start Guide` below. +--- ## Supported APIs @@ -31,71 +30,59 @@ Hashing: - Sha3_256 ( stateful/stateless ) - Sha3_384 ( stateful/stateless ) - Sha3_512 ( stateful/stateless ) - HMAC: - HmacMd5 ( stateful/stateless ) - HmacSha1 ( stateful/stateless ) - HmacSha256 ( stateful/stateless ) - HmacSha384 ( stateful/stateless ) - HmacSha512 ( stateful/stateless ) - HKDF: - HmacMd5 - HmacSha1 - HmacSha256 - HmacSha384 - HmacSha512 - Encryption: - AES-GCM Encrypt/Decrypt - ChaCha20-Poly1305 Encrypt/Decrypt - AES-CBC Encrypt/Decrypt - ECC: - ECDH Secret Agreement ( NistP256, NistP384, NistP521, Curve25519) - ECDSA Sign / Verify ( NistP256, NistP384, NistP521 ) - RSA: - PKCS1 ( Sign, Verify, Encrypt, Decrypt ) - PSS ( Sign, Verify ) - OAEP ( Encrypt, Decrypt ) - **Note**: `Md5` and `Sha1`, and `PKCS1 Encrypt/Decrypt` are considered weak crypto, and are only added for interop purposes. To enable either `Md5` or `Sha1`, or `Pkcs1 Encrypt/Decrypt` pass the `md5` or `sha1` or `pkcs1-encrypt-decrypt` flag into your `Cargo.toml`. --- - ## Quick Start Guide -As of version `0.6.0`, the `symcrypt` crate can take advantage of both static and dynamic linking. +As of version `0.6.0`, the `symcrypt` crate can take advantage of both static and dynamic linking. Static linking is enabled by default. --- - ## Static Linking: -**NOTE: Static linking is highly experimental and should not be used in production and or release builds. If you are Microsoft employee please contact the SymCrypt team for more info.** - -Static linking works by building the `SymCrypt` library from source and static linking to lib that is produced. - -Static linking is enabled by default and does not require any additional configuration. You can skip to the `Usage` section below. +**NOTE: Static linking should not be used in production and or release builds for Microsoft 1st Party. If you are Microsoft employee please contact the SymCrypt team for more info.** +Static linking works by building the `SymCrypt` library from source and static linking to lib that is produced, this will result in longer build times and larger binaries but gives the added benefit of not worrying about the distribution of a dynamic library. Static linking is enabled by default. --- + ## Dynamic Linking: -To enable Dynamic linking, you must enable the `dynamic` feature flag. +Dynamic linking assumes is required for FIPS. If the `dynamic` flag is set, the `symcrypt` crate will operate under the assumption that you have followed following instructions for configuring your system to do a dynamic link of the `SymCrypt` library. ```cargo [dependencies] symcrypt = {vesrion = "0.6.0", features = ["dynamic"]} hex = "0.4.3" -``` +``` ### Windows: Download the latest `symcrypt.dll` and `symcrypt.lib` for your corresponding CPU architecture from the [SymCrypt Releases Page](https://github.com/microsoft/SymCrypt/releases) and place them somewhere accessible on your machine. - Set the required `SYMCRYPT_LIB_PATH` environment variable. You can do this by using the following command: - `setx SYMCRYPT_LIB_PATH ""` You will need to restart `terminal` / `cmd` after setting the environment variable. @@ -107,7 +94,6 @@ For more information please see `INSTALL.md`. #### Azure Linux 3: SymCrypt is pre-installed on Azure Linux 3 machines. Please ensure that you have the most up to date version of SymCrypt by updating via `tdnf`. - #### Other distros: For Ubuntu, you can install SymCrypt via package manager by connecting to PMC ( Example shown for Ubuntu `24.04` ): @@ -125,7 +111,6 @@ If you want to try connecting with another flavour of Linux, or for more info pl ## Usage There are unit tests attached to each file that show how to use each function. Included is some sample code to do a stateless Sha256 hash. - **Note:** This code snippet also uses the [hex](https://crates.io/crates/hex) crate. ### Instructions: @@ -135,7 +120,7 @@ Add symcrypt to your `Cargo.toml` file. If static linking: ```cargo [dependencies] -symcrypt = "0.6.0" +symcrypt = {vesrion = "0.6.0"} hex = "0.4.3" ``` @@ -152,10 +137,8 @@ Include symcrypt in your code ```rust use symcrypt::hash::sha256; use hex; - let data = hex::decode("641ec2cf711e").unwrap(); let expected: &str = "cfdbd6c9acf9842ce04e8e6a0421838f858559cf22d2ea8a38bd07d5e4692233"; - let result = sha256(&data); assert_eq!(hex::encode(result), expected); ``` From 9003cb68fbab890d4747248f6e73f7dfd84b5ff3 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 5 Mar 2025 16:23:34 -0500 Subject: [PATCH 35/40] updating documentation for install --- rust-symcrypt/INSTALL.md | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/rust-symcrypt/INSTALL.md b/rust-symcrypt/INSTALL.md index 629a4ba6..bf3318e3 100644 --- a/rust-symcrypt/INSTALL.md +++ b/rust-symcrypt/INSTALL.md @@ -17,33 +17,28 @@ The SymCrypt crate needs to link against the `SymCrypt` import library during bu To do so you must set the `SYMCRYPT_LIB_PATH` environment variable. You can do this by using the following command: `setx SYMCRYPT_LIB_PATH ""` - The `symcrypt.dll` can be found in the the following path after SymCrypt has been downloaded and unzipped. - `C:\Your-Path-To-SymCrypt-Release-Download\dll\` - During runtime, Windows will handle finding all needed `dll`'s in order to run the intended program, this includes our `symcrypt.dll` file. The places Windows will look are: - 1. The folder from which the application loaded. 2. The system folder. Use the `GetSystemDirectory` function to retrieve the path of this folder. 3. The Windows folder. Use the `GetWindowsDirectory` function to get the path of this folder. 4. The current folder. 5. The directories listed in the PATH environment variable. - For more info please see: [Dynamic-link library search order](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order) - Here are 2 recommended options to ensure your `symcrypt.dll` is found by Windows during runtime. - 1. Put the `symcrypt.dll` in the same folder as your output `.exe` file. If you are doing development (not release), the common path will be: `C:\your-project\target\debug\`. 2. Permanently add the `symcrypt.dll` path into your System PATH environment variable. Doing this will ensure that any project that uses the SymCrypt crate will be able to access `symcrypt.lib` -**NOTE:** By setting the `SYMCRYPT_LIB_PATH` via `setx SYMCRYPT_LIB_PATH ""`; `symcrypt.dll` will already be on the `PATH` and you will not have to do any additional configuration for program. +**NOTE:** By setting the `SYMCRYPT_LIB_PATH` via `setx SYMCRYPT_LIB_PATH ""`; `symcrypt.dll` will already be on the `PATH` and you will not have to do any additional configuration for your program. ### Linux Install +Though the artifacts on the [SymCrypt Repo](https://github.com/microsoft/SymCrypt/releases/tag/v103.8.0). Have been built with `Ubuntu` in mind, the `SymCrypt` library has been built with very few standard library dependencies and should work on most Linux distributions. + After installing and unzipping SymCrypt on a Linux distro, the required `libsymcrypt.so*` files can be found in the following path: `~/Your-Path-To-SymCrypt-Release-Download/lib/` -The symcrypt crate needs to be able to link with these libs during build/run time. In order to mimic the installation path for other libraries, you must place the `libsymcrypt.so*` files into linker load path. The way that this is set will vary between distros. On most distros it set via the environment variable `$LD_LIBRARY_PATH`. +The `symcrypt` crate needs to be able to link with these libs during build/run time. In order to mimic the installation path for other libraries, you must place the `libsymcrypt.so*` files into linker load path. The way that this is set will vary between distros. On most distros it set via the environment variable `$LD_LIBRARY_PATH`. -**Note:** While the `symcrypt` crate has only been tested on `Ubuntu`, working with other distros should be similar. The goal is to place the `libsymcrypt.so*` files in a location where the your Linux distro can find the required libs at build/run time. The path may be different depending on your flavour of Linux, and architecture. +**Note:** While the `symcrypt` crate has only been tested on `Ubuntu`, working with other distros should be similar. The goal is to place the `libsymcrypt.so*` files in a location where the your Linux distro can find the required libs at build/run time. The path may be different depending on your flavour of Linux, and architecture. \ No newline at end of file From 4d3acbf534717af5b28b0f6bf8cc4340f6002b1c Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 5 Mar 2025 16:28:25 -0500 Subject: [PATCH 36/40] change author --- rust-symcrypt/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-symcrypt/Cargo.toml b/rust-symcrypt/Cargo.toml index 13faa0a8..b1364357 100644 --- a/rust-symcrypt/Cargo.toml +++ b/rust-symcrypt/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "symcrypt" -authors = ["nnmkhang"] +authors = ["Microsoft"] version = "0.6.0" license = "MIT OR Apache-2.0" description = "Friendly and Idiomatic Wrappers for SymCrypt" From c50fa13586ba92300df04dd319832e7f2971b633 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Wed, 5 Mar 2025 16:31:47 -0500 Subject: [PATCH 37/40] fix clippy --- rust-symcrypt/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust-symcrypt/README.md b/rust-symcrypt/README.md index 445e5de8..64f0f408 100644 --- a/rust-symcrypt/README.md +++ b/rust-symcrypt/README.md @@ -30,29 +30,35 @@ Hashing: - Sha3_256 ( stateful/stateless ) - Sha3_384 ( stateful/stateless ) - Sha3_512 ( stateful/stateless ) + HMAC: - HmacMd5 ( stateful/stateless ) - HmacSha1 ( stateful/stateless ) - HmacSha256 ( stateful/stateless ) - HmacSha384 ( stateful/stateless ) - HmacSha512 ( stateful/stateless ) + HKDF: - HmacMd5 - HmacSha1 - HmacSha256 - HmacSha384 - HmacSha512 + Encryption: - AES-GCM Encrypt/Decrypt - ChaCha20-Poly1305 Encrypt/Decrypt - AES-CBC Encrypt/Decrypt + ECC: - ECDH Secret Agreement ( NistP256, NistP384, NistP521, Curve25519) - ECDSA Sign / Verify ( NistP256, NistP384, NistP521 ) + RSA: - PKCS1 ( Sign, Verify, Encrypt, Decrypt ) - PSS ( Sign, Verify ) - OAEP ( Encrypt, Decrypt ) + **Note**: `Md5` and `Sha1`, and `PKCS1 Encrypt/Decrypt` are considered weak crypto, and are only added for interop purposes. To enable either `Md5` or `Sha1`, or `Pkcs1 Encrypt/Decrypt` pass the `md5` or `sha1` or `pkcs1-encrypt-decrypt` flag into your `Cargo.toml`. From 4af848425716d9c50dd654bbec8a2cae3c65d93e Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 6 Mar 2025 10:21:32 -0500 Subject: [PATCH 38/40] Fix docs --- rust-symcrypt/INSTALL.md | 2 -- rust-symcrypt/README.md | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/rust-symcrypt/INSTALL.md b/rust-symcrypt/INSTALL.md index bf3318e3..fffd1460 100644 --- a/rust-symcrypt/INSTALL.md +++ b/rust-symcrypt/INSTALL.md @@ -40,5 +40,3 @@ After installing and unzipping SymCrypt on a Linux distro, the required `libsymc `~/Your-Path-To-SymCrypt-Release-Download/lib/` The `symcrypt` crate needs to be able to link with these libs during build/run time. In order to mimic the installation path for other libraries, you must place the `libsymcrypt.so*` files into linker load path. The way that this is set will vary between distros. On most distros it set via the environment variable `$LD_LIBRARY_PATH`. - -**Note:** While the `symcrypt` crate has only been tested on `Ubuntu`, working with other distros should be similar. The goal is to place the `libsymcrypt.so*` files in a location where the your Linux distro can find the required libs at build/run time. The path may be different depending on your flavour of Linux, and architecture. \ No newline at end of file diff --git a/rust-symcrypt/README.md b/rust-symcrypt/README.md index 64f0f408..ae4de4fe 100644 --- a/rust-symcrypt/README.md +++ b/rust-symcrypt/README.md @@ -88,6 +88,7 @@ hex = "0.4.3" ### Windows: Download the latest `symcrypt.dll` and `symcrypt.lib` for your corresponding CPU architecture from the [SymCrypt Releases Page](https://github.com/microsoft/SymCrypt/releases) and place them somewhere accessible on your machine. + Set the required `SYMCRYPT_LIB_PATH` environment variable. You can do this by using the following command: `setx SYMCRYPT_LIB_PATH ""` From 1bdad6b02cb79d495d77809b6cf58d37d7f56574 Mon Sep 17 00:00:00 2001 From: Khang Nguyen Date: Thu, 6 Mar 2025 11:37:19 -0500 Subject: [PATCH 39/40] minor buildscript tweak --- symcrypt-sys/build/static_link.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/symcrypt-sys/build/static_link.rs b/symcrypt-sys/build/static_link.rs index 4391452e..143408b5 100644 --- a/symcrypt-sys/build/static_link.rs +++ b/symcrypt-sys/build/static_link.rs @@ -15,12 +15,17 @@ pub fn compile_and_link_symcrypt() -> std::io::Result<()> { let options = SymCryptOptions::new(); println!("Build config: {:?}", options); + // Rebuild if any of these files change + println!("cargo:rerun-if-changed=../symcrypt/lib/"); + println!("cargo:rerun-if-changed=../symcrypt/inc/"); + println!("cargo:rerun-if-changed=../inc/"); + // Required Windows bcrypt dependency for BCryptGenRandom const ADDITIONAL_DEPENDENCIES: &[&str] = &[ #[cfg(windows)] "bcrypt", ]; - println!("cargo:rerun-if-changed=upstream"); + println!("Compiling SymCrypt..."); // Compile and Build SymCrypt with provided SymCryptOptions From c95763d6d0536046c5702f1ad844c499944482c7 Mon Sep 17 00:00:00 2001 From: "Khang Nguyen (ENS)" Date: Mon, 30 Mar 2026 15:09:21 -0400 Subject: [PATCH 40/40] Add STATIC_LINKING.md for experimental branch --- rust-symcrypt/STATIC_LINKING.md | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 rust-symcrypt/STATIC_LINKING.md diff --git a/rust-symcrypt/STATIC_LINKING.md b/rust-symcrypt/STATIC_LINKING.md new file mode 100644 index 00000000..3d7c0564 --- /dev/null +++ b/rust-symcrypt/STATIC_LINKING.md @@ -0,0 +1,73 @@ +# Static Linking (Experimental) + +> **This feature is experimental and lives on the `experimental/static-linking` branch.** +> It is not part of any released version of `symcrypt`. APIs and behavior may change. + +> **NOTE: Static linking must not be used in production or release builds for Microsoft 1st Party. +> If you are a Microsoft employee please contact the SymCrypt team for more info.** + +Static linking does not offer FIPS compliance and is intended for rapid development and testing only. + +--- + +## Overview + +Static linking works by building the `SymCrypt` library from source and linking against the resulting static library. This results in longer build times and larger binaries, but removes the requirement to distribute or locate a dynamic library at runtime. + +Static linking is the default on this branch. To opt into dynamic linking instead, enable the `dynamic` feature flag (see below). + +### Supported Configurations + +| Operating Environment | Architecture | Static Linking | +| --------------------- | ------------ | -------------- | +| Windows user mode | AMD64, ARM64 | ✅ ⚠️ | +| Ubuntu | AMD64, ARM64 | ✅ ⚠️ | +| Azure Linux 3 | AMD64, ARM64 | ✅ ⚠️ | + +--- + +## Usage + +### Static linking (default on this branch) + +```cargo +[dependencies] +symcrypt = "0.6.0" +hex = "0.4.3" +``` + +### Dynamic linking + +If you need FIPS compliance or want to use a system-installed SymCrypt library, enable the `dynamic` feature: + +```cargo +[dependencies] +symcrypt = { version = "0.6.0", features = ["dynamic"] } +hex = "0.4.3" +``` + +For dynamic linking setup instructions see `INSTALL.md`. + +--- + +## Build Requirements + +Static linking requires the SymCrypt source to be available as a submodule. The build script in `symcrypt-sys/build/static_link.rs` handles compilation. Refer to `DEVELOPER.md` for submodule setup and regenerating bindings. + +### Windows + +No additional setup is required beyond a standard Rust + MSVC toolchain. + +### Linux + +The following packages are required: + +```bash +sudo apt-get install -y clang libclang-dev +``` + +For cross-compilation (ARM64): + +```bash +sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu +```