-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhandshake.rs
More file actions
47 lines (40 loc) · 1.24 KB
/
Copy pathhandshake.rs
File metadata and controls
47 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use permutation::Permutation;
use cube::Cube;
use crypto::blake2b::Blake2b;
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct SecretKey {
pub a: u64,
pub b: u64,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct PublicKey {
pub key: Permutation,
}
impl SecretKey {
pub fn to_public(&self) -> PublicKey {
let pa = Permutation::parse("U x'").unwrap();
let pb = Permutation::parse("L y'").unwrap();
PublicKey { key: self.a * pa + self.b * pb }
}
pub fn handshake(&self, key: PublicKey, salt: &[u8]) -> [u8; 16] {
let pa = Permutation::parse("U x'").unwrap();
let pb = Permutation::parse("L y'").unwrap();
let cube = Cube::default().apply(self.a * pa + key.key + self.b * pb);
let mut out = [0; 16];
Blake2b::blake2b(&mut out, &cube.serialize().as_bytes(), salt);
out
}
}
impl PublicKey {
pub fn serialize(&self) -> String {
Cube::default().apply(self.key).serialize()
}
pub fn unserialize(s: &str) -> Option<PublicKey> {
if let Some(cube) = Cube::unserialize(s) {
if let Some(perm) = Permutation::from_cube(cube) {
return Some(PublicKey { key: perm });
}
}
None
}
}