Skip to content

Commit 389d64c

Browse files
committed
Move all hash parameters into options argument
1 parent 0238962 commit 389d64c

7 files changed

Lines changed: 173 additions & 216 deletions

File tree

benchmark/crypto/argon2.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ const {
1111
const bench = common.createBenchmark(main, {
1212
mode: ['sync', 'async'],
1313
algorithm: ['argon2d', 'argon2i', 'argon2id'],
14-
iterations: [1, 3],
14+
passes: [1, 3],
1515
parallelism: [2, 4, 8],
1616
memory: [2 ** 11, 2 ** 16, 2 ** 21],
1717
n: [50],
1818
});
1919

20-
function measureSync(n, pass, salt, options) {
20+
function measureSync(n, message, nonce, options) {
2121
bench.start();
2222
for (let i = 0; i < n; ++i)
23-
argon2Sync(pass, salt, 64, options);
23+
argon2Sync({ ...options, message, nonce, tagLength: 64 });
2424
bench.end(n);
2525
}
2626

27-
function measureAsync(n, pass, salt, options) {
27+
function measureAsync(n, message, nonce, options) {
2828
let remaining = n;
2929
function done(err) {
3030
assert.ifError(err);
@@ -33,15 +33,15 @@ function measureAsync(n, pass, salt, options) {
3333
}
3434
bench.start();
3535
for (let i = 0; i < n; ++i)
36-
argon2(pass, salt, 64, options, done);
36+
argon2({ ...options, message, nonce, tagLength: 64 }, done);
3737
}
3838

3939
function main({ n, mode, ...options }) {
40-
// Pass, salt, secret, ad & output length does not affect performance
41-
const pass = randomBytes(32);
42-
const salt = randomBytes(16);
40+
// Message, nonce, secret, associated data & tag length do not affect performance
41+
const message = randomBytes(32);
42+
const nonce = randomBytes(16);
4343
if (mode === 'sync')
44-
measureSync(n, pass, salt, options);
44+
measureSync(n, message, nonce, options);
4545
else
46-
measureAsync(n, pass, salt, options);
46+
measureAsync(n, message, nonce, options);
4747
}

deps/ncrypto/ncrypto.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,14 +1861,14 @@ DataPointer pbkdf2(const Digest& md,
18611861
#ifndef OPENSSL_NO_ARGON2
18621862
DataPointer argon2(const Buffer<const char>& pass,
18631863
const Buffer<const unsigned char>& salt,
1864-
const Buffer<const unsigned char>& secret,
1865-
const Buffer<const unsigned char>& ad,
1866-
Argon2Type type,
1867-
uint32_t iter,
18681864
uint32_t lanes,
1865+
size_t length,
18691866
uint32_t memcost,
1867+
uint32_t iter,
18701868
uint32_t version,
1871-
size_t length) {
1869+
const Buffer<const unsigned char>& secret,
1870+
const Buffer<const unsigned char>& ad,
1871+
Argon2Type type) {
18721872
ClearErrorOnReturn clearErrorOnReturn;
18731873

18741874
std::string_view algorithm;
@@ -1917,12 +1917,12 @@ DataPointer argon2(const Buffer<const char>& pass,
19171917
OSSL_KDF_PARAM_PASSWORD, const_cast<char*>(pass.data), pass.len));
19181918
params.push_back(OSSL_PARAM_construct_octet_string(
19191919
OSSL_KDF_PARAM_SALT, const_cast<unsigned char*>(salt.data), salt.len));
1920-
params.push_back(OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iter));
19211920
params.push_back(OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_THREADS, &lanes));
19221921
params.push_back(
19231922
OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, &lanes));
19241923
params.push_back(
19251924
OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, &memcost));
1925+
params.push_back(OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ITER, &iter));
19261926

