Skip to content

Commit 69c1914

Browse files
authored
fix: return errors for invalid public keys and recovery ids instead of aborting (#21)
1 parent 76e652e commit 69c1914

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/Nethermind.Crypto.SecP256k1.Tests/SecP256k1Tests.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MIT
33

44
using System;
5+
using System.Security.Cryptography;
56
using NUnit.Framework;
67

78
namespace Nethermind.Crypto.Secp256k1.Test;
@@ -122,4 +123,35 @@ public void Can_recover_uncompressed()
122123
SecP256k1.RecoverKeyFromCompact(recovered, messageHash, signature, recoveryId, false);
123124
Assert.That(recovered, Has.Length.EqualTo(65));
124125
}
126+
127+
[Test]
128+
public void Recover_returns_false_for_out_of_range_recovery_id([Values(-1, 4, 255)] int recoveryId)
129+
{
130+
byte[] privateKey = new byte[32];
131+
privateKey[0] = 1;
132+
byte[] messageHash = new byte[32];
133+
messageHash[0] = 1;
134+
byte[]? signature = SecP256k1.SignCompact(messageHash, privateKey, out _);
135+
byte[] recovered = new byte[33];
136+
Assert.That(SecP256k1.RecoverKeyFromCompact(recovered, messageHash, signature, recoveryId, true), Is.False);
137+
}
138+
139+
[Test]
140+
public void Can_decompress()
141+
{
142+
byte[] privateKey = new byte[32];
143+
privateKey[0] = 1;
144+
byte[] compressed = SecP256k1.GetPublicKey(privateKey, true)!;
145+
byte[] uncompressed = SecP256k1.GetPublicKey(privateKey, false)!;
146+
Assert.That(SecP256k1.Decompress(compressed), Is.EqualTo(uncompressed));
147+
}
148+
149+
[TestCase("000000000000000000000000000000000000000000000000000000000000000000")]
150+
[TestCase("020000000000000000000000000000000000000000000000000000000000000000")]
151+
[TestCase("02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")]
152+
public void Decompress_throws_for_invalid_key(string compressedHex)
153+
{
154+
byte[] compressed = Convert.FromHexString(compressedHex);
155+
Assert.That(() => SecP256k1.Decompress(compressed), Throws.TypeOf<CryptographicException>());
156+
}
125157
}

‎src/Nethermind.Crypto.SecP256k1/SecP256k1.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ public static bool RecoverKeyFromCompact(Span<byte> output, ReadOnlySpan<byte> m
125125

126126
ArgumentOutOfRangeException.ThrowIfNotEqual(output.Length, expectedLength, nameof(output));
127127

128+
// libsecp256k1 aborts the process on a recovery id outside 0..3 instead of returning 0
129+
if ((uint)recoveryId > 3)
130+
return false;
131+
128132
byte* recoverableSignature = stackalloc byte[65];
129133

130134
fixed (byte* compactSigPtr = &MemoryMarshal.GetReference(compactSignature))
@@ -180,7 +184,12 @@ public static byte[] Decompress(Span<byte> compressed)
180184
{
181185
Span<byte> serializedKey = stackalloc byte[65];
182186
Span<byte> publicKey = stackalloc byte[64];
183-
PublicKeyParse(publicKey, compressed);
187+
188+
// A failed parse leaves a zeroed key, which libsecp256k1 rejects by aborting the process
189+
if (!PublicKeyParse(publicKey, compressed))
190+
{
191+
throw new CryptographicException("Failed parsing public key");
192+
}
184193

185194
if (!PublicKeySerialize(serializedKey, publicKey))
186195
{

0 commit comments

Comments
 (0)