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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use getrandom::{getrandom, Error as RngError};
use sha1::{Sha1 as Sha1_hash, Digest};
use sha2::Sha256 as Sha256_hash;
use hmac::{Hmac, Mac, NewMac, crypto_mac::InvalidKeyLength};
use pbkdf2::pbkdf2;

use crate::common::Password;

use crate::secret;

use base64;

/// Generate a nonce for SCRAM authentication.
pub fn generate_nonce() -> Result<String, RngError> {
    let mut data = [0u8; 32];
    getrandom(&mut data)?;
    Ok(base64::encode(&data))
}

#[derive(Debug, PartialEq)]
pub enum DeriveError {
    IncompatibleHashingMethod(String, String),
    IncorrectSalt,
    IncompatibleIterationCount(u32, u32),
}

impl std::fmt::Display for DeriveError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            DeriveError::IncompatibleHashingMethod(one, two) => write!(fmt, "incompatible hashing method, {} is not {}", one, two),
            DeriveError::IncorrectSalt => write!(fmt, "incorrect salt"),
            DeriveError::IncompatibleIterationCount(one, two) => write!(fmt, "incompatible iteration count, {} is not {}", one, two),
        }
    }
}

impl std::error::Error for DeriveError {}

/// A trait which defines the needed methods for SCRAM.
pub trait ScramProvider {
    /// The kind of secret this `ScramProvider` requires.
    type Secret: secret::Secret;

    /// 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]) -> Result<Vec<u8>, InvalidKeyLength>;

    /// A function which does PBKDF2 key derivation using the hash function.
    fn derive(data: &Password, salt: &[u8], iterations: u32) -> Result<Vec<u8>, DeriveError>;
}

/// A `ScramProvider` which provides SCRAM-SHA-1 and SCRAM-SHA-1-PLUS
pub struct Sha1;

impl ScramProvider for Sha1 {
    type Secret = secret::Pbkdf2Sha1;

    fn name() -> &'static str { "SHA-1" }

    fn hash(data: &[u8]) -> Vec<u8> {
        let hash = Sha1_hash::digest(data);
        let mut vec = Vec::with_capacity(Sha1_hash::output_size());
        vec.extend_from_slice(hash.as_slice());
        vec
    }

    fn hmac(data: &[u8], key: &[u8]) -> Result<Vec<u8>, InvalidKeyLength> {
        type HmacSha1 = Hmac<Sha1_hash>;
        let mut mac = HmacSha1::new_varkey(key)?;
        mac.update(data);
        let result = mac.finalize();
        let mut vec = Vec::with_capacity(Sha1_hash::output_size());
        vec.extend_from_slice(result.into_bytes().as_slice());
        Ok(vec)
    }

    fn derive(password: &Password, salt: &[u8], iterations: u32) -> Result<Vec<u8>, DeriveError> {
        match *password {
            Password::Plain(ref plain) => {
                let mut result = vec![0; 20];
                pbkdf2::<Hmac<Sha1_hash>>(plain.as_bytes(), salt, iterations, &mut result);
                Ok(result)
            },
            Password::Pbkdf2 { ref method, salt: ref my_salt, iterations: my_iterations, ref data } => {
                if method != Self::name() {
                    Err(DeriveError::IncompatibleHashingMethod(method.to_string(), Self::name().to_string()))
                }
                else if my_salt == &salt {
                    Err(DeriveError::IncorrectSalt)
                }
                else if my_iterations == iterations {
                    Err(DeriveError::IncompatibleIterationCount(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 {
    type Secret = secret::Pbkdf2Sha256;

    fn name() -> &'static str { "SHA-256" }

    fn hash(data: &[u8]) -> Vec<u8> {
        let hash = Sha256_hash::digest(data);
        let mut vec = Vec::with_capacity(Sha256_hash::output_size());
        vec.extend_from_slice(hash.as_slice());
        vec
    }

    fn hmac(data: &[u8], key: &[u8]) -> Result<Vec<u8>, InvalidKeyLength> {
        type HmacSha256 = Hmac<Sha256_hash>;
        let mut mac = HmacSha256::new_varkey(key)?;
        mac.update(data);
        let result = mac.finalize();
        let mut vec = Vec::with_capacity(Sha256_hash::output_size());
        vec.extend_from_slice(result.into_bytes().as_slice());
        Ok(vec)
    }

    fn derive(password: &Password, salt: &[u8], iterations: u32) -> Result<Vec<u8>, DeriveError> {
        match *password {
            Password::Plain(ref plain) => {
                let mut result = vec![0; 32];
                pbkdf2::<Hmac<Sha256_hash>>(plain.as_bytes(), salt, iterations, &mut result);
                Ok(result)
            },
            Password::Pbkdf2 { ref method, salt: ref my_salt, iterations: my_iterations, ref data } => {
                if method != Self::name() {
                    Err(DeriveError::IncompatibleHashingMethod(method.to_string(), Self::name().to_string()))
                }
                else if my_salt == &salt {
                    Err(DeriveError::IncorrectSalt)
                }
                else if my_iterations == iterations {
                    Err(DeriveError::IncompatibleIterationCount(my_iterations, iterations))
                }
                else {
                    Ok(data.to_vec())
                }
            },
        }
    }
}