2019-01-17 23:40:46 +01:00
|
|
|
use hmac::{Hmac, Mac};
|
2019-01-17 23:59:31 +01:00
|
|
|
use pbkdf2::pbkdf2;
|
2019-01-17 23:53:29 +01:00
|
|
|
use rand_os::{
|
|
|
|
|
rand_core::{Error as RngError, RngCore},
|
|
|
|
|
OsRng,
|
|
|
|
|
};
|
2019-01-17 23:32:39 +01:00
|
|
|
use sha1::{Digest, Sha1 as Sha1_hash};
|
|
|
|
|
use sha2::Sha256 as Sha256_hash;
|
2017-03-16 20:04:22 +01:00
|
|
|
|
2019-01-17 22:54:32 +01:00
|
|
|
use crate::common::Password;
|
2017-03-16 20:04:22 +01:00
|
|
|
|
2019-01-17 22:54:32 +01:00
|
|
|
use crate::secret;
|
2017-03-25 23:15:34 +01:00
|
|
|
|
2017-03-16 20:04:22 +01:00
|
|
|
use base64;
|
|
|
|
|
|
|
|
|
|
/// Generate a nonce for SCRAM authentication.
|
2019-01-17 23:53:29 +01:00
|
|
|
pub fn generate_nonce() -> Result<String, RngError> {
|
|
|
|
|
let mut data = [0u8; 32];
|
|
|
|
|
let mut rng = OsRng::new()?;
|
|
|
|
|
rng.fill_bytes(&mut data);
|
2017-03-16 20:04:22 +01:00
|
|
|
Ok(base64::encode(&data))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A trait which defines the needed methods for SCRAM.
|
|
|
|
|
pub trait ScramProvider {
|
2017-03-25 23:15:34 +01:00
|
|
|
/// The kind of secret this `ScramProvider` requires.
|
2017-03-25 23:45:30 +01:00
|
|
|
type Secret: secret::Secret;
|
2017-03-25 23:15:34 +01:00
|
|
|
|
2017-03-16 20:04:22 +01:00
|
|
|
/// The name of the hash function.
|
|
|
|
|
fn name() -> &'static str;
|
|
|
|
|
|
|
|
|
|
/// A function which hashes the data using the hash function.
|
|
|
|
|
fn hash(data: &[u8]) -> Vec<u8>;
|
|
|
|
|
|
|
|
|
|
/// A function which performs an HMAC using the hash function.
|
|
|
|
|
fn hmac(data: &[u8], key: &[u8]) -> Vec<u8>;
|
|
|
|
|
|
|
|
|
|
/// A function which does PBKDF2 key derivation using the hash function.
|
|
|
|
|
fn derive(data: &Password, salt: &[u8], iterations: usize) -> Result<Vec<u8>, String>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A `ScramProvider` which provides SCRAM-SHA-1 and SCRAM-SHA-1-PLUS
|
|
|
|
|
pub struct Sha1;
|
|
|
|
|
|
|
|
|
|
impl ScramProvider for Sha1 {
|
|
|
|
|
// TODO: look at all these unwraps
|
2017-03-25 23:45:30 +01:00
|
|
|
type Secret = secret::Pbkdf2Sha1;
|
2017-03-25 23:15:34 +01:00
|
|
|
|
2017-03-16 20:04:22 +01:00
|
|
|
fn name() -> &'static str {
|
|
|
|
|
"SHA-1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash(data: &[u8]) -> Vec<u8> {
|
2019-01-17 23:32:39 +01:00
|
|
|
let hash = Sha1_hash::digest(data);
|
|
|
|
|
let mut vec = Vec::with_capacity(Sha1_hash::output_size());
|
|
|
|
|
vec.extend_from_slice(hash.as_slice());
|
|
|
|
|
vec
|
2017-03-16 20:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hmac(data: &[u8], key: &[u8]) -> Vec<u8> {
|
2019-01-17 23:40:46 +01:00
|
|
|
type HmacSha1 = Hmac<Sha1_hash>;
|
|
|
|
|
let mut mac = HmacSha1::new_varkey(key).unwrap();
|
|
|
|
|
mac.input(data);
|
|
|
|
|
let result = mac.result();
|
|
|
|
|
let mut vec = Vec::with_capacity(Sha1_hash::output_size());
|
|
|
|
|
vec.extend_from_slice(result.code().as_slice());
|
|
|
|
|
vec
|
2017-03-16 20:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn derive(password: &Password, salt: &[u8], iterations: usize) -> Result<Vec<u8>, String> {
|
|
|
|
|
match *password {
|
|
|
|
|
Password::Plain(ref plain) => {
|
|
|
|
|
let mut result = vec![0; 20];
|
2019-01-17 23:59:31 +01:00
|
|
|
pbkdf2::<Hmac<Sha1_hash>>(plain.as_bytes(), salt, iterations, &mut result);
|
2017-03-16 20:04:22 +01:00
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
Password::Pbkdf2 {
|
|
|
|
|
ref method,
|
|
|
|
|
salt: ref my_salt,
|
|
|
|
|
iterations: my_iterations,
|
|
|
|
|
ref data,
|
|
|
|
|
} => {
|
|
|
|
|
if method != Self::name() {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"incompatible hashing method, {} is not {}",
|
|
|
|
|
method,
|
|
|
|
|
Self::name()
|
|
|
|
|
))
|
|
|
|
|
} else if my_salt == &salt {
|
|
|
|
|
Err(format!("incorrect salt"))
|
|
|
|
|
} else if my_iterations == iterations {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"incompatible iteration count, {} is not {}",
|
|
|
|
|
my_iterations, iterations
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(data.to_vec())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A `ScramProvider` which provides SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
|
|
|
|
|
pub struct Sha256;
|
|
|
|
|
|
|
|
|
|
impl ScramProvider for Sha256 {
|
|
|
|
|
// TODO: look at all these unwraps
|
2017-03-25 23:45:30 +01:00
|
|
|
type Secret = secret::Pbkdf2Sha256;
|
2017-03-25 23:15:34 +01:00
|
|
|
|
2017-03-16 20:04:22 +01:00
|
|
|
fn name() -> &'static str {
|
|
|
|
|
"SHA-256"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash(data: &[u8]) -> Vec<u8> {
|
2019-01-17 23:32:39 +01:00
|
|
|
let hash = Sha256_hash::digest(data);
|
|
|
|
|
let mut vec = Vec::with_capacity(Sha256_hash::output_size());
|
|
|
|
|
vec.extend_from_slice(hash.as_slice());
|
|
|
|
|
vec
|
2017-03-16 20:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hmac(data: &[u8], key: &[u8]) -> Vec<u8> {
|
2019-01-17 23:40:46 +01:00
|
|
|
type HmacSha256 = Hmac<Sha256_hash>;
|
|
|
|
|
let mut mac = HmacSha256::new_varkey(key).unwrap();
|
|
|
|
|
mac.input(data);
|
|
|
|
|
let result = mac.result();
|
|
|
|
|
let mut vec = Vec::with_capacity(Sha256_hash::output_size());
|
|
|
|
|
vec.extend_from_slice(result.code().as_slice());
|
|
|
|
|
vec
|
2017-03-16 20:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn derive(password: &Password, salt: &[u8], iterations: usize) -> Result<Vec<u8>, String> {
|
|
|
|
|
match *password {
|
|
|
|
|
Password::Plain(ref plain) => {
|
|
|
|
|
let mut result = vec![0; 32];
|
2019-01-17 23:59:31 +01:00
|
|
|
pbkdf2::<Hmac<Sha256_hash>>(plain.as_bytes(), salt, iterations, &mut result);
|
2017-03-16 20:04:22 +01:00
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
Password::Pbkdf2 {
|
|
|
|
|
ref method,
|
|
|
|
|
salt: ref my_salt,
|
|
|
|
|
iterations: my_iterations,
|
|
|
|
|
ref data,
|
|
|
|
|
} => {
|
|
|
|
|
if method != Self::name() {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"incompatible hashing method, {} is not {}",
|
|
|
|
|
method,
|
|
|
|
|
Self::name()
|
|
|
|
|
))
|
|
|
|
|
} else if my_salt == &salt {
|
|
|
|
|
Err(format!("incorrect salt"))
|
|
|
|
|
} else if my_iterations == iterations {
|
|
|
|
|
Err(format!(
|
|
|
|
|
"incompatible iteration count, {} is not {}",
|
|
|
|
|
my_iterations, iterations
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(data.to_vec())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|