authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-06-22 16:25:58+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2026-07-18 13:34:16+02:00
log297f3613d87a6bc1b1b421d5c1d09da94ddadebe
tree67593945eb6971a9276987c3ff3f5bcb48096f00
parentecb9b3d16dd5c5a18d376c2e312e0a0a65fd3014

crypto.argon2: add a function to return the required memory

Add a `calcSize()` function to return the number of bytes required for the argon2 scratch buffer.

1 files changed, 13 insertions(+), 4 deletions(-)

lib/std/crypto/argon2.zig+13-4
......@@ -471,6 +471,18 @@ fn indexAlpha(
471471 return ref_lane * lanes + @as(u32, @intCast(((s + m - (p + 1)) % lanes)));
472472}
473473
474fn blockCount(params: Params) u32 {
475 return @max(
476 params.m / (sync_points * params.p) * (sync_points * params.p),
477 2 * sync_points * params.p,
478 );
479}
480
481/// Compute the number of bytes of scratch memory `kdf` needs for `params`.
482pub fn calcSize(params: Params) usize {
483 return blockCount(params) * @sizeOf([block_length]u64);
484}
485
474486/// Derives a key from the password, salt, and argon2 parameters.
475487///
476488/// Derived key has to be at least 4 bytes length.
......@@ -494,10 +506,7 @@ pub fn kdf(
494506 if (params.m / 8 < params.p) return KdfError.WeakParameters;
495507
496508 var h0 = initHash(password, salt, params, derived_key.len, mode);
497 const memory = @max(
498 params.m / (sync_points * params.p) * (sync_points * params.p),
499 2 * sync_points * params.p,
500 );
509 const memory = blockCount(params);
501510
502511 var blocks = try Blocks.initCapacity(allocator, memory);
503512 defer blocks.deinit();