aboutsummaryrefslogtreecommitdiffstats
path: root/src/identity.rs
diff options
context:
space:
mode:
authorBernd Schoolmann <mail@quexten.com>2023-03-15 16:40:49 +0100
committerBernd Schoolmann <mail@quexten.com>2023-03-26 04:28:03 +0200
commit9645a4636f6f4b04f4e6aba84e3c77fa0f2f6961 (patch)
treecfaf219d625c1dc73991e225bafce8949e5d16be /src/identity.rs
parent2f9bd4eb45c57ce8e8d3011d7660223c05b50f98 (diff)
downloadrbw-9645a4636f6f4b04f4e6aba84e3c77fa0f2f6961.tar.gz
rbw-9645a4636f6f4b04f4e6aba84e3c77fa0f2f6961.zip
Implement argon2 kdf
Diffstat (limited to 'src/identity.rs')
-rw-r--r--src/identity.rs48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/identity.rs b/src/identity.rs
index 8a5dc61..d63dc9b 100644
--- a/src/identity.rs
+++ b/src/identity.rs
@@ -1,4 +1,7 @@
use crate::prelude::*;
+use sha2::Digest;
+extern crate argon2;
+use argon2::{Config, ThreadMode, Variant, Version};
pub struct Identity {
pub email: String,
@@ -10,7 +13,10 @@ impl Identity {
pub fn new(
email: &str,
password: &crate::locked::Password,
+ kdf: u32,
iterations: u32,
+ memory: Option<u32>,
+ parallelism: Option<u32>,
) -> Result<Self> {
let iterations = std::num::NonZeroU32::new(iterations)
.ok_or(Error::Pbkdf2ZeroIterations)?;
@@ -19,14 +25,42 @@ impl Identity {
keys.extend(std::iter::repeat(0).take(64));
let enc_key = &mut keys.data_mut()[0..32];
- pbkdf2::pbkdf2::<hmac::Hmac<sha2::Sha256>>(
- password.password(),
- email.as_bytes(),
- iterations.get(),
- enc_key,
- )
- .map_err(|_| Error::Pbkdf2)?;
+ match kdf {
+ 0 => {
+ pbkdf2::pbkdf2::<hmac::Hmac<sha2::Sha256>>(
+ password.password(),
+ email.as_bytes(),
+ iterations.get(),
+ enc_key,
+ )
+ .map_err(|_| Error::Pbkdf2)?;
+ }
+
+ 1 => {
+ let mut hasher = sha2::Sha256::new();
+ hasher.update(email.as_bytes());
+ let salt = hasher.finalize();
+
+ let config = Config {
+ variant: Variant::Argon2id,
+ version: Version::Version13,
+ mem_cost: memory.unwrap() * 1024,
+ time_cost: iterations.get(),
+ lanes: parallelism.unwrap(),
+ thread_mode: ThreadMode::Parallel,
+ secret: &[],
+ ad: &[],
+ hash_length: 32
+ };
+ let hash = argon2::hash_raw(password.password(), &salt[..], &config).map_err(|_| Error::Argon2)?;
+ enc_key.copy_from_slice(&hash);
+ }
+ _ => {
+ // todo throw error or switch to enum?
+ }
+ };
+
let mut hash = crate::locked::Vec::new();
hash.extend(std::iter::repeat(0).take(32));
pbkdf2::pbkdf2::<hmac::Hmac<sha2::Sha256>>(