Skip to content

Commit

Permalink
Backport more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwiggers committed Jan 2, 2024
1 parent 51e3bb2 commit 088bf56
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 314 deletions.
10 changes: 5 additions & 5 deletions implementations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ traits_version: 0.3.5

kems:
kyber:
version: 0.7.7
version: 0.7.9
x86_avx2_guard: 'target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos'
implementations: [clean, avx2, aarch64]
implementations: [clean, avx2]
schemes:
- name: kyber512
implementations: [clean, avx2, aarch64]
implementations: [clean, avx2]
- name: kyber768
implementations: [clean, avx2, aarch64]
implementations: [clean, avx2]
- name: kyber1024
implementations: [clean, avx2, aarch64]
implementations: [clean, avx2]
signs: {}
2 changes: 1 addition & 1 deletion pqclean
Submodule pqclean updated 2803 files
5 changes: 2 additions & 3 deletions pqcrypto-kyber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "pqcrypto-kyber"
description = "Post-Quantum Key-Encapsulation Mechanism kyber"
readme = "README.md"
version = "0.7.8"
version = "0.7.9"
authors = ["Thom Wiggers <thom@thomwiggers.nl>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -19,9 +19,8 @@ serde = { version = "1.0", features = ["derive"], optional = true }
serde-big-array = { version = "0.5.1", optional = true }

[features]
default = ["avx2", "neon", "std"]
default = ["avx2", "std"]
avx2 = ["std"]
neon = ["std"]
std = ["pqcrypto-traits/std"]
serialization = ["serde", "serde-big-array"]

Expand Down
3 changes: 0 additions & 3 deletions pqcrypto-kyber/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ methods only.
* ``kyber512``
* ``clean``
* ``avx2`` (if supported)
* ``aarch64`` (if supported)
* ``kyber768``
* ``clean``
* ``avx2`` (if supported)
* ``aarch64`` (if supported)
* ``kyber1024``
* ``clean``
* ``avx2`` (if supported)
* ``aarch64`` (if supported)


## License
Expand Down
46 changes: 0 additions & 46 deletions pqcrypto-kyber/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,6 @@ macro_rules! build_avx2 {
};
}

macro_rules! build_aarch64 {
($variant:expr) => {
let internals_include_path = &std::env::var("DEP_PQCRYPTO_INTERNALS_INCLUDEPATH").unwrap();
let common_dir = Path::new("pqclean/common");

let mut builder = cc::Build::new();
let target_dir: PathBuf = ["pqclean", "crypto_kem", $variant, "aarch64"]
.iter()
.collect();

let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os == "wasi" {
let wasi_sdk_path =
&std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR");
builder.flag(format!("--sysroot={}", wasi_sdk_path).as_str());
}

let scheme_files = glob::glob(target_dir.join("*.[csS]").to_str().unwrap()).unwrap();
builder.flag("-march=armv8-a");

builder
.include(internals_include_path)
.include(&common_dir)
.include(target_dir)
.files(
scheme_files
.into_iter()
.map(|p| p.unwrap().to_string_lossy().into_owned()),
);
builder.compile(format!("{}_aarch64", $variant).as_str());
};
}

