From 6261c1373168b265047db5704d9d0fd5f2e458f2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 26 Apr 2023 13:57:08 -0700 Subject: update codebase to use `@memset` and `@memcpy` --- lib/std/crypto/utils.zig | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'lib/std/crypto/utils.zig') diff --git a/lib/std/crypto/utils.zig b/lib/std/crypto/utils.zig index 38dc236455..14a235e418 100644 --- a/lib/std/crypto/utils.zig +++ b/lib/std/crypto/utils.zig @@ -134,12 +134,8 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, /// Sets a slice to zeroes. /// Prevents the store from being optimized out. -pub fn secureZero(comptime T: type, s: []T) void { - // TODO: implement `@memset` for non-byte-sized element type in the llvm backend - //@memset(@as([]volatile T, s), 0); - const ptr = @ptrCast([*]volatile u8, s.ptr); - const length = s.len * @sizeOf(T); - @memset(ptr[0..length], 0); +pub inline fn secureZero(comptime T: type, s: []T) void { + @memset(@as([]volatile T, s), 0); } test "crypto.utils.timingSafeEql" { @@ -148,7 +144,7 @@ test "crypto.utils.timingSafeEql" { random.bytes(a[0..]); random.bytes(b[0..]); try testing.expect(!timingSafeEql([100]u8, a, b)); - mem.copy(u8, a[0..], b[0..]); + a = b; try testing.expect(timingSafeEql([100]u8, a, b)); } @@ -201,7 +197,7 @@ test "crypto.utils.secureZero" { var a = [_]u8{0xfe} ** 8; var b = [_]u8{0xfe} ** 8; - mem.set(u8, a[0..], 0); + @memset(a[0..], 0); secureZero(u8, b[0..]); try testing.expectEqualSlices(u8, a[0..], b[0..]); -- cgit v1.2.3