From acba2645f776530e9a1ca287ae2336c928653724 Mon Sep 17 00:00:00 2001 From: Frank Denis <124872+jedisct1@users.noreply.github.com> Date: Wed, 20 Nov 2024 04:48:18 +0100 Subject: [PATCH] crypto.aes.soft: use std.atomic.cache_line instead of a harcoded value (#22026) --- lib/std/crypto/aes/soft.zig | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/std/crypto/aes/soft.zig b/lib/std/crypto/aes/soft.zig index fd0dfaf001..8430a3af7e 100644 --- a/lib/std/crypto/aes/soft.zig +++ b/lib/std/crypto/aes/soft.zig @@ -669,7 +669,7 @@ fn mul(a: u8, b: u8) u8 { return @as(u8, @truncate(s)); } -const cache_line_bytes = 64; +const cache_line_bytes = std.atomic.cache_line; inline fn sbox_lookup(sbox: *align(64) const [256]u8, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u8 { if (side_channels_mitigations == .none) { @@ -683,8 +683,8 @@ inline fn sbox_lookup(sbox: *align(64) const [256]u8, idx0: u8, idx1: u8, idx2: const stride = switch (side_channels_mitigations) { .none => unreachable, .basic => sbox.len / 4, - .medium => sbox.len / (sbox.len / cache_line_bytes) * 2, - .full => sbox.len / (sbox.len / cache_line_bytes), + .medium => @min(sbox.len, 2 * cache_line_bytes), + .full => @min(sbox.len, cache_line_bytes), }; const of0 = idx0 % stride; const of1 = idx1 % stride; @@ -718,12 +718,11 @@ inline fn table_lookup(table: *align(64) const [4][256]u32, idx0: u8, idx1: u8, table[3][idx3], }; } else { - const table_bytes = @sizeOf(@TypeOf(table[0])); const stride = switch (side_channels_mitigations) { .none => unreachable, .basic => table[0].len / 4, - .medium => table[0].len / (table_bytes / cache_line_bytes) * 2, - .full => table[0].len / (table_bytes / cache_line_bytes), + .medium => @max(1, @min(table[0].len, 2 * cache_line_bytes / 4)), + .full => @max(1, @min(table[0].len, cache_line_bytes / 4)), }; const of0 = idx0 % stride; const of1 = idx1 % stride;