Crypto engine design enhancements and new library entry point
This commit is contained in:
parent
cd83af4ef9
commit
693d7cfe51
9 changed files with 132 additions and 45 deletions
|
|
@ -6,17 +6,55 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
const AES = @import("crypto/aes.zig");
|
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 {
|
pub const PartialEngine = struct {
|
||||||
return struct {
|
aes_cbc256: ? AES.CBC256_Implementation = null,
|
||||||
const Self = @This();
|
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" {
|
test "AES encryption defaults" {
|
||||||
|
|
||||||
const crypto = CryptoEngine();
|
const crypto = resolveEngine(.{});
|
||||||
|
|
||||||
var data = [_]u8{ 1, 2, 3, 4 };
|
var data = [_]u8{ 1, 2, 3, 4 };
|
||||||
const key = [_]u8{ 1, 2, 3, 4 };
|
const key = [_]u8{ 1, 2, 3, 4 };
|
||||||
|
|
|
||||||
|
|
@ -1,41 +1,23 @@
|
||||||
// WARNING NOT FUNCTIONAL !!!
|
pub const CBC256_Implementation = struct {
|
||||||
// This is just a draft
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
pub const DefaultEngine = struct {
|
pub const BLOCK_SIZE = 16;
|
||||||
pub fn encrypt(data: []u8) void {
|
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;
|
_ = 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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
};
|
||||||
15
src/root.zig
15
src/root.zig
|
|
@ -1,2 +1,13 @@
|
||||||
pub const packet = @import("packet.zig");
|
const crypto = @import("crypto.zig");
|
||||||
// pub 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");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue