Changed ed25519 constants names to better align with the stlib implementation and made the generateKeys's function pubkey argument optional to spare a useless memcpy when possible
This commit is contained in:
parent
4a267c135c
commit
856f4f626b
1 changed files with 12 additions and 12 deletions
|
|
@ -5,37 +5,37 @@ const Ed25519 = @import("std").crypto.sign.Ed25519;
|
||||||
// Random number generation
|
// Random number generation
|
||||||
const rngProvider = @import("random.zig").Implementation;
|
const rngProvider = @import("random.zig").Implementation;
|
||||||
|
|
||||||
pub const secret_key_size: comptime_int = Ed25519.SecretKey.encoded_length;
|
pub const secret_key_length: comptime_int = Ed25519.SecretKey.encoded_length;
|
||||||
pub const public_key_size: comptime_int = Ed25519.PublicKey.encoded_length;
|
pub const public_key_length: comptime_int = Ed25519.PublicKey.encoded_length;
|
||||||
pub const signature_size : comptime_int = Ed25519.Signature.encoded_length;
|
pub const signature_length : comptime_int = Ed25519.Signature.encoded_length;
|
||||||
|
pub const seed_length : comptime_int = Ed25519.Signature.encoded_length;
|
||||||
|
|
||||||
pub const Implementation = struct {
|
pub const Implementation = struct {
|
||||||
|
|
||||||
generateKeys: *const fn (pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) CryptoError!void,
|
generateKeys: *const fn (pubkey_buffer: ?*[public_key_length]u8, privkey_buffer: *[secret_key_length]u8, rng: rngProvider) CryptoError!void,
|
||||||
sign: *const fn (key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) CryptoError!void,
|
sign: *const fn (key: *const [secret_key_length]u8, data: []const u8, signature_out: *const [signature_length]u8) CryptoError!void,
|
||||||
verify: *const fn (key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) CryptoError!void,
|
verify: *const fn (key: *const [public_key_length]u8, signature: *const [signature_length]u8, data: []const u8) CryptoError!void,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
pub fn generateKeys(pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) !void {
|
pub fn generateKeys(pubkey_buffer: ?*[public_key_length]u8, privkey_buffer: *[secret_key_length]u8, rng: rngProvider) !void {
|
||||||
|
|
||||||
var seed_buffer: [Ed25519.KeyPair.seed_length]u8 = undefined;
|
var seed_buffer: [Ed25519.KeyPair.seed_length]u8 = undefined;
|
||||||
rng.generate(&seed_buffer);
|
rng.generate(&seed_buffer);
|
||||||
const keypair: Ed25519.KeyPair = Ed25519.KeyPair.generateDeterministic(seed_buffer) catch return CryptoError.FailedKeyGeneration;
|
const keypair: Ed25519.KeyPair = Ed25519.KeyPair.generateDeterministic(seed_buffer) catch return CryptoError.FailedKeyGeneration;
|
||||||
|
|
||||||
|
if (pubkey_buffer != null) @memcpy(pubkey_buffer.?[0..public_key_length], &keypair.public_key.bytes);
|
||||||
@memcpy(pubkey_buffer[0..public_key_size], &keypair.public_key.bytes);
|
@memcpy(privkey_buffer[0..secret_key_length], &keypair.secret_key.bytes);
|
||||||
@memcpy(privkey_buffer[0..secret_key_size], &keypair.secret_key.bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign(key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) !void {
|
pub fn sign(key: *const [secret_key_length]u8, data: []const u8, signature_out: *const [signature_length]u8) !void {
|
||||||
_ = key;
|
_ = key;
|
||||||
_ = data;
|
_ = data;
|
||||||
_ = signature_out;
|
_ = signature_out;
|
||||||
return CryptoError.NotImplemented;
|
return CryptoError.NotImplemented;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify(key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) !void {
|
pub fn verify(key: *const [public_key_length]u8, signature: *const [signature_length]u8, data: []const u8) !void {
|
||||||
// TODO maybe find a more efficient way to do that (excessive copies)
|
// TODO maybe find a more efficient way to do that (excessive copies)
|
||||||
const sig = Ed25519.Signature.fromBytes(signature.*);
|
const sig = Ed25519.Signature.fromBytes(signature.*);
|
||||||
const pubkey = Ed25519.PublicKey.fromBytes(key.*) catch return CryptoError.InvalidKey;
|
const pubkey = Ed25519.PublicKey.fromBytes(key.*) catch return CryptoError.InvalidKey;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue