authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-09-06 17:23:38+12:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-09-06 17:23:38+12:00
log29923efb954aa6e3297e6c2010eaf92fdb977c53
treef7188e0d76eb20d1d721d9891fe361088b8efe29
parent6632d85e5f1120784b3eb0a7ab8be0792ba27b85
parent15d30b967af3c4bd4d22a9e746c14b52b7974ba4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1480 from kristate/x25519-pubkey-fix

X25519: Fix createPublicKey signature and add test

1 files changed, 12 insertions(+), 1 deletions(-)

std/crypto/x25519.zig+12-1
...@@ -4,6 +4,7 @@...@@ -4,6 +4,7 @@
44
5const std = @import("../index.zig");5const std = @import("../index.zig");
6const builtin = @import("builtin");6const builtin = @import("builtin");
7const fmt = std.fmt;
78
8const Endian = builtin.Endian;9const Endian = builtin.Endian;
9const readInt = std.mem.readInt;10const readInt = std.mem.readInt;
...@@ -114,7 +115,7 @@ pub const X25519 = struct {...@@ -114,7 +115,7 @@ pub const X25519 = struct {
114 return !zerocmp(u8, out);115 return !zerocmp(u8, out);
115 }116 }
116117
117 pub fn createPublicKey(public_key: []const u8, private_key: []const u8) bool {118 pub fn createPublicKey(public_key: [] u8, private_key: []const u8) bool {
118 var base_point = []u8{9} ++ []u8{0} ** 31;119 var base_point = []u8{9} ++ []u8{0} ** 31;
119 return create(public_key, private_key, base_point);120 return create(public_key, private_key, base_point);
120 }121 }
...@@ -573,6 +574,16 @@ const Fe = struct {...@@ -573,6 +574,16 @@ const Fe = struct {
573 }574 }
574};575};
575576
577test "x25519 public key calculation from secret key" {
578 var sk: [32]u8 = undefined;
579 var pk_expected: [32]u8 = undefined;
580 var pk_calculated: [32]u8 = undefined;
581 try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166");
582 try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50");
583 std.debug.assert(X25519.createPublicKey(pk_calculated[0..], sk));
584 std.debug.assert(std.mem.eql(u8, pk_calculated, pk_expected));
585}
586
576test "x25519 rfc7748 vector1" {587test "x25519 rfc7748 vector1" {
577 const secret_key = "\xa5\x46\xe3\x6b\xf0\x52\x7c\x9d\x3b\x16\x15\x4b\x82\x46\x5e\xdd\x62\x14\x4c\x0a\xc1\xfc\x5a\x18\x50\x6a\x22\x44\xba\x44\x9a\xc4";588 const secret_key = "\xa5\x46\xe3\x6b\xf0\x52\x7c\x9d\x3b\x16\x15\x4b\x82\x46\x5e\xdd\x62\x14\x4c\x0a\xc1\xfc\x5a\x18\x50\x6a\x22\x44\xba\x44\x9a\xc4";
578 const public_key = "\xe6\xdb\x68\x67\x58\x30\x30\xdb\x35\x94\xc1\xa4\x24\xb1\x5f\x7c\x72\x66\x24\xec\x26\xb3\x35\x3b\x10\xa9\x03\xa6\xd0\xab\x1c\x4c";589 const public_key = "\xe6\xdb\x68\x67\x58\x30\x30\xdb\x35\x94\xc1\xa4\x24\xb1\x5f\x7c\x72\x66\x24\xec\x26\xb3\x35\x3b\x10\xa9\x03\xa6\xd0\xab\x1c\x4c";