diff --git a/Cargo.toml b/Cargo.toml index 78a6b85..ca2fbea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,12 @@ rand = "0.7.3" dislog-hal = { git = "https://github.com/Yinet-project/dislog-hal"} tiny-keccak = { version = "2.0.0", features = ["sha3"] } libsm = { git = "https://github.com/citahub/libsm"} +serde = { version = "1.0", features = ["derive"] } +dislog-hal-sm2 = { git = "https://github.com/Yinet-project/dislog-hal-sm2"} + [dev-dependencies] dislog-hal-curve25519 = { git = "https://github.com/Yinet-project/dislog-hal-curve25519"} -dislog-hal-sm2 = { git = "https://github.com/Yinet-project/dislog-hal-sm2"} byteorder = "1" [dev-dependencies.cargo-husky] diff --git a/src/hasher/mod.rs b/src/hasher/mod.rs index 6fd3ed3..362750f 100644 --- a/src/hasher/mod.rs +++ b/src/hasher/mod.rs @@ -1,5 +1,2 @@ pub mod sha3; -pub use sha3::Sha3; - pub mod sm3; -pub use sm3::Sm3; diff --git a/src/hasher/sha3.rs b/src/hasher/sha3.rs index 86bbff1..ade16fd 100644 --- a/src/hasher/sha3.rs +++ b/src/hasher/sha3.rs @@ -1,41 +1,6 @@ use crate::prelude::Splitable; +use crate::NewU864; use core::fmt::Debug; -use hex::{FromHex, FromHexError}; - -pub struct NewU864(pub [u8; 64]); - -impl Debug for NewU864 { - fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - let mut info = f.debug_list(); - for i in 0..self.0.len() { - info.entry(&self.0[i]); - } - info.finish() - } -} - -impl PartialEq for NewU864 { - fn eq(&self, other: &Self) -> bool { - self.0[..] == other.0[..] - } -} - -impl FromHex for NewU864 { - type Error = FromHexError; - - fn from_hex>(hex: T) -> Result { - match <[u8; 64]>::from_hex(hex) { - Ok(x) => Ok(Self(x)), - Err(err) => Err(err), - } - } -} - -impl AsRef<[u8]> for NewU864 { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} pub struct Sha3(pub tiny_keccak::Sha3); diff --git a/src/keypair/curve25519.rs b/src/keypair/curve25519.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/keypair/curve25519.rs @@ -0,0 +1 @@ + diff --git a/src/keypair/ed25519.rs b/src/keypair/ed25519.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/keypair/ed25519.rs @@ -0,0 +1 @@ + diff --git a/src/key_pair_gen.rs b/src/keypair/mod.rs similarity index 54% rename from src/key_pair_gen.rs rename to src/keypair/mod.rs index 5667f5f..80a5cb3 100644 --- a/src/key_pair_gen.rs +++ b/src/keypair/mod.rs @@ -1,31 +1,56 @@ +pub mod curve25519; +pub mod ed25519; +pub mod sm2; + use crate::prelude::Splitable; use crate::CryptoError; use core::fmt::Debug; use core::marker::PhantomData; -use dislog_hal::{Bytes, DisLogPoint, Hasher, Point, Scalar, ScalarNumber}; +use dislog_hal::{DisLogPoint, Hasher, Point, Scalar, ScalarNumber}; use hex::{FromHex, ToHex}; use rand::RngCore; +use serde::{Deserialize, Serialize}; + +pub trait SliceN: + Default + + AsRef<[u8]> + + Debug + + ToHex + + FromHex + + PartialEq + + Clone + + Serialize + + for<'de> Deserialize<'de> +{ +} + +impl SliceN for [u8; 32] {} -#[derive(Debug)] -pub struct KeyPair< - N: Default + AsRef<[u8]> + AsMut<[u8]> + Sized + Debug + ToHex + FromHex + PartialEq + Clone, +#[derive(Debug, Serialize, Deserialize)] +pub struct Keypair< + N: SliceN, H: Hasher + Default + Splitable, P: DisLogPoint, - S: ScalarNumber + Bytes, + S: ScalarNumber, > { + #[serde(bound(deserialize = "N: SliceN"))] seed: N, + #[serde(bound(deserialize = "S: ScalarNumber"))] secret_key: Scalar, + #[serde(bound(deserialize = "P: DisLogPoint"))] public_key: Point

