forked from Yinet-project/asymmetric-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add trait for Keypair, and adjust project struct
- Loading branch information
Showing
12 changed files
with
260 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
pub mod sha3; | ||
pub use sha3::Sha3; | ||
|
||
pub mod sm3; | ||
pub use sm3::Sm3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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<dislog_hal_sm2::ScalarInner>; | ||
|
||
type Public = Point<dislog_hal_sm2::PointInner>; | ||
|
||
type Code = [u8; 32]; | ||
|
||
type Signature = signature::sm2::Signature<dislog_hal_sm2::ScalarInner>; | ||
|
||
fn generate<R: RngCore>(rng: &mut R) -> Result<Self, CryptoError> { | ||
match Keypair::generate::<R>(rng) { | ||
Ok(x) => Ok(Self(x)), | ||
Err(_) => Err(CryptoError::KeyPairGenError), | ||
} | ||
} | ||
|
||
fn generate_from_seed(seed: Self::Seed) -> Result<Self, CryptoError> { | ||
match Keypair::generate_from_seed(seed) { | ||
Ok(x) => Ok(Self(x)), | ||
Err(_) => Err(CryptoError::KeyPairGenError), | ||
} | ||
} | ||
|
||
fn sign<H: Default + Hasher<Output = [u8; 32]> + Hasher, R: RngCore>( | ||
&self, | ||
msg: &[u8], | ||
rng: &mut R, | ||
) -> Result<Self::Signature, CryptoError> { | ||
let mut hasher = H::default(); | ||
hasher.update(msg); | ||
signature::sm2::sm2_signature::<_, H, _, _, R>(hasher, &self.0.get_secret_key(), rng) | ||
} | ||
|
||
fn verify<H: Default + Hasher<Output = [u8; 32]> + Hasher>( | ||
&self, | ||
msg: &[u8], | ||
sig: &Self::Signature, | ||
) -> Result<bool, CryptoError> { | ||
Ok(signature::sm2::sm2_verify::<_, H, _, _>( | ||
msg, | ||
&self.0.get_public_key(), | ||
sig, | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<R: RngCore>(rng: &mut R) -> Result<Self, CryptoError>; | ||
|
||
fn generate_from_seed(seed: Self::Seed) -> Result<Self, CryptoError>; | ||
|
||
fn sign<H: Default + Hasher<Output = [u8; 32]> + Hasher, R: RngCore>( | ||
&self, | ||
msg: &[u8], | ||
rng: &mut R, | ||
) -> Result<Self::Signature, CryptoError>; | ||
|
||
fn verify<H: Default + Hasher<Output = [u8; 32]> + Hasher>( | ||
&self, | ||
msg: &[u8], | ||
sig: &Self::Signature, | ||
) -> Result<bool, CryptoError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod sm2; |
Oops, something went wrong.