Skip to content

Commit

Permalink
Move crypto.randomBytes() to crypto.random.bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Oct 29, 2020
1 parent 81f5e1e commit bd3ad0c
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 44 deletions.
5 changes: 4 additions & 1 deletion lib/std/crypto.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ pub const nacl = struct {
};

const std = @import("std.zig");
pub const randomBytes = std.os.getrandom;

pub const random = struct {
pub const bytes = std.os.getrandom;
};

test "crypto" {
inline for (std.meta.declarations(@This())) |decl| {
Expand Down
8 changes: 4 additions & 4 deletions lib/std/crypto/25519/ed25519.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub const Ed25519 = struct {
pub fn create(seed: ?[seed_length]u8) !KeyPair {
const ss = seed orelse ss: {
var random_seed: [seed_length]u8 = undefined;
try crypto.randomBytes(&random_seed);
try crypto.random.bytes(&random_seed);
break :ss random_seed;
};
var az: [Sha512.digest_length]u8 = undefined;
Expand Down Expand Up @@ -179,7 +179,7 @@ pub const Ed25519 = struct {

var z_batch: [count]Curve.scalar.CompressedScalar = undefined;
for (z_batch) |*z| {
try std.crypto.randomBytes(z[0..16]);
try std.crypto.random.bytes(z[0..16]);
mem.set(u8, z[16..], 0);
}

Expand Down Expand Up @@ -232,8 +232,8 @@ test "ed25519 batch verification" {
const key_pair = try Ed25519.KeyPair.create(null);
var msg1: [32]u8 = undefined;
var msg2: [32]u8 = undefined;
try std.crypto.randomBytes(&msg1);
try std.crypto.randomBytes(&msg2);
try std.crypto.random.bytes(&msg1);
try std.crypto.random.bytes(&msg2);
const sig1 = try Ed25519.sign(&msg1, key_pair, null);
const sig2 = try Ed25519.sign(&msg2, key_pair, null);
var signature_batch = [_]Ed25519.BatchElement{
Expand Down
4 changes: 2 additions & 2 deletions lib/std/crypto/25519/edwards25519.zig
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ test "edwards25519 packing/unpacking" {
test "edwards25519 point addition/substraction" {
var s1: [32]u8 = undefined;
var s2: [32]u8 = undefined;
try std.crypto.randomBytes(&s1);
try std.crypto.randomBytes(&s2);
try std.crypto.random.bytes(&s1);
try std.crypto.random.bytes(&s2);
const p = try Edwards25519.basePoint.clampedMul(s1);
const q = try Edwards25519.basePoint.clampedMul(s2);
const r = p.add(q).add(q).sub(q).sub(q);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/crypto/25519/x25519.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const X25519 = struct {
pub fn create(seed: ?[seed_length]u8) !KeyPair {
const sk = seed orelse sk: {
var random_seed: [seed_length]u8 = undefined;
try crypto.randomBytes(&random_seed);
try crypto.random.bytes(&random_seed);
break :sk random_seed;
};
var kp: KeyPair = undefined;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/crypto/bcrypt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn strHashInternal(password: []const u8, rounds_log: u6, salt: [salt_length]u8)
/// and then use the resulting hash as the password parameter for bcrypt.
pub fn strHash(password: []const u8, rounds_log: u6) ![hash_length]u8 {
var salt: [salt_length]u8 = undefined;
try crypto.randomBytes(&salt);
try crypto.random.bytes(&salt);
return strHashInternal(password, rounds_log, salt);
}

Expand All @@ -282,7 +282,7 @@ pub fn strVerify(h: [hash_length]u8, password: []const u8) BcryptError!void {

test "bcrypt codec" {
var salt: [salt_length]u8 = undefined;
try crypto.randomBytes(&salt);
try crypto.random.bytes(&salt);
var salt_str: [salt_str_length]u8 = undefined;
Codec.encode(salt_str[0..], salt[0..]);
var salt2: [salt_length]u8 = undefined;
Expand Down
Loading

0 comments on commit bd3ad0c

Please sign in to comment.