// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Argon2: passlib's `passlib.handlers.argon2`. //! //! Alex Biryukov, Daniel Dinu and Dmitry Khovratovich's winner of the //! Password Hashing Competition, 2015, standardized as RFC 9106. Like scrypt //! it makes memory the cost of a guess, filling `memory_cost` KiB of blocks //! and making `time_cost` passes over them in `parallelism` independent //! lanes. It comes in three types: Argon2d reads memory in an order that //! depends on the password, which resists GPUs best and leaks timing; //! Argon2i reads it in a fixed order, which leaks nothing; Argon2id does the //! first half pass like Argon2i and the rest like Argon2d, and is the one to //! use. //! //! The format is the PHC string the reference implementation writes: //! `$argon2id$v=19$m=65536,t=3,p=4$$`, the salt and digest in //! base64 without padding. `v=19` is version 1.3, which changed how later //! passes combine with earlier ones; a hash with no `v=` is version 1.0, //! `0x10`, from before the version was written down. //! //! ## Which parser passlib uses //! //! passlib 1.7.4 verifies through `argon2_cffi`, which hands the whole //! string to the reference implementation's own decoder, and parses with its //! own regular expression only for `needs_update`. The two disagree, and //! this follows each where passlib uses it: //! //! * `verify` reads the string as libargon2 does. Numbers have no leading //! zeros, base64 has no padding and no stray bits, the string ends at the //! digest (or at a NUL, since it is a C string), and the `keyid` and //! `data` fields are not understood. The version is not checked: any //! version but 1.0 computes as 1.3 with its own number hashed in, which //! no one writes, so a hash with a version other than 16 or 19 verifies //! nothing here rather than something the implementation below cannot //! compute. //! * `needsUpdate` reads it as passlib's pattern does, with Python's `int()` //! and lenient base64: leading zeros, `data=`, padding and a missing digest //! are accepted, `keyid=` is refused, and the version must be 16 or 19. const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; const Mode = std.crypto.pwhash.argon2.Mode; const testing = std.testing; const crypto_ext = @import("std_crypto_ext"); const encoding = @import("../encoding.zig"); const std64 = encoding.std64; const handler = @import("../handler.zig"); const Context = handler.Context; const Error = handler.Error; const python = @import("../python.zig"); pub const default_rounds = 3; pub const default_memory_cost = 65536; pub const default_parallelism = 4; pub const default_salt_size = 16; pub const default_digest_size = 32; pub const min_salt_size = 8; pub const min_digest_size = 16; pub const min_memory_cost = 8; /// libargon2's `ARGON2_MAX_LANES`. pub const max_parallelism = (1 << 24) - 1; /// The three variants. pub const Type = enum { id, i, d, fn mode(t: Type) Mode { return switch (t) { .id => .argon2id, .i => .argon2i, .d => .argon2d, }; } fn fromName(s: []const u8) ?Type { inline for (.{ .id, .i, .d }) |t| { if (std.mem.eql(u8, s, @tagName(t))) return t; } return null; } }; /// `bytes` as unpadded base64, written in pieces so that no buffer need /// hold all of it. fn writeB64s(w: *Io.Writer, bytes: []const u8) Io.Writer.Error!void { var i: usize = 0; while (i < bytes.len) { // A multiple of three, so the pieces join without padding between. const n = @min(bytes.len - i, 48); var buf: [64]u8 = undefined; try w.writeAll(std64.encode(&buf, bytes[i..][0..n])); i += n; } } /// What libargon2's `decode_string` reads out of a hash. const Decoded = struct { version: u32, m: u32, t: u32, p: u32, salt: []u8, digest: []u8, fn deinit(d: Decoded, gpa: Allocator) void { gpa.free(d.salt); gpa.free(d.digest); } }; /// libargon2's reader over a hash: prefixes that must match exactly, /// decimals without leading zeros, and strict base64. const Reader = struct { s: []const u8, pos: usize = 0, fn expect(r: *Reader, prefix: []const u8) Error!void { if (!std.mem.startsWith(u8, r.s[r.pos..], prefix)) return error.MalformedHash; r.pos += prefix.len; } /// `decode_decimal` and the `UINT32_MAX` check after it. fn decimal(r: *Reader) Error!u32 { const start = r.pos; while (r.pos < r.s.len and std.ascii.isDigit(r.s[r.pos])) r.pos += 1; const digits = r.s[start..r.pos]; if (digits.len == 0 or (digits[0] == '0' and digits.len > 1)) return error.MalformedHash; return std.fmt.parseInt(u32, digits, 10) catch error.MalformedHash; } /// `from_base64`: characters of the standard alphabet up to the first /// that is not, with no stray bits left over. fn base64(r: *Reader, gpa: Allocator) Error![]u8 { const start = r.pos; while (r.pos < r.s.len and std64.value(r.s[r.pos]) != null) r.pos += 1; const run = r.s[start..r.pos]; const len = try std64.decodedLen(run.len); if (!std64.hasCleanPadding(run)) return error.MalformedHash; const out = try gpa.alloc(u8, len); errdefer gpa.free(out); _ = try std64.decode(out, run); return out; } }; /// The type libargon2 is asked to verify as: passlib's `_byte_ident_map` /// looked up by everything to the second `$`, Argon2i if that is unknown. fn verifyType(h: []const u8) Type { if (h.len < 2) return .i; const second = std.mem.findScalar(u8, h[1..], '$') orelse return .i; const prefix = h[0 .. second + 2]; if (!std.mem.startsWith(u8, prefix, "$argon2")) return .i; return Type.fromName(prefix["$argon2".len .. prefix.len - 1]) orelse .i; } /// libargon2's `decode_string` and `validate_inputs`, over `h` up to its /// first NUL. fn decodeForVerify(gpa: Allocator, ty: Type, h: []const u8) Error!Decoded { var r: Reader = .{ .s = h }; try r.expect("$argon2"); try r.expect(@tagName(ty)); var version: u32 = 0x10; if (std.mem.startsWith(u8, r.s[r.pos..], "$v=")) { r.pos += 3; version = try r.decimal(); } try r.expect("$m="); const m = try r.decimal(); try r.expect(",t="); const t = try r.decimal(); try r.expect(",p="); const p = try r.decimal(); try r.expect("$"); const salt = try r.base64(gpa); errdefer gpa.free(salt); try r.expect("$"); const digest = try r.base64(gpa); errdefer gpa.free(digest); if (digest.len < 4 or salt.len < min_salt_size) return error.MalformedHash; // The C computes `8 * lanes` in 32 bits; a product that wraps is caught // by the lane limit below either way. if (m < min_memory_cost or m < @as(u32, 8) *% p) return error.MalformedHash; if (t < 1 or p < 1 or p > max_parallelism) return error.MalformedHash; if (r.pos != r.s.len) return error.MalformedHash; return .{ .version = version, .m = m, .t = t, .p = p, .salt = salt, .digest = digest }; } /// What passlib's `from_string` reads, as far as `needsUpdate` needs it. const Settings = struct { type: Type, version: u32, memory_cost: u64, rounds: u32, parallelism: u64, /// The digest's length in bytes, or null for a configuration string. digest_len: ?usize, }; /// passlib's `_hash_regex`, then the checks its constructor makes. fn parseSettings(h: []const u8) Error!Settings { var pos: usize = 0; const s = h; if (!std.mem.startsWith(u8, s, "$argon2")) return error.MalformedHash; pos = "$argon2".len; const type_start = pos; while (pos < s.len and std.ascii.isLower(s[pos])) pos += 1; const type_name = s[type_start..pos]; if (type_name.len == 0) return error.MalformedHash; if (pos >= s.len or s[pos] != '$') return error.MalformedHash; pos += 1; const digitsAt = struct { fn f(str: []const u8, at: *usize) Error![]const u8 { const start = at.*; while (at.* < str.len and std.ascii.isDigit(str[at.*])) at.* += 1; if (at.* == start) return error.MalformedHash; return str[start..at.*]; } }.f; const expect = struct { fn f(str: []const u8, at: *usize, prefix: []const u8) Error!void { if (!std.mem.startsWith(u8, str[at.*..], prefix)) return error.MalformedHash; at.* += prefix.len; } }.f; var version_digits: ?[]const u8 = null; if (std.mem.startsWith(u8, s[pos..], "v=")) { pos += 2; version_digits = try digitsAt(s, &pos); try expect(s, &pos, "$"); } try expect(s, &pos, "m="); const m_digits = try digitsAt(s, &pos); try expect(s, &pos, ",t="); const t_digits = try digitsAt(s, &pos); try expect(s, &pos, ",p="); const p_digits = try digitsAt(s, &pos); // `[^,$]+` after an optional prefix: consumed only if non-empty. const optionalField = struct { fn f(str: []const u8, at: *usize, prefix: []const u8) ?[]const u8 { if (!std.mem.startsWith(u8, str[at.*..], prefix)) return null; var end = at.* + prefix.len; const start = end; while (end < str.len and str[end] != ',' and str[end] != '$') end += 1; if (end == start) return null; at.* = end; return str[start..end]; } }.f; const keyid = optionalField(s, &pos, ",keyid="); const data = optionalField(s, &pos, ",data="); var salt: ?[]const u8 = null; var digest: ?[]const u8 = null; if (pos < s.len and s[pos] == '$') { var end = pos + 1; while (end < s.len and s[end] != '$') end += 1; if (end > pos + 1) { salt = s[pos + 1 .. end]; pos = end; // `(?:\$(?P.+))?`: `.` is anything but a newline. if (pos < s.len and s[pos] == '$') { var d_end = pos + 1; while (d_end < s.len and s[d_end] != '\n') d_end += 1; if (d_end > pos + 1) { digest = s[pos + 1 .. d_end]; pos = d_end; } } } } // `$`: the end, or a final newline. if (!(pos == s.len or (pos + 1 == s.len and s[pos] == '\n'))) return error.MalformedHash; if (keyid != null) return error.MalformedHash; const ty = Type.fromName(type_name) orelse return error.MalformedHash; const version: u32 = if (version_digits) |v| blk: { const n = try python.pythonInt(v); if (n != 0x10 and n != 0x13) return error.MalformedHash; break :blk @intCast(n); } else 0x10; const memory_cost = try python.pythonInt(m_digits); if (memory_cost < min_memory_cost) return error.MalformedHash; const rounds = try python.pythonInt(t_digits); if (rounds < 1 or rounds > std.math.maxInt(u32)) return error.MalformedHash; const parallelism = try python.pythonInt(p_digits); if (parallelism < 1) return error.MalformedHash; if (data) |d| _ = try python.pythonB64sDecode(null, d); const salt_len = try python.pythonB64sDecode(null, salt orelse return error.MalformedHash); if (salt_len < min_salt_size or salt_len > std.math.maxInt(u32)) return error.MalformedHash; const digest_len = if (digest) |d| try python.pythonB64sDecode(null, d) else null; return .{ .type = ty, .version = version, .memory_cost = memory_cost, .rounds = @intCast(rounds), .parallelism = parallelism, .digest_len = digest_len, }; } /// passlib's `argon2`. pub const argon2 = struct { pub const name = "argon2"; /// passlib's defaults are `argon2_cffi`'s, which are RFC 9106's second /// recommendation: Argon2id, 64 MiB, three passes, four lanes. pub const Options = struct { /// The type to write. `needsUpdate` reports any other. type: Type = .id, /// The number of passes, `time_cost`, at least 1. rounds: u32 = default_rounds, /// KiB of memory, at least 8 and at least 8 per lane. `needsUpdate` /// reports any other. memory_cost: u32 = default_memory_cost, /// Lanes, at least 1. `needsUpdate` reports any other. parallelism: u32 = default_parallelism, /// The salt, as bytes, at least 8 of them. Null, the default, draws /// `salt_size` random bytes. salt: ?[]const u8 = null, /// The length of a random salt in bytes, at least 8. salt_size: u32 = default_salt_size, /// The length of the digest in bytes, at least 16. `needsUpdate` /// reports any other. digest_size: u32 = default_digest_size, /// `needsUpdate` reports a hash with fewer passes than this, and /// `hash` raises `rounds` to it. min_desired_rounds: ?u32 = null, /// `needsUpdate` reports a hash with more passes than this, and /// `hash` lowers `rounds` to it. max_desired_rounds: ?u32 = null, }; /// `$argon2`, lower-case letters, `$`: passlib's `_ident_regex`, which /// claims types it does not know. pub fn identify(h: []const u8) bool { if (!std.mem.startsWith(u8, h, "$argon2")) return false; var pos: usize = "$argon2".len; const start = pos; while (pos < h.len and std.ascii.isLower(h[pos])) pos += 1; return pos > start and pos < h.len and h[pos] == '$'; } /// What passlib's `using()` checks, and the passes `hash` will use. fn checkOptions(options: Options) Error!u32 { if (options.rounds < 1) return error.InvalidOptions; if (options.parallelism < 1) return error.InvalidOptions; if (options.memory_cost < min_memory_cost) return error.InvalidOptions; if (options.memory_cost < 8 * @as(u64, options.parallelism)) return error.InvalidOptions; if (options.salt_size < min_salt_size) return error.InvalidOptions; if (options.digest_size < min_digest_size) return error.InvalidOptions; if (options.salt) |s| if (s.len < min_salt_size or s.len > std.math.maxInt(u32)) return error.InvalidOptions; if (options.min_desired_rounds) |m| if (m < 1) return error.InvalidOptions; if (options.max_desired_rounds) |m| if (m < 1) return error.InvalidOptions; if (options.min_desired_rounds != null and options.max_desired_rounds != null and options.max_desired_rounds.? < options.min_desired_rounds.?) return error.InvalidOptions; var r = options.rounds; if (options.min_desired_rounds) |m| r = @max(r, m); if (options.max_desired_rounds) |m| r = @min(r, m); return r; } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { const rounds = try checkOptions(options); // libargon2 refuses more lanes than this, and passlib reports it. if (options.parallelism > max_parallelism) return error.InvalidOptions; try handler.checkPasswordLen(password); var random: []u8 = &.{}; defer ctx.gpa.free(random); const salt = options.salt orelse blk: { random = try ctx.gpa.alloc(u8, options.salt_size); break :blk try handler.randomBytes(ctx.io, random); }; const digest = try ctx.gpa.alloc(u8, options.digest_size); defer ctx.gpa.free(digest); try crypto_ext.argon2.kdf(ctx.gpa, digest, password, salt, .{ .t = rounds, .m = options.memory_cost, .p = @intCast(options.parallelism), }, options.type.mode(), .v0x13, ctx.io); try w.print("$argon2{t}$v=19$m={d},t={d},p={d}$", .{ options.type, options.memory_cost, rounds, options.parallelism }); try writeB64s(w, salt); try w.writeAll("$"); try writeB64s(w, digest); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { try handler.checkPasswordLen(password); const ty = verifyType(h); // libargon2 is handed a C string. const hz = h[0 .. std.mem.findScalar(u8, h, 0) orelse h.len]; const d = try decodeForVerify(ctx.gpa, ty, hz); defer d.deinit(ctx.gpa); const version: crypto_ext.argon2.Version = switch (d.version) { 0x10 => .v0x10, 0x13 => .v0x13, // See the top of this file. else => return false, }; const digest = try ctx.gpa.alloc(u8, d.digest.len); defer ctx.gpa.free(digest); try crypto_ext.argon2.kdf(ctx.gpa, digest, password, d.salt, .{ .t = d.t, .m = d.m, // `decodeForVerify` has held it to 2^24 - 1. .p = @intCast(d.p), }, ty.mode(), version, ctx.io); return handler.eql(d.digest, digest); } /// True for a hash of another type, of version 1.0, or with a memory /// cost, digest size or parallelism other than `options`', or with /// passes outside the desired bounds. The number of passes is not /// otherwise compared, as passlib does not compare it. pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = try checkOptions(options); const s = try parseSettings(h); if (s.type != options.type) return true; if (s.version < 0x13) return true; if (s.memory_cost != options.memory_cost) return true; if ((s.digest_len orelse options.digest_size) != options.digest_size) return true; if (s.parallelism != options.parallelism) return true; return handler.roundsOutside(s.rounds, options.min_desired_rounds, options.max_desired_rounds); } }; /// The schemes this module defines, in the order `registry.identify` tries /// them. pub const schemes = .{argon2}; comptime { handler.check(argon2); } const test_ctx: Context = .{ .io = testing.io, .gpa = testing.allocator }; const small: argon2.Options = .{ .salt = "somesalt", .rounds = 1, .memory_cost = 64, .parallelism = 1 }; fn expectHash(password: []const u8, options: argon2.Options, expected: []const u8) !void { var buf: [256]u8 = undefined; var w: Io.Writer = .fixed(&buf); try argon2.hash(test_ctx, password, options, &w); try testing.expectEqualStrings(expected, w.buffered()); try testing.expect(try argon2.verify(test_ctx, password, expected)); try testing.expect(!try argon2.verify(test_ctx, "wrong", expected)); } test "fixed salts, every type" { // passlib 1.7.4 with argon2_cffi: // argon2.using(salt=b"somesalt", rounds=1, memory_cost=64, parallelism=1, ...).hash(...) try expectHash("password", small, "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70"); var o = small; o.type = .i; try expectHash("password", o, "$argon2i$v=19$m=64,t=1,p=1$c29tZXNhbHQ$m+x4L7hN2ZRjBBfcMx29Bo5JdJxIE52dqtM6I/wGjDY"); o.type = .d; try expectHash("password", o, "$argon2d$v=19$m=64,t=1,p=1$c29tZXNhbHQ$Bj+eR+LycLGV0+FiRfL3OgxHJE8XA/vAQ0igWFvefE8"); try expectHash("pass\x00word", small, "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$6oIEETo0BJ7q/vyz7jFW6wFb/GyfaZJ1ejETi5d+Muo"); try expectHash("password", .{ .salt = "somesalt", .rounds = 2, .memory_cost = 64, .parallelism = 2, .digest_size = 16 }, "$argon2id$v=19$m=64,t=2,p=2$c29tZXNhbHQ$06Fg+NIOVokX4kDZhuwsHA"); } test "version 1.0" { // argon2_cffi's hash_secret(..., version=0x10) with passlib's `$v=16$` // removed, as passlib writes it; passlib verifies both spellings. try testing.expect(try argon2.verify(test_ctx, "password", "$argon2i$m=64,t=2,p=1$c29tZXNhbHQ$Bh21/eJH8YBDyij2rzNMFBJxlNjZOXgA8TmVH6qMZno")); try testing.expect(try argon2.verify(test_ctx, "password", "$argon2i$v=16$m=64,t=2,p=1$c29tZXNhbHQ$Bh21/eJH8YBDyij2rzNMFBJxlNjZOXgA8TmVH6qMZno")); try testing.expect(!try argon2.verify(test_ctx, "passwordx", "$argon2i$m=64,t=2,p=1$c29tZXNhbHQ$Bh21/eJH8YBDyij2rzNMFBJxlNjZOXgA8TmVH6qMZno")); try testing.expect(try argon2.needsUpdate("$argon2i$m=64,t=2,p=1$c29tZXNhbHQ$Bh21/eJH8YBDyij2rzNMFBJxlNjZOXgA8TmVH6qMZno", .{ .type = .i, .memory_cost = 64, .parallelism = 1 })); } test "verify reads as libargon2 does" { const h = "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70"; // A C string ends at its NUL. try testing.expect(try argon2.verify(test_ctx, "password", h ++ "\x00garbage")); // A version libargon2 would compute and no one writes. try testing.expect(!try argon2.verify(test_ctx, "password", "$argon2id$v=18$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70")); // Four bytes of digest is enough to compare. try testing.expect(!try argon2.verify(test_ctx, "password", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$AAAAAA")); // passlib 1.7.4 refuses each of these in verify. for ([_][]const u8{ "$argon2id$v=19$m=064,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=019$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1,keyid=c29tZQ$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1,data=c29tZQ$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ=$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHR$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbA$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$AAAA", h ++ "\n", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG71", "$argon2I$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2x$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=4294967296,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=15,t=1,p=2$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=0$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=0,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=7,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=16777216$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXN!bHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", h ++ "$x", "$argon2id$v=19$m=64,t=1,p=1$$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", }) |v| try testing.expectError(error.MalformedHash, argon2.verify(test_ctx, "password", v)); } test "needsUpdate reads as passlib's pattern does" { const h = "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70"; const o: argon2.Options = .{ .memory_cost = 64, .parallelism = 1 }; try testing.expect(!try argon2.needsUpdate(h, o)); try testing.expect(try argon2.needsUpdate(h, .{})); // Accepted by passlib's pattern though libargon2 would refuse them. for ([_][]const u8{ "$argon2id$v=19$m=064,t=01,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=019$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1,data=c29tZQ$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ=$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHR$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ", h ++ "\n", }) |v| try testing.expect(!try argon2.needsUpdate(v, o)); // Parsed, and different from what `o` asks for. for ([_][]const u8{ "$argon2id$v=19$m=4294967296,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=15,t=1,p=2$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$AAAA", "$argon2id$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2i$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", }) |v| try testing.expect(try argon2.needsUpdate(v, o)); // Refused by it. for ([_][]const u8{ "$argon2id$v=18$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=20$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1,keyid=c29tZQ$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbA$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$", "$argon2I$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2x$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=0$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=0,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=7,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1$c29tZXN!bHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", h ++ "$x", "$argon2id$v=19$m=64,t=1,p=1$$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70", "$argon2id$v=19$m=64,t=1,p=1", }) |v| try testing.expectError(error.MalformedHash, argon2.needsUpdate(v, o)); const h2 = "$argon2i$v=19$m=64,t=2,p=2$c29tZXNhbHQ$VtZsivUmF09y1PmJvFBVcg"; const match: argon2.Options = .{ .type = .i, .rounds = 2, .memory_cost = 64, .parallelism = 2, .digest_size = 16 }; try testing.expect(!try argon2.needsUpdate(h2, match)); var diff = match; diff.type = .id; try testing.expect(try argon2.needsUpdate(h2, diff)); diff = match; diff.memory_cost = 128; try testing.expect(try argon2.needsUpdate(h2, diff)); diff = match; diff.parallelism = 1; try testing.expect(try argon2.needsUpdate(h2, diff)); diff = match; diff.digest_size = 32; try testing.expect(try argon2.needsUpdate(h2, diff)); diff = match; diff.min_desired_rounds = 3; try testing.expect(try argon2.needsUpdate(h2, diff)); diff = match; diff.salt_size = 32; try testing.expect(!try argon2.needsUpdate(h2, diff)); } test "identify" { try testing.expect(argon2.identify("$argon2i$")); try testing.expect(argon2.identify("$argon2qq$")); try testing.expect(!argon2.identify("$argon2$")); try testing.expect(!argon2.identify("$argon2i")); try testing.expect(!argon2.identify("$argon2ID$")); try testing.expect(!argon2.identify("$argon21$")); } test "options" { var buf: [256]u8 = undefined; var w: Io.Writer = .fixed(&buf); var o = small; o.salt = "somesal"; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); o = small; o.digest_size = 15; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); o = small; o.memory_cost = 15; o.parallelism = 2; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); o = small; o.rounds = 0; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); o = small; o.parallelism = 0; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); o = small; o.salt = null; o.salt_size = 7; try testing.expectError(error.InvalidOptions, argon2.hash(test_ctx, "x", o, &w)); } test "round trip with a random salt" { var buf: [256]u8 = undefined; var w: Io.Writer = .fixed(&buf); try argon2.hash(test_ctx, "hunter2", .{ .rounds = 1, .memory_cost = 64, .parallelism = 2 }, &w); const h = w.buffered(); try testing.expect(std.mem.startsWith(u8, h, "$argon2id$v=19$m=64,t=1,p=2$")); try testing.expect(try argon2.verify(test_ctx, "hunter2", h)); try testing.expect(!try argon2.verify(test_ctx, "hunter3", h)); try testing.expect(!try argon2.needsUpdate(h, .{ .rounds = 1, .memory_cost = 64, .parallelism = 2 })); }