From 7103b6adf3c52649970d66d3d424bbea2aa40368 Mon Sep 17 00:00:00 2001 From: jiegec Date: Mon, 8 Mar 2021 19:09:51 +0800 Subject: Add sm3 cryptographic hash support --- openssl-sys/src/evp.rs | 2 ++ openssl/src/hash.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/openssl-sys/src/evp.rs b/openssl-sys/src/evp.rs index ec5df30a..3808c6bf 100644 --- a/openssl-sys/src/evp.rs +++ b/openssl-sys/src/evp.rs @@ -243,6 +243,8 @@ extern "C" { #[cfg(ossl111)] pub fn EVP_shake256() -> *const EVP_MD; pub fn EVP_ripemd160() -> *const EVP_MD; + #[cfg(all(ossl111, not(osslconf = "OPENSSL_NO_SM3")))] + pub fn EVP_sm3() -> *const EVP_MD; pub fn EVP_des_ecb() -> *const EVP_CIPHER; pub fn EVP_des_ede3() -> *const EVP_CIPHER; pub fn EVP_des_ede3_cbc() -> *const EVP_CIPHER; diff --git a/openssl/src/hash.rs b/openssl/src/hash.rs index 317d20e2..30ba8fcf 100644 --- a/openssl/src/hash.rs +++ b/openssl/src/hash.rs @@ -128,6 +128,11 @@ impl MessageDigest { unsafe { MessageDigest(ffi::EVP_ripemd160()) } } + #[cfg(all(ossl111, not(osslconf = "OPENSSL_NO_SM3")))] + pub fn sm3() -> MessageDigest { + unsafe { MessageDigest(ffi::EVP_sm3()) } + } + #[allow(clippy::trivially_copy_pass_by_ref)] pub fn as_ptr(&self) -> *const ffi::EVP_MD { self.0 @@ -626,6 +631,18 @@ mod tests { } } + #[test] + fn test_sm3() { + let tests = [( + "616263", + "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", + )]; + + for test in tests.iter() { + hash_test(MessageDigest::sm3(), test); + } + } + #[test] fn from_nid() { assert_eq!( -- cgit v1.2.3 From 44230aff797be3f555752c5d35518616ecba610b Mon Sep 17 00:00:00 2001 From: jiegec Date: Mon, 8 Mar 2021 19:19:17 +0800 Subject: Add NID definition for sm3 --- openssl-sys/src/obj_mac.rs | 2 ++ openssl/src/nid.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/openssl-sys/src/obj_mac.rs b/openssl-sys/src/obj_mac.rs index d78416da..dbe494a1 100644 --- a/openssl-sys/src/obj_mac.rs +++ b/openssl-sys/src/obj_mac.rs @@ -255,6 +255,8 @@ pub const NID_md5: c_int = 4; pub const NID_md5_sha1: c_int = 114; pub const NID_hmacWithMD5: c_int = 797; pub const NID_hmacWithSHA1: c_int = 163; +pub const NID_sm3: c_int = 1143; +pub const NID_sm3WithRSAEncryption: c_int = 1144; pub const NID_hmacWithSHA224: c_int = 798; pub const NID_hmacWithSHA256: c_int = 799; pub const NID_hmacWithSHA384: c_int = 800; diff --git a/openssl/src/nid.rs b/openssl/src/nid.rs index 8545666c..ab3112d5 100644 --- a/openssl/src/nid.rs +++ b/openssl/src/nid.rs @@ -367,6 +367,8 @@ impl Nid { pub const MD5_SHA1: Nid = Nid(ffi::NID_md5_sha1); pub const HMACWITHMD5: Nid = Nid(ffi::NID_hmacWithMD5); pub const HMACWITHSHA1: Nid = Nid(ffi::NID_hmacWithSHA1); + pub const SM3: Nid = Nid(ffi::NID_sm3); + pub const SM3WITHRSAENCRYPTION: Nid = Nid(ffi::NID_sm3WithRSAEncryption); pub const HMACWITHSHA224: Nid = Nid(ffi::NID_hmacWithSHA224); pub const HMACWITHSHA256: Nid = Nid(ffi::NID_hmacWithSHA256); pub const HMACWITHSHA384: Nid = Nid(ffi::NID_hmacWithSHA384); -- cgit v1.2.3 From 5c214017e4cba8d2f1ae3df748accfc40b425de9 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 8 Mar 2021 19:19:55 +0800 Subject: Specify libressl version requriement of SM3 Signed-off-by: Harry Chen --- openssl-sys/src/evp.rs | 2 +- openssl/src/hash.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/openssl-sys/src/evp.rs b/openssl-sys/src/evp.rs index 3808c6bf..893e9828 100644 --- a/openssl-sys/src/evp.rs +++ b/openssl-sys/src/evp.rs @@ -243,7 +243,7 @@ extern "C" { #[cfg(ossl111)] pub fn EVP_shake256() -> *const EVP_MD; pub fn EVP_ripemd160() -> *const EVP_MD; - #[cfg(all(ossl111, not(osslconf = "OPENSSL_NO_SM3")))] + #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM3")))] pub fn EVP_sm3() -> *const EVP_MD; pub fn EVP_des_ecb() -> *const EVP_CIPHER; pub fn EVP_des_ede3() -> *const EVP_CIPHER; diff --git a/openssl/src/hash.rs b/openssl/src/hash.rs index 30ba8fcf..666022c7 100644 --- a/openssl/src/hash.rs +++ b/openssl/src/hash.rs @@ -128,7 +128,7 @@ impl MessageDigest { unsafe { MessageDigest(ffi::EVP_ripemd160()) } } - #[cfg(all(ossl111, not(osslconf = "OPENSSL_NO_SM3")))] + #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM3")))] pub fn sm3() -> MessageDigest { unsafe { MessageDigest(ffi::EVP_sm3()) } } @@ -631,6 +631,7 @@ mod tests { } } + #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM3")))] #[test] fn test_sm3() { let tests = [( -- cgit v1.2.3 From 7819f3e7d8298e6388e093911ee95bd2c24bf40b Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 8 Mar 2021 19:41:47 +0800 Subject: Fix NID definitions of SM3 Signed-off-by: Harry Chen --- openssl-sys/src/obj_mac.rs | 6 ++++-- openssl/src/nid.rs | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openssl-sys/src/obj_mac.rs b/openssl-sys/src/obj_mac.rs index dbe494a1..49fc4add 100644 --- a/openssl-sys/src/obj_mac.rs +++ b/openssl-sys/src/obj_mac.rs @@ -255,8 +255,6 @@ pub const NID_md5: c_int = 4; pub const NID_md5_sha1: c_int = 114; pub const NID_hmacWithMD5: c_int = 797; pub const NID_hmacWithSHA1: c_int = 163; -pub const NID_sm3: c_int = 1143; -pub const NID_sm3WithRSAEncryption: c_int = 1144; pub const NID_hmacWithSHA224: c_int = 798; pub const NID_hmacWithSHA256: c_int = 799; pub const NID_hmacWithSHA384: c_int = 800; @@ -922,3 +920,7 @@ pub const NID_X448: c_int = 1035; pub const NID_ED25519: c_int = 1087; #[cfg(ossl111)] pub const NID_ED448: c_int = 1088; +#[cfg(any(ossl111, libressl291))] +pub const NID_sm3: c_int = 1143; +#[cfg(any(ossl111, libressl291))] +pub const NID_sm3WithRSAEncryption: c_int = 1144; diff --git a/openssl/src/nid.rs b/openssl/src/nid.rs index ab3112d5..8545666c 100644 --- a/openssl/src/nid.rs +++ b/openssl/src/nid.rs @@ -367,8 +367,6 @@ impl Nid { pub const MD5_SHA1: Nid = Nid(ffi::NID_md5_sha1); pub const HMACWITHMD5: Nid = Nid(ffi::NID_hmacWithMD5); pub const HMACWITHSHA1: Nid = Nid(ffi::NID_hmacWithSHA1); - pub const SM3: Nid = Nid(ffi::NID_sm3); - pub const SM3WITHRSAENCRYPTION: Nid = Nid(ffi::NID_sm3WithRSAEncryption); pub const HMACWITHSHA224: Nid = Nid(ffi::NID_hmacWithSHA224); pub const HMACWITHSHA256: Nid = Nid(ffi::NID_hmacWithSHA256); pub const HMACWITHSHA384: Nid = Nid(ffi::NID_hmacWithSHA384); -- cgit v1.2.3 From 6bb010f4761a303639b40d8586c7608cbe1275db Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 8 Mar 2021 20:14:40 +0800 Subject: Fix NID of SM3 series in libreSSL Signed-off-by: Harry Chen --- openssl-sys/src/obj_mac.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/openssl-sys/src/obj_mac.rs b/openssl-sys/src/obj_mac.rs index 49fc4add..63f5cb38 100644 --- a/openssl-sys/src/obj_mac.rs +++ b/openssl-sys/src/obj_mac.rs @@ -920,7 +920,11 @@ pub const NID_X448: c_int = 1035; pub const NID_ED25519: c_int = 1087; #[cfg(ossl111)] pub const NID_ED448: c_int = 1088; -#[cfg(any(ossl111, libressl291))] +#[cfg(ossl111)] pub const NID_sm3: c_int = 1143; -#[cfg(any(ossl111, libressl291))] +#[cfg(libressl291)] +pub const NID_sm3: c_int = 968; +#[cfg(ossl111)] pub const NID_sm3WithRSAEncryption: c_int = 1144; +#[cfg(libressl291)] +pub const NID_sm3WithRSAEncryption: c_int = 969; -- cgit v1.2.3 From fb5f94e0346135540dab354311f31561d84a5dee Mon Sep 17 00:00:00 2001 From: jiegec Date: Tue, 9 Mar 2021 09:40:42 +0800 Subject: Pass OPENSSL_NO_SM3 definition to rust properly --- openssl-sys/build/expando.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/openssl-sys/build/expando.c b/openssl-sys/build/expando.c index 2f8b1b4d..dd3dace0 100644 --- a/openssl-sys/build/expando.c +++ b/openssl-sys/build/expando.c @@ -81,3 +81,7 @@ RUST_CONF_OPENSSL_NO_TLSEXT #ifdef OPENSSL_NO_STDIO RUST_CONF_OPENSSL_NO_STDIO #endif + +#ifdef OPENSSL_NO_SM3 +RUST_CONF_OPENSSL_NO_SM3 +#endif -- cgit v1.2.3