Setup Serialization/Deserialization for Identity

This commit is contained in:
Sarah Jamie Lewis 2021-01-12 15:52:00 -08:00
parent a29c662221
commit 629cca918e
3 changed files with 25 additions and 5 deletions

View File

@ -15,14 +15,14 @@ name = "simple_setup"
rand = "0.7.3" rand = "0.7.3"
curve25519-dalek = "3.0.0" curve25519-dalek = "3.0.0"
x25519-dalek = "1.1" x25519-dalek = "1.1"
ed25519-dalek = "1.0.1" ed25519-dalek = {version = "1.0.1", features=["serde"]}
merlin = "2.0.0" merlin = "2.0.0"
hex = "0.4.2" hex = "0.4.2"
base32 = "0.4.0" base32 = "0.4.0"
base64 = "0.13.0" base64 = "0.13.0"
sha3 = "0.9.1" sha3 = "0.9.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.59" serde_json = "1.0.61"
byteorder = "1.3.4" byteorder = "1.3.4"
socks = "0.3.3" socks = "0.3.3"
integer-encoding = "2.1.1" integer-encoding = "2.1.1"

View File

@ -1,11 +1,13 @@
use crate::connections::utils::public_key_to_hostname; use crate::connections::utils::public_key_to_hostname;
use ed25519_dalek::{ExpandedSecretKey, Keypair, PublicKey}; use ed25519_dalek::{ExpandedSecretKey, Keypair, PublicKey};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use std::intrinsics::transmute; use std::intrinsics::transmute;
use std::sync::Arc; use std::sync::Arc;
use x25519_dalek::PublicKey as X25519PublicKey; use x25519_dalek::PublicKey as X25519PublicKey;
use x25519_dalek::{SharedSecret, StaticSecret}; use x25519_dalek::{SharedSecret, StaticSecret};
#[derive(Serialize, Deserialize, Debug)]
/// Identity - An ed25519 keypair, required for established a Tor v3 onion service and used to /// Identity - An ed25519 keypair, required for established a Tor v3 onion service and used to
/// maintain a consistent cryptographic identity for a peer. /// maintain a consistent cryptographic identity for a peer.
pub struct Identity { pub struct Identity {
@ -21,7 +23,7 @@ impl Identity {
/// Initialize an ephemeral identity - used for both ephemeral diffie hellman key exchanges /// Initialize an ephemeral identity - used for both ephemeral diffie hellman key exchanges
/// in addition to anonymous primitives connections to various onion services. /// in addition to anonymous primitives connections to various onion services.
pub fn initialize_ephemeral_identity() -> Identity { pub fn initialize_ephemeral_identity() -> Identity {
let mut csprng = OsRng {}; let mut csprng = OsRng::default();
let keypair = ed25519_dalek::Keypair::generate(&mut csprng); let keypair = ed25519_dalek::Keypair::generate(&mut csprng);
Identity { keypair } Identity { keypair }
} }
@ -58,6 +60,7 @@ impl Identity {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::primitives::identity::Identity; use crate::primitives::identity::Identity;
use ed25519_dalek::Signer;
#[test] #[test]
fn test_identity() { fn test_identity() {
@ -68,4 +71,22 @@ mod tests {
println!("Alice Shared Secret: {} {:?}", alice.hostname(), alice.edh(bob.keypair.public).to_bytes()); 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()); 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());
}
} }

View File

@ -11,7 +11,6 @@ mod test {
use tapir_cwtch::connections::{Connection, InboundConnection, OutboundConnection}; use tapir_cwtch::connections::{Connection, InboundConnection, OutboundConnection};
use tapir_cwtch::primitives::transcript::Transcript; use tapir_cwtch::primitives::transcript::Transcript;
#[test] #[test]
fn test_simple_setup() { fn test_simple_setup() {
let mut auth_control_port = TorProcess::connect(9051) let mut auth_control_port = TorProcess::connect(9051)