authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2025-12-03 12:18:09+01:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2025-12-03 12:18:09+01:00
logd73fbcc3ae920eef4bc0b41df2a4cf74966dd01f
tree22177f4f22fc14cb95b689dbca9213c64ec80835
parentcb115cf73a986ab3f246628a332d06c0deb8feda
parent6fe95c28cf0909b4ea172c700a0df6416385cf94

Merge pull request 'Argon2: use the std.Io interface' (#30084) from jedisct1/zig:argon2 into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30084

2 files changed, 62 insertions(+), 46 deletions(-)

lib/std/crypto/argon2.zig+51-44
......@@ -7,12 +7,11 @@ const builtin = @import("builtin");
77
88const blake2 = crypto.hash.blake2;
99const crypto = std.crypto;
10const Io = std.Io;
1011const math = std.math;
1112const mem = std.mem;
1213const phc_format = pwhash.phc_format;
1314const pwhash = crypto.pwhash;
14
15const Thread = std.Thread;
1615const Blake2b512 = blake2.Blake2b512;
1716const Blocks = std.array_list.AlignedManaged([block_length]u64, .@"16");
1817const H0 = [Blake2b512.digest_length + 8]u8;
......@@ -204,20 +203,20 @@ fn initBlocks(
204203}
205204
206205fn processBlocks(
207 allocator: mem.Allocator,
208206 blocks: *Blocks,
209207 time: u32,
210208 memory: u32,
211209 threads: u24,
212210 mode: Mode,
213) KdfError!void {
211 io: Io,
212) void {
214213 const lanes = memory / threads;
215214 const segments = lanes / sync_points;
216215
217216 if (builtin.single_threaded or threads == 1) {
218217 processBlocksSt(blocks, time, memory, threads, mode, lanes, segments);
219218 } else {
220 try processBlocksMt(allocator, blocks, time, memory, threads, mode, lanes, segments);
219 processBlocksMt(blocks, time, memory, threads, mode, lanes, segments, io);
221220 }
222221}
223222
......@@ -243,7 +242,6 @@ fn processBlocksSt(
243242}
244243
245244fn processBlocksMt(
246 allocator: mem.Allocator,
247245 blocks: *Blocks,
248246 time: u32,
249247 memory: u32,
......@@ -251,26 +249,20 @@ fn processBlocksMt(
251249 mode: Mode,
252250 lanes: u32,
253251 segments: u32,
254) KdfError!void {
255 var threads_list = try std.array_list.Managed(Thread).initCapacity(allocator, threads);
256 defer threads_list.deinit();
257
252 io: Io,
253) void {
258254 var n: u32 = 0;
259255 while (n < time) : (n += 1) {
260256 var slice: u32 = 0;
261257 while (slice < sync_points) : (slice += 1) {
258 var group: Io.Group = .init;
262259 var lane: u24 = 0;
263260 while (lane < threads) : (lane += 1) {
264 const thread = try Thread.spawn(.{}, processSegment, .{
261 group.async(io, processSegment, .{
265262 blocks, time, memory, threads, mode, lanes, segments, n, slice, lane,
266263 });
267 threads_list.appendAssumeCapacity(thread);
268 }
269 lane = 0;
270 while (lane < threads) : (lane += 1) {
271 threads_list.items[lane].join();
272264 }
273 threads_list.clearRetainingCapacity();
265 group.wait(io);
274266 }
275267 }
276268}
......@@ -489,6 +481,7 @@ pub fn kdf(
489481 salt: []const u8,
490482 params: Params,
491483 mode: Mode,
484 io: Io,
492485) KdfError!void {
493486 if (derived_key.len < 4) return KdfError.WeakParameters;
494487 if (derived_key.len > max_int) return KdfError.OutputTooLong;
......@@ -510,7 +503,7 @@ pub fn kdf(
510503 blocks.appendNTimesAssumeCapacity(@splat(0), memory);
511504
512505 initBlocks(&blocks, &h0, memory, params.p);
513 try processBlocks(allocator, &blocks, params.t, memory, params.p, mode);
506 processBlocks(&blocks, params.t, memory, params.p, mode, io);
514507 finalize(&blocks, memory, params.p, derived_key);
515508}
516509
......@@ -533,6 +526,7 @@ const PhcFormatHasher = struct {
533526 params: Params,
534527 mode: Mode,
535528 buf: []u8,
529 io: Io,
536530 ) HasherError![]const u8 {
537531 if (params.secret != null or params.ad != null) return HasherError.InvalidEncoding;
538532
......@@ -540,7 +534,7 @@ const PhcFormatHasher = struct {
540534 crypto.random.bytes(&salt);
541535
542536 var hash: [default_hash_len]u8 = undefined;
543 try kdf(allocator, &hash, password, &salt, params, mode);
537 try kdf(allocator, &hash, password, &salt, params, mode, io);
544538
545539 return phc_format.serialize(HashResult{
546540 .alg_id = @tagName(mode),
......@@ -557,6 +551,7 @@ const PhcFormatHasher = struct {
557551 allocator: mem.Allocator,
558552 str: []const u8,
559553 password: []const u8,
554 io: Io,
560555 ) HasherError!void {
561556 const hash_result = try phc_format.deserialize(HashResult, str);
562557
......@@ -572,7 +567,7 @@ const PhcFormatHasher = struct {
572567 if (expected_hash.len > hash_buf.len) return HasherError.InvalidEncoding;
573568 const hash = hash_buf[0..expected_hash.len];
574569
575 try kdf(allocator, hash, password, hash_result.salt.constSlice(), params, mode);
570 try kdf(allocator, hash, password, hash_result.salt.constSlice(), params, mode, io);
576571 if (!mem.eql(u8, hash, expected_hash)) return HasherError.PasswordVerificationFailed;
577572 }
578573};
......@@ -595,6 +590,7 @@ pub fn strHash(
595590 password: []const u8,
596591 options: HashOptions,
597592 out: []u8,
593 io: Io,
598594) Error![]const u8 {
599595 const allocator = options.allocator orelse return Error.AllocatorRequired;
600596 switch (options.encoding) {
......@@ -604,6 +600,7 @@ pub fn strHash(
604600 options.params,
605601 options.mode,
606602 out,
603 io,
607604 ),
608605 .crypt => return Error.InvalidEncoding,
609606 }
......@@ -621,9 +618,10 @@ pub fn strVerify(
621618 str: []const u8,
622619 password: []const u8,
623620 options: VerifyOptions,
621 io: Io,
624622) Error!void {
625623 const allocator = options.allocator orelse return Error.AllocatorRequired;
626 return PhcFormatHasher.verify(allocator, str, password);
624 return PhcFormatHasher.verify(allocator, str, password, io);
627625}
628626
629627test "argon2d" {
......@@ -640,6 +638,7 @@ test "argon2d" {
640638 &salt,
641639 .{ .t = 3, .m = 32, .p = 4, .secret = &secret, .ad = &ad },
642640 .argon2d,
641 std.testing.io,
643642 );
644643
645644 const want = [_]u8{
......@@ -665,6 +664,7 @@ test "argon2i" {
665664 &salt,
666665 .{ .t = 3, .m = 32, .p = 4, .secret = &secret, .ad = &ad },
667666 .argon2i,
667 std.testing.io,
668668 );
669669
670670 const want = [_]u8{
......@@ -690,6 +690,7 @@ test "argon2id" {
690690 &salt,
691691 .{ .t = 3, .m = 32, .p = 4, .secret = &secret, .ad = &ad },
692692 .argon2id,
693 std.testing.io,
693694 );
694695
695696 const want = [_]u8{
......@@ -800,44 +801,44 @@ test "kdf" {
800801 .{
801802 .mode = .argon2i,
802803 .time = 4,
803 .memory = 4096,
804 .memory = 256,
804805 .threads = 4,
805 .hash = "a11f7b7f3f93f02ad4bddb59ab62d121e278369288a0d0e7",
806 .hash = "f7dbbacbf16999e3700817a7e06f65a8db2e9fa9504ede4c",
806807 },
807808 .{
808809 .mode = .argon2d,
809810 .time = 4,
810 .memory = 4096,
811 .memory = 256,
811812 .threads = 4,
812 .hash = "935598181aa8dc2b720914aa6435ac8d3e3a4210c5b0fb2d",
813 .hash = "ea2970501cf49faa5ba1d2e6370204e9b57ca90a8fea937b",
813814 },
814815 .{
815816 .mode = .argon2id,
816817 .time = 4,
817 .memory = 4096,
818 .memory = 256,
818819 .threads = 4,
819 .hash = "145db9733a9f4ee43edf33c509be96b934d505a4efb33c5a",
820 .hash = "fbd40d5a8cb92f88c20bda4b3cdb1f9d5af1efa937032410",
820821 },
821822 .{
822823 .mode = .argon2i,
823824 .time = 4,
824 .memory = 1024,
825 .memory = 256,
825826 .threads = 8,
826 .hash = "0cdd3956aa35e6b475a7b0c63488822f774f15b43f6e6e17",
827 .hash = "15d3c398364e53f68fd12d19baf3f21432d964254fe27467",
827828 },
828829 .{
829830 .mode = .argon2d,
830831 .time = 4,
831 .memory = 1024,
832 .memory = 256,
832833 .threads = 8,
833 .hash = "83604fc2ad0589b9d055578f4d3cc55bc616df3578a896e9",
834 .hash = "23c9adc06f06e21e4612c1466a1be02627690932b02c0df0",
834835 },
835836 .{
836837 .mode = .argon2id,
837838 .time = 4,
838 .memory = 1024,
839 .memory = 256,
839840 .threads = 8,
840 .hash = "8dafa8e004f8ea96bf7c0f93eecf67a6047476143d15577f",
841 .hash = "f22802f8ca47be93f9954e4ce20c1e944e938fbd4a125d9d",
841842 },
842843 .{
843844 .mode = .argon2i,
......@@ -863,23 +864,23 @@ test "kdf" {
863864 .{
864865 .mode = .argon2i,
865866 .time = 3,
866 .memory = 1024,
867 .memory = 256,
867868 .threads = 6,
868 .hash = "d236b29c2b2a09babee842b0dec6aa1e83ccbdea8023dced",
869 .hash = "ebc8f91964abd8ceab49a12963b0a9e57d635bfa2aad2884",
869870 },
870871 .{
871872 .mode = .argon2d,
872873 .time = 3,
873 .memory = 1024,
874 .memory = 256,
874875 .threads = 6,
875 .hash = "a3351b0319a53229152023d9206902f4ef59661cdca89481",
876 .hash = "1dd7202fd68da6675f769f4034b7a1db30d8785331954117",
876877 },
877878 .{
878879 .mode = .argon2id,
879880 .time = 3,
880 .memory = 1024,
881 .memory = 256,
881882 .threads = 6,
882 .hash = "1640b932f4b60e272f5d2207b9a9c626ffa1bd88d2349016",
883 .hash = "424436b6ee22a66b04b9d0cf78f190305c5c166bae8baa09",
883884 },
884885 };
885886 for (test_vectors) |v| {
......@@ -894,6 +895,7 @@ test "kdf" {
894895 salt,
895896 .{ .t = v.time, .m = v.memory, .p = v.threads },
896897 v.mode,
898 std.testing.io,
897899 );
898900
899901 try std.testing.expectEqualSlices(u8, &dk, &want);
......@@ -903,6 +905,7 @@ test "kdf" {
903905test "phc format hasher" {
904906 const allocator = std.testing.allocator;
905907 const password = "testpass";
908 const io = std.testing.io;
906909
907910 var buf: [128]u8 = undefined;
908911 const hash = try PhcFormatHasher.create(
......@@ -911,25 +914,29 @@ test "phc format hasher" {
911914 .{ .t = 3, .m = 32, .p = 4 },
912915 .argon2id,
913916 &buf,
917 io,
914918 );
915 try PhcFormatHasher.verify(allocator, hash, password);
919 try PhcFormatHasher.verify(allocator, hash, password, io);
916920}
917921
918922test "password hash and password verify" {
919923 const allocator = std.testing.allocator;
920924 const password = "testpass";
925 const io = std.testing.io;
921926
922927 var buf: [128]u8 = undefined;
923928 const hash = try strHash(
924929 password,
925930 .{ .allocator = allocator, .params = .{ .t = 3, .m = 32, .p = 4 } },
926931 &buf,
932 io,
927933 );
928 try strVerify(hash, password, .{ .allocator = allocator });
934 try strVerify(hash, password, .{ .allocator = allocator }, io);
929935}
930936
931937test "kdf derived key length" {
932938 const allocator = std.testing.allocator;
939 const io = std.testing.io;
933940
934941 const password = "testpass";
935942 const salt = "saltsalt";
......@@ -937,11 +944,11 @@ test "kdf derived key length" {
937944 const mode = Mode.argon2id;
938945
939946 var dk1: [11]u8 = undefined;
940 try kdf(allocator, &dk1, password, salt, params, mode);
947 try kdf(allocator, &dk1, password, salt, params, mode, io);
941948
942949 var dk2: [77]u8 = undefined;
943 try kdf(allocator, &dk2, password, salt, params, mode);
950 try kdf(allocator, &dk2, password, salt, params, mode, io);
944951
945952 var dk3: [111]u8 = undefined;
946 try kdf(allocator, &dk3, password, salt, params, mode);
953 try kdf(allocator, &dk3, password, salt, params, mode, io);
947954}
lib/std/crypto/benchmark.zig+11-2
......@@ -450,6 +450,7 @@ fn benchmarkPwhash(
450450 comptime ty: anytype,
451451 comptime params: *const anyopaque,
452452 comptime count: comptime_int,
453 io: std.Io,
453454) !f64 {
454455 const password = "testpass" ** 2;
455456 const opts = ty.HashOptions{
......@@ -459,12 +460,20 @@ fn benchmarkPwhash(
459460 };
460461 var buf: [256]u8 = undefined;
461462
463 const strHash = ty.strHash;
464 const strHashFnInfo = @typeInfo(@TypeOf(strHash)).@"fn";
465 const needs_io = strHashFnInfo.params.len == 4;
466
462467 var timer = try Timer.start();
463468 const start = timer.lap();
464469 {
465470 var i: usize = 0;
466471 while (i < count) : (i += 1) {
467 _ = try ty.strHash(password, opts, &buf);
472 if (needs_io) {
473 _ = try strHash(password, opts, &buf, io);
474 } else {
475 _ = try strHash(password, opts, &buf);
476 }
468477 mem.doNotOptimizeAway(&buf);
469478 }
470479 }
......@@ -623,7 +632,7 @@ pub fn main() !void {
623632
624633 inline for (pwhashes) |H| {
625634 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
626 const throughput = try benchmarkPwhash(arena_allocator, H.ty, H.params, mode(64));
635 const throughput = try benchmarkPwhash(arena_allocator, H.ty, H.params, mode(64), io);
627636 try stdout.print("{s:>17}: {d:10.3} s/ops\n", .{ H.name, throughput });
628637 try stdout.flush();
629638 }