| author | |
| committer | |
| log | 7bd363f61b024c8925eaf86cbf87315094d5ae04 |
| tree | d0d9ed862332423856dd879a1188747005f3baad |
| parent | bb1f225bc8db6fe565f7e39714ef3565cf1b6e0a |
| parent | 7a755c4d2d13229537c55c291ff3f322ad6aba85 |
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31984
Reviewed-by: Andrew Kelley <andrew@ziglang.org>4 files changed, 21 insertions(+), 9 deletions(-)
lib/std/crypto/aes.zig+12| ... | @@ -137,6 +137,18 @@ test "BlockVec invMixColumns" { | ... | @@ -137,6 +137,18 @@ test "BlockVec invMixColumns" { |
| 137 | } | 137 | } |
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | test "BlockVec bitwise operations" { | ||
| 141 | const a_bytes: [32]u8 = @splat(0xaa); | ||
| 142 | const b_bytes: [32]u8 = @splat(0xbb); | ||
| 143 | const a = BlockVec(2).fromBytes(&a_bytes); | ||
| 144 | const b = BlockVec(2).fromBytes(&b_bytes); | ||
| 145 | |||
| 146 | try testing.expectEqual(@as([32]u8, @splat(0x11)), a.xorBytes(&b_bytes)); | ||
| 147 | try testing.expectEqual(@as([32]u8, @splat(0x11)), a.xorBlocks(b).toBytes()); | ||
| 148 | try testing.expectEqual(@as([32]u8, @splat(0xbb)), a.orBlocks(b).toBytes()); | ||
| 149 | try testing.expectEqual(@as([32]u8, @splat(0xaa)), a.andBlocks(b).toBytes()); | ||
| 150 | } | ||
| 151 | |||
| 140 | test "expand 256-bit key" { | 152 | test "expand 256-bit key" { |
| 141 | const key = [_]u8{ | 153 | const key = [_]u8{ |
| 142 | 0x60, 0x3d, 0xeb, 0x10, | 154 | 0x60, 0x3d, 0xeb, 0x10, |
lib/std/crypto/aes/aesni.zig+1-1| ... | @@ -312,7 +312,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { | ... | @@ -312,7 +312,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { |
| 312 | } | 312 | } |
| 313 | 313 | ||
| 314 | /// Apply the bitwise OR operation to the content of two block vectors. | 314 | /// Apply the bitwise OR operation to the content of two block vectors. |
| 315 | pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { | 315 | pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { |
| 316 | var out: Self = undefined; | 316 | var out: Self = undefined; |
| 317 | inline for (0..native_words) |i| { | 317 | inline for (0..native_words) |i| { |
| 318 | out.repr[i] = block_vec1.repr[i] | block_vec2.repr[i]; | 318 | out.repr[i] = block_vec1.repr[i] | block_vec2.repr[i]; |
lib/std/crypto/aes/armcrypto.zig+4-4| ... | @@ -216,10 +216,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { | ... | @@ -216,10 +216,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { |
| 216 | } | 216 | } |
| 217 | 217 | ||
| 218 | /// XOR the block vector with a byte sequence. | 218 | /// XOR the block vector with a byte sequence. |
| 219 | pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [32]u8 { | 219 | pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8 { |
| 220 | var out: Self = undefined; | 220 | var out: [blocks_count * 16]u8 = undefined; |
| 221 | inline for (0..native_words) |i| { | 221 | inline for (0..native_words) |i| { |
| 222 | out.repr[i] = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); | 222 | out[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); |
| 223 | } | 223 | } |
| 224 | return out; | 224 | return out; |
| 225 | } | 225 | } |
| ... | @@ -279,7 +279,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { | ... | @@ -279,7 +279,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { |
| 279 | } | 279 | } |
| 280 | 280 | ||
| 281 | /// Apply the bitwise OR operation to the content of two block vectors. | 281 | /// Apply the bitwise OR operation to the content of two block vectors. |
| 282 | pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { | 282 | pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { |
| 283 | var out: Self = undefined; | 283 | var out: Self = undefined; |
| 284 | inline for (0..native_words) |i| { | 284 | inline for (0..native_words) |i| { |
| 285 | out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); | 285 | out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); |
lib/std/crypto/aes/soft.zig+4-4| ... | @@ -391,10 +391,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { | ... | @@ -391,10 +391,10 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { |
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | /// XOR the block vector with a byte sequence. | 393 | /// XOR the block vector with a byte sequence. |
| 394 | pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [32]u8 { | 394 | pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8 { |
| 395 | var out: Self = undefined; | 395 | var out: [blocks_count * 16]u8 = undefined; |
| 396 | for (0..native_words) |i| { | 396 | for (0..native_words) |i| { |
| 397 | out.repr[i] = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); | 397 | out[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]); |
| 398 | } | 398 | } |
| 399 | return out; | 399 | return out; |
| 400 | } | 400 | } |
| ... | @@ -454,7 +454,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { | ... | @@ -454,7 +454,7 @@ pub fn BlockVec(comptime blocks_count: comptime_int) type { |
| 454 | } | 454 | } |
| 455 | 455 | ||
| 456 | /// Apply the bitwise OR operation to the content of two block vectors. | 456 | /// Apply the bitwise OR operation to the content of two block vectors. |
| 457 | pub fn orBlocks(block_vec1: Self, block_vec2: Block) Self { | 457 | pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self { |
| 458 | var out: Self = undefined; | 458 | var out: Self = undefined; |
| 459 | for (0..native_words) |i| { | 459 | for (0..native_words) |i| { |
| 460 | out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); | 460 | out.repr[i] = block_vec1.repr[i].orBlocks(block_vec2.repr[i]); |