Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add ecdsa malleability checks #21

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
.hash = "1220ebf62505957cdd9c87b9c517ceba60c46f5c9578a504ab3299b3970cc3ab0a1d",
},
.@"zig-eth-secp256k1" = .{
.url = "https://github.com/jsign/zig-eth-secp256k1/archive/refs/tags/v0.1.0-beta-3.tar.gz",
.hash = "1220fcf062f8ee89b343e1588ac3cc002f37ee3f72841dd7f9493d9c09acad7915a3",
.url = "https://github.com/jsign/zig-eth-secp256k1/archive/refs/tags/v0.1.0-beta-4.tar.gz",
.hash = "1220c2dbdc5ddd85906c8858f2046b52870f4fe793e0c9af50b8591d10a3f267e250",
Comment on lines -10 to +11
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do some update in my dep, so updating here.

},
.httpz = .{
.url = "https://github.com/karlseguin/http.zig/archive/1a82beb0dfc22e6fc38e9918e323f8fbd3cb78a3.tar.gz",
Expand Down
10 changes: 10 additions & 0 deletions src/crypto/ecdsa.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ pub const Signer = struct {
}
};

pub fn validateSignatureFields(r: u256, s: u256) !void {
if (r > secp256k1.Secp256k1.order) {
return error.InvalidR;
}
// Malleability check.
if (s > secp256k1.Secp256k1.order / 2) {
return error.InvalidS;
}
}
Comment on lines +28 to +36
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the important checks.


// The following test values where generated using geth, as a reference.
const hashed_msg = common.comptimeHexToBytes("05e0e0ff09b01e5626daac3165b82afa42be29197b82e8a5a8800740ee7519d2");
const private_key = common.comptimeHexToBytes("f457cd3bd0186e342d243ea40ad78fe8e81743f90852e87074e68d8c94c2a42e");
Expand Down
7 changes: 6 additions & 1 deletion src/signer/signer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ pub const TxSigner = struct {

var sig: ecdsa.Signature = undefined;

// TODO: solve malleability problem.
sig[64] = switch (tx) {
Tx.LegacyTx => |itx| blk: {
try ecdsa.validateSignatureFields(itx.r, itx.s);

std.mem.writeIntSlice(u256, sig[0..32], itx.r, std.builtin.Endian.Big);
std.mem.writeIntSlice(u256, sig[32..64], itx.s, std.builtin.Endian.Big);

Expand All @@ -58,11 +59,15 @@ pub const TxSigner = struct {
break :blk @intCast(itx.v - v_eip155);
},
Tx.AccessListTx => |itx| blk: {
try ecdsa.validateSignatureFields(itx.r, itx.s);

std.mem.writeIntSlice(u256, sig[0..32], itx.r, std.builtin.Endian.Big);
std.mem.writeIntSlice(u256, sig[32..64], itx.s, std.builtin.Endian.Big);
break :blk @intCast(itx.y_parity);
},
Tx.FeeMarketTx => |itx| blk: {
try ecdsa.validateSignatureFields(itx.r, itx.s);

std.mem.writeIntSlice(u256, sig[0..32], itx.r, std.builtin.Endian.Big);
std.mem.writeIntSlice(u256, sig[32..64], itx.s, std.builtin.Endian.Big);
break :blk @intCast(itx.y_parity);
Expand Down
Loading