From b70c1ad89f96c49d9812655b53c05b690ea42be2 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 20 Apr 2026 12:39:25 +0200 Subject: [PATCH 1/2] std.crypto.aes: fix BlockVec xorBytes and orBlocks signatures Both methods had wrong parameter or return types that would cause compilation failures. --- lib/std/crypto/aes/aesni.zig | 2 +- lib/std/crypto/aes/armcrypto.zig | 8 ++++---- lib/std/crypto/aes/soft.zig | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/std/crypto/aes/aesni.zig b/lib/std/crypto/aes/aesni.zig index c7b82e0fb964a62a0cdb79b09cbee84a233def41..f21e57dae5919ad674cf3cb38878085928a2fd7a 100644 --- a/lib/std/crypto/aes/aesni.zig +++ b/lib/std/crypto/aes/aesni.zig @@ -312,7 +312,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { } /// Apply the bitwise OR operation to the content of two block vectors. - pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { + pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { var out: Self = undefined; inline for (0..native_words) |i| { out.repr[i] = block_vec1.repr[i] | block_vec2.repr[i]; diff --git a/lib/std/crypto/aes/armcrypto.zig b/lib/std/crypto/aes/armcrypto.zig index 02cf207777a86b03b70fd03c5d9e453cfa89a506..438f27abf6b6cb5e041dd8b9ce700607507a6eec 100644 --- a/lib/std/crypto/aes/armcrypto.zig +++ b/lib/std/crypto/aes/armcrypto.zig @@ -216,10 +216,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { } /// XOR the block vector with a byte sequence. - pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [32]u8 { - var out: Self = undefined; + pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8 { + var out: [blocks_count * 16]u8 = undefined; inline for (0..native_words) |i| { - out.repr[i] = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); + out[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); } return out; } @@ -279,7 +279,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { } /// Apply the bitwise OR operation to the content of two block vectors. - pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { + pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { var out: Self = undefined; inline for (0..native_words) |i| { out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); diff --git a/lib/std/crypto/aes/soft.zig b/lib/std/crypto/aes/soft.zig index 989635208bfeb7e8ddba58c4cc46f9e4e6f2fb5c..28a0ac9f98efbffc6d2a34d9780617ed46afd512 100644 --- a/lib/std/crypto/aes/soft.zig +++ b/lib/std/crypto/aes/soft.zig @@ -391,10 +391,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { } /// XOR the block vector with a byte sequence. - pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [32]u8 { - var out: Self = undefined; + pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8 { + var out: [blocks_count * 16]u8 = undefined; for (0..native_words) |i| { - out.repr[i] = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); + out[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); } return out; } @@ -454,7 +454,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { } /// Apply the bitwise OR operation to the content of two block vectors. - pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { + pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { var out: Self = undefined; for (0..native_words) |i| { out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); -- 2.54.0 From 7a755c4d2d13229537c55c291ff3f322ad6aba85 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 20 Apr 2026 12:45:05 +0200 Subject: [PATCH 2/2] Add AES tests for or/xor/and blocks --- lib/std/crypto/aes.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/std/crypto/aes.zig b/lib/std/crypto/aes.zig index 99f61eda6c240fda41ea241191e03dbd66c91360..acf3a72e1fd2616a6074f896c3cf53da02364c48 100644 --- a/lib/std/crypto/aes.zig +++ b/lib/std/crypto/aes.zig @@ -137,6 +137,18 @@ test "BlockVec invMixColumns" { } } +test "BlockVec bitwise operations" { + const a_bytes: [32]u8 = @splat(0xaa); + const b_bytes: [32]u8 = @splat(0xbb); + const a = BlockVec(2).fromBytes(&a_bytes); + const b = BlockVec(2).fromBytes(&b_bytes); + + try testing.expectEqual(@as([32]u8, @splat(0x11)), a.xorBytes(&b_bytes)); + try testing.expectEqual(@as([32]u8, @splat(0x11)), a.xorBlocks(b).toBytes()); + try testing.expectEqual(@as([32]u8, @splat(0xbb)), a.orBlocks(b).toBytes()); + try testing.expectEqual(@as([32]u8, @splat(0xaa)), a.andBlocks(b).toBytes()); +} + test "expand 256-bit key" { const key = [_]u8{ 0x60, 0x3d, 0xeb, 0x10, -- 2.54.0