From dab8c7a52387b3a570cc7f21fdd9e380d3f52e11 Mon Sep 17 00:00:00 2001 From: Theo Buehler Date: Fri, 20 Oct 2023 16:55:34 +0200 Subject: [PATCH 1/2] Enable BN_mod_sqrt() for upcoming LibreSSL 3.8.2 This API was inherited from OpenSSL, so it has always been present. Enable it for the upcoming LibreSSL release. The test as it was written would fail since LibreSSL returns the other square root. Improve the test to work with all possible implementations with a more interesting test case. Also check for error in the simplest situation where no square root exists. --- openssl-sys/src/handwritten/bn.rs | 2 +- openssl/src/bn.rs | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/openssl-sys/src/handwritten/bn.rs b/openssl-sys/src/handwritten/bn.rs index fc42c13946..c93521ad91 100644 --- a/openssl-sys/src/handwritten/bn.rs +++ b/openssl-sys/src/handwritten/bn.rs @@ -75,7 +75,7 @@ extern "C" { m: *const BIGNUM, ctx: *mut BN_CTX, ) -> c_int; - #[cfg(ossl110)] + #[cfg(any(ossl110, libressl382))] pub fn BN_mod_sqrt( ret: *mut BIGNUM, a: *const BIGNUM, diff --git a/openssl/src/bn.rs b/openssl/src/bn.rs index a67d0807aa..e1cde2c878 100644 --- a/openssl/src/bn.rs +++ b/openssl/src/bn.rs @@ -655,7 +655,7 @@ impl BigNumRef { /// Places into `self` the modular square root of `a` such that `self^2 = a (mod p)` #[corresponds(BN_mod_sqrt)] - #[cfg(ossl110)] + #[cfg(any(ossl110, libressl382))] pub fn mod_sqrt( &mut self, a: &BigNumRef, @@ -1490,17 +1490,24 @@ mod tests { assert!(b.is_const_time()) } - #[cfg(ossl110)] + #[cfg(any(ossl110, libressl382))] #[test] fn test_mod_sqrt() { let mut ctx = BigNumContext::new().unwrap(); - let s = BigNum::from_hex_str("47A8DD7626B9908C80ACD7E0D3344D69").unwrap(); - let p = BigNum::from_hex_str("81EF47265B58BCE5").unwrap(); + let s = BigNum::from_hex_str("2").unwrap(); + let p = BigNum::from_hex_str("7DEB1").unwrap(); + let mut sqrt = BigNum::new().unwrap(); let mut out = BigNum::new().unwrap(); - out.mod_sqrt(&s, &p, &mut ctx).unwrap(); - assert_eq!(out, BigNum::from_hex_str("7C6D179E19B97BDD").unwrap()); + // Square the root because OpenSSL randomly returns one of 2E42C or 4FA85 + sqrt.mod_sqrt(&s, &p, &mut ctx).unwrap(); + out.mod_sqr(&sqrt, &p, &mut ctx).unwrap(); + assert!(out == s); + + let s = BigNum::from_hex_str("3").unwrap(); + let p = BigNum::from_hex_str("5").unwrap(); + assert!(out.mod_sqrt(&s, &p, &mut ctx).is_err()); } #[test] From c84e3fcd6b7bfbfb0669d955eac0d88fc0ba9d59 Mon Sep 17 00:00:00 2001 From: Theo Buehler Date: Fri, 20 Oct 2023 17:11:11 +0200 Subject: [PATCH 2/2] Enable BN_mod_sqrt() unconditionally --- openssl-sys/src/handwritten/bn.rs | 1 - openssl/src/bn.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/openssl-sys/src/handwritten/bn.rs b/openssl-sys/src/handwritten/bn.rs index c93521ad91..fb55f6b82c 100644 --- a/openssl-sys/src/handwritten/bn.rs +++ b/openssl-sys/src/handwritten/bn.rs @@ -75,7 +75,6 @@ extern "C" { m: *const BIGNUM, ctx: *mut BN_CTX, ) -> c_int; - #[cfg(any(ossl110, libressl382))] pub fn BN_mod_sqrt( ret: *mut BIGNUM, a: *const BIGNUM, diff --git a/openssl/src/bn.rs b/openssl/src/bn.rs index e1cde2c878..1ae450bb75 100644 --- a/openssl/src/bn.rs +++ b/openssl/src/bn.rs @@ -655,7 +655,6 @@ impl BigNumRef { /// Places into `self` the modular square root of `a` such that `self^2 = a (mod p)` #[corresponds(BN_mod_sqrt)] - #[cfg(any(ossl110, libressl382))] pub fn mod_sqrt( &mut self, a: &BigNumRef, @@ -1490,7 +1489,6 @@ mod tests { assert!(b.is_const_time()) } - #[cfg(any(ossl110, libressl382))] #[test] fn test_mod_sqrt() { let mut ctx = BigNumContext::new().unwrap();