Added the random crypto module with its default implementation, implemented the Ed25519 keys generation function
This commit is contained in:
parent
1216dc2282
commit
ff60995aaa
3 changed files with 44 additions and 26 deletions
|
|
@ -12,6 +12,7 @@ const HMAC = @import("crypto/hmac.zig");
|
||||||
const SHA256 = @import("crypto/sha256.zig");
|
const SHA256 = @import("crypto/sha256.zig");
|
||||||
const SHA512 = @import("crypto/sha512.zig");
|
const SHA512 = @import("crypto/sha512.zig");
|
||||||
const X25519= @import("crypto/x25519.zig");
|
const X25519= @import("crypto/x25519.zig");
|
||||||
|
const RANDOM = @import("crypto/random.zig");
|
||||||
|
|
||||||
|
|
||||||
///////////////// Structs
|
///////////////// Structs
|
||||||
|
|
@ -25,6 +26,7 @@ pub const PartialEngine = struct {
|
||||||
sha256: ? SHA256.Implementation = null,
|
sha256: ? SHA256.Implementation = null,
|
||||||
sha512: ? SHA512.Implementation = null,
|
sha512: ? SHA512.Implementation = null,
|
||||||
x25519: ? X25519.Implementation = null,
|
x25519: ? X25519.Implementation = null,
|
||||||
|
random: ? RANDOM.Implementation = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Engine = struct {
|
pub const Engine = struct {
|
||||||
|
|
@ -35,6 +37,7 @@ pub const Engine = struct {
|
||||||
sha256: SHA256.Implementation,
|
sha256: SHA256.Implementation,
|
||||||
sha512: SHA512.Implementation,
|
sha512: SHA512.Implementation,
|
||||||
x25519: X25519.Implementation,
|
x25519: X25519.Implementation,
|
||||||
|
random: RANDOM.Implementation,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -53,6 +56,7 @@ pub fn resolveEngine(comptime provider: PartialEngine) Engine {
|
||||||
.sha256 = provider.sha256 orelse SHA256.DefaultImplementation,
|
.sha256 = provider.sha256 orelse SHA256.DefaultImplementation,
|
||||||
.sha512 = provider.sha512 orelse SHA512.DefaultImplementation,
|
.sha512 = provider.sha512 orelse SHA512.DefaultImplementation,
|
||||||
.x25519 = provider.x25519 orelse X25519.DefaultImplementation,
|
.x25519 = provider.x25519 orelse X25519.DefaultImplementation,
|
||||||
|
.random = provider.random orelse RANDOM.DefaultImplementation,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,40 +3,39 @@ pub const Implementation = struct {
|
||||||
pub const KEY_SIZE = 32;
|
pub const KEY_SIZE = 32;
|
||||||
pub const SIGNATURE_SIZE = 64;
|
pub const SIGNATURE_SIZE = 64;
|
||||||
|
|
||||||
generateKey: *const fn (privkey_buffer: *[KEY_SIZE]u8) void,
|
generateKeys: *const fn (pubkey_buffer: []u8, privkey_buffer: []u8) void,
|
||||||
sign: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature_out: *const [SIGNATURE_SIZE]u8) void,
|
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,
|
verify: *const fn (key: *const [KEY_SIZE]u8, data: *const []const u8, signature: *const [SIGNATURE_SIZE]u8) void,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
pub fn defaultImplementation() type {
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
const Ed25519 = @import("std").crypto.sign.Ed25519;
|
const Ed25519 = @import("std").crypto.sign.Ed25519;
|
||||||
_ = Ed25519;
|
|
||||||
|
|
||||||
// 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 generateKeys(pubkey_buffer: []u8, privkey_buffer: []u8) !void {
|
||||||
|
if (Ed25519.SecretKey.encoded_length > privkey_buffer or
|
||||||
|
Ed25519.SecretKey.encoded_length > privkey_buffer)
|
||||||
|
{
|
||||||
|
return error.BufferTooShort;
|
||||||
}
|
}
|
||||||
|
// TODO replace by a secure random number generator
|
||||||
|
const keypair: Ed25519.KeyPair = try Ed25519.KeyPair.generateDeterministic(undefined);
|
||||||
|
|
||||||
pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void {
|
@memcpy(pubkey_buffer[0..Ed25519.PublicKey.encoded_length], keypair.public_key.bytes);
|
||||||
_ = key;
|
@memcpy(privkey_buffer[0..Ed25519.SecretKey.encoded_length], keypair.secret_key.bytes);
|
||||||
_ = data;
|
}
|
||||||
_ = signature_out;
|
|
||||||
return error.NotImplemented;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify(signature: *const [64]u8, data: *const []const u8) !void {
|
pub fn sign(key: *const [32]u8, data: *const []const u8, signature_out: *const [64]u8) !void {
|
||||||
_ = signature;
|
_ = key;
|
||||||
_ = data;
|
_ = data;
|
||||||
return error.NotImplemented;
|
_ = signature_out;
|
||||||
}
|
return error.NotImplemented;
|
||||||
};
|
}
|
||||||
}
|
|
||||||
|
pub fn verify(signature: *const [64]u8, data: *const []const u8) !void {
|
||||||
|
_ = signature;
|
||||||
|
_ = data;
|
||||||
|
return error.NotImplemented;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
15
src/crypto/random.zig
Normal file
15
src/crypto/random.zig
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
pub const Implementation = struct {
|
||||||
|
generate: *const fn (out: []u8) void,
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
pub const defaultImplementation = struct {
|
||||||
|
|
||||||
|
pub fn generate(out: []u8) void {
|
||||||
|
// WARNING This is not secure at all, use only for testing purposes
|
||||||
|
for(out) |*byte| {
|
||||||
|
byte *= undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue