|
|
@ -1,11 +1,13 @@ |
|
|
|
use crate::connections::utils::public_key_to_hostname; |
|
|
|
use ed25519_dalek::{ExpandedSecretKey, Keypair, PublicKey}; |
|
|
|
use rand::rngs::OsRng; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use std::intrinsics::transmute; |
|
|
|
use std::sync::Arc; |
|
|
|
use x25519_dalek::PublicKey as X25519PublicKey; |
|
|
|
use x25519_dalek::{SharedSecret, StaticSecret}; |
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)] |
|
|
|
/// Identity - An ed25519 keypair, required for established a Tor v3 onion service and used to
|
|
|
|
/// maintain a consistent cryptographic identity for a peer.
|
|
|
|
pub struct Identity { |
|
|
@ -21,7 +23,7 @@ impl Identity { |
|
|
|
/// Initialize an ephemeral identity - used for both ephemeral diffie hellman key exchanges
|
|
|
|
/// in addition to anonymous primitives connections to various onion services.
|
|
|
|
pub fn initialize_ephemeral_identity() -> Identity { |
|
|
|
let mut csprng = OsRng {}; |
|
|
|
let mut csprng = OsRng::default(); |
|
|
|
let keypair = ed25519_dalek::Keypair::generate(&mut csprng); |
|
|
|
Identity { keypair } |
|
|
|
} |
|
|
@ -58,6 +60,7 @@ impl Identity { |
|
|
|
#[cfg(test)] |
|
|
|
mod tests { |
|
|
|
use crate::primitives::identity::Identity; |
|
|
|
use ed25519_dalek::Signer; |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn test_identity() { |
|
|
@ -68,4 +71,22 @@ mod tests { |
|
|
|
println!("Alice Shared Secret: {} {:?}", alice.hostname(), alice.edh(bob.keypair.public).to_bytes()); |
|
|
|
println!("Bob Shared Secret: {} {:?}", bob.hostname(), bob.edh(alice.keypair.public).to_bytes()); |
|
|
|
} |
|
|
|
|
|
|
|
#[test] |
|
|
|
fn test_serialize() { |
|
|
|
// Create an emphemeral identity
|
|
|
|
let alice = Identity::initialize_ephemeral_identity(); |
|
|
|
|
|
|
|
// Serialize
|
|
|
|
let json = serde_json::to_string(&alice).unwrap(); |
|
|
|
println!("{}", json); |
|
|
|
|
|
|
|
// Deserialize
|
|
|
|
let deserialized: Identity = serde_json::from_str(&json).unwrap(); |
|
|
|
println!("deserialized = {:?}", deserialized); |
|
|
|
|
|
|
|
// Check that we can sign something with the deserialized key and check it with the original
|
|
|
|
let sig = deserialized.keypair.sign("message".as_bytes()); |
|
|
|
assert_eq!(true, alice.keypair.public.verify_strict("message".as_bytes(), &sig).is_ok()); |
|
|
|
} |
|
|
|
} |
|
|
|