Made some advancements on the cryptographic primitives implementation

This commit is contained in:
Gu://em_ 2026-07-03 21:56:17 +02:00
parent cb72a9cdc9
commit 0eae5818a0
8 changed files with 69 additions and 17 deletions

View file

@ -54,7 +54,6 @@ pub fn resolveEngine(comptime provider: PartialEngine) Engine {
.sha512 = provider.sha512 orelse SHA512.DefaultImplementation,
.x25519 = provider.x25519 orelse X25519.DefaultImplementation,
};
}

View file

@ -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;

View file

@ -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;
}
};
}

View file

@ -4,4 +4,6 @@ pub const Implementation = struct {
pub const defaultImplementation = struct {
const Hkdf = @import("std").crypto.kdf.hkdf.Hkdf;
};

View file

@ -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;
}
};

View file

@ -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, .{});
}
};

View file

@ -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, .{});
}
};

View file

@ -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
};