authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2022-07-06 08:30:43+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-06 08:30:43+02:00
log38096960fb7defc707e002093de65e61ff8d94cd
treecd5ba2b0317a7f4302d42584223dfeb4e02bc908
parent6279a1d684193ffbe34b384250cdbb5ab4a328f9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

crypto.sign.ecdsa: fix toCompressedSec1()/toUnompressedSec1() (#12009)

The Ecdsa.PublicKey type is not a direct alias for a curve element. So, use the inner field containing the curve element for serialization.

1 files changed, 16 insertions(+), 4 deletions(-)

lib/std/crypto/ecdsa.zig+16-4
......@@ -62,13 +62,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
6262 }
6363
6464 /// Encode the public key using the compressed SEC-1 format.
65 pub fn toCompressedSec1(p: Curve) [compressed_sec1_encoded_length]u8 {
66 return p.toCompressedSec1();
65 pub fn toCompressedSec1(pk: PublicKey) [compressed_sec1_encoded_length]u8 {
66 return pk.p.toCompressedSec1();
6767 }
6868
6969 /// Encoding the public key using the uncompressed SEC-1 format.
70 pub fn toUncompressedSec1(p: Curve) [uncompressed_sec1_encoded_length]u8 {
71 return p.toUncompressedSec1();
70 pub fn toUncompressedSec1(pk: PublicKey) [uncompressed_sec1_encoded_length]u8 {
71 return pk.p.toUncompressedSec1();
7272 }
7373 };
7474
......@@ -743,3 +743,15 @@ fn tvTry(vector: TestVector) !void {
743743 const sig = try Scheme.Signature.fromDer(sig_der);
744744 try sig.verify(msg, pk);
745745}
746
747test "ECDSA - Sec1 encoding/decoding" {
748 const Scheme = EcdsaP384Sha384;
749 const kp = try Scheme.KeyPair.create(null);
750 const pk = kp.public_key;
751 const pk_compressed_sec1 = pk.toCompressedSec1();
752 const pk_recovered1 = try Scheme.PublicKey.fromSec1(&pk_compressed_sec1);
753 try testing.expectEqualSlices(u8, &pk_recovered1.toCompressedSec1(), &pk_compressed_sec1);
754 const pk_uncompressed_sec1 = pk.toUncompressedSec1();
755 const pk_recovered2 = try Scheme.PublicKey.fromSec1(&pk_uncompressed_sec1);
756 try testing.expectEqualSlices(u8, &pk_recovered2.toUncompressedSec1(), &pk_uncompressed_sec1);
757}