From 57a38426b5e4716f6f14e639e5b3b923ca7e3319 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Wed, 17 May 2017 13:30:42 -0700 Subject: more sketches, help from manishearth --- src/lib.rs | 3 +++ src/spake2.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index abab8b1..b972f4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ +extern crate rand; +extern crate curve25519_dalek; + pub mod spake2; //use spake2::*; diff --git a/src/spake2.rs b/src/spake2.rs index 7d813d9..ab9ee86 100644 --- a/src/spake2.rs +++ b/src/spake2.rs @@ -1,44 +1,88 @@ -pub fn foo() -> u8 { - 1 -} - +use curve25519_dalek::scalar::Scalar as c2_Scalar; +use curve25519_dalek::curve::ExtendedPoint as c2_Element; +use curve25519_dalek::curve::BasepointMult; +use curve25519_dalek::curve::ScalarMult; +use rand::OsRng; trait Group { type Scalar; type Element; - pub fn scalarmult(s: Scalar) -> Element; - pub fn scalar_from_integer(u8) -> Scalar; + // const element_length: usize; // in unstable, or u8 + //type ElementBytes : Index+IndexMut; // later + fn random_scalar() -> Self::Scalar; + fn basepoint_mult(s: &Self::Scalar) -> Self::Element; + fn scalarmult(e: &Self::Element, s: &Self::Scalar) -> Self::Element; + fn add(a: &Self::Element, b: &Self::Element) -> Self::Element; +} + +struct Ed25519Group; + +impl Group for Ed25519Group { + type Scalar = c2_Scalar; + type Element = c2_Element; + //type ElementBytes = Vec; + //type ElementBytes = [u8; 32]; + //type ScalarBytes + + fn random_scalar() -> c2_Scalar { + let mut cspring: OsRng = OsRng::new().unwrap(); + c2_Scalar::random(&mut cspring) + } + fn basepoint_mult(s: &c2_Scalar) -> c2_Element { + c2_Element::basepoint_mult(s) + } + fn scalarmult(e: &c2_Element, s: &c2_Scalar) -> c2_Element { + e.scalar_mult(s) + } + fn add(a: &c2_Element, b: &c2_Element) -> c2_Element { + a.add(b) + } } +/* "session type pattern" */ + struct SPAKE2 { x: G::Scalar, password: Vec, idA: Vec, idB: Vec, + msg1: Vec, pw: G::Scalar, } -impl for SPAKE2 { - pub fn new(password: &[u8], idA: &[u8], idB: &[u8]) -> SPAKE2 { +impl SPAKE2 { + pub fn new(password: &[u8], idA: &[u8], idB: &[u8]) -> (SPAKE2, Vec) { let pw: G::Scalar = hash_to_scalar::(password); let x: G::Scalar = random_scalar::; - let M1 G::Element = MAGIC(); - let msg1 = ... + let M1: G::Element = unimplemented!(); + let msg1 = unimplemented!(); // M1 to bytes let mut pv = Vec::new(); pv.extend_from_slice(password); - (SPAKE2 {x: x, password: pv, ... }, msg1) + let mut idA_copy = Vec::new(); + idA_copy.extend_from_slice(idA); + let mut idB_copy = Vec::new(); + idB_copy.extend_from_slice(idB); + (SPAKE2 {x: x, + password: pv, + idA: idA_copy, + idB: idB_copy, + msg1: msg1.clone(), + pw: unimplemented!(), + }, msg1) } - - pub fn finish(self, msg2: &[u8]) -> Result { + + pub fn finish(self, msg2: &[u8]) -> Result, SPAKEErr> { } } +/* { let (mut s, msg1) = SPAKE2::(&password, &idA, &idB); //let msg1 = s.msg1; let key = s.finish(msg2); } +*/ -- cgit v1.2.3