fn main() {
#[allow(unused_variables)]
let aes_enabled = env::var("CARGO_FEATURE_AES").is_ok();
Expand All @@ -131,30 +98,17 @@ fn main() {
if target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos {
build_avx2!("kyber512");
}
if target_arch == "aarch64" && neon_enabled {
build_aarch64!("kyber512");
}
build_clean!("kyber768");
if target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos {
build_avx2!("kyber768");
}
if target_arch == "aarch64" && neon_enabled {
build_aarch64!("kyber768");
}
build_clean!("kyber1024");
if target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos {
build_avx2!("kyber1024");
}
if target_arch == "aarch64" && neon_enabled {
build_aarch64!("kyber1024");
}

if target_arch == "x86_64" && avx2_enabled && !is_windows && !is_macos {
// Print enableing flag for AVX2 implementation
println!("cargo:rustc-cfg=enable_x86_avx2");
}
if target_arch == "aarch64" && neon_enabled {
// Print enableing flag for AARCH64 implementation
println!("cargo:rustc-cfg=enable_aarch64_neon");
}
}
193 changes: 0 additions & 193 deletions pqcrypto-kyber/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ pub const PQCLEAN_KYBER512_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 768;
#[cfg(enable_x86_avx2)]
pub const PQCLEAN_KYBER512_AVX2_CRYPTO_BYTES: usize = 32;

#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER512_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 1632;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER512_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 800;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER512_AARCH64_CRYPTO_CIPHERTEXTBYTES: usize = 768;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER512_AARCH64_CRYPTO_BYTES: usize = 32;

pub const PQCLEAN_KYBER768_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 2400;
pub const PQCLEAN_KYBER768_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1184;
pub const PQCLEAN_KYBER768_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 1088;
Expand All @@ -51,15 +42,6 @@ pub const PQCLEAN_KYBER768_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 1088;
#[cfg(enable_x86_avx2)]
pub const PQCLEAN_KYBER768_AVX2_CRYPTO_BYTES: usize = 32;

#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER768_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 2400;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER768_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 1184;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER768_AARCH64_CRYPTO_CIPHERTEXTBYTES: usize = 1088;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER768_AARCH64_CRYPTO_BYTES: usize = 32;

pub const PQCLEAN_KYBER1024_CLEAN_CRYPTO_SECRETKEYBYTES: usize = 3168;
pub const PQCLEAN_KYBER1024_CLEAN_CRYPTO_PUBLICKEYBYTES: usize = 1568;
pub const PQCLEAN_KYBER1024_CLEAN_CRYPTO_CIPHERTEXTBYTES: usize = 1568;
Expand All @@ -74,15 +56,6 @@ pub const PQCLEAN_KYBER1024_AVX2_CRYPTO_CIPHERTEXTBYTES: usize = 1568;
#[cfg(enable_x86_avx2)]
pub const PQCLEAN_KYBER1024_AVX2_CRYPTO_BYTES: usize = 32;

#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER1024_AARCH64_CRYPTO_SECRETKEYBYTES: usize = 3168;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER1024_AARCH64_CRYPTO_PUBLICKEYBYTES: usize = 1568;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER1024_AARCH64_CRYPTO_CIPHERTEXTBYTES: usize = 1568;
#[cfg(enable_aarch64_neon)]
pub const PQCLEAN_KYBER1024_AARCH64_CRYPTO_BYTES: usize = 32;

#[link(name = "kyber512_clean")]
extern "C" {
pub fn PQCLEAN_KYBER512_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
Expand All @@ -106,25 +79,6 @@ extern "C" {
-> c_int;
}

#[cfg(enable_aarch64_neon)]
#[link(name = "kyber512_aarch64")]
extern "C" {
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER512_AARCH64_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER512_AARCH64_crypto_kem_enc(
ct: *mut u8,
ss: *mut u8,
pk: *const u8,
) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER512_AARCH64_crypto_kem_dec(
ss: *mut u8,
ct: *const u8,
sk: *const u8,
) -> c_int;
}

#[link(name = "kyber768_clean")]
extern "C" {
pub fn PQCLEAN_KYBER768_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
Expand All @@ -148,25 +102,6 @@ extern "C" {
-> c_int;
}

#[cfg(enable_aarch64_neon)]
#[link(name = "kyber768_aarch64")]
extern "C" {
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER768_AARCH64_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER768_AARCH64_crypto_kem_enc(
ct: *mut u8,
ss: *mut u8,
pk: *const u8,
) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER768_AARCH64_crypto_kem_dec(
ss: *mut u8,
ct: *const u8,
sk: *const u8,
) -> c_int;
}

#[link(name = "kyber1024_clean")]
extern "C" {
pub fn PQCLEAN_KYBER1024_CLEAN_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
Expand Down Expand Up @@ -194,25 +129,6 @@ extern "C" {
) -> c_int;
}

#[cfg(enable_aarch64_neon)]
#[link(name = "kyber1024_aarch64")]
extern "C" {
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER1024_AARCH64_crypto_kem_keypair(pk: *mut u8, sk: *mut u8) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER1024_AARCH64_crypto_kem_enc(
ct: *mut u8,
ss: *mut u8,
pk: *const u8,
) -> c_int;
#[cfg(enable_aarch64_neon)]
pub fn PQCLEAN_KYBER1024_AARCH64_crypto_kem_dec(
ss: *mut u8,
ct: *const u8,
sk: *const u8,
) -> c_int;
}

#[cfg(test)]
mod test_kyber512_clean {
use super::*;
Expand Down Expand Up @@ -287,41 +203,6 @@ mod test_kyber512_avx2 {
}
}

#[cfg(all(test, enable_aarch64_neon, feature = "neon"))]
mod test_kyber512_aarch64 {
use super::*;
use alloc::vec;

#[test]
fn test_ffi() {
unsafe {
let mut pk = vec![0u8; PQCLEAN_KYBER512_AARCH64_CRYPTO_PUBLICKEYBYTES];
let mut sk = vec![0u8; PQCLEAN_KYBER512_AARCH64_CRYPTO_SECRETKEYBYTES];
let mut ct = vec![0u8; PQCLEAN_KYBER512_AARCH64_CRYPTO_CIPHERTEXTBYTES];
let mut ss1 = vec![0u8; PQCLEAN_KYBER512_AARCH64_CRYPTO_BYTES];
let mut ss2 = vec![0u8; PQCLEAN_KYBER512_AARCH64_CRYPTO_BYTES];

assert_eq!(
0,
PQCLEAN_KYBER512_AARCH64_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr())
);
assert_eq!(
0,
PQCLEAN_KYBER512_AARCH64_crypto_kem_enc(
ct.as_mut_ptr(),
ss1.as_mut_ptr(),
pk.as_ptr()
)
);
assert_eq!(
0,
PQCLEAN_KYBER512_AARCH64_crypto_kem_dec(ss2.as_mut_ptr(), ct.as_ptr(), sk.as_ptr())
);
assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal");
}
}
}

#[cfg(test)]
mod test_kyber768_clean {
use super::*;
Expand Down Expand Up @@ -396,41 +277,6 @@ mod test_kyber768_avx2 {
}
}

#[cfg(all(test, enable_aarch64_neon, feature = "neon"))]
mod test_kyber768_aarch64 {
use super::*;
use alloc::vec;

#[test]
fn test_ffi() {
unsafe {
let mut pk = vec![0u8; PQCLEAN_KYBER768_AARCH64_CRYPTO_PUBLICKEYBYTES];
let mut sk = vec![0u8; PQCLEAN_KYBER768_AARCH64_CRYPTO_SECRETKEYBYTES];
let mut ct = vec![0u8; PQCLEAN_KYBER768_AARCH64_CRYPTO_CIPHERTEXTBYTES];
let mut ss1 = vec![0u8; PQCLEAN_KYBER768_AARCH64_CRYPTO_BYTES];
let mut ss2 = vec![0u8; PQCLEAN_KYBER768_AARCH64_CRYPTO_BYTES];

assert_eq!(
0,
PQCLEAN_KYBER768_AARCH64_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr())
);
assert_eq!(
0,
PQCLEAN_KYBER768_AARCH64_crypto_kem_enc(
ct.as_mut_ptr(),
ss1.as_mut_ptr(),
pk.as_ptr()
)
);
assert_eq!(
0,
PQCLEAN_KYBER768_AARCH64_crypto_kem_dec(ss2.as_mut_ptr(), ct.as_ptr(), sk.as_ptr())
);
assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal");
}
}
}

#[cfg(test)]
mod test_kyber1024_clean {
use super::*;
Expand Down Expand Up @@ -504,42 +350,3 @@ mod test_kyber1024_avx2 {
}
}
}

