From 0eae5818a07b92908e02684d8f5c3faf0af08353 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Fri, 3 Jul 2026 21:56:17 +0200 Subject: [PATCH] Made some advancements on the cryptographic primitives implementation --- src/crypto.zig | 1 - src/crypto/aes.zig | 4 ++++ src/crypto/ed25519.zig | 43 +++++++++++++++++++++++++++--------------- src/crypto/hkdf.zig | 2 ++ src/crypto/hmac.zig | 8 ++++++++ src/crypto/sha256.zig | 11 +++++++++++ src/crypto/sha512.zig | 11 +++++++++++ src/crypto/x25519.zig | 6 +++++- 8 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/crypto.zig b/src/crypto.zig index 5027da2..679c46d 100644 --- a/src/crypto.zig +++ b/src/crypto.zig @@ -54,7 +54,6 @@ pub fn resolveEngine(comptime provider: PartialEngine) Engine { .sha512 = provider.sha512 orelse SHA512.DefaultImplementation, .x25519 = provider.x25519 orelse X25519.DefaultImplementation, }; - } diff --git a/src/crypto/aes.zig b/src/crypto/aes.zig index 853f39f..df886b2 100644 --- a/src/crypto/aes.zig +++ b/src/crypto/aes.zig @@ -10,6 +10,10 @@ pub const CBC256_Implementation = struct { // TODO pub const CBC265_DefaultImplementation = struct { + + // Zig stdlib seems to provide no support for AES CBC265 + // It may need some custom implementation or another library + pub fn encrypt(key: *const [32]u8, iv: *const [16]u8, data: *const []u8) !void { _ = key; _ = iv; diff --git a/src/crypto/ed25519.zig b/src/crypto/ed25519.zig index 2da4bc1..e58c993 100644 --- a/src/crypto/ed25519.zig +++ b/src/crypto/ed25519.zig @@ -9,21 +9,34 @@ pub const Implementation = struct { }; // TODO -pub const defaultImplementation = struct { +pub fn defaultImplementation() type { - pub fn generateKeys(privkey_buffer: *[32]u8) !void { - _ = privkey_buffer; - } - - pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void { - _ = key; - _ = data; - _ = signature_out; - } + const Ed25519 = @import("std").crypto.sign.Ed25519; + _ = Ed25519; - pub fn verify(signature: *const [64]u8, data: *const []const u8) !void { - _ = signature; - _ = data; - } + // 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: *[32]u8, privkey_buffer: *[32]u8) !void { + _ = pubkey_buffer; + _ = privkey_buffer; + 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; + } + }; +} diff --git a/src/crypto/hkdf.zig b/src/crypto/hkdf.zig index 5344700..f1e9458 100644 --- a/src/crypto/hkdf.zig +++ b/src/crypto/hkdf.zig @@ -4,4 +4,6 @@ pub const Implementation = struct { pub const defaultImplementation = struct { + const Hkdf = @import("std").crypto.kdf.hkdf.Hkdf; + }; diff --git a/src/crypto/hmac.zig b/src/crypto/hmac.zig index 5344700..cb096e8 100644 --- a/src/crypto/hmac.zig +++ b/src/crypto/hmac.zig @@ -4,4 +4,12 @@ pub const Implementation = struct { pub const defaultImplementation = struct { + const Hmac = @import("std").crypto.auth.hmac.Hmac; + + pub fn create(out: []u8, msg: []const u8, key: []const u8) !void { + _ = out; + _ = msg; + _ = key; + return error.NotImplemented; + } }; diff --git a/src/crypto/sha256.zig b/src/crypto/sha256.zig index 5344700..70478e0 100644 --- a/src/crypto/sha256.zig +++ b/src/crypto/sha256.zig @@ -1,7 +1,18 @@ pub const Implementation = struct { + hash: *const fn (buffer: []const u8, out: []u8) void, }; pub const defaultImplementation = struct { + const Sha256 = @import("std").crypto.hash.sha2.Sha256; + + const HASH_LEN = 32; // In bytes + + pub fn hash(buffer: []const u8, out: []u8) !void { + if (out.len < HASH_LEN) { + return error.BufferTooShort; + } + Sha256.hash(buffer, out, .{}); + } }; diff --git a/src/crypto/sha512.zig b/src/crypto/sha512.zig index 5344700..a69a88d 100644 --- a/src/crypto/sha512.zig +++ b/src/crypto/sha512.zig @@ -1,7 +1,18 @@ pub const Implementation = struct { + hash: *const fn (buffer: []const u8, out: []u8) void, }; pub const defaultImplementation = struct { + const Sha512 = @import("std").crypto.sha2.Sha512; + + const HASH_LEN = 64; // In bytes + + pub fn hash(buffer: []const u8, out: []u8) !void { + if (out.len < HASH_LEN) { + return error.BufferTooShort; + } + Sha512.hash(buffer, out, .{}); + } }; diff --git a/src/crypto/x25519.zig b/src/crypto/x25519.zig index 5344700..f3f6607 100644 --- a/src/crypto/x25519.zig +++ b/src/crypto/x25519.zig @@ -3,5 +3,9 @@ pub const Implementation = struct { }; pub const defaultImplementation = struct { - + + const X25519 = @import("std").std.crypto.dh.X25519; + + // One problem, it uses the zig's io module, which is not supported yet as it depends on the target OS + // It may need a custom implementation or some other library };