crypto.aes.soft: use std.atomic.cache_line instead of a harcoded value (#22026)

This commit is contained in:
Frank Denis 2024-11-20 04:48:18 +01:00 committed by GitHub
parent 5f3a70ed5f
commit acba2645f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -669,7 +669,7 @@ fn mul(a: u8, b: u8) u8 {
return @as(u8, @truncate(s)); 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 { 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) { 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) { const stride = switch (side_channels_mitigations) {
.none => unreachable, .none => unreachable,
.basic => sbox.len / 4, .basic => sbox.len / 4,
.medium => sbox.len / (sbox.len / cache_line_bytes) * 2, .medium => @min(sbox.len, 2 * cache_line_bytes),
.full => sbox.len / (sbox.len / cache_line_bytes), .full => @min(sbox.len, cache_line_bytes),
}; };
const of0 = idx0 % stride; const of0 = idx0 % stride;
const of1 = idx1 % 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], table[3][idx3],
}; };
} else { } else {
const table_bytes = @sizeOf(@TypeOf(table[0]));
const stride = switch (side_channels_mitigations) { const stride = switch (side_channels_mitigations) {
.none => unreachable, .none => unreachable,
.basic => table[0].len / 4, .basic => table[0].len / 4,
.medium => table[0].len / (table_bytes / cache_line_bytes) * 2, .medium => @max(1, @min(table[0].len, 2 * cache_line_bytes / 4)),
.full => table[0].len / (table_bytes / cache_line_bytes), .full => @max(1, @min(table[0].len, cache_line_bytes / 4)),
}; };
const of0 = idx0 % stride; const of0 = idx0 % stride;
const of1 = idx1 % stride; const of1 = idx1 % stride;