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
use argon2rs::{ Argon2, Variant, ParamErr };
use seckey::Bytes;
use super::{ KeyDerive, KeyDerivationFail };


/// Interactive Opslimit. parameter from `libsodium`.
pub const OPSLIMIT_INTERACTIVE: u32 = 4;
/// Interactive Memlimit. parameter from `libsodium`.
pub const MEMLIMIT_INTERACTIVE: u32 = 33554432;
/// Moderate Opslimit. parameter from `libsodium`.
pub const OPSLIMIT_MODERATE: u32 = 6;
/// Moderate Memlimit. parameter from `libsodium`.
pub const MEMLIMIT_MODERATE: u32 = 134217728;
/// Sensitive Opslimit. parameter from `libsodium`.
pub const OPSLIMIT_SENSITIVE: u32 = 8;
/// Sensitive Memlimit. parameter from `libsodium`.
pub const MEMLIMIT_SENSITIVE: u32 = 536870912;

/// Argon2i.
///
/// # Example(keyderive)
/// ```
/// # extern crate rand;
/// # extern crate seckey;
/// # #[macro_use] extern crate sarkara;
/// # fn main() {
/// use seckey::Bytes;
/// use sarkara::pwhash::{ Argon2i, KeyDerive };
///
/// let (pass, salt) = (rand!(8), rand!(8));
/// let key = Argon2i::default()
///     .derive::<Bytes>(&pass, &salt)
///     .unwrap();
/// # assert!(key != pass);
/// # }
/// ```
///
/// # Example(pwhash)
/// ```
/// # extern crate rand;
/// # extern crate seckey;
/// # #[macro_use] extern crate sarkara;
/// # fn main() {
/// use seckey::Bytes;
/// use sarkara::pwhash::{ Argon2i, KeyDerive };
///
/// let pass = rand!(8);
/// let key = Argon2i::default()
///     .with_size(16)
///     .pwhash::<Bytes>(&pass)
///     .unwrap();
/// # assert_eq!(key.len(), 16);
/// # }
/// ```
///
/// # Example(keyverify)
/// ```
/// # extern crate rand;
/// # extern crate seckey;
/// # #[macro_use] extern crate sarkara;
/// # fn main() {
/// use seckey::Bytes;
/// use sarkara::pwhash::{ Argon2i, KeyDerive, KeyVerify };
///
/// let (pass, salt) = (rand!(8), rand!(8));
/// let key = Argon2i::default()
///     .derive::<Bytes>(&pass, &salt)
///     .unwrap();
///
/// assert!(Argon2i::default().verify(&pass, &salt, &key).unwrap());
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Argon2i {
    key: Bytes,
    aad: Bytes,
    outlen: usize,
    passes: u32,
    lanes: u32,
    kib: u32
}

impl Default for Argon2i {
    fn default() -> Argon2i {
        Argon2i {
            key: Bytes::empty(),
            aad: Bytes::empty(),
            outlen: 16,
            passes: OPSLIMIT_INTERACTIVE,
            lanes: 1,
            kib: MEMLIMIT_INTERACTIVE / 1024
        }
    }
}

impl KeyDerive for Argon2i {
    fn with_size(&mut self, len: usize) -> &mut Self {
        self.outlen = len;
        self
    }
    fn with_key(&mut self, key: &[u8]) -> &mut Self {
        self.key = Bytes::new(key);
        self
    }
    fn with_aad(&mut self, aad: &[u8]) -> &mut Self {
        self.aad = Bytes::new(aad);
        self
    }
    fn with_opslimit(&mut self, opslimit: u32) -> &mut Self {
        self.passes = opslimit;
        self
    }
    fn with_memlimit(&mut self, memlimit: u32) -> &mut Self {
        self.kib = memlimit / 1024;
        self
    }

    fn derive<K>(&self, password: &[u8], salt: &[u8])
        -> Result<K, KeyDerivationFail>
        where K: From<Vec<u8>>
    {
        if salt.len() < 8 { Err(KeyDerivationFail::SaltTooShort)? };
        if salt.len() > 0xffffffff { Err(KeyDerivationFail::SaltTooLong)? };
        if self.outlen < 4 { Err(KeyDerivationFail::OutLenTooShort)? };
        if self.outlen > 0xffffffff { Err(KeyDerivationFail::OutLenTooLong)? };

        let mut output = vec![0; self.outlen];
        Argon2::new(self.passes, self.lanes, self.kib, Variant::Argon2i)?
            .hash(&mut output, password, salt, &self.key, &self.aad);
        Ok(output.into())
    }
}

impl From<ParamErr> for KeyDerivationFail {
    fn from(err: ParamErr) -> KeyDerivationFail {
        use std::error::Error;
        KeyDerivationFail::ParameterError(err.description().to_string())
    }
}