-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
82 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::{ | ||
keys::{key_encryptable::CryptoKey, utils::derive_kdf_key}, | ||
EncString, Kdf, KeyEncryptable, Result, SymmetricCryptoKey, | ||
}; | ||
|
||
/// Pin Key. | ||
/// | ||
/// Derived from a specific password, used for pin encryption and exports. | ||
pub struct PinKey(SymmetricCryptoKey); | ||
|
||
impl PinKey { | ||
pub fn new(key: SymmetricCryptoKey) -> Self { | ||
Self(key) | ||
} | ||
|
||
/// Derives a users master key from their password, email and KDF. | ||
pub fn derive(password: &[u8], email: &[u8], kdf: &Kdf) -> Result<Self> { | ||
derive_kdf_key(password, email, kdf).map(Self) | ||
} | ||
} | ||
|
||
impl CryptoKey for PinKey {} | ||
|
||
impl KeyEncryptable<PinKey, EncString> for &[u8] { | ||
fn encrypt_with_key(self, key: &PinKey) -> Result<EncString> { | ||
self.encrypt_with_key(&key.0) | ||
} | ||
} | ||
|
||
impl KeyEncryptable<PinKey, EncString> for String { | ||
fn encrypt_with_key(self, key: &PinKey) -> Result<EncString> { | ||
self.as_bytes().encrypt_with_key(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use sha2::Digest; | ||
|
||
use crate::{Kdf, Result, SymmetricCryptoKey}; | ||
|
||
/// Derive a generic key from a secret and salt using the provided KDF. | ||
pub(super) fn derive_kdf_key(secret: &[u8], salt: &[u8], kdf: &Kdf) -> Result<SymmetricCryptoKey> { | ||
let mut hash = match kdf { | ||
Kdf::PBKDF2 { iterations } => crate::util::pbkdf2(secret, salt, iterations.get()), | ||
|
||
Kdf::Argon2id { | ||
iterations, | ||
memory, | ||
parallelism, | ||
} => { | ||
use argon2::*; | ||
|
||
let argon = Argon2::new( | ||
Algorithm::Argon2id, | ||
Version::V0x13, | ||
Params::new( | ||
memory.get() * 1024, // Convert MiB to KiB | ||
iterations.get(), | ||
parallelism.get(), | ||
Some(32), | ||
) | ||
.unwrap(), | ||
); | ||
|
||
let salt_sha = sha2::Sha256::new().chain_update(salt).finalize(); | ||
|
||
let mut hash = [0u8; 32]; | ||
argon | ||
.hash_password_into(secret, &salt_sha, &mut hash) | ||
.unwrap(); | ||
hash | ||
} | ||
}; | ||
SymmetricCryptoKey::try_from(hash.as_mut_slice()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters