Skip to content

Commit

Permalink
o5 base
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Dec 7, 2023
1 parent f1807a0 commit 56846e7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
Cargo.lock
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ pkg-config = "0.3.16"

[dev-dependencies]
tracing-subscriber = "0.3.18"

# o5 pqc test
# rand = "^0.8.5"
# x25519-dalek = {version="2.0.0", features=["getrandom"]}
pqc_kyber = {version="0.7.1", features=["kyber1024", "std"]}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(warnings)]

pub mod ident;
// pub mod o5;
pub mod o5;
// pub mod o7;
pub mod obfs4;

Expand Down
66 changes: 66 additions & 0 deletions src/o5/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

#[derive(Debug)]
enum Error {
PQCError(pqc_kyber::KyberError),
Other(Box<dyn std::error::Error>),
}

impl From<pqc_kyber::KyberError> for Error {
fn from(e: pqc_kyber::KyberError) -> Self {
return Error::PQCError(e);
}
}

// impl From<&dyn std::error::Error> for Error {
// fn from(e: &dyn std::error::Error) -> Self {
// Error::Other(Box::new(e))
// }
// }

struct Kyber1024XKeypair {}

impl Kyber1024XKeypair {
fn new() -> Result<Self> {
todo!()
}
}

type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
use super::*;
use pqc_kyber::*;
use x25519_dalek::{EphemeralSecret, PublicKey};

#[test]
fn it_works() -> Result<()> {
let mut rng = rand::thread_rng();

// Generate Keypair
let alice_secret = EphemeralSecret::random_from_rng(&mut rng);
let alice_public = PublicKey::from(&alice_secret);
let keys_alice = keypair(&mut rng)?;
// alice -> bob public keys
let mut kyber1024x_pubkey = alice_public.as_bytes().to_vec();
kyber1024x_pubkey.extend_from_slice(&keys_alice.public);

assert_eq!(kyber1024x_pubkey.len(), 1600);

let bob_secret = EphemeralSecret::random_from_rng(&mut rng);
let bob_public = PublicKey::from(&bob_secret);

// Bob encapsulates a shared secret using Alice's public key
let (ciphertext, shared_secret_bob) = encapsulate(&keys_alice.public, &mut rng)?;
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

// // Alice decapsulates a shared secret using the ciphertext sent by Bob
let shared_secret_alice = decapsulate(&ciphertext, &keys_alice.secret)?;
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);

assert_eq!(alice_shared_secret.as_bytes(), bob_shared_secret.as_bytes());
assert_eq!(shared_secret_bob, shared_secret_alice);

Ok(())
}
}

0 comments on commit 56846e7

Please sign in to comment.