From ff60995aaa771b20ddd331ef0d742e6bc6d88b4f Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Sat, 4 Jul 2026 11:08:11 +0200 Subject: [PATCH] Added the random crypto module with its default implementation, implemented the Ed25519 keys generation function --- src/crypto.zig | 4 ++++ src/crypto/ed25519.zig | 51 +++++++++++++++++++++--------------------- src/crypto/random.zig | 15 +++++++++++++ 3 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 src/crypto/random.zig diff --git a/src/crypto.zig b/src/crypto.zig index 679c46d..83abb09 100644 --- a/src/crypto.zig +++ b/src/crypto.zig @@ -12,6 +12,7 @@ const HMAC = @import("crypto/hmac.zig"); const SHA256 = @import("crypto/sha256.zig"); const SHA512 = @import("crypto/sha512.zig"); const X25519= @import("crypto/x25519.zig"); +const RANDOM = @import("crypto/random.zig"); ///////////////// Structs @@ -25,6 +26,7 @@ pub const PartialEngine = struct { sha256: ? SHA256.Implementation = null, sha512: ? SHA512.Implementation = null, x25519: ? X25519.Implementation = null, + random: ? RANDOM.Implementation = null, }; pub const Engine = struct { @@ -35,6 +37,7 @@ pub const Engine = struct { sha256: SHA256.Implementation, sha512: SHA512.Implementation, x25519: X25519.Implementation, + random: RANDOM.Implementation, }; @@ -53,6 +56,7 @@ pub fn resolveEngine(comptime provider: PartialEngine) Engine { .sha256 = provider.sha256 orelse SHA256.DefaultImplementation, .sha512 = provider.sha512 orelse SHA512.DefaultImplementation, .x25519 = provider.x25519 orelse X25519.DefaultImplementation, + .random = provider.random orelse RANDOM.DefaultImplementation, }; } diff --git a/src/crypto/ed25519.zig b/src/crypto/ed25519.zig index e58c993..f058b62 100644 --- a/src/crypto/ed25519.zig +++ b/src/crypto/ed25519.zig @@ -3,40 +3,39 @@ pub const Implementation = struct { pub const KEY_SIZE = 32; pub const SIGNATURE_SIZE = 64; - generateKey: *const fn (privkey_buffer: *[KEY_SIZE]u8) void, + generateKeys: *const fn (pubkey_buffer: []u8, privkey_buffer: []u8) void, sign: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature_out: *const [SIGNATURE_SIZE]u8) void, verify: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature: *const [SIGNATURE_SIZE]u8) void, }; // TODO -pub fn defaultImplementation() type { +pub const defaultImplementation = struct { const Ed25519 = @import("std").crypto.sign.Ed25519; - _ = Ed25519; - // Again, some Ed25519 functions seem to rely on the io module that is not necessarily available on embedded systems - // It may need some custom implementation or other library - - return struct { + pub fn generateKeys(pubkey_buffer: []u8, privkey_buffer: []u8) !void { + if (Ed25519.SecretKey.encoded_length > privkey_buffer or + Ed25519.SecretKey.encoded_length > privkey_buffer) + { + return error.BufferTooShort; + } + // TODO replace by a secure random number generator + const keypair: Ed25519.KeyPair = try Ed25519.KeyPair.generateDeterministic(undefined); - pub fn generateKeys(pubkey_buffer: *[32]u8, privkey_buffer: *[32]u8) !void { - _ = pubkey_buffer; - _ = privkey_buffer; - return error.NotImplemented; + @memcpy(pubkey_buffer[0..Ed25519.PublicKey.encoded_length], keypair.public_key.bytes); + @memcpy(privkey_buffer[0..Ed25519.SecretKey.encoded_length], keypair.secret_key.bytes); + } - } - - pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void { - _ = key; - _ = data; - _ = signature_out; - return error.NotImplemented; - } + pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void { + _ = key; + _ = data; + _ = signature_out; + return error.NotImplemented; + } - pub fn verify(signature: *const [64]u8, data: *const []const u8) !void { - _ = signature; - _ = data; - return error.NotImplemented; - } - }; -} + pub fn verify(signature: *const [64]u8, data: *const []const u8) !void { + _ = signature; + _ = data; + return error.NotImplemented; + } +}; diff --git a/src/crypto/random.zig b/src/crypto/random.zig new file mode 100644 index 0000000..b4d99d9 --- /dev/null +++ b/src/crypto/random.zig @@ -0,0 +1,15 @@ +pub const Implementation = struct { + generate: *const fn (out: []u8) void, +}; + +// TODO +pub const defaultImplementation = struct { + + pub fn generate(out: []u8) void { + // WARNING This is not secure at all, use only for testing purposes + for(out) |*byte| { + byte *= undefined; + } + } +}; +