Skip to content

Commit

Permalink
progress commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Jun 12, 2024
1 parent 7627be5 commit 358d764
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/obfs4/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hmac::Hmac;
use sha2::Sha256;

pub(crate) mod ct;
pub(crate) mod curve25519;
pub(crate) mod x25519_elligator2;
pub(crate) mod kdf;

mod skip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pub use x25519_dalek::{PublicKey, SharedSecret, StaticSecret};
pub use curve25519_elligator2::elligator2::representative_from_privkey;

pub(crate) struct EphemeralSecret (x25519_dalek::EphemeralSecret, u8);
pub(crate) struct EphemeralSecret (x25519_dalek::StaticSecret, u8);

impl EphemeralSecret {
pub(crate) fn random() -> Self {}
Expand Down Expand Up @@ -117,7 +117,7 @@ impl<'a> From<&'a [u8; 32]> for PublicRepresentative {
impl<'a> From<&'a EphemeralSecret> for Option<PublicRepresentative> {
/// Given an x25519 [`EphemeralSecret`] key, compute its corresponding [`PublicRepresentative`].
fn from(secret: &'a EphemeralSecret) -> Option<PublicRepresentative> {
let repres = representative_from_privkey(&secret.0, secret.1);
let repres = representative_from_privkey(&secret.0.to_bytes(), secret.1);
let res: Option<[u8; 32]> = repres;
Some(PublicRepresentative(res?))
}
Expand Down Expand Up @@ -187,7 +187,12 @@ impl Keys {
/// Generate a new Elligator2 representable ['StaticSecret'].
pub fn random_static() -> StaticSecret {
let mut private = StaticSecret::random();
let mut repres: Option<PublicRepresentative> = (&private).into();
let mut tweak = [0u8];
getrandom::getrandom(&mut tweak).expect("failed to get random bytes");

let mut key = EphemeralSecret(private, tweak[0]);

let mut repres: Option<PublicRepresentative> = &key.into();

for _ in 0..Self::RETRY_LIMIT {
if repres.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion crates/obfs4/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use tor_llcrypto::pk::rsa::RSA_ID_LEN;

use crate::{
common::{curve25519::REPRESENTATIVE_LENGTH, drbg},
common::{x25519_elligator2::REPRESENTATIVE_LENGTH, drbg},
framing,
handshake::AUTHCODE_LENGTH,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/obfs4/src/framing/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
common::{
curve25519::{PublicKey, PublicRepresentative},
x25519_elligator2::{PublicKey, PublicRepresentative},
HmacSha256,
},
constants::*,
Expand Down
6 changes: 3 additions & 3 deletions crates/obfs4/src/framing/messages_v1/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Message for KyberAcceptMessage {
mod tests {
use pqc_kyber::*;

use crate::common::curve25519::{PublicKey, Representable};
use crate::common::x25519_elligator2::{PublicKey, Keys};
use crate::handshake::Obfs4NtorSecretKey;

type Result<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -124,7 +124,7 @@ mod tests {
let mut rng = rand::thread_rng();

// Generate Keypair
let alice_secret = Representable::ephemeral_from_rng(&mut rng);
let alice_secret = Keys::ephemeral_from_rng(&mut rng);
let alice_public = PublicKey::from(&alice_secret);
let keys_alice = keypair(&mut rng)?;
// alice -> bob public keys
Expand All @@ -133,7 +133,7 @@ mod tests {

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

let bob_secret = Representable::ephemeral_from_rng(&mut rng);
let bob_secret = Keys::ephemeral_from_rng(&mut rng);
let bob_public = PublicKey::from(&bob_secret);

// Bob encapsulates a shared secret using Alice's public key
Expand Down
4 changes: 2 additions & 2 deletions crates/obfs4/src/handshake/handshake_client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
common::{
curve25519::{PublicRepresentative, REPRESENTATIVE_LENGTH},
x25519_elligator2::{PublicRepresentative, REPRESENTATIVE_LENGTH},
HmacSha256,
},
framing::handshake::{ClientHandshakeMessage, ServerHandshakeMessage},
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(super) fn client_handshake_obfs4(
materials: &HandshakeMaterials,
) -> Result<(NtorHandshakeState, Vec<u8>)> {
let rng = rand::thread_rng();
let my_sk = Representable::static_from_rng(rng);
let my_sk = Keys::static_from_rng(rng);
client_handshake_obfs4_no_keygen(my_sk, materials.clone())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/obfs4/src/handshake/handshake_server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{
common::{
curve25519::{PublicRepresentative, REPRESENTATIVE_LENGTH},
x25519_elligator2::{PublicRepresentative, REPRESENTATIVE_LENGTH},
HmacSha256,
},
framing::{build_and_marshall, ClientHandshakeMessage, MessageTypes, ServerHandshakeMessage},
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Server {
T: AsRef<[u8]>,
{
let rng = thread_rng();
let session_sk = Representable::ephemeral_from_rng(rng);
let session_sk = Keys::ephemeral_from_rng(rng);

self.server_handshake_obfs4_no_keygen(session_sk, msg, materials)
}
Expand Down
12 changes: 6 additions & 6 deletions crates/obfs4/src/handshake/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ fn about_half() -> Result<()> {
let mut not_found = 0;
let mut not_match = 0;
for _ in 0..1_000 {
let sk = curve25519::StaticSecret::random_from_rng(&mut rng);
let rp: Option<curve25519::PublicRepresentative> = (&sk).into();
let sk = x25519_elligator2::StaticSecret::random_from_rng(&mut rng);
let rp: Option<x25519_elligator2::PublicRepresentative> = (&sk).into();
let repres = match rp {
Some(r) => r,
None => {
Expand All @@ -277,9 +277,9 @@ fn about_half() -> Result<()> {
}
};

let pk = curve25519::PublicKey::from(&sk);
let pk = x25519_elligator2::PublicKey::from(&sk);

let decoded_pk = curve25519::PublicKey::from(&repres);
let decoded_pk = x25519_elligator2::PublicKey::from(&repres);
if hex::encode(pk) != hex::encode(decoded_pk) {
not_match += 1;
continue;
Expand All @@ -303,9 +303,9 @@ fn keypair() -> Result<()> {
let kp = Obfs4NtorSecretKey::generate_for_test(&mut rng);

let pk = kp.pk.pk.to_bytes();
let repres: Option<curve25519::PublicRepresentative> = (&kp.sk).into();
let repres: Option<x25519_elligator2::PublicRepresentative> = (&kp.sk).into();

let pubkey = curve25519::PublicKey::from(&repres.unwrap());
let pubkey = x25519_elligator2::PublicKey::from(&repres.unwrap());
assert_eq!(hex::encode(pk), hex::encode(pubkey.to_bytes()));
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/obfs4/src/handshake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
common::{
ct,
curve25519::{EphemeralSecret, PublicKey, Representable, SharedSecret, StaticSecret},
x25519_elligator2::{EphemeralSecret, PublicKey, Keys, SharedSecret, StaticSecret},
kdf::{Kdf, Ntor1Kdf},
ntor_arti::{
AuxDataReply, ClientHandshake, KeyGenerator, RelayHandshakeError, RelayHandshakeResult,
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Obfs4NtorSecretKey {

/// Construct a new ['Obfs4NtorSecretKey'] from a CSPRNG.
pub(crate) fn getrandom() -> Self {
let sk = Representable::random_static();
let sk = Keys::random_static();
let mut id = [0_u8; NODE_ID_LENGTH];
getrandom::getrandom(&mut id).expect("internal randomness error");
Self::new(sk, RsaIdentity::from(id))
Expand All @@ -198,7 +198,7 @@ impl Obfs4NtorSecretKey {
// Random bytes will work for testing, but aren't necessarily actually a valid id.
rng.fill_bytes(&mut id);

let sk = Representable::static_from_rng(rng);
let sk = Keys::static_from_rng(rng);

let pk = Obfs4NtorPublicKey {
pk: (&sk).into(),
Expand Down
2 changes: 1 addition & 1 deletion crates/obfs4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod dev {
#[cfg(test)]
mod test {
use super::*;
use crate::common::curve25519::StaticSecret;
use crate::common::x25519_elligator2::StaticSecret;
use crate::constants::*;
use crate::handshake::Obfs4NtorSecretKey;
use crate::{ClientBuilder, ServerBuilder};
Expand Down
2 changes: 1 addition & 1 deletion crates/obfs4/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
client::ClientBuilder,
common::{
colorize,
curve25519::{PublicKey, StaticSecret},
x25519_elligator2::{PublicKey, StaticSecret},
drbg,
replay_filter::{self, ReplayFilter},
HmacSha256,
Expand Down

0 comments on commit 358d764

Please sign in to comment.