harden MWP error handling. return errors from signing instead of
unreachable, fix memory leak on deserialization failure, validate peer publication timestamps against max skew
This commit is contained in:
@@ -91,9 +91,9 @@ pub const KeyPair = struct {
|
||||
}
|
||||
|
||||
/// Create from existing secret key bytes
|
||||
pub fn fromSecretKey(secret: [64]u8) KeyPair {
|
||||
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch unreachable;
|
||||
const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch unreachable;
|
||||
pub fn fromSecretKey(secret: [64]u8) error{InvalidKeyData}!KeyPair {
|
||||
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch return error.InvalidKeyData;
|
||||
const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch return error.InvalidKeyData;
|
||||
return .{
|
||||
.public = kp.public_key.toBytes(),
|
||||
.secret = secret,
|
||||
@@ -101,14 +101,14 @@ pub const KeyPair = struct {
|
||||
}
|
||||
|
||||
/// Sign a 32-byte hash
|
||||
pub fn sign(self: KeyPair, hash: [32]u8) [64]u8 {
|
||||
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable;
|
||||
const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch unreachable;
|
||||
pub fn sign(self: KeyPair, hash: [32]u8) error{InvalidKeyData}![64]u8 {
|
||||
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch return error.InvalidKeyData;
|
||||
const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData;
|
||||
const kp = std.crypto.sign.Ed25519.KeyPair{
|
||||
.secret_key = sk,
|
||||
.public_key = pk,
|
||||
};
|
||||
const sig = std.crypto.sign.Ed25519.KeyPair.sign(kp, &hash, null) catch unreachable;
|
||||
const sig = std.crypto.sign.Ed25519.KeyPair.sign(kp, &hash, null) catch return error.InvalidKeyData;
|
||||
return sig.toBytes();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user