Skip to content

Commit

Permalink
refactor: rename a few params, clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed May 24, 2021
1 parent f9cbfe6 commit 1e37407
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ path = "wasm/lib.rs"

[dependencies]
wee_alloc = "0.4.5"
argon2 = "0.2.0"
argon2 = { version = "0.2.0", features = [ "password-hash" ] }

[profile.release]
panic = "abort"
Expand Down
76 changes: 40 additions & 36 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function transferToWasm(arr: Uint8Array): [number, number] {
* - **argon2i**: optimized to resist side-channel attacks
* - **argon2id**: (default) hybrid version
*/
export const algorithm = {
export const variant = {
argon2d: 0,
argon2i: 1,
argon2id: 3,
argon2id: 2,
} as const;

/**
Expand All @@ -42,65 +42,69 @@ export interface Argon2Params {
/**
* The associated data
*/
ad?: Uint8Array;
ad: Uint8Array;
/**
* The Argon2 algorithm, see {@link algorithm}
* The Argon2 variant, see {@link variant}
*/
algorithm?: typeof algorithm[keyof typeof algorithm];
variant: typeof variant[keyof typeof variant];
/**
* Number of iterations, between 1 and (2^32)-1.
* Memory size, expressed in kilobytes, between 1 and (2^32)-1.
*/
timeCost?: number;
m: number;
/**
* Memory size, expressed in kilobytes, between 1 and (2^32)-1.
* Number of iterations, between 1 and (2^32)-1.
*/
memoryCost?: number;
t: number;
/**
* The number of lanes
* Degree of parallelism, between 1 and 255
*/
lanes?: number;
p: number;
/**
* The length of the output in bytes
*/
outputLength?: number;
outputLength: number;
/**
* The Argon2 version, see {@link version}
*/
version?: typeof version[keyof typeof version];
version: typeof version[keyof typeof version];
}

function defaultParams(params?: Partial<Argon2Params>): Argon2Params {
return {
secret: params?.secret,
ad: params?.ad ?? new Uint8Array(),
variant: params?.variant ?? variant.argon2id,
m: params?.m ?? 4096,
t: params?.t ?? 3,
p: params?.p ?? 1,
outputLength: params?.outputLength ?? 32,
version: params?.version ?? version.V0x13,
};
}

/**
* Computes a hash for the password, salt and parameters
* Computes the hash for the password, salt and parameters
*/
export function hash(
password: Uint8Array,
salt: Uint8Array,
params?: Argon2Params,
partialParams?: Partial<Argon2Params>,
): Uint8Array {
const secret = params?.secret;
const ad = params?.ad ?? new Uint8Array();
const alg = params?.algorithm ?? algorithm.argon2id;
const timeCost = params?.timeCost ?? 3;
const memoryCost = params?.memoryCost ?? 4096;
const lanes = params?.lanes ?? 1;
const outLen = params?.outputLength ?? 32;
const ver = params?.version ?? version.V0x13;

console.log();
const params = defaultParams(partialParams);

const [pwdPtr, pwdLen] = transferToWasm(password);
const [saltPtr, saltLen] = transferToWasm(salt);

let secretPtr = 0;
let secretLen = 0;
if (secret !== undefined) {
[secretPtr, secretLen] = transferToWasm(secret);
if (params.secret !== undefined) {
[secretPtr, secretLen] = transferToWasm(params.secret);
}

const adLen = ad.length;
const adLen = params.ad.length;
const adPtr = alloc(adLen);
const adArr = new Uint8Array(memory.buffer, adPtr, adLen);
adArr.set(ad);
adArr.set(params.ad);

const outPtr = hashRaw(
pwdPtr,
Expand All @@ -111,13 +115,13 @@ export function hash(
secretLen,
adPtr,
adLen,
alg,
timeCost,
memoryCost,
lanes,
outLen,
ver,
params.variant,
params.m,
params.t,
params.p,
params.outputLength,
params.version,
);

return new Uint8Array(memory.buffer, outPtr, outLen);
return new Uint8Array(memory.buffer, outPtr, params.outputLength);
}
50 changes: 25 additions & 25 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { algorithm, hash, version } from "./mod.ts";
import { hash, variant, version } from "./mod.ts";

const encoder = new TextEncoder();
const encode = (str: string) => encoder.encode(str);
Expand All @@ -16,11 +16,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2d,
variant: variant.argon2d,
version: version.V0x10,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"2ec0d925358f5830caf0c1cc8a3ee58b34505759428b859c79b72415f51f9221",
);
Expand All @@ -32,11 +32,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2d,
variant: variant.argon2d,
version: version.V0x13,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"955e5d5b163a1b60bba35fc36d0496474fba4f6b59ad53628666f07fb2f93eaf",
);
Expand All @@ -48,11 +48,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2i,
variant: variant.argon2i,
version: version.V0x10,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"f6c4db4a54e2a370627aff3db6176b94a2a209a62c8e36152711802f7b30c694",
);
Expand All @@ -64,11 +64,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2i,
variant: variant.argon2i,
version: version.V0x13,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"c1628832147d9720c5bd1cfd61367078729f6dfb6f8fea9ff98158e0d7816ed0",
);
Expand All @@ -80,11 +80,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2id,
variant: variant.argon2id,
version: version.V0x10,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"980ebd24a4e667f16346f9d4a78b175728783613e0cc6fb17c2ec884b16435df",
);
Expand All @@ -96,11 +96,11 @@ Deno.test({
fn: () => {
assertEquals(
hex(hash(password, salt, {
algorithm: algorithm.argon2id,
variant: variant.argon2id,
version: version.V0x13,
timeCost: 2,
memoryCost: 65536,
lanes: 1,
t: 2,
m: 65536,
p: 1,
})),
"09316115d5cf24ed5a15a31a3ba326e5cf32edc24702987c02b6566f61913cf7",
);
Expand Down
17 changes: 9 additions & 8 deletions wasm/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![no_std]
#![feature(core_intrinsics, lang_items, alloc_error_handler)]

use argon2::{Algorithm, Argon2, Version};
use argon2::Algorithm;
use argon2::Argon2;
use argon2::Version;

extern crate alloc;
extern crate wee_alloc;
Expand Down Expand Up @@ -55,10 +57,10 @@ pub unsafe fn hash_raw(
ad_ptr: *const u8,
ad_len: usize,

alg: usize,
time_cost: u32,
memory_cost: u32,
lanes: u32,
variant: usize,
m: u32,
t: u32,
p: u32,
out_len: usize,
version: usize,
) -> *const u8 {
Expand All @@ -71,7 +73,7 @@ pub unsafe fn hash_raw(
};
let ad = core::slice::from_raw_parts(ad_ptr, ad_len);

let alg = match alg {
let alg = match variant {
0 => Algorithm::Argon2d,
1 => Algorithm::Argon2i,
_ => Algorithm::Argon2id,
Expand All @@ -81,8 +83,7 @@ pub unsafe fn hash_raw(
_ => Version::V0x13,
};

let argon2 =
Argon2::new(secret, time_cost, memory_cost, lanes, version).unwrap();
let argon2 = Argon2::new(secret, t, m, p, version).unwrap();
let out_ptr = alloc(out_len);
let out = core::slice::from_raw_parts_mut(out_ptr, out_len);

Expand Down
8 changes: 4 additions & 4 deletions wasm/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const hashRaw = instance.exports.hash_raw as (
secretLen: number,
adPtr: number,
adLen: number,
alg: number,
tCost: number,
mCost: number,
lanes: number,
variant: number,
m: number,
t: number,
p: number,
outLen: number,
version: number,
) => number;
2 changes: 1 addition & 1 deletion wasm/wasm.js

Large diffs are not rendered by default.

0 comments on commit 1e37407

Please sign in to comment.