-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
273 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use ed25519_dalek::{SigningKey, VerifyingKey}; | ||
use rand::rngs::OsRng; | ||
use rsa::signature::SignerMut; | ||
|
||
use crate::CryptoError; | ||
|
||
pub fn generate_ed25519_keypair() -> Result<(Vec<u8>, Vec<u8>), CryptoError> { | ||
let secret = SigningKey::generate(&mut OsRng); | ||
let public = VerifyingKey::from(&secret); | ||
let secret_bytes: Vec<u8> = secret.to_bytes().to_vec(); | ||
let public_bytes = public.as_bytes().to_vec(); | ||
Ok((secret_bytes.to_vec(), public_bytes)) | ||
} | ||
|
||
pub fn sign(data: Vec<u8>, secret: Vec<u8>) -> Result<Vec<u8>, CryptoError> { | ||
let secret_fixed: [u8; 32] = secret.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let mut secret = SigningKey::from_bytes(&secret_fixed); | ||
let data_fixed: &[u8] = data.as_slice(); | ||
let signature = secret.sign(data_fixed); | ||
Ok(signature.to_bytes().to_vec()) | ||
} | ||
|
||
pub fn verify(data: Vec<u8>, signature: Vec<u8>, public: Vec<u8>) -> Result<bool, CryptoError> { | ||
let public_fixed: [u8; 32] = public.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let public = VerifyingKey::from_bytes(&public_fixed).map_err(|_| CryptoError::InvalidKey)?; | ||
let data_fixed: &[u8] = data.as_slice(); | ||
let signature_fixed: [u8; 64] = signature.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let signature = ed25519_dalek::Signature::from_bytes(&signature_fixed); | ||
let res = public.verify_strict(data_fixed, &signature).map_err(|_| CryptoError::InvalidKey); | ||
Ok(res.is_ok()) | ||
} | ||
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,20 @@ | ||
use x25519_dalek::{EphemeralSecret, PublicKey, StaticSecret}; | ||
use rand::rngs::OsRng; | ||
|
||
use crate::CryptoError; | ||
|
||
fn generate_x25519_keypair() -> Result<(Vec<u8>, Vec<u8>), CryptoError> { | ||
let secret = StaticSecret::new(OsRng); | ||
let public = PublicKey::from(&secret); | ||
let secret_bytes: Vec<u8> = secret.to_bytes().to_vec(); | ||
let public_bytes = public.as_bytes().to_vec(); | ||
Ok((secret_bytes.to_vec(), public_bytes)) | ||
} | ||
|
||
fn derive_shared(pubkey: Vec<u8>) -> Result<Vec<u8>, CryptoError> { | ||
let pubkey_fixed: [u8; 32] = pubkey.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let public = PublicKey::from(pubkey_fixed); | ||
let secret = EphemeralSecret::new(OsRng); | ||
let shared_secret = secret.diffie_hellman(&public); | ||
Ok(shared_secret.as_bytes().to_vec()) | ||
} | ||
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,30 @@ | ||
use std::io::Read; | ||
|
||
use ml_kem::kem::Encapsulate; | ||
use ml_kem::kem::Decapsulate; | ||
use crate::{CryptoError}; | ||
|
||
pub fn generate_keypair() -> Result<(Vec<u8>, Vec<u8>), CryptoError> { | ||
let mut rng = rand::thread_rng(); | ||
let (sk, pk) = x_wing::generate_key_pair(&mut rng); | ||
let sk_bytes = sk.as_bytes(); | ||
let pk_bytes = pk.as_bytes(); | ||
Ok((sk_bytes.to_vec(), pk_bytes.to_vec())) | ||
} | ||
|
||
pub fn encapsulate(pk: &[u8]) -> Result<(Vec<u8>, Vec<u8>), CryptoError> { | ||
let pk_fixed_slize: &[u8; 1216] = pk.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let mut rng = rand::thread_rng(); | ||
let pk = x_wing::EncapsulationKey::from(pk_fixed_slize); | ||
let (ct, ss_sender) = pk.encapsulate(&mut rng).unwrap(); | ||
Ok((ct.as_bytes().to_vec(), ss_sender.to_vec())) | ||
} | ||
|
||
pub fn decapsulate(sk: &[u8], ct: &[u8]) -> Result<Vec<u8>, CryptoError> { | ||
let sk_fixed_slize: [u8; 32] = sk.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let ct_fixed_slize: &[u8; 1120] = ct.try_into().map_err(|_| CryptoError::InvalidKey)?; | ||
let sk = x_wing::DecapsulationKey::from(sk_fixed_slize); | ||
let ct = x_wing::Ciphertext::from(ct_fixed_slize); | ||
let ss_receiver = sk.decapsulate(&ct).unwrap(); | ||
Ok(ss_receiver.to_vec()) | ||
} | ||
Oops, something went wrong.