-
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.
Implement encrypted export
- Loading branch information
Showing
9 changed files
with
447 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
use crate::{ | ||
keys::{ | ||
key_encryptable::CryptoKey, | ||
utils::{derive_kdf_key, stretch_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 pin key from their password, email and KDF. | ||
pub fn derive(password: &[u8], salt: &[u8], kdf: &Kdf) -> Result<Self> { | ||
derive_kdf_key(password, salt, kdf).map(Self) | ||
} | ||
} | ||
|
||
impl CryptoKey for PinKey {} | ||
|
||
impl KeyEncryptable<PinKey, EncString> for &[u8] { | ||
fn encrypt_with_key(self, key: &PinKey) -> Result<EncString> { | ||
let stretched_key = stretch_kdf_key(&key.0)?; | ||
|
||
self.encrypt_with_key(&stretched_key) | ||
} | ||
} | ||
|
||
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,85 @@ | ||
use std::pin::Pin; | ||
|
||
use generic_array::{typenum::U32, GenericArray}; | ||
use sha2::Digest; | ||
|
||
use crate::{util::hkdf_expand, 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()) | ||
} | ||
|
||
pub(super) fn stretch_kdf_key(k: &SymmetricCryptoKey) -> Result<SymmetricCryptoKey> { | ||
let key: Pin<Box<GenericArray<u8, U32>>> = hkdf_expand(&k.key, Some("enc"))?; | ||
let mac_key: Pin<Box<GenericArray<u8, U32>>> = hkdf_expand(&k.key, Some("mac"))?; | ||
|
||
Ok(SymmetricCryptoKey::new(key, Some(mac_key))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_stretch_kdf_key() { | ||
let key = SymmetricCryptoKey::new( | ||
Box::pin( | ||
[ | ||
31, 79, 104, 226, 150, 71, 177, 90, 194, 80, 172, 209, 17, 129, 132, 81, 138, | ||
167, 69, 167, 254, 149, 2, 27, 39, 197, 64, 42, 22, 195, 86, 75, | ||
] | ||
.into(), | ||
), | ||
None, | ||
); | ||
|
||
let stretched = stretch_kdf_key(&key).unwrap(); | ||
|
||
assert_eq!( | ||
[ | ||
111, 31, 178, 45, 238, 152, 37, 114, 143, 215, 124, 83, 135, 173, 195, 23, 142, | ||
134, 120, 249, 61, 132, 163, 182, 113, 197, 189, 204, 188, 21, 237, 96 | ||
], | ||
stretched.key.as_slice() | ||
); | ||
assert_eq!( | ||
[ | ||
221, 127, 206, 234, 101, 27, 202, 38, 86, 52, 34, 28, 78, 28, 185, 16, 48, 61, 127, | ||
166, 209, 247, 194, 87, 232, 26, 48, 85, 193, 249, 179, 155 | ||
], | ||
stretched.mac_key.as_ref().unwrap().as_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
Oops, something went wrong.