19271927
if (ad.len != 0) {
19281928
params.push_back(OSSL_PARAM_construct_octet_string(

deps/ncrypto/ncrypto.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,14 +1544,14 @@ enum class Argon2Type { ARGON2I, ARGON2D, ARGON2ID };
15441544

15451545
DataPointer argon2(const Buffer<const char>& pass,
15461546
const Buffer<const unsigned char>& salt,
1547-
const Buffer<const unsigned char>& secret,
1548-
const Buffer<const unsigned char>& ad,
1549-
Argon2Type type,
1550-
uint32_t iter,
15511547
uint32_t lanes,
1548+
size_t length,
15521549
uint32_t memcost,
1550+
uint32_t iter,
15531551
uint32_t version,
1554-
size_t length);
1552+
const Buffer<const unsigned char>& secret,
1553+
const Buffer<const unsigned char>& ad,
1554+
Argon2Type type);
15551555
#endif // !OPENSSL_NO_ARGON2
15561556

15571557
// ============================================================================

doc/api/crypto.md

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,35 +2949,34 @@ Does not perform any other validation checks on the certificate.
29492949

29502950
## `node:crypto` module methods and properties
29512951

2952-
### `crypto.argon2(password, salt, keylen, options, callback)`
2952+
### `crypto.argon2(options, callback)`
29532953

29542954
<!-- YAML
29552955
added: REPLACEME
29562956
-->
29572957

29582958
> Stability: 1.2 - Release candidate
29592959
2960-
* `password` {string|ArrayBuffer|Buffer|TypedArray|DataView}
2961-
* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
2962-
least 8 bytes long.
2963-
* `keylen` {number} The length of the key to generate. Must be greater than 4 and less
2964-
than `2**32-1`.
29652960
* `options` {Object}
2966-
* `algorithm` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or
2967-
`"argon2id"`.
2968-
* `iterations` {number} Number of iterations (passes). Must be greater than 1 and
2969-
less than `2**32-1`.
2961+
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView}
2962+
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
2963+
least 8 bytes long.
29702964
* `parallelism` {number} Parallelization parameter (number of lanes and threads).
29712965
Must be greater than 1 and less than `2**24-1`.
2966+
* `tagLength` {number} The length of the key to generate. Must be greater than 4 and
2967+
less than `2**32-1`.
29722968
* `memory` {number} Memory cost in 1KiB blocks. Must be greater than
29732969
`8 * parallelism` and less than `2**32-1`. The actual number of blocks is rounded
29742970
down to the nearest multiple of `4 * parallelism`.
2971+
* `passes` {number} Number of passes (iterations). Must be greater than 1 and less
2972+
than `2**32-1`.
29752973
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView} Random additional input,
29762974
similar to the salt, that should **NOT** be stored with the derived key. Also known
29772975
as a pepper. If used, must have a length not greater than `2**32-1` bytes.
29782976
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional data to
29792977
be added to the hash, functionally equivalent to salt or secret, but meant for
29802978
non-random data. If used, must have a length not greater than `2**32-1` bytes.
2979+
* `type` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
29812980
* `callback` {Function}
29822981
* `err` {Error}
29832982
* `derivedKey` {Buffer}
@@ -2986,10 +2985,10 @@ Provides an asynchronous [argon2][] implementation. Argon2 is a password-based
29862985
key derivation function that is designed to be expensive computationally and
29872986
memory-wise in order to make brute-force attacks unrewarding.
29882987

2989-
The `salt` should be as unique as possible. It is recommended that a salt is
2988+
The `nonce` should be as unique as possible. It is recommended that a nonce is
29902989
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
29912990

2992-
When passing strings for `password`, `salt`, `secret` or `associatedData`, please
2991+
When passing strings for `message`, `nonce`, `secret` or `associatedData`, please
29932992
consider [caveats when using strings as inputs to cryptographic APIs][].
29942993

29952994
The `callback` function is called with two arguments: `err` and `derivedKey`.
@@ -3005,8 +3004,8 @@ const {
30053004
randomBytes,
30063005
} = await import('node:crypto');
30073006

