// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Cisco's password formats: passlib's `passlib.handlers.cisco`, and the three //! newer ones passlib 1.7.4 never got to. //! //! The newer ones, first, because they are the ones to use: //! //! * `cisco_type8`, IOS's `secret 8`: `$8$`, fourteen salt characters, `$`, //! and PBKDF2-HMAC-SHA256 of the password at 20000 iterations. //! * `cisco_type9`, IOS's `secret 9`: the same shape, with scrypt at N = //! 2^14, r = 1, p = 1 in place of PBKDF2. Cisco's recommendation. //! * `cisco_asa_pbkdf2`, the ASA's since 9.5: `$sha512$`, an iteration //! count, and a salt and a digest in padded standard base64. The digest is //! the first sixteen bytes of PBKDF2-HMAC-SHA512. //! //! None of the three has a published specification. What is here was //! established from hashes the devices wrote and checked against two //! independent implementations; the tests say which vector came from where. //! passlib's own issue for them is #87. //! //! The older ones: //! //! * `cisco_type4`, IOS's `secret 4`: one unsalted SHA-256 of the password, //! in the alphabet of types 8 and 9. Cisco meant it to be PBKDF2 and //! shipped this by mistake, then withdrew it after IOS 15.3(3)M3. //! * `cisco_pix`, the PIX firewall's "encrypted" passwords: MD5 of the //! password, NUL-padded to sixteen bytes and optionally followed by the //! first four bytes of the user name, with every fourth byte of the //! digest dropped and the remaining twelve written as sixteen characters //! of `h64`. //! * `cisco_asa`, the ASA's variant of it, which pads longer passwords to //! 32 bytes and stops appending the user name at 28. //! * `cisco_type7`, IOS's `password 7`: not a hash at all, but the password //! XORed with a fixed, published key, written as a two-digit offset into //! the key and upper-case hex. //! //! **The four older ones are obsolete, and `cisco_type7` is not even a //! hash.** `cisco_type4` is as weak as an unsalted digest can be. Type 7 //! was cracked in 1995 and any password stored with it can be read back //! instantly; it only ever kept passwords from being read over a shoulder. //! The PIX and ASA formats are one unsalted MD5, or one salted with four //! bytes of a user name, so a table covers them. Cisco's own type 8 and 9 //! (PBKDF2 and scrypt) replaced them. They are here to read old //! configurations and to check the passwords in them. const std = @import("std"); const Io = std.Io; const testing = std.testing; const Md5 = std.crypto.hash.Md5; const encoding = @import("../encoding.zig"); const h64 = encoding.h64; const handler = @import("../handler.zig"); const Context = handler.Context; const Error = handler.Error; const python = @import("../python.zig"); // -- cisco_pix and cisco_asa ------------------------------------------------ fn Pix(comptime scheme_name: []const u8, comptime asa: bool) type { return struct { pub const name = scheme_name; pub const Options = struct {}; /// The longest password `hash` accepts: sixteen bytes for the PIX, /// 32 for the ASA. pub const truncate_size = if (asa) 32 else 16; const checksum_len = 16; fn parse(h: []const u8) Error![]const u8 { if (h.len != checksum_len or !h64.isValid(h)) return error.MalformedHash; return h; } /// passlib's `cisco_pix._calc_checksum`. /// /// For `verify`, a password longer than `truncate_size` is hashed /// with itself and 32 bytes of 0xFF appended after the padding, so /// that it can never match what the truncated password would give; /// passlib calls that spoiling the digest. fn compute(ctx: Context, password: []const u8, for_hash: bool, out: *[checksum_len]u8) Error!void { const spoil = password.len > truncate_size; if (spoil and for_hash) return error.PasswordTooLong; // The password, the user name, and the padding, in at most 32 // bytes: a longer password was spoiled above and only its first // `pad_size` bytes are used here. var buf = [_]u8{0} ** 32; defer std.crypto.secureZero(u8, &buf); var len = @min(password.len, buf.len); @memcpy(buf[0..len], password[0..len]); var total = password.len; if (ctx.user) |user| { if (user.len > 0 and (!asa or password.len < 28)) { // passlib's `repeat_string(user, 4)`: the user name // repeated or cut to exactly four bytes. for (0..4) |i| { if (len < buf.len) { buf[len] = user[i % user.len]; len += 1; } } total += 4; } } const pad_size: usize = if (asa and total > 16) 32 else 16; var md5 = Md5.init(.{}); md5.update(buf[0..pad_size]); if (spoil) { md5.update(password); md5.update(&([_]u8{0xff} ** 32)); } var digest: [Md5.digest_length]u8 = undefined; md5.final(&digest); var kept: [12]u8 = undefined; for (0..12) |i| kept[i] = digest[i + i / 3]; _ = h64.encode(out, &kept); } /// passlib has no pattern for this format and identifies it by /// parsing it: sixteen characters of `./0-9A-Za-z`. pub fn identify(h: []const u8) bool { _ = parse(h) catch return false; return true; } /// `error.PasswordTooLong` for a password longer than /// `truncate_size`, as passlib raises `PasswordSizeError`. pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = options; try handler.checkPasswordLen(password); var out: [checksum_len]u8 = undefined; try compute(ctx, password, true, &out); try w.writeAll(&out); } /// False, not an error, for a password longer than `truncate_size`. pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { try handler.checkPasswordLen(password); const stored = try parse(h); var out: [checksum_len]u8 = undefined; try compute(ctx, password, false, &out); return handler.eql(stored, &out); } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; _ = try parse(h); return false; } }; } /// The PIX firewall's hash. The user name, from `Context.user`, is optional: /// the enable password has none, and passlib treats an empty one as none. pub const cisco_pix = Pix("cisco_pix", false); /// The ASA's hash. As `cisco_pix`, but a password of more than twelve bytes /// with a user, or of more than sixteen without one, is padded to 32 bytes, /// and a password of 28 bytes or more is not salted with the user at all. pub const cisco_asa = Pix("cisco_asa", true); // -- cisco_type7 ------------------------------------------------------------ /// IOS's type 7 encoding: a two-digit decimal offset, from 00 to 52, and /// the password XORed with Cisco's key from that offset on, as upper-case /// hex. /// /// It is reversible, and `decode` reverses it. passlib parses the offset /// with Python's `int()`, which also takes a sign or a space in either of /// the two places (`+5`, ` 5`, `5 `, `-0`), so this does too. The encoded /// part is accepted in either case, and anything that is not hex is /// `error.MalformedHash`; one of the wrong length verifies as false. pub const cisco_type7 = struct { pub const name = "cisco_type7"; /// The highest offset into `key`. pub const max_salt = 52; const key = "dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"; pub const Options = struct { /// The offset into the key, from 0 to 52. Null, the default, draws /// one from 0 to 15, as passlib does. salt: ?u8 = null, }; const Parsed = struct { salt: u8, checksum: []const u8 }; fn parse(h: []const u8) Error!Parsed { if (h.len < 2) return error.MalformedHash; // `pythonInt` refuses a negative offset, which passlib's range // check refuses after its `int()`. const salt = try python.pythonInt(h[0..2]); if (salt > max_salt) return error.MalformedHash; const checksum = h[2..]; if (!encoding.allIn(checksum, encoding.hex_chars)) return error.MalformedHash; return .{ .salt = @intCast(salt), .checksum = checksum }; } fn keyAt(salt: u8, i: usize) u8 { return key[(salt + i) % key.len]; } /// passlib's `cisco_type7.decode`: the password `h` encodes, into `out`, /// which must be at least half the length of the encoded part. pub fn decode(h: []const u8, out: []u8) Error![]u8 { const parsed = try parse(h); const raw = try encoding.hexDecode(out, parsed.checksum); for (raw, 0..) |*b, i| b.* ^= keyAt(parsed.salt, i); return raw; } /// passlib parses the offset and the hex, so that is what identifies it. pub fn identify(h: []const u8) bool { _ = parse(h) catch return false; return true; } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { try handler.checkPasswordLen(password); const salt: u8 = if (options.salt) |s| blk: { if (s > max_salt) return error.InvalidOptions; break :blk s; } else blk: { var r: [1]u8 = undefined; break :blk (try handler.randomChars(ctx.io, &r, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"))[0]; }; try w.print("{d:0>2}", .{salt}); for (password, 0..) |c, i| { var hex: [2]u8 = undefined; try w.writeAll(encoding.hexUpper(&hex, &[_]u8{c ^ keyAt(salt, i)})); } } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; try handler.checkPasswordLen(password); const parsed = try parse(h); // passlib compares hex strings, so a length that is not twice the // password's is simply unequal. if (parsed.checksum.len != 2 * password.len) return false; var stored_buf: [handler.max_password_len]u8 = undefined; const stored = try encoding.hexDecode(&stored_buf, parsed.checksum); var diff: u8 = 0; for (stored, password, 0..) |s, c, i| diff |= s ^ c ^ keyAt(parsed.salt, i); return diff == 0; } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; _ = try parse(h); return false; } }; /// The schemes this module defines, in the order `registry.identify` tries /// them. // -- cisco_type8 and cisco_type9 --------------------------------------------- /// The alphabet of types 8 and 9, which is `crypt`'s, packed the way standard /// base64 packs its bits. So it is `h64big`: the same characters as `h64`, /// in the opposite bit order. const h64big = encoding.h64big; /// IOS's `$8$` and `$9$`. `Kdf.derive` turns a password and the salt, as /// its fourteen ASCII characters, into 32 bytes. fn IosSecret(comptime scheme_name: []const u8, comptime ident: []const u8, comptime Kdf: type) type { return struct { pub const name = scheme_name; /// Every device writes fourteen. pub const salt_len = 14; const digest_len = 32; const checksum_len = h64big.encodedLen(digest_len); pub const Options = struct { /// The salt: fourteen characters of `./0-9A-Za-z`. Null, the /// default, draws a random one. salt: ?[]const u8 = null, }; pub fn identify(h: []const u8) bool { return std.mem.startsWith(u8, h, ident); } const Parsed = struct { salt: []const u8, digest: [digest_len]u8 }; fn parse(h: []const u8) Error!Parsed { if (!identify(h)) return error.MalformedHash; const fields = try handler.splitExact(2, h[ident.len..], '$'); if (fields[0].len != salt_len or !h64big.isValid(fields[0])) return error.MalformedHash; if (fields[1].len != checksum_len) return error.MalformedHash; var parsed: Parsed = .{ .salt = fields[0], .digest = undefined }; _ = try h64big.decode(&parsed.digest, fields[1]); return parsed; } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { try handler.checkPasswordLen(password); var salt_buf: [salt_len]u8 = undefined; const salt = if (options.salt) |given| blk: { if (given.len != salt_len or !h64big.isValid(given)) return error.InvalidOptions; break :blk given; } else try handler.randomChars(ctx.io, &salt_buf, encoding.hash64_chars); var digest: [digest_len]u8 = undefined; try Kdf.derive(ctx.gpa, &digest, password, salt); var checksum: [checksum_len]u8 = undefined; try w.print("{s}{s}${s}", .{ ident, salt, h64big.encode(&checksum, &digest) }); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { const parsed = try parse(h); try handler.checkPasswordLen(password); var digest: [digest_len]u8 = undefined; try Kdf.derive(ctx.gpa, &digest, password, parsed.salt); return handler.eql(&parsed.digest, &digest); } /// Always false for a hash that parses: the cost is fixed by the /// type, so there is nothing to be out of date but the type itself. pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; _ = try parse(h); return false; } }; } /// IOS `secret 8`: PBKDF2-HMAC-SHA256, 20000 iterations. pub const cisco_type8 = IosSecret("cisco_type8", "$8$", struct { fn derive(gpa: std.mem.Allocator, out: *[32]u8, password: []const u8, salt: []const u8) Error!void { _ = gpa; try std.crypto.pwhash.pbkdf2(out, password, salt, 20000, std.crypto.auth.hmac.sha2.HmacSha256); } }); /// IOS `secret 9`: scrypt at N = 16384, r = 1, p = 1, which is two MiB of /// memory. pub const cisco_type9 = IosSecret("cisco_type9", "$9$", struct { fn derive(gpa: std.mem.Allocator, out: *[32]u8, password: []const u8, salt: []const u8) Error!void { try std.crypto.pwhash.scrypt.kdf(gpa, out, password, salt, .{ .ln = 14, .r = 1, .p = 1 }); } }); /// IOS `secret 4`: SHA-256 of the password, 43 characters, nothing else. No /// prefix, so any 43-character string of the alphabet is claimed; the /// `secret 4` in front of it in a configuration is what says what it is. pub const cisco_type4 = struct { pub const name = "cisco_type4"; pub const Options = struct {}; const checksum_len = h64big.encodedLen(32); pub fn identify(h: []const u8) bool { return h.len == checksum_len and h64big.isValid(h); } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { _ = ctx; _ = options; try handler.checkPasswordLen(password); var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(password, &digest, .{}); var checksum: [checksum_len]u8 = undefined; try w.writeAll(h64big.encode(&checksum, &digest)); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; if (!identify(h)) return error.MalformedHash; try handler.checkPasswordLen(password); var stored: [32]u8 = undefined; _ = try h64big.decode(&stored, h); var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(password, &digest, .{}); return handler.eql(&stored, &digest); } pub fn needsUpdate(h: []const u8, options: Options) Error!bool { _ = options; if (!identify(h)) return error.MalformedHash; return false; } }; // -- cisco_asa_pbkdf2 ------------------------------------------------------ /// The ASA's PBKDF2 format, since ASA 9.5: `$sha512$` + iterations + `$` + /// salt + `$` + digest, the salt and the digest in padded standard base64. /// The digest is the first sixteen bytes of PBKDF2-HMAC-SHA512 over the /// decoded salt, which the ASA makes sixteen random bytes. /// /// The ASA writes it as `enable password $sha512$... pbkdf2`, and Cisco /// never numbered it; it is sometimes called type 10. pub const cisco_asa_pbkdf2 = struct { pub const name = "cisco_asa_pbkdf2"; const ident = "$sha512$"; const digest_len = 16; pub const default_salt_size = 16; /// The longest salt accepted, far past the ASA's sixteen bytes. pub const max_salt_size = 1024; pub const default_rounds = 5000; pub const Options = struct { /// The salt, as bytes. Null, the default, draws `salt_size` random /// ones. salt: ?[]const u8 = null, salt_size: u16 = default_salt_size, /// Iterations of PBKDF2, at least one. The ASA writes 5000. rounds: u32 = default_rounds, /// A hash with fewer or more iterations than these needs updating. min_desired_rounds: ?u32 = null, max_desired_rounds: ?u32 = null, }; pub fn identify(h: []const u8) bool { return std.mem.startsWith(u8, h, ident); } const Parsed = struct { rounds: u32, salt: [max_salt_size]u8, salt_len: usize, digest: [digest_len]u8, }; fn parse(h: []const u8) Error!Parsed { if (!identify(h)) return error.MalformedHash; const fields = try handler.splitExact(3, h[ident.len..], '$'); const rounds = try handler.parseDecimal(u32, fields[0], false); if (rounds < 1) return error.MalformedHash; var parsed: Parsed = .{ .rounds = rounds, .salt = undefined, .salt_len = 0, .digest = undefined }; const salt_len = try encoding.b64.decodedLen(fields[1]); if (salt_len < 1 or salt_len > max_salt_size) return error.MalformedHash; parsed.salt_len = (try encoding.b64.decode(&parsed.salt, fields[1])).len; if ((try encoding.b64.decodedLen(fields[2])) != digest_len) return error.MalformedHash; _ = try encoding.b64.decode(&parsed.digest, fields[2]); return parsed; } fn derive(out: *[digest_len]u8, password: []const u8, salt: []const u8, rounds: u32) Error!void { try std.crypto.pwhash.pbkdf2(out, password, salt, rounds, std.crypto.auth.hmac.sha2.HmacSha512); } pub fn hash(ctx: Context, password: []const u8, options: Options, w: *Io.Writer) Error!void { try handler.checkPasswordLen(password); if (options.rounds < 1) return error.InvalidOptions; var salt_buf: [max_salt_size]u8 = undefined; const salt = if (options.salt) |given| blk: { if (given.len < 1 or given.len > max_salt_size) return error.InvalidOptions; break :blk given; } else blk: { if (options.salt_size < 1 or options.salt_size > max_salt_size) return error.InvalidOptions; break :blk try handler.randomBytes(ctx.io, salt_buf[0..options.salt_size]); }; var digest: [digest_len]u8 = undefined; try derive(&digest, password, salt, options.rounds); var salt_text: [encoding.b64.encodedLen(max_salt_size)]u8 = undefined; var digest_text: [encoding.b64.encodedLen(digest_len)]u8 = undefined; try w.print("{s}{d}${s}${s}", .{ ident, options.rounds, encoding.b64.encode(&salt_text, salt), encoding.b64.encode(&digest_text, &digest), }); } pub fn verify(ctx: Context, password: []const u8, h: []const u8) Error!bool { _ = ctx; const parsed = try parse(h); try handler.checkPasswordLen(password); var digest: [digest_len]u8 = undefined; try derive(&digest, password, parsed.salt[0..parsed.salt_len], parsed.rounds); return handler.eql(&parsed.digest, &digest); } /// True when the iterations fall outside `min_desired_rounds` and /// `max_desired_rounds`. pub fn needsUpdate(h: []const u8, options: Options) Error!bool { const parsed = try parse(h); return handler.roundsOutside(parsed.rounds, options.min_desired_rounds, options.max_desired_rounds); } }; /// The schemes this module defines, in the order `registry.identify` tries /// them. pub const schemes = .{ cisco_type9, cisco_type8, cisco_asa_pbkdf2, cisco_pix, cisco_asa, cisco_type7, cisco_type4 }; comptime { for (schemes) |S| handler.check(S); } fn ctxFor(user: ?[]const u8) Context { return .{ .io = testing.io, .gpa = testing.allocator, .user = user }; } fn hashTo(buf: []u8, comptime S: type, ctx: Context, password: []const u8, options: S.Options) ![]const u8 { var w: Io.Writer = .fixed(buf); try S.hash(ctx, password, options, &w); return w.buffered(); } test "hashes reproduce passlib" { // passlib 1.7.4: cisco_pix.hash("cisco"), cisco_pix.hash("cisco", // user="admin"), and so on. var buf: [64]u8 = undefined; try testing.expectEqualStrings("2KFQnbNIdI.2KYOU", try hashTo(&buf, cisco_pix, ctxFor(null), "cisco", .{})); try testing.expectEqualStrings("2KFQnbNIdI.2KYOU", try hashTo(&buf, cisco_pix, ctxFor(""), "cisco", .{})); try testing.expectEqualStrings("f3UhLvUj1QsXsuK7", try hashTo(&buf, cisco_pix, ctxFor("admin"), "cisco", .{})); try testing.expectEqualStrings("f3UhLvUj1QsXsuK7", try hashTo(&buf, cisco_asa, ctxFor("admin"), "cisco", .{})); try testing.expectEqualStrings("p4awaAPleKMbIylD", try hashTo(&buf, cisco_asa, ctxFor("admin"), "0123456789abcdefghij", .{})); try testing.expectEqualStrings("eiYlo4wlCa0UExyM", try hashTo(&buf, cisco_asa, ctxFor("admin"), "0123456789abcdefghijklmnopqrstuv", .{})); // cisco_type7.using(salt=5).hash("secure "), and salt=52. try testing.expectEqualStrings("0518030C345E4B49", try hashTo(&buf, cisco_type7, ctxFor(null), "secure ", .{ .salt = 5 })); try testing.expectEqualStrings("524705001513541902", try hashTo(&buf, cisco_type7, ctxFor(null), "password", .{ .salt = 52 })); } test "round trips" { var buf: [64]u8 = undefined; inline for (schemes) |S| { const h = try hashTo(&buf, S, ctxFor("user"), "hunter2", .{}); try testing.expect(S.identify(h)); try testing.expect(try S.verify(ctxFor("user"), "hunter2", h)); try testing.expect(!try S.verify(ctxFor("user"), "hunter3", h)); try testing.expect(!try S.needsUpdate(h, .{})); } } test "cisco_type7 parsing follows Python's int()" { var out: [16]u8 = undefined; try testing.expectEqualStrings("secure ", try cisco_type7.decode("04480E051A33490E", &out)); // passlib: all of these verify "secure ", and "05" verifies "". try testing.expect(try cisco_type7.verify(ctxFor(null), "secure ", "+4480E051A33490E")); try testing.expect(try cisco_type7.verify(ctxFor(null), "secure ", "4 480E051A33490E")); try testing.expect(try cisco_type7.verify(ctxFor(null), "secure ", "04480e051a33490e")); try testing.expect(try cisco_type7.verify(ctxFor(null), "", "05")); try testing.expect(!try cisco_type7.verify(ctxFor(null), "secure ", "04480E051A33490")); try testing.expectError(error.MalformedHash, cisco_type7.verify(ctxFor(null), "x", "5")); try testing.expectError(error.MalformedHash, cisco_type7.verify(ctxFor(null), "x", "-1AB")); try testing.expectError(error.MalformedHash, cisco_type7.verify(ctxFor(null), "x", "04480E051A33490G")); try testing.expectError(error.MalformedHash, cisco_type7.verify(ctxFor(null), "x", "99400E4812")); try testing.expect(!cisco_type7.identify("0A480E051A33490E")); } test "refusals and length limits" { var buf: [64]u8 = undefined; try testing.expectError(error.PasswordTooLong, hashTo(&buf, cisco_pix, ctxFor(null), "a" ** 17, .{})); try testing.expectError(error.PasswordTooLong, hashTo(&buf, cisco_asa, ctxFor(null), "a" ** 33, .{})); try testing.expectError(error.InvalidOptions, hashTo(&buf, cisco_type7, ctxFor(null), "x", .{ .salt = 53 })); // passlib: a longer password verifies as false against the hash of its // first sixteen bytes rather than raising. const h = try hashTo(&buf, cisco_pix, ctxFor(null), "0123456789abcdef", .{}); try testing.expect(!try cisco_pix.verify(ctxFor(null), "0123456789abcdefX", h)); } test "type 8 and type 9 against hashcat and ciscoPWDhasher" { // hashcat's example hashes for modes 9200 and 9300, whose password is // always "hashcat". try testing.expect(try cisco_type8.verify(ctxFor(null), "hashcat", "$8$TnGX/fE4KGHOVU$pEhnEvxrvaynpi8j4f.EMHr6M.FzU8xnZnBr/tJdFWk")); try testing.expect(try cisco_type9.verify(ctxFor(null), "hashcat", "$9$2MJBozw/9R3UsU$2lFhcKvpghcyw8deP25GOfyZaagyUOGBymkryvOdfo6")); try testing.expect(!try cisco_type8.verify(ctxFor(null), "hashcaT", "$8$TnGX/fE4KGHOVU$pEhnEvxrvaynpi8j4f.EMHr6M.FzU8xnZnBr/tJdFWk")); // Fixed salts through ciscoPWDhasher's algorithm: Python's hashlib, the // salt as its ASCII, and standard base64 moved to crypt's alphabet with // its padding cut. var buf: [128]u8 = undefined; const cases = .{ .{ cisco_type8, "cisco", "$8$abcdefghijklmn$rt0LL8O81iDSssx3puV4Oa3IfublJvaBsGMoO0k3few" }, .{ cisco_type9, "cisco", "$9$abcdefghijklmn$RFcXqsqQNxcP.jVxtSR46HTz61Aopg1LmY.FBa4vnXE" }, .{ cisco_type8, "p\xc3\xa4ss", "$8$abcdefghijklmn$5C0PqLgwQmdIpdip/qFU8JLgESPBgmoiMmL9YP47gAo" }, .{ cisco_type9, "p\xc3\xa4ss", "$9$abcdefghijklmn$totXrKWzBewXRCnqi6sCSOH4vOIdYJjhVBOzLfoACD2" }, }; inline for (cases) |c| { try testing.expectEqualStrings(c[2], try hashTo(&buf, c[0], ctxFor(null), c[1], .{ .salt = "abcdefghijklmn" })); } } test "the ASA's PBKDF2" { // A hash an ASA wrote, for the password "cisco", published at // attackdebris.com and reproduced in the ciscoPWDhasher issue for this // format. try testing.expect(try cisco_asa_pbkdf2.verify(ctxFor(null), "cisco", "$sha512$5000$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A==")); try testing.expect(!try cisco_asa_pbkdf2.verify(ctxFor(null), "Cisco", "$sha512$5000$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A==")); // Fixed salts through Python's hashlib.pbkdf2_hmac. var buf: [128]u8 = undefined; try testing.expectEqualStrings( "$sha512$5000$MDEyMzQ1Njc4OWFiY2RlZg==$0yibq7ziL6O9LDj0JHtVZg==", try hashTo(&buf, cisco_asa_pbkdf2, ctxFor(null), "cisco", .{ .salt = "0123456789abcdef" }), ); try testing.expectEqualStrings( "$sha512$1000$MDEyMzQ1Njc4OWFiY2RlZg==$ymePjiPc0WXs8l0H+LarSg==", try hashTo(&buf, cisco_asa_pbkdf2, ctxFor(null), "p\xc3\xa4ss", .{ .salt = "0123456789abcdef", .rounds = 1000 }), ); const at_1000 = "$sha512$1000$MDEyMzQ1Njc4OWFiY2RlZg==$ymePjiPc0WXs8l0H+LarSg=="; try testing.expect(try cisco_asa_pbkdf2.needsUpdate(at_1000, .{ .min_desired_rounds = 5000 })); try testing.expect(!try cisco_asa_pbkdf2.needsUpdate(at_1000, .{})); } test "the new formats' refusals" { const ctx = ctxFor(null); for ([_][]const u8{ "$8$TnGX/fE4KGHOV$pEhnEvxrvaynpi8j4f.EMHr6M.FzU8xnZnBr/tJdFWk", // salt of 13 "$8$TnGX/fE4KGHOVU$pEhnEvxrvaynpi8j4f.EMHr6M.FzU8xnZnBr/tJdFW", // checksum of 42 "$8$TnGX/fE4KGHOV!$pEhnEvxrvaynpi8j4f.EMHr6M.FzU8xnZnBr/tJdFWk", "$8$TnGX/fE4KGHOVU", }) |h| try testing.expectError(error.MalformedHash, cisco_type8.verify(ctx, "x", h)); for ([_][]const u8{ "$sha512$05000$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A==", "$sha512$0$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A==", "$sha512$5000$$Yu0w7sFjhLnbtZQJ/nyp+A==", "$sha512$5000$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A", "$sha512$5000$SvZkzlRDO115YrLXsZuWCg==$Yu0w7sFjhLnbtZQJ/nyp+A+A", }) |h| try testing.expectError(error.MalformedHash, cisco_asa_pbkdf2.verify(ctx, "x", h)); var buf: [128]u8 = undefined; try testing.expectError(error.InvalidOptions, hashTo(&buf, cisco_type9, ctx, "x", .{ .salt = "short" })); } test "type 4" { // hashcat's example for mode 5700, whose password is "hashcat". try testing.expect(try cisco_type4.verify(ctxFor(null), "hashcat", "2btjjy78REtmYkkW0csHUbJZOstRXoWdX1mGrmmfeHI")); try testing.expect(!try cisco_type4.verify(ctxFor(null), "hashcaT", "2btjjy78REtmYkkW0csHUbJZOstRXoWdX1mGrmmfeHI")); // Python's hashlib, encoded the same way. var buf: [64]u8 = undefined; try testing.expectEqualStrings("tnhtc92DXBhelxjYk8LWJrPV36S2i4ntXrpb4RFmfqY", try hashTo(&buf, cisco_type4, ctxFor(null), "cisco", .{})); try testing.expectError(error.MalformedHash, cisco_type4.verify(ctxFor(null), "x", "2btjjy78REtmYkkW0csHUbJZOstRXoWdX1mGrmmfeH")); }