Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Argon2id. #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
initial
  • Loading branch information
bryant committed Dec 3, 2017
commit 1543238005c7e71d10c25b739b5e559681a408b5
32 changes: 24 additions & 8 deletions src/argon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use workers::Workers;
pub enum Variant {
Argon2d = 0,
Argon2i = 1,
Argon2id = 2,
}

const DEF_B2HASH_LEN: usize = 64;
Expand Down Expand Up @@ -267,15 +268,24 @@ impl Argon2 {
fn fill_slice(&self, blks: &mut Matrix, pass: u32, lane: u32, slice: u32,
offset: u32) {
let mut jgen = Gen2i::new(offset as usize, pass, lane, slice,
self.lanes * self.lanelen, self.passes);
self.lanes * self.lanelen, self.passes,
self.variant);
let slicelen = self.lanelen / SLICES_PER_LANE;

use Variant::*;

for idx in offset..slicelen {
let (j1, j2) = if self.variant == Variant::Argon2i {
jgen.nextj()
} else {
let col = self.prev(slice * slicelen + idx);
split_u64((blks[(lane, col)])[0].0)
let (j1, j2) = match self.variant {
Argon2i => jgen.nextj(),
Argon2d => {
let col = self.prev(slice * slicelen + idx);
split_u64((blks[(lane, col)])[0].0)
},
Argon2id if pass == 0 && slice < 2 => jgen.nextj(),
Argon2id => {
let col = self.prev(slice * slicelen + idx);
split_u64((blks[(lane, col)])[0].0)
},
};
self.fill_block(blks, pass, lane, slice, idx, j1, j2);
}
Expand Down Expand Up @@ -382,13 +392,13 @@ struct Gen2i {
impl Gen2i {
#[cfg_attr(rustfmt, rustfmt_skip)]
fn new(start_at: usize, pass: u32, lane: u32, slice: u32, totblocks: u32,
totpasses: u32)
totpasses: u32, variant: Variant)
-> Gen2i {
use block::zero;

let mut rv = Gen2i { arg: zero(), pseudos: zero(), idx: start_at };
let args = [(pass, lane), (slice, totblocks),
(totpasses, Variant::Argon2i as u32)];
(totpasses, variant as u32)];
for (k, &(lo, hi)) in rv.arg.iter_mut().zip(args.into_iter()) {
*k = u64x2(lo as u64, hi as u64);
}
Expand Down Expand Up @@ -633,4 +643,10 @@ mod tests {
compare_kats("kats/0x10/argon2d", Variant::Argon2d, Version::_0x10);
compare_kats("kats/0x13/argon2d", Variant::Argon2d, Version::_0x13);
}

#[test]
fn argon2id_kat() {
compare_kats("kats/0x10/argon2id", Variant::Argon2id, Version::_0x10);
compare_kats("kats/0x13/argon2id", Variant::Argon2id, Version::_0x13);
}
}
20 changes: 16 additions & 4 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ impl<'a> Parser<'a> {
self.err()
}

fn read_until(&mut self, stopchar: u8) -> &'a [u8] {
let start = self.pos;
let stop = |c: &u8| *c == stopchar;
self.pos = match self.enc[self.pos..].iter().position(stop) {
None => self.enc.len() - 1,
Some(end) => self.pos + end,
};
&self.enc[start..self.pos]
}

fn read_u32(&mut self) -> Parsed<u32> {
let is_digit = |c: u8| 48 <= c && c <= 57;
let mut end = self.pos;
Expand Down Expand Up @@ -239,10 +249,11 @@ impl Encoded {

try_unit!(p.expect(b"$argon2"));

let variant = match try!(p.one_of(b"di")) {
v if v == 'd' as u8 => Variant::Argon2d,
v if v == 'i' as u8 => Variant::Argon2i,
_ => unreachable!(),
let variant = match p.read_until('$' as u8) {
b"d" => Variant::Argon2d,
b"i" => Variant::Argon2i,
b"id" => Variant::Argon2id,
x => return Err(p.pos - x.len()),
};

try_unit!(p.expect(b"$"));
Expand Down Expand Up @@ -309,6 +320,7 @@ impl Encoded {
let vcode = |v| match v {
Variant::Argon2i => "i",
Variant::Argon2d => "d",
Variant::Argon2id => "id",
};
let b64 = |x| String::from_utf8(base64_no_pad(x)).unwrap()
;
Expand Down