3008-
const salt = randomBytes(16);
3009-
argon2('password', salt, 64, { iterations: 3, parallelism: 4, memory: 65536 }, (err, derivedKey) => {
3007+
const nonce = randomBytes(16);
3008+
argon2({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 }, (err, derivedKey) => {
30103009
if (err) throw err;
30113010
console.log(derivedKey.toString('hex')); // '0de3036...22afcc5'
30123011
});
@@ -3018,9 +3017,9 @@ const {
30183017
randomBytes,
30193018
} = require('node:crypto');
30203019

3021-
randomBytes(16, (err, salt) => {
3020+
randomBytes(16, (err, nonce) => {
30223021
if (err) throw err;
3023-
argon2('password', salt, 64, { iterations: 3, parallelism: 4, memory: 65536 }, (err, derivedKey) => {
3022+
argon2({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 }, (err, derivedKey) => {
30243023
if (err) throw err;
30253024
console.log(derivedKey.toString('hex')); // '0de3036...22afcc5'
30263025
});
@@ -3035,37 +3034,36 @@ added: REPLACEME
30353034

30363035
> Stability: 1.2 - Release candidate
30373036
3038-
* `password` {string|Buffer|TypedArray|DataView}
3039-
* `salt` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
3040-
least 8 bytes long.
3041-
* `keylen` {number} The length of the key to generate. Must be greater than 4 and less
3042-
than `2**32-1`.
30433037
* `options` {Object}
3044-
* `algorithm` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or
3045-
`"argon2id"`.
3046-
* `iterations` {number} Number of iterations (passes). Must be greater than 1 and
3047-
less than `2**32-1`.
3038+
* `message` {string|ArrayBuffer|Buffer|TypedArray|DataView}
3039+
* `nonce` {string|ArrayBuffer|Buffer|TypedArray|DataView} The salt value. Must be at
3040+
least 8 bytes long.
30483041
* `parallelism` {number} Parallelization parameter (number of lanes and threads).
30493042
Must be greater than 1 and less than `2**24-1`.
3043+
* `tagLength` {number} The length of the key to generate. Must be greater than 4 and
3044+
less than `2**32-1`.
30503045
* `memory` {number} Memory cost in 1KiB blocks. Must be greater than
30513046
`8 * parallelism` and less than `2**32-1`. The actual number of blocks is rounded
30523047
down to the nearest multiple of `4 * parallelism`.
3048+
* `passes` {number} Number of passes (iterations). Must be greater than 1 and less
3049+
than `2**32-1`.
30533050
* `secret` {string|ArrayBuffer|Buffer|TypedArray|DataView} Random additional input,
30543051
similar to the salt, that should **NOT** be stored with the derived key. Also known
30553052
as a pepper. If used, must have a length not greater than `2**32-1` bytes.
30563053
* `associatedData` {string|ArrayBuffer|Buffer|TypedArray|DataView} Additional data to
30573054
be added to the hash, functionally equivalent to salt or secret, but meant for
30583055
non-random data. If used, must have a length not greater than `2**32-1` bytes.
3056+
* `type` {string} Variant of Argon2, one of `"argon2d"`, `"argon2i"` or `"argon2id"`.
30593057
* Returns: {Buffer}
30603058

30613059
Provides a synchronous [argon2][] implementation. Argon2 is a password-based
30623060
key derivation function that is designed to be expensive computationally and
30633061
memory-wise in order to make brute-force attacks unrewarding.
30643062

3065-
The `salt` should be as unique as possible. It is recommended that a salt is
3063+
The `nonce` should be as unique as possible. It is recommended that a nonce is
30663064
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
30673065

3068-
When passing strings for `password`, `salt`, `secret` or `associatedData`, please
3066+
When passing strings for `message`, `nonce`, `secret` or `associatedData`, please
30693067
consider [caveats when using strings as inputs to cryptographic APIs][].
30703068

30713069
An exception is thrown when key derivation fails, otherwise the derived key is
@@ -3079,10 +3077,9 @@ const {
30793077
argon2Sync,
30803078
randomBytes,
30813079
} = await import('node:crypto');
3082-
// Using the factory defaults.
30833080

3084-
const salt = randomBytes(16);
3085-
const key = argon2Sync('password', salt, 64, { iterations: 3, parallelism: 4, memory: 65536 });
3081+
const nonce = randomBytes(16);
3082+
const key = argon2Sync({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 });
30863083
console.log(key.toString('hex')); // '3745e48...08d59ae'
30873084
```
30883085

@@ -3091,10 +3088,9 @@ const {
30913088
argon2Sync,
30923089
randomBytes,
30933090
} = require('node:crypto');
3094-
// Using the factory defaults.
30953091

3096-
const salt = randomBytes(16);
3097-
const key = argon2Sync('password', salt, 64, { iterations: 3, parallelism: 4, memory: 65536 });
3092+
const nonce = randomBytes(16);
3093+
const key = argon2Sync({ message: 'password', nonce, parallelism: 4, tagLength: 64, memory: 65536, passes: 3 });
30983094
console.log(key.toString('hex')); // '3745e48...08d59ae'
30993095
```
31003096

0 commit comments

Comments
 (0)