, + #[serde(bound(deserialize = "N: SliceN"))] code: N, + #[serde(bound(deserialize = "H: Hasher + Default + Splitable"))] _hash: PhantomData, } impl< - N: Default + AsRef<[u8]> + AsMut<[u8]> + Sized + Debug + ToHex + FromHex + PartialEq + Clone, + N: SliceN + AsMut<[u8]>, H: Hasher + Default + Splitable, P: DisLogPoint, - S: ScalarNumber + Bytes, - > KeyPair + S: ScalarNumber, + > Keypair { pub fn generate(rng: &mut R) -> Result { let mut seed = N::default(); @@ -36,29 +61,32 @@ impl< Err(_) => Err(CryptoError::KeyPairGenError), } } +} +impl< + N: SliceN, + H: Hasher + Default + Splitable, + P: DisLogPoint, + S: ScalarNumber, + > Keypair +{ pub fn generate_from_seed(seed: N) -> Result { let mut hasher = H::default(); hasher.update(seed.as_ref()); let (secret_key_x, code) = hasher.split_finalize(); - let secret_key; - match S::from_bytes(secret_key_x) { - Ok(x) => { - secret_key = Scalar { inner: x }; - } + let secret_key = match Scalar::::from_bytes(secret_key_x.as_ref()) { + Ok(secret_key) => secret_key, Err(_) => return Err(CryptoError::KeyPairGenError), - } + }; - if secret_key.inner == S::zero() { + if secret_key == Scalar::::zero() { return Err(CryptoError::KeyPairGenError); } Ok(Self { seed, - public_key: Point { - inner: P::generator(), - } * &secret_key, + public_key: Point::

