-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…5519-dalek #16 update ed25519 dalek
- Loading branch information
Showing
8 changed files
with
191 additions
and
94 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
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,61 +1,61 @@ | ||
use std::sync::mpsc; | ||
|
||
use ed25519_dalek::{Keypair, Signature, Signer, Verifier}; | ||
use sha3::{Digest, Sha3_256}; | ||
|
||
use crate::signatures::ed25519::{ed25519_sign_with_key_pair, ed25519_verify_with_public_key, get_ed25519_key_pair}; | ||
|
||
use super::cas_digital_signature_rsa::{ | ||
ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult, | ||
}; | ||
|
||
pub struct SHA256ED25519DigitalSignature; | ||
|
||
impl ED25519DigitalSignature for SHA256ED25519DigitalSignature { | ||
fn digital_signature_ed25519(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult { | ||
fn digital_signature_ed25519(data_to_sign: &[u8]) -> SHAED25519DalekDigitalSignatureResult { | ||
let mut hasher = Sha3_256::new(); | ||
hasher.update(data_to_sign); | ||
let sha_hasher_result = hasher.finalize(); | ||
let mut csprng = rand_07::rngs::OsRng {}; | ||
let keypair = ed25519_dalek::Keypair::generate(&mut csprng); | ||
|
||
let signature = keypair.sign(&sha_hasher_result); | ||
let signature_bytes = signature.to_bytes(); | ||
let public_keypair_bytes = keypair.public.to_bytes(); | ||
let sha_hash_bytes = sha_hasher_result.as_slice(); | ||
let key_pair = get_ed25519_key_pair(); | ||
let signature = ed25519_sign_with_key_pair(key_pair, sha_hash_bytes); | ||
let result = SHAED25519DalekDigitalSignatureResult { | ||
public_key: public_keypair_bytes.to_vec(), | ||
signature: signature_bytes.to_vec(), | ||
public_key: signature.public_key, | ||
signature: signature.signature, | ||
}; | ||
result | ||
} | ||
|
||
fn digital_signature_ed25519_verify(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool { | ||
fn digital_signature_ed25519_verify( | ||
public_key: [u8; 32], | ||
data_to_verify: &[u8], | ||
signature: [u8; 64] | ||
) -> bool { | ||
let mut hasher = Sha3_256::new(); | ||
hasher.update(data_to_verify); | ||
let sha_hasher_result = hasher.finalize(); | ||
|
||
let public_key_parsed = ed25519_dalek::PublicKey::from_bytes(&public_key).unwrap(); | ||
let signature_parsed = Signature::from_bytes(&signature).unwrap(); | ||
return public_key_parsed | ||
.verify(&sha_hasher_result, &signature_parsed) | ||
.is_ok(); | ||
let sha_hash_bytes = sha_hasher_result.as_slice(); | ||
return ed25519_verify_with_public_key(public_key, signature, sha_hash_bytes); | ||
} | ||
|
||
fn digital_signature_ed25519_threadpool(data_to_sign: Vec<u8>) -> SHAED25519DalekDigitalSignatureResult { | ||
fn digital_signature_ed25519_threadpool(data_to_sign: &[u8]) -> SHAED25519DalekDigitalSignatureResult { | ||
let (sender, receiver) = mpsc::channel(); | ||
let data_clone = data_to_sign.to_vec(); | ||
rayon::spawn(move || { | ||
let result = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign); | ||
let result = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(&data_clone); | ||
sender.send(result); | ||
}); | ||
let result = receiver.recv().unwrap(); | ||
result | ||
} | ||
|
||
fn digital_signature_ed25519_verify_threadpool(public_key: Vec<u8>, data_to_verify: Vec<u8>, signature: Vec<u8>) -> bool { | ||
fn digital_signature_ed25519_verify_threadpool(public_key: [u8; 32], data_to_verify: &[u8], signature: [u8; 64]) -> bool { | ||
let (sender, receiver) = mpsc::channel(); | ||
let data_to_verify_clone = data_to_verify.to_vec(); | ||
rayon::spawn(move || { | ||
let result = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(public_key, data_to_verify, signature); | ||
let result = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(public_key, &data_to_verify_clone, signature); | ||
sender.send(result); | ||
}); | ||
let result = receiver.recv().unwrap(); | ||
result | ||
} | ||
} | ||
} |
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,4 +1,4 @@ | ||
pub struct Ed25519ByteSignature{ | ||
pub signature: Vec<u8>, | ||
pub public_key: Vec<u8> | ||
pub signature: [u8; 64], | ||
pub public_key: [u8; 32] | ||
} |
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,36 @@ | ||
#[cfg(test)] | ||
mod digital_signatures { | ||
use cas_lib::digital_signature::{cas_digital_signature_rsa::{ED25519DigitalSignature, SHAED25519DalekDigitalSignatureResult}, sha_256_ed25519::SHA256ED25519DigitalSignature, sha_256_rsa::SHA256RSADigitalSignature, sha_512_ed25519::SHA512ED25519DigitalSignature}; | ||
|
||
#[test] | ||
pub fn ed25519_sha_512_digital_signature_verify() { | ||
let data_to_sign = b"This is a test of a digital signature"; | ||
let result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign); | ||
let verification = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(result.public_key, data_to_sign, result.signature); | ||
assert_eq!(true, verification); | ||
} | ||
|
||
#[test] | ||
pub fn ed25519_sha_512_digital_signature_threadpool_verify() { | ||
let data_to_sign = b"This is a test of a digital signature"; | ||
let result: SHAED25519DalekDigitalSignatureResult = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_threadpool(data_to_sign); | ||
let verification = <SHA512ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify_threadpool(result.public_key, data_to_sign, result.signature); | ||
assert_eq!(true, verification); | ||
} | ||
|
||
#[test] | ||
pub fn ed25519_sha_256_digital_signature_verify() { | ||
let data_to_sign = b"This is a test of a digital signature"; | ||
let result: SHAED25519DalekDigitalSignatureResult = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519(data_to_sign); | ||
let verification = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify(result.public_key, data_to_sign, result.signature); | ||
assert_eq!(true, verification); | ||
} | ||
|
||
#[test] | ||
pub fn ed25519_sha_256_digital_signature_threadpool_verify() { | ||
let data_to_sign = b"This is a test of a digital signature"; | ||
let result: SHAED25519DalekDigitalSignatureResult = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_threadpool(data_to_sign); | ||
let verification = <SHA256ED25519DigitalSignature as ED25519DigitalSignature>::digital_signature_ed25519_verify_threadpool(result.public_key, data_to_sign, result.signature); | ||
assert_eq!(true, verification); | ||
} | ||
} |
Oops, something went wrong.