2026-06-13 23:20:25 +02:00
|
|
|
///////////////// Imports
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
const AES = @import("crypto/aes.zig");
|
2026-06-15 15:23:41 +02:00
|
|
|
const ED25519 = @import("crypto/ed25519.zig");
|
|
|
|
|
const HKDF = @import("crypto/hkdf.zig");
|
|
|
|
|
const HMAC = @import("crypto/hmac.zig");
|
|
|
|
|
const SHA256 = @import("crypto/sha256.zig");
|
|
|
|
|
const SHA512 = @import("crypto/sha512.zig");
|
|
|
|
|
const X25519= @import("crypto/x25519.zig");
|
2026-07-04 11:08:11 +02:00
|
|
|
const RANDOM = @import("crypto/random.zig");
|
2026-06-13 23:20:25 +02:00
|
|
|
|
|
|
|
|
|
2026-06-15 15:23:41 +02:00
|
|
|
///////////////// Structs
|
2026-06-13 23:20:25 +02:00
|
|
|
//
|
|
|
|
|
|
2026-06-15 15:23:41 +02:00
|
|
|
pub const PartialEngine = struct {
|
|
|
|
|
aes_cbc256: ? AES.CBC256_Implementation = null,
|
|
|
|
|
ed25519: ? ED25519.Implementation = null,
|
|
|
|
|
hkdf: ? HKDF.Implementation = null,
|
|
|
|
|
hmac: ? HMAC.Implementation = null,
|
|
|
|
|
sha256: ? SHA256.Implementation = null,
|
|
|
|
|
sha512: ? SHA512.Implementation = null,
|
|
|
|
|
x25519: ? X25519.Implementation = null,
|
2026-07-04 11:08:11 +02:00
|
|
|
random: ? RANDOM.Implementation = null,
|
2026-06-15 15:23:41 +02:00
|
|
|
};
|
2026-06-13 23:20:25 +02:00
|
|
|
|
2026-06-15 15:23:41 +02:00
|
|
|
pub const Engine = struct {
|
|
|
|
|
aes_cbc256: AES.CBC256_Implementation,
|
|
|
|
|
ed25519: ED25519.Implementation,
|
|
|
|
|
hkdf: HKDF.Implementation,
|
|
|
|
|
hmac: HMAC.Implementation,
|
|
|
|
|
sha256: SHA256.Implementation,
|
|
|
|
|
sha512: SHA512.Implementation,
|
|
|
|
|
x25519: X25519.Implementation,
|
2026-07-04 11:08:11 +02:00
|
|
|
random: RANDOM.Implementation,
|
2026-06-15 15:23:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////// Functions
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Initializes the cyrpto engine with the functions given inside Provider
|
|
|
|
|
// These functions must follow the same structure as the struct returned by this
|
|
|
|
|
// function and its sub-structs.
|
|
|
|
|
pub fn resolveEngine(comptime provider: PartialEngine) Engine {
|
2026-07-08 17:18:56 +02:00
|
|
|
|
|
|
|
|
// Used by other engine components
|
|
|
|
|
const randomImpl = provider.random orelse RANDOM.defaultImplementation;
|
|
|
|
|
|
2026-06-15 15:23:41 +02:00
|
|
|
return .{
|
|
|
|
|
.aes_cbc256 = provider.aes_cbc256 orelse AES.CBC256_DefaultImplementation,
|
2026-07-09 23:42:48 +02:00
|
|
|
.ed25519 = provider.ed25519 orelse ED25519.defaultImplementation(randomImpl),
|
2026-06-15 15:23:41 +02:00
|
|
|
.hkdf = provider.hkdf orelse HKDF.DefaultImplementation,
|
|
|
|
|
.hmac = provider.hmac orelse HMAC.DefaultImplementation,
|
|
|
|
|
.sha256 = provider.sha256 orelse SHA256.DefaultImplementation,
|
|
|
|
|
.sha512 = provider.sha512 orelse SHA512.DefaultImplementation,
|
|
|
|
|
.x25519 = provider.x25519 orelse X25519.DefaultImplementation,
|
2026-07-08 17:18:56 +02:00
|
|
|
.random = randomImpl,
|
2026-06-13 23:20:25 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////// Tests
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
|
|
test "AES encryption defaults" {
|
|
|
|
|
|
2026-06-15 15:23:41 +02:00
|
|
|
const crypto = resolveEngine(.{});
|
2026-07-09 23:42:48 +02:00
|
|
|
|
|
|
|
|
var data: []u8 = undefined;
|
|
|
|
|
const key: [32]u8 = undefined;
|
|
|
|
|
const iv: [16]u8 = undefined;
|
|
|
|
|
crypto.aes_cbc256.encrypt(&key, &iv, &data);
|
2026-06-13 23:20:25 +02:00
|
|
|
}
|