mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
d29871977f
We already have a LICENSE file that covers the Zig Standard Library. We no longer need to remind everyone that the license is MIT in every single file. Previously this was introduced to clarify the situation for a fork of Zig that made Zig's LICENSE file harder to find, and replaced it with their own license that required annual payments to their company. However that fork now appears to be dead. So there is no need to reinforce the copyright notice in every single file.
35 lines
958 B
Zig
35 lines
958 B
Zig
//! CSPRNG
|
|
|
|
const std = @import("std");
|
|
const Random = std.rand.Random;
|
|
const mem = std.mem;
|
|
const Gimli = @This();
|
|
|
|
random: Random,
|
|
state: std.crypto.core.Gimli,
|
|
|
|
pub const secret_seed_length = 32;
|
|
|
|
/// The seed must be uniform, secret and `secret_seed_length` bytes long.
|
|
pub fn init(secret_seed: [secret_seed_length]u8) Gimli {
|
|
var initial_state: [std.crypto.core.Gimli.BLOCKBYTES]u8 = undefined;
|
|
mem.copy(u8, initial_state[0..secret_seed_length], &secret_seed);
|
|
mem.set(u8, initial_state[secret_seed_length..], 0);
|
|
var self = Gimli{
|
|
.random = Random{ .fillFn = fill },
|
|
.state = std.crypto.core.Gimli.init(initial_state),
|
|
};
|
|
return self;
|
|
}
|
|
|
|
fn fill(r: *Random, buf: []u8) void {
|
|
const self = @fieldParentPtr(Gimli, "random", r);
|
|
|
|
if (buf.len != 0) {
|
|
self.state.squeeze(buf);
|
|
} else {
|
|
self.state.permute();
|
|
}
|
|
mem.set(u8, self.state.toSlice()[0..std.crypto.core.Gimli.RATE], 0);
|
|
}
|