authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2025-02-19 19:25:04+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-02-21 22:45:38+01:00
log65e7ede4994237f160e8db558e74da113f886f98
treeccc4491c0bfc8ff0159acd71f46976efd48203f0
parent05d8b565ad1a1d6c0c1e93dc47f1d828043fcafc

crypto.Ed25519.KeyPair: return an error rather than assert

When runtime safety is turned on, `Ed25519.fromSecretKey()` can currently hit an assertion if the format of the secret key is invalid. Return an error instead, so that applications can recover.

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

lib/std/crypto/25519/ed25519.zig+3-1
...@@ -299,7 +299,9 @@ pub const Ed25519 = struct {...@@ -299,7 +299,9 @@ pub const Ed25519 = struct {
299 if (std.debug.runtime_safety) {299 if (std.debug.runtime_safety) {
300 const pk_p = try Curve.fromBytes(secret_key.publicKeyBytes());300 const pk_p = try Curve.fromBytes(secret_key.publicKeyBytes());
301 const recomputed_kp = try generateDeterministic(secret_key.seed());301 const recomputed_kp = try generateDeterministic(secret_key.seed());
302 debug.assert(mem.eql(u8, &recomputed_kp.public_key.toBytes(), &pk_p.toBytes()));302 if (!mem.eql(u8, &recomputed_kp.public_key.toBytes(), &pk_p.toBytes())) {
303 return error.NonCanonical;
304 }
303 }305 }
304 return KeyPair{306 return KeyPair{
305 .public_key = try PublicKey.fromBytes(secret_key.publicKeyBytes()),307 .public_key = try PublicKey.fromBytes(secret_key.publicKeyBytes()),