#[cfg(all(test, enable_aarch64_neon, feature = "neon"))]
mod test_kyber1024_aarch64 {
use super::*;
use alloc::vec;

#[test]
fn test_ffi() {
unsafe {
let mut pk = vec![0u8; PQCLEAN_KYBER1024_AARCH64_CRYPTO_PUBLICKEYBYTES];
let mut sk = vec![0u8; PQCLEAN_KYBER1024_AARCH64_CRYPTO_SECRETKEYBYTES];
let mut ct = vec![0u8; PQCLEAN_KYBER1024_AARCH64_CRYPTO_CIPHERTEXTBYTES];
let mut ss1 = vec![0u8; PQCLEAN_KYBER1024_AARCH64_CRYPTO_BYTES];
let mut ss2 = vec![0u8; PQCLEAN_KYBER1024_AARCH64_CRYPTO_BYTES];

assert_eq!(
0,
PQCLEAN_KYBER1024_AARCH64_crypto_kem_keypair(pk.as_mut_ptr(), sk.as_mut_ptr())
);
assert_eq!(
0,
PQCLEAN_KYBER1024_AARCH64_crypto_kem_enc(
ct.as_mut_ptr(),
ss1.as_mut_ptr(),
pk.as_ptr()
)
);
assert_eq!(
0,
PQCLEAN_KYBER1024_AARCH64_crypto_kem_dec(
ss2.as_mut_ptr(),
ct.as_ptr(),
sk.as_ptr()
)
);
assert_eq!(&ss1[..], &ss2[..], "Shared secrets should be equal");
}
}
}
Loading

0 comments on commit 088bf56

Please sign in to comment.