extern crate rust_sodium;
use rust_sodium::randombytes::randombytes;
use cryptography::crypto_box::PrecomputedKey;
use cryptography::sha256;
use cryptography::{shared_secret_from_keys, shared_secret_from_password};
use session::Session;
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum Credentials {
LoginPassword {
login: Vec<u8>,
password: Vec<u8>,
},
Password {
password: Vec<u8>,
},
None,
}
pub trait ToAuthChallenge {
fn to_auth_challenge_bytes(&self, session: &Session) -> [u8; 12];
fn make_shared_secret(&self, session: &Session) -> PrecomputedKey;
fn make_shared_secret_key(&self, session: &Session) -> PrecomputedKey {
self.make_shared_secret(session)
}
}
impl ToAuthChallenge for Credentials {
fn to_auth_challenge_bytes(&self, _session: &Session) -> [u8; 12] {
match *self {
Credentials::None => {
let bytes = randombytes(11);
[
0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6],
bytes[7], bytes[8], bytes[9], bytes[10],
]
}
Credentials::Password { ref password } => {
let hashed_password = sha256::hash(&password).0;
let hash = sha256::hash(&hashed_password).0;
[
1, hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], 0, 0, 0, 0,
]
}
Credentials::LoginPassword {
password: _,
ref login,
} => {
let hash = sha256::hash(&login).0;
[
2, hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], 0, 0, 0, 0,
]
}
}
}
fn make_shared_secret(&self, session: &Session) -> PrecomputedKey {
match *self {
Credentials::None => {
shared_secret_from_keys(&session.my_perm_sk, &session.their_perm_pk)
}
Credentials::Password { ref password }
| Credentials::LoginPassword {
ref password,
login: _,
} => shared_secret_from_password(password, &session.my_perm_sk, &session.their_perm_pk),
}
}
}