::generator() * &secret_key, secret_key, code, _hash: PhantomData, diff --git a/src/keypair/sm2.rs b/src/keypair/sm2.rs new file mode 100644 index 0000000..b7ca880 --- /dev/null +++ b/src/keypair/sm2.rs @@ -0,0 +1,61 @@ +use crate::hasher::sha3::Sha3; +use crate::keypair::Keypair; +use crate::{signature, CryptoError}; +use dislog_hal::{Hasher, Point, Scalar}; +use rand::RngCore; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct KeyPairSm2( + pub Keypair<[u8; 32], Sha3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner>, +); + +impl crate::prelude::Keypair for KeyPairSm2 { + type Seed = [u8; 32]; + + type Secret = Scalar; + + type Public = Point; + + type Code = [u8; 32]; + + type Signature = signature::sm2::Signature; + + fn generate(rng: &mut R) -> Result { + match Keypair::generate::(rng) { + Ok(x) => Ok(Self(x)), + Err(_) => Err(CryptoError::KeyPairGenError), + } + } + + fn generate_from_seed(seed: Self::Seed) -> Result { + match Keypair::generate_from_seed(seed) { + Ok(x) => Ok(Self(x)), + Err(_) => Err(CryptoError::KeyPairGenError), + } + } + + fn sign + Hasher, R: RngCore>( + &self, + msg: &[u8], + rng: &mut R, + ) -> Result { + let mut hasher = H::default(); + hasher.update(msg); + signature::sm2::sm2_signature::<_, H, _, _, R>(hasher, &self.0.get_secret_key(), rng) + } + + fn verify + Hasher>( + &self, + msg: &[u8], + sig: &Self::Signature, + ) -> Result { + let mut hasher = H::default(); + hasher.update(msg); + Ok(signature::sm2::sm2_verify::<_, H, _, _>( + hasher, + &self.0.get_public_key(), + sig, + )) + } +} diff --git a/src/lib.rs b/src/lib.rs index dd92dec..61b1895 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,47 @@ -mod key_pair_gen; -pub use key_pair_gen::KeyPair; +pub mod hasher; +pub mod keypair; +pub mod signature; -mod prelude; +pub mod prelude; pub use prelude::Splitable; -mod sigture; -pub use sigture::{sm2_signature, sm2_verify}; +use core::fmt::Debug; +use hex::{FromHex, FromHexError}; -mod hasher; -pub use hasher::{Sha3, Sm3}; +pub struct NewU864(pub [u8; 64]); + +impl Debug for NewU864 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + let mut info = f.debug_list(); + for i in 0..self.0.len() { + info.entry(&self.0[i]); + } + info.finish() + } +} + +impl PartialEq for NewU864 { + fn eq(&self, other: &Self) -> bool { + self.0[..] == other.0[..] + } +} + +impl FromHex for NewU864 { + type Error = FromHexError; + + fn from_hex>(hex: T) -> Result { + match <[u8; 64]>::from_hex(hex) { + Ok(x) => Ok(Self(x)), + Err(err) => Err(err), + } + } +} + +impl AsRef<[u8]> for NewU864 { + fn as_ref(&self) -> &[u8] { + &self.0 + } +} #[derive(Debug)] pub enum CryptoError { diff --git a/src/prelude.rs b/src/prelude.rs index e795b96..0257ef1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,8 +1,40 @@ +use crate::CryptoError; use core::fmt::Debug; +use dislog_hal::{Bytes, Hasher}; use hex::{FromHex, ToHex}; +use rand::RngCore; +use serde::{Deserialize, Serialize}; pub trait Splitable { type Half: Debug + ToHex + FromHex + PartialEq; fn split_finalize(self) -> (Self::Half, Self::Half); } + +pub trait Keypair: Serialize + for<'de> Deserialize<'de> { + type Seed; + + type Secret; + + type Public; + + type Code; + + type Signature: Serialize + for<'de> Deserialize<'de> + Bytes; + + fn generate(rng: &mut R) -> Result; + + fn generate_from_seed(seed: Self::Seed) -> Result; + + fn sign + Hasher, R: RngCore>( + &self, + msg: &[u8], + rng: &mut R, + ) -> Result; + + fn verify + Hasher>( + &self, + msg: &[u8], + sig: &Self::Signature, + ) -> Result; +} diff --git a/src/signature/ecdsa.rs b/src/signature/ecdsa.rs new file mode 100644 index 0000000..ce3c04c --- /dev/null +++ b/src/signature/ecdsa.rs @@ -0,0 +1,135 @@ +use crate::CryptoError; +use core::fmt::Debug; +use dislog_hal::{Bytes, DisLogPoint, Hasher, Point, Scalar, ScalarNumber}; +use hex::{FromHex, ToHex}; +use rand::RngCore; + +#[derive(Debug, Clone)] +pub struct Signature { + r: Scalar, + s: Scalar, +} + +impl Signature { + pub fn get_r(&self) -> Scalar { + self.r.clone() + } + + pub fn get_s(&self) -> Scalar { + self.s.clone() + } +} + +pub fn ecdsa_signature< + N: Default + AsRef<[u8]> + Debug + ToHex + FromHex + PartialEq + Clone, + H: Hasher + Default, + P: DisLogPoint + Bytes, + S: ScalarNumber + Bytes, + R: RngCore, +>( + hasher: H, + pri_key: &Scalar, + rng: &mut R, +) -> Result, CryptoError> { + let digest = hasher.finalize(); + + // 实际上Scalar::转换不可能抛出错误 + let e = Scalar::::from_bytes(digest.as_ref()).unwrap(); + + loop { + let k = Scalar::::random(rng); + + if k == Scalar::::zero() { + continue; + } + + let r = (Point::

