aboutsummaryrefslogtreecommitdiffstats
path: root/tool
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2022-10-31 23:54:06 +0100
committerLibravatar Rutger Broekhoff2022-10-31 23:54:06 +0100
commit523fff3ca4f50d859bdddd27c8343933a46bc926 (patch)
tree9cbce064ad2f37f4cbf297fb950622c64564529e /tool
parent885a21d0f3226d1acc93aa16a6fe8ea37235c7e0 (diff)
downloadzig-nkeys-523fff3ca4f50d859bdddd27c8343933a46bc926.tar.gz
zig-nkeys-523fff3ca4f50d859bdddd27c8343933a46bc926.zip
Make compatible with Zig 0.10.0
This does not build on unpatched Zig 0.10.0, as there is still an issue with the revamped Ed25519 implementation in the standard library. I will report an issue which should make this program buildable. There is still some cleanup work to perform.
Diffstat (limited to 'tool')
-rw-r--r--tool/znk.zig20
1 files changed, 11 insertions, 9 deletions
diff --git a/tool/znk.zig b/tool/znk.zig
index e086879..fcd03ff 100644
--- a/tool/znk.zig
+++ b/tool/znk.zig
@@ -266,8 +266,8 @@ pub fn cmdSign(arena: Allocator, args: []const []const u8) !void {
266 defer nkey.wipe(); 266 defer nkey.wipe();
267 267
268 const sig = nkey.sign(content) catch fatal("could not generate signature", .{}); 268 const sig = nkey.sign(content) catch fatal("could not generate signature", .{});
269 var encoded_sig = try arena.alloc(u8, std.base64.standard.Encoder.calcSize(sig.len)); 269 var encoded_sig = try arena.alloc(u8, std.base64.standard.Encoder.calcSize(std.crypto.sign.Ed25519.Signature.encoded_length));
270 _ = std.base64.standard.Encoder.encode(encoded_sig, &sig); 270 _ = std.base64.standard.Encoder.encode(encoded_sig, &sig.toBytes());
271 try stdout.writeAll(encoded_sig); 271 try stdout.writeAll(encoded_sig);
272 try stdout.writeAll("\n"); 272 try stdout.writeAll("\n");
273} 273}
@@ -377,14 +377,16 @@ pub fn cmdVerify(arena: Allocator, args: []const []const u8) !void {
377 const decoded_len = std.base64.standard.Decoder.calcSizeForSlice(trimmed_signature_b64) catch { 377 const decoded_len = std.base64.standard.Decoder.calcSizeForSlice(trimmed_signature_b64) catch {
378 fatal("invalid signature encoding", .{}); 378 fatal("invalid signature encoding", .{});
379 }; 379 };
380 if (decoded_len != std.crypto.sign.Ed25519.signature_length) 380 if (decoded_len != std.crypto.sign.Ed25519.Signature.encoded_length)
381 fatal("invalid signature length", .{}); 381 fatal("invalid signature length", .{});
382 const signature = try arena.alloc(u8, decoded_len);
383 382
384 _ = std.base64.standard.Decoder.decode(signature, trimmed_signature_b64) catch { 383 var signature_bytes: [std.crypto.sign.Ed25519.Signature.encoded_length]u8 = undefined;
384 _ = std.base64.standard.Decoder.decode(&signature_bytes, trimmed_signature_b64) catch {
385 fatal("invalid signature encoding", .{}); 385 fatal("invalid signature encoding", .{});
386 }; 386 };
387 k.verify(content, signature[0..std.crypto.sign.Ed25519.signature_length].*) catch { 387
388 const signature = std.crypto.sign.Ed25519.Signature.fromBytes(signature_bytes);
389 k.verify(content, signature) catch {
388 fatal("bad signature", .{}); 390 fatal("bad signature", .{});
389 }; 391 };
390 392
@@ -462,7 +464,7 @@ fn PrefixKeyGenerator(comptime EntropyReaderType: type) type {
462 } else struct { 464 } else struct {
463 pub fn generate(self: *Self) !void { 465 pub fn generate(self: *Self) !void {
464 var cpu_count = try std.Thread.getCpuCount(); 466 var cpu_count = try std.Thread.getCpuCount();
465 var threads = try self.allocator.alloc(std.Thread, cpu_count*4); 467 var threads = try self.allocator.alloc(std.Thread, cpu_count * 4);
466 defer self.allocator.free(threads); 468 defer self.allocator.free(threads);
467 for (threads) |*thread| thread.* = try std.Thread.spawn(.{}, Self.generatePrivate, .{self}); 469 for (threads) |*thread| thread.* = try std.Thread.spawn(.{}, Self.generatePrivate, .{self});
468 for (threads) |thread| thread.join(); 470 for (threads) |thread| thread.join();
@@ -495,7 +497,7 @@ pub const Nkey = union(enum) {
495 pub fn verify( 497 pub fn verify(
496 self: *const Self, 498 self: *const Self,
497 msg: []const u8, 499 msg: []const u8,
498 sig: [std.crypto.sign.Ed25519.signature_length]u8, 500 sig: std.crypto.sign.Ed25519.Signature,
499 ) !void { 501 ) !void {
500 return switch (self.*) { 502 return switch (self.*) {
501 .seed_key_pair => |*kp| try kp.verify(msg, sig), 503 .seed_key_pair => |*kp| try kp.verify(msg, sig),
@@ -507,7 +509,7 @@ pub const Nkey = union(enum) {
507 pub fn sign( 509 pub fn sign(
508 self: *const Self, 510 self: *const Self,
509 msg: []const u8, 511 msg: []const u8,
510 ) ![std.crypto.sign.Ed25519.signature_length]u8 { 512 ) !std.crypto.sign.Ed25519.Signature {
511 return switch (self.*) { 513 return switch (self.*) {
512 .seed_key_pair => |*kp| try kp.sign(msg), 514 .seed_key_pair => |*kp| try kp.sign(msg),
513 .private_key => |*pk| try pk.sign(msg), 515 .private_key => |*pk| try pk.sign(msg),