diff --git a/src/crypto.zig b/src/crypto.zig index c1500fd..5027da2 100644 --- a/src/crypto.zig +++ b/src/crypto.zig @@ -6,17 +6,55 @@ // const AES = @import("crypto/aes.zig"); +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"); -///////////////// Public functions +///////////////// Structs // -pub fn CryptoEngine(comptime Provider: anytype) type { - return struct { - const Self = @This(); +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, +}; - pub const Aes = AES.AesEngine(Provider); +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, +}; + + +///////////////// 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 { + return .{ + .aes_cbc256 = provider.aes_cbc256 orelse AES.CBC256_DefaultImplementation, + .ed25519 = provider.ed25519 orelse ED25519.DefaultImplementation, + .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, }; + } @@ -27,7 +65,7 @@ const std = @import("std"); test "AES encryption defaults" { - const crypto = CryptoEngine(); + const crypto = resolveEngine(.{}); var data = [_]u8{ 1, 2, 3, 4 }; const key = [_]u8{ 1, 2, 3, 4 }; diff --git a/src/crypto/aes.zig b/src/crypto/aes.zig index 4facf45..ec77f0c 100644 --- a/src/crypto/aes.zig +++ b/src/crypto/aes.zig @@ -1,41 +1,23 @@ -// WARNING NOT FUNCTIONAL !!! -// This is just a draft -// TODO +pub const CBC256_Implementation = struct { -pub const DefaultEngine = struct { - pub fn encrypt(data: []u8) void { + pub const BLOCK_SIZE = 16; + pub const KEY_SIZE = 32; + + // NOTE: data should be padded to make its length be a multiple of BLOCK_SIZE + encypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8, ) void, + decrypt: *const fn (key: *const [KEY_SIZE]u8, iv: *const [BLOCK_SIZE]u8, data: *const []u8, ) void, +}; + +// TODO +pub const CBC265_DefaultImplementation = struct { + pub fn encrypt( key: *const [32]u8, iv: *const [16]u8, data: *const []u8 ) void { + _ = key; + _ = iv; + _ = data; + } + pub fn decrypt( key: *const [32]u8, iv: *const [16]u8, data: *const []u8 ) void { + _ = key; + _ = iv; _ = data; } }; - -pub fn AesEngine(comptime Provider: anytype) type { - - _ = Provider; - - return struct { - const Self = @This(); - - pub const CBC_265 = struct { - const key_length = 256; - - // Encrypts the data block using AES 256 CBC - pub fn encrypt(data: []u8, key: [key_length]u8, iv: []u8) void { - _ = data; - _ = key; - _ = iv; - } - }; - - pub const CBC_128 = struct { - const key_length = 128; - - // Encrypts the data block using AES 128 CBC - pub fn encrypt(data: []u8, key: [key_length]u8, iv: []u8) void { - _ = data; - _ = key; - _ = iv; - } - }; - - }; -} diff --git a/src/crypto/ed25519.zig b/src/crypto/ed25519.zig index e69de29..5ab0ca1 100644 --- a/src/crypto/ed25519.zig +++ b/src/crypto/ed25519.zig @@ -0,0 +1,21 @@ +pub const Implementation = struct { + pub const KEY_SIZE = 32; + pub const SIGNATURE_SIZE = 64; + 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 const defaultImplementation = struct { + fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) void { + _ = key; + _ = data; + _ = signature_out; + + } + fn verify(signature: *const [64]u8, data: *const []const u8) void { + _ = signature; + _ = data; + } + +}; diff --git a/src/crypto/hkdf.zig b/src/crypto/hkdf.zig index e69de29..5344700 100644 --- a/src/crypto/hkdf.zig +++ b/src/crypto/hkdf.zig @@ -0,0 +1,7 @@ +pub const Implementation = struct { + +}; + +pub const defaultImplementation = struct { + +}; diff --git a/src/crypto/hmac.zig b/src/crypto/hmac.zig index e69de29..5344700 100644 --- a/src/crypto/hmac.zig +++ b/src/crypto/hmac.zig @@ -0,0 +1,7 @@ +pub const Implementation = struct { + +}; + +pub const defaultImplementation = struct { + +}; diff --git a/src/crypto/sha256.zig b/src/crypto/sha256.zig index e69de29..5344700 100644 --- a/src/crypto/sha256.zig +++ b/src/crypto/sha256.zig @@ -0,0 +1,7 @@ +pub const Implementation = struct { + +}; + +pub const defaultImplementation = struct { + +}; diff --git a/src/crypto/sha512.zig b/src/crypto/sha512.zig index e69de29..5344700 100644 --- a/src/crypto/sha512.zig +++ b/src/crypto/sha512.zig @@ -0,0 +1,7 @@ +pub const Implementation = struct { + +}; + +pub const defaultImplementation = struct { + +}; diff --git a/src/crypto/x25519.zig b/src/crypto/x25519.zig index e69de29..5344700 100644 --- a/src/crypto/x25519.zig +++ b/src/crypto/x25519.zig @@ -0,0 +1,7 @@ +pub const Implementation = struct { + +}; + +pub const defaultImplementation = struct { + +}; diff --git a/src/root.zig b/src/root.zig index 95a57a1..9ab6784 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,2 +1,13 @@ -pub const packet = @import("packet.zig"); -// pub const crypto = @import("crypto.zig"); +const crypto = @import("crypto.zig"); + +// This function is used to initialize the library +// You can pass your own cryptographic functions to take advantage of your +// platform or use the library's software implementations for compatibility +// Use its return value to access the library modules +pub fn RnsZero(comptime cryptoProvider: crypto.PartialEngine) type { + const cryptoEngine = crypto.resolveEngine(cryptoProvider); + _ = cryptoEngine; + return struct { + pub const packet = @import("packet.zig"); + }; +}