Fixed remaining compilation issues
This commit is contained in:
parent
fc71c1a65b
commit
e594fab52f
6 changed files with 33 additions and 21 deletions
|
|
@ -11,6 +11,16 @@ const X25519= @import("crypto/x25519.zig");
|
||||||
const RANDOM = @import("crypto/random.zig");
|
const RANDOM = @import("crypto/random.zig");
|
||||||
|
|
||||||
|
|
||||||
|
///////////////// Errors
|
||||||
|
//
|
||||||
|
|
||||||
|
pub const CryptoError = error {
|
||||||
|
InvalidSignature,
|
||||||
|
FailedKeyGeneration,
|
||||||
|
InvalidKey,
|
||||||
|
NotImplemented
|
||||||
|
};
|
||||||
|
|
||||||
///////////////// Structs
|
///////////////// Structs
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
// Errors
|
||||||
|
const CryptoError = @import("../crypto.zig").CryptoError;
|
||||||
// Constants and default implementation
|
// Constants and default implementation
|
||||||
const Ed25519 = @import("std").crypto.sign.Ed25519;
|
const Ed25519 = @import("std").crypto.sign.Ed25519;
|
||||||
// Random number generation
|
// Random number generation
|
||||||
|
|
@ -9,35 +11,36 @@ pub const signature_size : comptime_int = Ed25519.Signature.encoded_length;
|
||||||
|
|
||||||
pub const Implementation = struct {
|
pub const Implementation = struct {
|
||||||
|
|
||||||
generateKeys: *const fn (pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) void,
|
generateKeys: *const fn (pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) CryptoError!void,
|
||||||
sign: *const fn (key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) void,
|
sign: *const fn (key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) CryptoError!void,
|
||||||
verify: *const fn (key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) void,
|
verify: *const fn (key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) CryptoError!void,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
pub fn generateKeys(pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) !void {
|
pub fn generateKeys(pubkey_buffer: *[public_key_size]u8, privkey_buffer: *[secret_key_size]u8, rng: rngProvider) !void {
|
||||||
|
|
||||||
const seed_buffer: [Ed25519.KeyPair.seed_length]u8 = undefined;
|
var seed_buffer: [Ed25519.KeyPair.seed_length]u8 = undefined;
|
||||||
rng.generate(&seed_buffer);
|
rng.generate(&seed_buffer);
|
||||||
const keypair: Ed25519.KeyPair = try Ed25519.KeyPair.generateDeterministic(seed_buffer);
|
const keypair: Ed25519.KeyPair = Ed25519.KeyPair.generateDeterministic(seed_buffer) catch return CryptoError.FailedKeyGeneration;
|
||||||
|
|
||||||
|
|
||||||
@memcpy(pubkey_buffer[0..public_key_size], keypair.public_key.bytes);
|
@memcpy(pubkey_buffer[0..public_key_size], &keypair.public_key.bytes);
|
||||||
@memcpy(privkey_buffer[0..secret_key_size], keypair.secret_key.bytes);
|
@memcpy(privkey_buffer[0..secret_key_size], &keypair.secret_key.bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign(key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) !void {
|
pub fn sign(key: *const [secret_key_size]u8, data: []const u8, signature_out: *const [signature_size]u8) !void {
|
||||||
_ = key;
|
_ = key;
|
||||||
_ = data;
|
_ = data;
|
||||||
_ = signature_out;
|
_ = signature_out;
|
||||||
return error.NotImplemented;
|
return CryptoError.NotImplemented;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify(key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) !void {
|
pub fn verify(key: *const [public_key_size]u8, signature: *const [signature_size]u8, data: []const u8) !void {
|
||||||
const sig = Ed25519.Signature.fromBytes(signature);
|
// TODO maybe find a more efficient way to do that (excessive copies)
|
||||||
const pubkey = Ed25519.PublicKey.fromBytes(key);
|
const sig = Ed25519.Signature.fromBytes(signature.*);
|
||||||
|
const pubkey = Ed25519.PublicKey.fromBytes(key.*) catch return CryptoError.InvalidKey;
|
||||||
|
|
||||||
return sig.verify(sig, data, pubkey);
|
return sig.verify(data, pubkey) catch CryptoError.InvalidSignature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,13 @@ pub const Implementation = struct {
|
||||||
|
|
||||||
|
|
||||||
fn expand(out: []u8, ctx: []const u8, prk: *[prk_length]u8) void {
|
fn expand(out: []u8, ctx: []const u8, prk: *[prk_length]u8) void {
|
||||||
Hkdf.expand(out, ctx, prk);
|
// TODO enhance efficiency (excessive copies)
|
||||||
|
Hkdf.expand(out, ctx, prk.*);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract(out: *[prk_length]u8, salt: []const u8, ikm: []const u8) void {
|
fn extract(out: *[prk_length]u8, salt: []const u8, ikm: []const u8) void {
|
||||||
out *= Hkdf.extract(salt, ikm);
|
// TODO enhance efficiency (excessive copies)
|
||||||
|
out.* = Hkdf.extract(salt, ikm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ pub const Implementation = struct {
|
||||||
fn generate(out: []u8) void {
|
fn generate(out: []u8) void {
|
||||||
// WARNING This is not secure at all, use only for testing purposes
|
// WARNING This is not secure at all, use only for testing purposes
|
||||||
for(out) |*byte| {
|
for(out) |*byte| {
|
||||||
byte *= undefined;
|
byte.* = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ pub const Implementation = struct {
|
||||||
hash: *const fn (buffer: []const u8, out: *[hash_len]u8) void,
|
hash: *const fn (buffer: []const u8, out: *[hash_len]u8) void,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn hash(buffer: []const u8, out: *[hash_len]u8) !void {
|
pub fn hash(buffer: []const u8, out: *[hash_len]u8) void {
|
||||||
Sha256.hash(buffer, out, .{});
|
Sha256.hash(buffer, out, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,13 @@
|
||||||
const Sha512 = @import("std").crypto.sha2.Sha512;
|
const Sha512 = @import("std").crypto.hash.sha2.Sha512;
|
||||||
|
|
||||||
pub const hash_len: comptime_int = Sha512.digest_length;
|
pub const hash_len: comptime_int = Sha512.digest_length;
|
||||||
|
|
||||||
pub const Implementation = struct {
|
pub const Implementation = struct {
|
||||||
|
|
||||||
hash: *const fn (buffer: []const u8, out: []u8) void,
|
hash: *const fn (buffer: []const u8, out: *[hash_len]u8) void,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn hash(buffer: []const u8, out: []u8) !void {
|
pub fn hash(buffer: []const u8, out: *[hash_len]u8) void {
|
||||||
if (out.len < hash_len) {
|
|
||||||
return error.BufferTooShort;
|
|
||||||
}
|
|
||||||
Sha512.hash(buffer, out, .{});
|
Sha512.hash(buffer, out, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue