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
/*-
* Scram-rs
* Copyright (C) 2021  Aleksandr Morozov
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

use sha1::{Sha1, Digest as Digest1};
use sha2::{Sha256, Sha512, Digest as Digest2};
use hmac::{Hmac, Mac, NewMac, crypto_mac::InvalidKeyLength};
use pbkdf2::pbkdf2;

use super::scram_error::{ScramResult, ScramRuntimeError, ScramErrorCode};
use super::{scram_error, scram_error_map};


pub trait ScramHashing 
{
    /// 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]) -> ScramResult<Vec<u8>>;

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

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

impl ScramHashing for ScramSha1 
{
    fn hash(data: &[u8]) -> Vec<u8> 
    {
        let hash = Sha1::digest(data);

        return Vec::from(hash.as_slice());
    }

    fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>> 
    {
        type HmacSha1 = Hmac<Sha1>;
        let mut mac = HmacSha1::new_varkey(key)
                                .map_err(|e| scram_error_map!(ScramErrorCode::ExternalError, 
                                                                "hmac() HmacSha1 err, {}", e))?;
        mac.update(data);
        let result = mac.finalize();
        
        return Ok(Vec::from(result.into_bytes().as_slice()));
    }

    fn derive(password: &[u8], salt: &[u8], iterations: u32) -> ScramResult<Vec<u8>> 
    {
        let mut result = vec![0; 20];
        pbkdf2::<Hmac<Sha1>>(password, salt, iterations, &mut result);

        return Ok(result);
    }
}

/// A `ScramProvider` which provides SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
pub struct ScramSha256;

impl ScramHashing for ScramSha256 
{
    fn hash(data: &[u8]) -> Vec<u8> 
    {
        let hash = Sha256::digest(data);

        return Vec::from(hash.as_slice());
    }

    fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>> 
    {
        let mut mac = Hmac::<Sha256>::new_varkey(key)
                                    .map_err(|e| scram_error_map!(ScramErrorCode::ExternalError, 
                                        "hmac() Hmac::<Sha256> err, {}", e))?;
        mac.update(data);
        let result = mac.finalize();
        let ret = Vec::from(result.into_bytes().as_slice());

        return Ok(ret);
    }

    fn derive(password: &[u8], salt: &[u8], iterations: u32) -> ScramResult<Vec<u8>> 
    {

        let mut salted = vec![0; 32];
        pbkdf2::<Hmac<Sha256>>(password, salt, iterations, &mut salted);

        return Ok(salted);
    }

}

/// A `ScramProvider` which provides SCRAM-SHA-512 and SCRAM-SHA-512-PLUS 
pub struct ScramSha512;

impl ScramHashing for ScramSha512 
{
    fn hash(data: &[u8]) -> Vec<u8> 
    {
        let hash = Sha512::digest(data);

        return Vec::from(hash.as_slice());
    }

    fn hmac(data: &[u8], key: &[u8]) -> ScramResult<Vec<u8>> 
    {
        let mut mac = Hmac::<Sha512>::new_varkey(key)
                                    .map_err(|e| scram_error_map!(ScramErrorCode::ExternalError, 
                                        "hmac() Hmac::<Sha512> err, {}", e))?;
        mac.update(data);
        let result = mac.finalize();
        let ret = Vec::from(result.into_bytes().as_slice());

        return Ok(ret);
    }

    fn derive(password: &[u8], salt: &[u8], iterations: u32) -> ScramResult<Vec<u8>> 
    {

        let mut salted = vec![0; 64];
        pbkdf2::<Hmac<Sha512>>(password, salt, iterations, &mut salted);

        return Ok(salted);
    }

}