diff options
author | Brian Warner <warner@lothar.com> | 2018-05-25 11:54:30 -0700 |
---|---|---|
committer | Brian Warner <warner@lothar.com> | 2018-05-25 11:55:02 -0700 |
commit | 2e0fbf22e56ee9cb706a131aacee02c28891ec71 (patch) | |
tree | 94edd1c8309ec79ee41c2b8a2156a82a514a32ae | |
parent | 2c1f68d6be76af9c6f93c2f80880de23511b00ab (diff) | |
download | PAKEs-2e0fbf22e56ee9cb706a131aacee02c28891ec71.tar.xz |
implement Debug for SPAKE2, to help downstream applications derive it
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/spake2.rs | 33 |
2 files changed, 34 insertions, 5 deletions
@@ -1,18 +1,14 @@ extern crate curve25519_dalek; +extern crate hex; extern crate hkdf; extern crate num_bigint; extern crate rand; extern crate sha2; -//extern crate hex; - mod spake2; pub use spake2::*; #[cfg(test)] -extern crate hex; - -#[cfg(test)] mod tests { use spake2::{Ed25519Group, ErrorType, SPAKE2, SPAKEErr}; diff --git a/src/spake2.rs b/src/spake2.rs index d1cb4d9..07c92a3 100644 --- a/src/spake2.rs +++ b/src/spake2.rs @@ -4,10 +4,12 @@ use curve25519_dalek::constants::ED25519_BASEPOINT_POINT; use curve25519_dalek::edwards::CompressedEdwardsY; use curve25519_dalek::edwards::EdwardsPoint as c2_Element; use curve25519_dalek::scalar::Scalar as c2_Scalar; +use hex; use hkdf::Hkdf; use num_bigint::BigUint; use rand::{CryptoRng, OsRng, Rng}; use sha2::{Digest, Sha256}; +use std::fmt; //use hex::ToHex; @@ -47,6 +49,7 @@ pub trait Group { fn add(a: &Self::Element, b: &Self::Element) -> Self::Element; } +#[derive(Debug)] pub struct Ed25519Group; impl Group for Ed25519Group { @@ -254,11 +257,13 @@ fn ed25519_hash_symmetric( /* "session type pattern" */ +#[derive(Debug)] enum Side { A, B, Symmetric, } + pub struct SPAKE2<G: Group> { //where &G::Scalar: Neg { side: Side, @@ -464,6 +469,26 @@ impl<G: Group> SPAKE2<G> { } } +fn maybe_utf8(s: &[u8]) -> String { + match String::from_utf8(s.to_vec()) { + Ok(m) => format!("(s={})", m), + Err(_) => format!("(hex={})", hex::encode(s)), + } +} + +impl<G: Group> fmt::Debug for SPAKE2<G> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "SPAKE2(G=?, side={:?}, idA={}, idB={}, idS={})", + self.side, + maybe_utf8(&self.id_a), + maybe_utf8(&self.id_b), + maybe_utf8(&self.id_s) + ) + } +} + #[cfg(test)] mod test { /* This compares results against the python compatibility tests: @@ -617,4 +642,12 @@ mod test { ); } + #[test] + fn test_debug() { + let (s1, _msg1) = SPAKE2::<Ed25519Group>::start_a(b"password", b"idA", b"idB"); + println!("s1: {:?}", s1); + let (s2, _msg1) = SPAKE2::<Ed25519Group>::start_symmetric(b"password", b"idS"); + println!("s2: {:?}", s2); + } + } |