]> git.lizzy.rs Git - PAKEs.git/blob - src/spake2.rs
more sketches, help from manishearth
[PAKEs.git] / src / spake2.rs
1
2 use curve25519_dalek::scalar::Scalar as c2_Scalar;
3 use curve25519_dalek::curve::ExtendedPoint as c2_Element;
4 use curve25519_dalek::curve::BasepointMult;
5 use curve25519_dalek::curve::ScalarMult;
6 use rand::OsRng;
7
8 trait Group {
9     type Scalar;
10     type Element;
11     // const element_length: usize; // in unstable, or u8
12     //type ElementBytes : Index<usize, Output=u8>+IndexMut<usize>; // later
13     fn random_scalar() -> Self::Scalar;
14     fn basepoint_mult(s: &Self::Scalar) -> Self::Element;
15     fn scalarmult(e: &Self::Element, s: &Self::Scalar) -> Self::Element;
16     fn add(a: &Self::Element, b: &Self::Element) -> Self::Element;
17 }
18
19 struct Ed25519Group;
20
21 impl Group for Ed25519Group {
22     type Scalar = c2_Scalar;
23     type Element = c2_Element;
24     //type ElementBytes = Vec<u8>;
25     //type ElementBytes = [u8; 32];
26     //type ScalarBytes
27
28     fn random_scalar() -> c2_Scalar {
29         let mut cspring: OsRng = OsRng::new().unwrap();
30         c2_Scalar::random(&mut cspring)
31     }
32     fn basepoint_mult(s: &c2_Scalar) -> c2_Element {
33         c2_Element::basepoint_mult(s)
34     }
35     fn scalarmult(e: &c2_Element, s: &c2_Scalar) -> c2_Element {
36         e.scalar_mult(s)
37     }
38     fn add(a: &c2_Element, b: &c2_Element) -> c2_Element {
39         a.add(b)
40     }
41 }
42
43
44 /* "session type pattern" */
45
46 struct SPAKE2<G: Group> {
47     x: G::Scalar,
48     password: Vec<u8>,
49     idA: Vec<u8>,
50     idB: Vec<u8>,
51     msg1: Vec<u8>,
52     pw: G::Scalar,
53 }
54
55 impl<G: Group> SPAKE2<G> {
56     pub fn new(password: &[u8], idA: &[u8], idB: &[u8]) -> (SPAKE2<G>, Vec<u8>) {
57         let pw: G::Scalar = hash_to_scalar::<G::Scalar>(password);
58         let x: G::Scalar = random_scalar::<G::Scalar>;
59
60         let M1: G::Element = unimplemented!();
61         let msg1 = unimplemented!(); // M1 to bytes
62         let mut pv = Vec::new();
63         pv.extend_from_slice(password);
64         let mut idA_copy = Vec::new();
65         idA_copy.extend_from_slice(idA);
66         let mut idB_copy = Vec::new();
67         idB_copy.extend_from_slice(idB);
68         (SPAKE2 {x: x,
69                  password: pv,
70                  idA: idA_copy,
71                  idB: idB_copy,
72                  msg1: msg1.clone(),
73                  pw: unimplemented!(),
74         }, msg1)
75     }
76
77     pub fn finish(self, msg2: &[u8]) -> Result<Vec<u8>, SPAKEErr> {
78     }
79 }
80
81
82 /*
83 {
84     let (mut s, msg1) = SPAKE2::<Ed25519>(&password, &idA, &idB);
85     //let msg1 = s.msg1;
86     let key = s.finish(msg2);
87 }
88 */