::one() * &k).get_x(); + + let mut s = &e + &r * pri_key; + + if r == Scalar::::zero() || s == Scalar::zero() { + continue; + } + + s = k.inv() * s; + + return Ok(Signature:: { r, s }); + } +} + +pub fn ecdsa_verify< + N: Default + AsRef<[u8]> + Debug + ToHex + FromHex + PartialEq + Clone, + H: Hasher + Default, + P: DisLogPoint + Bytes, + S: ScalarNumber + Bytes, +>( + hasher: H, + pub_key: &Point

, + sig: &Signature, +) -> bool { + let digest = hasher.finalize(); + + // 实际上Scalar::转换不可能抛出错误 + let e = Scalar::::from_bytes(digest.as_ref()).unwrap(); + + let r = sig.get_r(); + let s = sig.get_s(); + + let p = s.inv() * e * Point::

::one() + s.inv() * &r * pub_key; + + // check R == r? + r == p.get_x() +} + +#[cfg(test)] +mod tests { + use super::{ecdsa_signature, ecdsa_verify}; + use crate::hasher::{sha3::Sha3, sm3::Sm3}; + use crate::keypair::Keypair; + use dislog_hal::Hasher; + + #[test] + fn it_works() { + let data_b = [ + 34, 65, 213, 57, 9, 244, 187, 83, 43, 5, 198, 33, 107, 223, 3, 114, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, + ]; + let info_b = Keypair::< + [u8; 32], + Sha3, + dislog_hal_sm2::PointInner, + dislog_hal_sm2::ScalarInner, + >::generate_from_seed(data_b) + .unwrap(); + + let text = [244, 187, 83, 43, 5, 198, 33]; + let mut rng = rand::thread_rng(); + + let mut hasher_2 = Sm3::default(); + hasher_2.update(&text[..]); + let sig_info = ecdsa_signature::< + [u8; 32], + Sm3, + dislog_hal_sm2::PointInner, + dislog_hal_sm2::ScalarInner, + _, + >(hasher_2, &info_b.get_secret_key(), &mut rng) + .unwrap(); + + println!("sigture: {:?}", sig_info); + + let mut hasher_2 = Sm3::default(); + hasher_2.update(&text[..]); + let ans = ecdsa_verify(hasher_2, &info_b.get_public_key(), &sig_info); + assert_eq!(ans, true); + + let mut msg_wrapper_err = [0u8; 7]; + msg_wrapper_err.copy_from_slice(&text[..]); + msg_wrapper_err[0] += 1; + + let mut hasher_2 = Sm3::default(); + hasher_2.update(&msg_wrapper_err[..]); + let ans = ecdsa_verify(hasher_2, &info_b.get_public_key(), &sig_info); + assert_eq!(ans, false); + } +} diff --git a/src/signature/mod.rs b/src/signature/mod.rs new file mode 100644 index 0000000..fc45fa8 --- /dev/null +++ b/src/signature/mod.rs @@ -0,0 +1,2 @@ +pub mod ecdsa; +pub mod sm2; diff --git a/src/sigture.rs b/src/signature/sm2.rs similarity index 61% rename from src/sigture.rs rename to src/signature/sm2.rs index a4c9181..7ad18f1 100644 --- a/src/sigture.rs +++ b/src/signature/sm2.rs @@ -1,15 +1,41 @@ use crate::CryptoError; +use crate::NewU864; +use core::convert::AsRef; use core::fmt::Debug; use dislog_hal::{Bytes, DisLogPoint, Hasher, Point, Scalar, ScalarNumber}; use hex::{FromHex, ToHex}; use rand::RngCore; +use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Signature { + #[serde(bound(deserialize = "S: ScalarNumber"))] r: Scalar, + #[serde(bound(deserialize = "S: ScalarNumber"))] s: Scalar, } +impl Bytes for Signature { + type BytesType = NewU864; + + type Error = CryptoError; + + fn from_bytes(bytes: &[u8]) -> Result { + assert_eq!(bytes.len(), 64); + Ok(Self { + r: Scalar::::from_bytes(&bytes[..32]).unwrap(), + s: Scalar::::from_bytes(&bytes[32..]).unwrap(), + }) + } + + fn to_bytes(&self) -> Self::BytesType { + let mut ret = [0u8; 64]; + ret[..32].clone_from_slice(self.r.to_bytes().as_ref()); + ret[32..].clone_from_slice(self.s.to_bytes().as_ref()); + NewU864(ret) + } +} + impl Signature { pub fn get_r(&self) -> Scalar { self.r.clone() @@ -21,21 +47,19 @@ impl Signature { } pub fn sm2_signature< - N: Default + AsRef<[u8]> + AsMut<[u8]> + Sized + Debug + ToHex + FromHex + PartialEq + Clone, - H2: Hasher + Default, + N: Default + AsRef<[u8]> + Debug + ToHex + FromHex + PartialEq + Clone, + H: Hasher + Default, P: DisLogPoint + Bytes, S: ScalarNumber + Bytes, R: RngCore, >( - msg_wrapper: &[u8], + hasher: H, pri_key: &Scalar, rng: &mut R, ) -> Result, CryptoError> { - let mut hasher = H2::default(); - hasher.update(msg_wrapper); let digest = hasher.finalize(); - let e = Scalar::::from_bytes(digest).unwrap(); + let e = Scalar::::from_bytes(digest.as_ref()).unwrap(); loop { // k = rand() @@ -67,20 +91,18 @@ pub fn sm2_signature< } pub fn sm2_verify< - N: Default + AsRef<[u8]> + AsMut<[u8]> + Sized + Debug + ToHex + FromHex + PartialEq + Clone, - H2: Hasher + Default, + N: Default + AsRef<[u8]> + Debug + ToHex + FromHex + PartialEq + Clone, + H: Hasher + Default, P: DisLogPoint + Bytes, S: ScalarNumber + Bytes, >( - msg_wrapper: &[u8], + hasher: H, pub_key: &Point

, sig: &Signature, ) -> bool { - let mut hasher = H2::default(); - hasher.update(msg_wrapper); let digest = hasher.finalize(); - let e = Scalar::::from_bytes(digest).unwrap(); + let e = Scalar::::from_bytes(digest.as_ref()).unwrap(); let s = sig.get_s(); let r = sig.get_r(); diff --git a/tests/test.rs b/tests/test.rs index 1641e44..138650e 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,4 +1,6 @@ -use asymmetric_crypto::{sm2_signature, sm2_verify, KeyPair, Sha3, Sm3}; +use asymmetric_crypto::hasher::{sha3::Sha3, sm3::Sm3}; +use asymmetric_crypto::keypair::{sm2::KeyPairSm2, Keypair}; +use asymmetric_crypto::signature::sm2::{sm2_signature, sm2_verify}; use byteorder::{BigEndian, WriteBytesExt}; use core::convert::AsRef; use dislog_hal::{Bytes, Hasher, Point}; @@ -9,7 +11,7 @@ use rand::thread_rng; fn test_key_pair_curve25519_gen() { let mut rng = thread_rng(); - let info_a = KeyPair::< + let info_a = Keypair::< [u8; 32], Sha3, dislog_hal_curve25519::PointInner, @@ -23,7 +25,7 @@ fn test_key_pair_curve25519_gen() { 187, 106, 9, 139, 107, 13, 195, 224, 202, 130, 3, 243, 167, 193, 182, 87, 81, 183, 243, 81, 74, 222, 16, 87, 21, 206, 127, 54, 32, 51, 18, 110, ]; - let info_b = KeyPair::< + let info_b = Keypair::< [u8; 32], Sha3, dislog_hal_curve25519::PointInner, @@ -39,14 +41,14 @@ fn test_key_pair_curve25519_gen() { ] ); assert_eq!( - info_b.get_secret_key().inner.to_bytes(), + info_b.get_secret_key().to_bytes(), [ 87, 7, 77, 176, 244, 182, 94, 31, 180, 131, 71, 165, 24, 196, 136, 15, 252, 125, 185, 230, 56, 228, 42, 161, 117, 43, 81, 248, 50, 5, 246, 13 ] ); assert_eq!( - info_b.get_public_key().inner.to_bytes(), + info_b.get_public_key().to_bytes(), [ 46, 170, 200, 38, 199, 246, 214, 187, 69, 5, 152, 75, 233, 6, 232, 150, 174, 190, 32, 251, 147, 169, 7, 163, 11, 84, 164, 36, 35, 57, 2, 96 @@ -65,7 +67,7 @@ fn test_key_pair_curve25519_gen() { fn test_key_pair_sm2_gen() { let mut rng = thread_rng(); - let info_a = KeyPair::< + let info_a = Keypair::< [u8; 32], Sha3, dislog_hal_sm2::PointInner, @@ -79,7 +81,7 @@ fn test_key_pair_sm2_gen() { 34, 65, 213, 57, 9, 244, 187, 83, 43, 5, 198, 33, 107, 223, 3, 114, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, ]; - let info_b = KeyPair::< + let info_b = Keypair::< [u8; 32], Sha3, dislog_hal_sm2::PointInner, @@ -95,14 +97,14 @@ fn test_key_pair_sm2_gen() { ] ); assert_eq!( - info_b.get_secret_key().inner.to_bytes().as_ref(), + info_b.get_secret_key().to_bytes().as_ref(), &[ 100, 228, 238, 48, 82, 171, 142, 44, 136, 11, 25, 200, 143, 219, 38, 151, 240, 198, 203, 172, 209, 197, 254, 44, 122, 177, 156, 57, 38, 227, 43, 111 ][..] ); assert_eq!( - info_b.get_public_key().inner.to_bytes().as_ref(), + info_b.get_public_key().to_bytes().as_ref(), &[ 3, 31, 15, 213, 251, 207, 39, 245, 108, 63, 234, 202, 80, 139, 13, 202, 236, 135, 128, 216, 113, 219, 223, 148, 108, 142, 131, 166, 167, 255, 152, 114, 125 @@ -124,7 +126,7 @@ fn test_sm2_sigture() { 34, 65, 213, 57, 9, 244, 187, 83, 43, 5, 198, 33, 107, 223, 3, 114, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, ]; - let info_b = KeyPair::< + let info_b = Keypair::< [u8; 32], Sha3, dislog_hal_sm2::PointInner, @@ -134,19 +136,23 @@ fn test_sm2_sigture() { let text = [244, 187, 83, 43, 5, 198, 33]; + let mut hasher_1: Sm3 = Sm3::default(); + hasher_1.update(&text[..]); let sig_info = sm2_signature::< [u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner, ThreadRng, - >(&text[..], &info_b.get_secret_key(), &mut rng) + >(hasher_1, &info_b.get_secret_key(), &mut rng) .unwrap(); println!("sigture: {:?}", sig_info); + let mut hasher_2: Sm3 = Sm3::default(); + hasher_2.update(&text[..]); let ans = sm2_verify::<[u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner>( - &text[..], + hasher_2, &info_b.get_public_key(), &sig_info, ); @@ -156,8 +162,10 @@ fn test_sm2_sigture() { text_err.copy_from_slice(&Vec::from(&text[..])[..]); text_err[0] += 1; + let mut hasher_2: Sm3 = Sm3::default(); + hasher_2.update(&text_err[..]); let ans = sm2_verify::<[u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner>( - &text_err[..], + hasher_2, &info_b.get_public_key(), &sig_info, ); @@ -219,7 +227,7 @@ fn test_compat_libsm_sigture() { 34, 65, 213, 57, 9, 244, 187, 83, 43, 5, 198, 33, 107, 223, 3, 114, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, ]; - let info_b = KeyPair::< + let info_b = Keypair::< [u8; 32], Sha3, dislog_hal_sm2::PointInner, @@ -231,19 +239,23 @@ fn test_compat_libsm_sigture() { let msg_wrapper = compat_libsm_hash(&text, &info_b.get_public_key()); + let mut hasher_2: Sm3 = Sm3::default(); + hasher_2.update(&msg_wrapper[..]); let sig_info = sm2_signature::< [u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner, ThreadRng, - >(&msg_wrapper[..], &info_b.get_secret_key(), &mut rng) + >(hasher_2, &info_b.get_secret_key(), &mut rng) .unwrap(); println!("sigture: {:?}", sig_info); + let mut hasher_2: Sm3 = Sm3::default(); + hasher_2.update(&msg_wrapper[..]); let ans = sm2_verify::<[u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner>( - &msg_wrapper[..], + hasher_2, &info_b.get_public_key(), &sig_info, ); @@ -253,10 +265,34 @@ fn test_compat_libsm_sigture() { msg_wrapper_err.copy_from_slice(&Vec::from(&msg_wrapper[..])[..]); msg_wrapper_err[0] += 1; + let mut hasher_2: Sm3 = Sm3::default(); + hasher_2.update(&msg_wrapper_err[..]); let ans = sm2_verify::<[u8; 32], Sm3, dislog_hal_sm2::PointInner, dislog_hal_sm2::ScalarInner>( - &msg_wrapper_err[..], + hasher_2, &info_b.get_public_key(), &sig_info, ); assert_eq!(ans, false); } + +#[test] +fn test_sm2_trait() { + use asymmetric_crypto::prelude::Keypair; + + let data_b = [ + 34, 65, 213, 57, 9, 244, 187, 83, 43, 5, 198, 33, 107, 223, 3, 114, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, + ]; + + let mut rng = thread_rng(); + let keypair_sm2: KeyPairSm2 = KeyPairSm2::generate(&mut rng).unwrap(); + + let sig_info = keypair_sm2 + .sign::(&data_b[..], &mut thread_rng()) + .unwrap(); + + println!("sigture: {:?}", sig_info.to_bytes()); + + let ans = keypair_sm2.verify::(&data_b[..], &sig_info).unwrap(); + assert_eq!(ans, true); +}