Skip to content

Commit 5415142

Browse files
committed
std.crypto: better names for everything in utils
std.crypto has quite a few instances of breaking naming conventions. This is the beginning of an effort to address that. Deprecates `std.crypto.utils`.
1 parent ae5bf2f commit 5415142

18 files changed

+105
-84
lines changed

doc/langref.html.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5053,7 +5053,7 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
50535053
It may have any alignment, and it may have any element type.</p>
50545054
<p>{#syntax#}elem{#endsyntax#} is coerced to the element type of {#syntax#}dest{#endsyntax#}.</p>
50555055
<p>For securely zeroing out sensitive contents from memory, you should use
5056-
{#syntax#}std.crypto.utils.secureZero{#endsyntax#}</p>
5056+
{#syntax#}std.crypto.secureZero{#endsyntax#}</p>
50575057
{#header_close#}
50585058

50595059
{#header_open|@min#}

lib/std/crypto.zig

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const root = @import("root");
44

5+
pub const timing_safe = @import("crypto/timing_safe.zig");
6+
57
/// Authenticated Encryption with Associated Data
68
pub const aead = struct {
79
pub const aegis = struct {
@@ -180,8 +182,6 @@ pub const nacl = struct {
180182
pub const SealedBox = salsa20.SealedBox;
181183
};
182184

183-
pub const utils = @import("crypto/utils.zig");
184-
185185
/// Finite-field arithmetic.
186186
pub const ff = @import("crypto/ff.zig");
187187

@@ -301,7 +301,8 @@ test {
301301
_ = nacl.SecretBox;
302302
_ = nacl.SealedBox;
303303

304-
_ = utils;
304+
_ = secureZero;
305+
_ = timing_safe;
305306
_ = ff;
306307
_ = random;
307308
_ = errors;
@@ -353,3 +354,36 @@ test "issue #4532: no index out of bounds" {
353354
try std.testing.expectEqual(out1, out2);
354355
}
355356
}
357+
358+
/// Sets a slice to zeroes.
359+
/// Prevents the store from being optimized out.
360+
pub inline fn secureZero(comptime T: type, s: []volatile T) void {
361+
@memset(s, 0);
362+
}
363+
364+
test secureZero {
365+
var a = [_]u8{0xfe} ** 8;
366+
var b = [_]u8{0xfe} ** 8;
367+
368+
@memset(&a, 0);
369+
secureZero(u8, &b);
370+
371+
try std.testing.expectEqualSlices(u8, &a, &b);
372+
}
373+
374+
/// Deprecated in favor of `std.crypto`. To be removed after Zig 0.14.0 is released.
375+
///
376+
/// As a reminder, never use "utils" in a namespace (in any programming language).
377+
/// https://ziglang.org/documentation/0.13.0/#Avoid-Redundancy-in-Names
378+
pub const utils = struct {
379+
/// Deprecated in favor of `std.crypto.secureZero`.
380+
pub const secureZero = std.crypto.secureZero;
381+
/// Deprecated in favor of `std.crypto.timing_safe.eql`.
382+
pub const timingSafeEql = timing_safe.eql;
383+
/// Deprecated in favor of `std.crypto.timing_safe.compare`.
384+
pub const timingSafeCompare = timing_safe.compare;
385+
/// Deprecated in favor of `std.crypto.timing_safe.add`.
386+
pub const timingSafeAdd = timing_safe.add;
387+
/// Deprecated in favor of `std.crypto.timing_safe.sub`.
388+
pub const timingSafeSub = timing_safe.sub;
389+
};

lib/std/crypto/aegis.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type {
208208
blocks[4] = blocks[4].xorBlocks(AesBlock.fromBytes(dst[16..32]));
209209
}
210210
var computed_tag = state.mac(tag_bits, ad.len, m.len);
211-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
211+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
212212
if (!verify) {
213-
crypto.utils.secureZero(u8, &computed_tag);
213+
crypto.secureZero(u8, &computed_tag);
214214
@memset(m, undefined);
215215
return error.AuthenticationFailed;
216216
}
@@ -390,9 +390,9 @@ fn Aegis256Generic(comptime tag_bits: u9) type {
390390
blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(&dst));
391391
}
392392
var computed_tag = state.mac(tag_bits, ad.len, m.len);
393-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
393+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
394394
if (!verify) {
395-
crypto.utils.secureZero(u8, &computed_tag);
395+
crypto.secureZero(u8, &computed_tag);
396396
@memset(m, undefined);
397397
return error.AuthenticationFailed;
398398
}

lib/std/crypto/aes_gcm.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ fn AesGcm(comptime Aes: anytype) type {
9595
computed_tag[i] ^= x;
9696
}
9797

98-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
98+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
9999
if (!verify) {
100-
crypto.utils.secureZero(u8, &computed_tag);
100+
crypto.secureZero(u8, &computed_tag);
101101
@memset(m, undefined);
102102
return error.AuthenticationFailed;
103103
}

lib/std/crypto/aes_ocb.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ fn AesOcb(comptime Aes: anytype) type {
234234
var e = xorBlocks(xorBlocks(sum, offset), lx.dol);
235235
aes_enc_ctx.encrypt(&e, &e);
236236
var computed_tag = xorBlocks(e, hash(aes_enc_ctx, &lx, ad));
237-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
237+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
238238
if (!verify) {
239-
crypto.utils.secureZero(u8, &computed_tag);
239+
crypto.secureZero(u8, &computed_tag);
240240
@memset(m, undefined);
241241
return error.AuthenticationFailed;
242242
}

lib/std/crypto/ascon.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn State(comptime endian: std.builtin.Endian) type {
152152

153153
/// Clear the entire state, disabling compiler optimizations.
154154
pub fn secureZero(self: *Self) void {
155-
std.crypto.utils.secureZero(u64, &self.st);
155+
std.crypto.secureZero(u64, &self.st);
156156
}
157157

158158
/// Apply a reduced-round permutation to the state.

lib/std/crypto/bcrypt.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const pwhash = crypto.pwhash;
99
const testing = std.testing;
1010
const HmacSha512 = crypto.auth.hmac.sha2.HmacSha512;
1111
const Sha512 = crypto.hash.sha2.Sha512;
12-
const utils = crypto.utils;
1312

1413
const phc_format = @import("phc_encoding.zig");
1514

@@ -446,7 +445,7 @@ pub fn bcrypt(
446445
state.expand0(passwordZ);
447446
state.expand0(salt[0..]);
448447
}
449-
utils.secureZero(u8, &password_buf);
448+
crypto.secureZero(u8, &password_buf);
450449

451450
var cdata = [6]u32{ 0x4f727068, 0x65616e42, 0x65686f6c, 0x64657253, 0x63727944, 0x6f756274 }; // "OrpheanBeholderScryDoubt"
452451
k = 0;
@@ -556,8 +555,8 @@ const pbkdf_prf = struct {
556555
}
557556

558557
// zap
559-
crypto.utils.secureZero(u32, &cdata);
560-
crypto.utils.secureZero(u32, &state.subkeys);
558+
crypto.secureZero(u32, &cdata);
559+
crypto.secureZero(u32, &state.subkeys);
561560

562561
return out;
563562
}

lib/std/crypto/chacha20.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,9 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type {
714714
var computed_tag: [16]u8 = undefined;
715715
mac.final(computed_tag[0..]);
716716

717-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
717+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
718718
if (!verify) {
719-
crypto.utils.secureZero(u8, &computed_tag);
719+
crypto.secureZero(u8, &computed_tag);
720720
@memset(m, undefined);
721721
return error.AuthenticationFailed;
722722
}

lib/std/crypto/ff.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ pub fn Uint(comptime max_bits: comptime_int) type {
225225

226226
/// Returns `true` if both integers are equal.
227227
pub fn eql(x: Self, y: Self) bool {
228-
return crypto.utils.timingSafeEql([max_limbs_count]Limb, x.limbs_buffer, y.limbs_buffer);
228+
return crypto.timing_safe.eql([max_limbs_count]Limb, x.limbs_buffer, y.limbs_buffer);
229229
}
230230

231231
/// Compares two integers.
232232
pub fn compare(x: Self, y: Self) math.Order {
233-
return crypto.utils.timingSafeCompare(
233+
return crypto.timing_safe.compare(
234234
Limb,
235235
x.limbsConst(),
236236
y.limbsConst(),

lib/std/crypto/ghash_polyval.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const builtin = @import("builtin");
33
const assert = std.debug.assert;
44
const math = std.math;
55
const mem = std.mem;
6-
const utils = std.crypto.utils;
76

87
const Precomp = u128;
98

@@ -403,7 +402,7 @@ fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type {
403402
st.pad();
404403
mem.writeInt(u128, out[0..16], st.acc, endian);
405404

406-
utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Self)]);
405+
std.crypto.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Self)]);
407406
}
408407

409408
/// Compute the GHASH of a message.

lib/std/crypto/isap.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ pub const IsapA128A = struct {
158158
/// Contents of `m` are undefined if an error is returned.
159159
pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) AuthenticationError!void {
160160
var computed_tag = mac(c, ad, npub, key);
161-
const verify = crypto.utils.timingSafeEql([tag_length]u8, computed_tag, tag);
161+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
162162
if (!verify) {
163-
crypto.utils.secureZero(u8, &computed_tag);
163+
crypto.secureZero(u8, &computed_tag);
164164
@memset(m, undefined);
165165
return error.AuthenticationFailed;
166166
}

lib/std/crypto/keccak_p.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub fn KeccakF(comptime f: u11) type {
132132

133133
/// Clear the entire state, disabling compiler optimizations.
134134
pub fn secureZero(self: *Self) void {
135-
std.crypto.utils.secureZero(T, &self.st);
135+
std.crypto.secureZero(T, &self.st);
136136
}
137137

138138
inline fn round(self: *Self, rc: T) void {

lib/std/crypto/ml_kem.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ fn Mat(comptime K: u8) type {
15081508

15091509
// Returns `true` if a ≠ b.
15101510
fn ctneq(comptime len: usize, a: [len]u8, b: [len]u8) u1 {
1511-
return 1 - @intFromBool(crypto.utils.timingSafeEql([len]u8, a, b));
1511+
return 1 - @intFromBool(crypto.timing_safe.eql([len]u8, a, b));
15121512
}
15131513

15141514
// Copy src into dst given b = 1.

lib/std/crypto/pcurves/common.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn Field(comptime params: FieldParams) type {
5757
mem.writeInt(std.meta.Int(.unsigned, encoded_length * 8), &fos, field_order, .little);
5858
break :fos fos;
5959
};
60-
if (crypto.utils.timingSafeCompare(u8, &s, &field_order_s, .little) != .lt) {
60+
if (crypto.timing_safe.compare(u8, &s, &field_order_s, .little) != .lt) {
6161
return error.NonCanonical;
6262
}
6363
}

lib/std/crypto/poly1305.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const std = @import("../std.zig");
2-
const utils = std.crypto.utils;
32
const mem = std.mem;
43
const mulWide = std.math.mulWide;
54

@@ -185,7 +184,7 @@ pub const Poly1305 = struct {
185184
mem.writeInt(u64, out[0..8], st.h[0], .little);
186185
mem.writeInt(u64, out[8..16], st.h[1], .little);
187186

188-
utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Poly1305)]);
187+
std.crypto.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Poly1305)]);
189188
}
190189

191190
pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {

lib/std/crypto/salsa20.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const crypto = std.crypto;
44
const debug = std.debug;
55
const math = std.math;
66
const mem = std.mem;
7-
const utils = std.crypto.utils;
87

98
const Poly1305 = crypto.onetimeauth.Poly1305;
109
const Blake2b = crypto.hash.blake2.Blake2b;
@@ -419,9 +418,9 @@ pub const XSalsa20Poly1305 = struct {
419418
var computed_tag: [tag_length]u8 = undefined;
420419
mac.final(&computed_tag);
421420

422-
const verify = utils.timingSafeEql([tag_length]u8, computed_tag, tag);
421+
const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
423422
if (!verify) {
424-
utils.secureZero(u8, &computed_tag);
423+
crypto.secureZero(u8, &computed_tag);
425424
@memset(m, undefined);
426425
return error.AuthenticationFailed;
427426
}
@@ -540,7 +539,7 @@ pub const SealedBox = struct {
540539
const nonce = createNonce(ekp.public_key, public_key);
541540
c[0..public_length].* = ekp.public_key;
542541
try Box.seal(c[Box.public_length..], m, nonce, public_key, ekp.secret_key);
543-
utils.secureZero(u8, ekp.secret_key[0..]);
542+
crypto.secureZero(u8, ekp.secret_key[0..]);
544543
}
545544

546545
/// Decrypt a message using a key pair.

0 commit comments

Comments
 (0)