Implemented Hkdf and Hmac and added a digest length property to all of them to get closer to the zig stdlib implementation
This commit is contained in:
parent
1b98394fe4
commit
150c5b2e38
3 changed files with 37 additions and 13 deletions
|
|
@ -1,9 +1,29 @@
|
|||
pub const Implementation = struct {
|
||||
|
||||
prk_length: comptime_int,
|
||||
|
||||
expand: *const fn (out: []u8, ctx: []const u8, prk: *[Implementation.prk_length]u8) void,
|
||||
extract: *const fn (out: *[Implementation.prk_length]u8, salt: []const u8, ikm: []const u8) void,
|
||||
|
||||
};
|
||||
|
||||
pub const defaultImplementation = struct {
|
||||
|
||||
const Hkdf = @import("std").crypto.kdf.hkdf.Hkdf;
|
||||
const Sha256 = @import("std").crypto.hash.sha2.Sha256;
|
||||
|
||||
const HashImpl = Sha256;
|
||||
const HmacImpl = @import("std").crypto.auth.hmac.Hmac(HashImpl);
|
||||
|
||||
const Hkdf = @import("std").crypto.kdf.hkdf.Hkdf(HmacImpl);
|
||||
|
||||
pub const prk_length = Hkdf.prk_length;
|
||||
|
||||
pub fn expand(out: []u8, ctx: []const u8, prk: *[prk_length]u8) void {
|
||||
Hkdf.expand(out, ctx, prk);
|
||||
}
|
||||
|
||||
pub fn extract(out: *[prk_length]u8, salt: []const u8, ikm: []const u8) void {
|
||||
out *= Hkdf.extract(salt, ikm);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,15 +1,20 @@
|
|||
pub const Implementation = struct {
|
||||
|
||||
mac_length: comptime_int,
|
||||
|
||||
create: *const fn (out: *[Implementation.mac_length]u8, msg: []const u8, key: []const u8) void,
|
||||
};
|
||||
|
||||
pub const defaultImplementation = struct {
|
||||
|
||||
const Hmac = @import("std").crypto.auth.hmac.Hmac;
|
||||
const Sha256 = @import("std").crypto.hash.sha2.Sha256;
|
||||
|
||||
pub fn create(out: []u8, msg: []const u8, key: []const u8) !void {
|
||||
_ = out;
|
||||
_ = msg;
|
||||
_ = key;
|
||||
return error.NotImplemented;
|
||||
const HashImpl = Sha256;
|
||||
const Hmac = @import("std").crypto.auth.hmac.Hmac(Sha256);
|
||||
|
||||
pub const mac_length = Hmac.mac_length;
|
||||
|
||||
pub fn create(out: *[mac_length]u8, msg: []const u8, key: []const u8) void {
|
||||
Hmac.create(out, msg, key);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
pub const Implementation = struct {
|
||||
|
||||
hash: *const fn (buffer: []const u8, out: []u8) void,
|
||||
hash_length: comptime_int,
|
||||
|
||||
hash: *const fn (buffer: []const u8, out: *[Implementation.hash_length]u8) void,
|
||||
};
|
||||
|
||||
pub const defaultImplementation = struct {
|
||||
|
||||
const Sha256 = @import("std").crypto.hash.sha2.Sha256;
|
||||
|
||||
const HASH_LEN = 32; // In bytes
|
||||
pub const hash_len = Sha256.digest_length;
|
||||
|
||||
pub fn hash(buffer: []const u8, out: []u8) !void {
|
||||
if (out.len < HASH_LEN) {
|
||||
return error.BufferTooShort;
|
||||
}
|
||||
pub fn hash(buffer: []const u8, out: *[hash_len]u8) !void {
|
||||
Sha256.hash(buffer, out, .{});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue