Implemented X25519 generateKeys and fromEd25519 functions
This commit is contained in:
parent
856f4f626b
commit
547a515952
1 changed files with 31 additions and 5 deletions
|
|
@ -1,13 +1,39 @@
|
||||||
//TODO
|
// Errors
|
||||||
|
const CryptoError = @import("../crypto.zig").CryptoError;
|
||||||
|
// Constants and default implementation
|
||||||
|
const X25519 = @import("std").crypto.dh.X25519;
|
||||||
|
const Ed25519 = @import("std").crypto.sign.Ed25519;
|
||||||
|
// Random number generation
|
||||||
|
const rngProvider = @import("random.zig").Implementation;
|
||||||
|
|
||||||
const X25519 = @import("std").std.crypto.dh.X25519;
|
pub const secret_key_length: comptime_int = X25519.secret_length;
|
||||||
|
pub const public_key_length: comptime_int = X25519.public_length;
|
||||||
|
pub const seed_length: comptime_int = X25519.seed_length;
|
||||||
|
|
||||||
pub const Implementation = struct {
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
generateKeys: *const fn (pubkey_buffer: ?*[public_key_length]u8, privkey_buffer: *[secret_key_length]u8, rng: rngProvider) CryptoError!void,
|
||||||
|
fromEd25519: *const fn (ed25519_keypair: *Ed25519.Keypair, x25519_keypair: *X25519.KeyPair, rng: rngProvider) CryptoError!void,
|
||||||
|
// TODO
|
||||||
|
// maybe switch to a Keypair system based on the stdlib implementation
|
||||||
|
// generateKeys: *const fn (keypair: *X25519.KeyPair, rng: rngProvider) CryptoError!void,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn generateKeys (pubkey_buffer: ?*[public_key_length]u8, privkey_buffer: *[secret_key_length]u8, rng: rngProvider) !void {
|
||||||
|
|
||||||
|
var seed_buffer: [seed_length]u8 = undefined;
|
||||||
|
rng.generate(&seed_buffer);
|
||||||
|
const keypair: X25519.KeyPair = X25519.KeyPair.generateDeterministic(seed_buffer) catch return CryptoError.FailedKeyGeneration;
|
||||||
|
|
||||||
|
if (pubkey_buffer != null) @memcpy(pubkey_buffer.?[0..public_key_length], &keypair.public_key.bytes);
|
||||||
|
@memcpy(privkey_buffer[0..secret_key_length], &keypair.secret_key.bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fromEd25519 (ed25519_keypair: *Ed25519.Keypair, x25519_keypair: *X25519.KeyPair) CryptoError!void {
|
||||||
|
x25519_keypair.* = X25519.KeyPair.fromEd25519(ed25519_keypair);
|
||||||
|
}
|
||||||
|
|
||||||
pub const defaultImplementation: Implementation = .{
|
pub const defaultImplementation: Implementation = .{
|
||||||
|
.generateKeys = generateKeys,
|
||||||
// One problem, it uses the zig's io module, which is not supported yet as it depends on the target OS
|
.fromEd25519 = fromEd25519,
|
||||||
// It may need a custom implementation or some other library
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue