跳转到内容

std.crypto.aes.soft

标准库Zig 0.17.0官方原文
源码std/crypto/aes/soft.zig

第 1 行

std.crypto.aes.soft.Aes128

命名空间

第 695 行

采用标准密钥扩展的 AES-128。

std.crypto.aes.soft.Aes128.block

别名

第 698 行

别名目标 std.crypto.aes.soft.Block

std.crypto.aes.soft.Aes128.initDec

函数

第 706 行

创建新的解密上下文。

pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128)

参数

key: [key_bits / 8]u8

std.crypto.aes.soft.Aes128.initEnc

函数

第 701 行

创建新的加密上下文。

pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128)

参数

key: [key_bits / 8]u8

std.crypto.aes.soft.Aes128.key_bits

常量

第 696 行

usize

std.crypto.aes.soft.Aes128.rounds

常量

第 697 行

std.crypto.aes.soft.Aes256

命名空间

第 712 行

采用标准密钥编排的 AES-256。

std.crypto.aes.soft.Aes256.block

别名

第 715 行

别名目标 std.crypto.aes.soft.Block

std.crypto.aes.soft.Aes256.initDec

函数

第 723 行

创建新的解密上下文。

pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256)

参数

key: [key_bits / 8]u8

std.crypto.aes.soft.Aes256.initEnc

函数

第 718 行

创建新的加密上下文。

pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256)

参数

key: [key_bits / 8]u8

std.crypto.aes.soft.Aes256.key_bits

常量

第 713 行

usize

std.crypto.aes.soft.Aes256.rounds

常量

第 714 行

std.crypto.aes.soft.AesDecryptCtx

类型函数

第 640 行

使用标准 AES 密钥调度进行解密的上下文。

pub fn AesDecryptCtx(comptime Aes: type) type

参数

Aes: type

字段

key_schedule: KeySchedule(Aes)
packed_keys: PackedKeys(rounds)

std.crypto.aes.soft.AesDecryptCtx.block

常量

第 646 行

std.crypto.aes.soft.AesDecryptCtx.block_length

常量

第 647 行

std.crypto.aes.soft.AesDecryptCtx.decrypt

函数

第 677 行

解密单个数据块。

pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void

参数

ctx: Self
dst: *[16]u8
src: *const [16]u8

std.crypto.aes.soft.AesDecryptCtx.decryptUnprotectedBlock

函数

第 666 行

解密单个块,不提供任何侧信道防护。

fn decryptUnprotectedBlock(ctx: Self, dst: *[16]u8, src: *const [16]u8) void

参数

ctx: Self
dst: *[16]u8
src: *const [16]u8

std.crypto.aes.soft.AesDecryptCtx.decryptWide

函数

第 682 行

解密多个数据块,可能利用并行处理。

pub fn decryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void

参数

ctx: Self
count: usize
dst: *[16 * count]u8
src: *const [16 * count]u8

std.crypto.aes.soft.AesDecryptCtx.init

函数

第 660 行

使用给定密钥创建新的解密上下文。

pub fn init(key: [Aes.key_bits / 8]u8) Self

参数

key: [Aes.key_bits / 8]u8

std.crypto.aes.soft.AesDecryptCtx.initFromEnc

函数

第 652 行

从现有加密上下文创建解密上下文。

pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self

参数

ctx: AesEncryptCtx(Aes)

std.crypto.aes.soft.AesDecryptCtx.Self

别名

第 645 行

别名目标 std.crypto.aes.soft.AesDecryptCtx

std.crypto.aes.soft.AesEncryptCtx

类型函数

第 578 行

使用标准 AES 密钥扩展进行加密的上下文。

pub fn AesEncryptCtx(comptime Aes: type) type

参数

Aes: type

字段

key_schedule: KeySchedule(Aes)
packed_keys: PackedKeys(rounds)

std.crypto.aes.soft.AesEncryptCtx.block

常量

第 584 行

std.crypto.aes.soft.AesEncryptCtx.block_length

常量

第 585 行

std.crypto.aes.soft.AesEncryptCtx.encrypt

函数

第 610 行

加密单个数据块。

pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void

参数

ctx: Self
dst: *[16]u8
src: *const [16]u8

std.crypto.aes.soft.AesEncryptCtx.encryptUnprotectedBlock

函数

第 599 行

加密单个块,不提供任何侧信道防护。

fn encryptUnprotectedBlock(ctx: Self, dst: *[16]u8, src: *const [16]u8) void

参数

ctx: Self
dst: *[16]u8
src: *const [16]u8

std.crypto.aes.soft.AesEncryptCtx.encryptWide

函数

第 620 行

加密多个块,可能利用并行处理。

pub fn encryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void

参数

ctx: Self
count: usize
dst: *[16 * count]u8
src: *const [16 * count]u8

std.crypto.aes.soft.AesEncryptCtx.init

函数

第 590 行

使用给定密钥创建新的加密上下文。

pub fn init(key: [Aes.key_bits / 8]u8) Self

参数

key: [Aes.key_bits / 8]u8

std.crypto.aes.soft.AesEncryptCtx.Self

别名

第 583 行

别名目标 std.crypto.aes.soft.AesEncryptCtx

std.crypto.aes.soft.AesEncryptCtx.xor

函数

第 615 行

对单个块执行加密和异或。

pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void

参数

ctx: Self
dst: *[16]u8
src: *const [16]u8
counter: [16]u8

std.crypto.aes.soft.AesEncryptCtx.xorWide

函数

第 631 行

对多个块执行加密及异或,可能利用并行处理。

pub fn xorWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8, counters: [16 * count]u8) void

参数

ctx: Self
count: usize
dst: *[16 * count]u8
src: *const [16 * count]u8
counters: [16 * count]u8

std.crypto.aes.soft.bitsliced

别名

第 4 行

别名目标 std.crypto.aes.bitsliced

std.crypto.aes.soft.bitslicedOperation

函数

第 551 行

对 count 个分组执行位切片运算

fn bitslicedOperation( comptime rounds: usize, comptime operation: enum { encrypt, decrypt }, keys: *const [rounds + 1]bitsliced.State, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8, ) void

参数

rounds: usize
operation: enum { encrypt, decrypt }
keys: *const [rounds + 1]bitsliced.State
count: usize
dst: *[16 * count]u8
src: *const [16 * count]u8

std.crypto.aes.soft.Block

容器

第 11 行

单个 AES 数据块。

字段

repr: Repr align(16)

数据分组的内部表示。

std.crypto.aes.soft.Block.andBlocks

函数

第 252 行

对两个块的内容应用按位与操作。

pub fn andBlocks(block1: Block, block2: Block) Block

参数

block1: Block
block2: Block

std.crypto.aes.soft.Block.block_length

常量

第 14 行

usize

std.crypto.aes.soft.Block.decrypt

函数

第 146 行

用轮密钥解密一个块。

pub fn decrypt(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.decryptLast

函数

第 216 行

使用最后一轮密钥解密一个块。

pub fn decryptLast(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.decryptUnprotected

函数

第 171 行

使用轮密钥解密一个块,不提供任何侧信道防护

pub fn decryptUnprotected(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.encrypt

函数

第 50 行

使用轮密钥加密一个块。

pub fn encrypt(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.encryptLast

函数

第 120 行

使用最后一轮的轮密钥加密一个分组。

pub fn encryptLast(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.encryptUnprotected

函数

第 75 行

使用轮密钥加密一个数据块,不提供任何侧信道防护。

pub fn encryptUnprotected(block: Block, round_key: Block) Block

参数

block: Block
round_key: Block

std.crypto.aes.soft.Block.fromBytes

函数

第 20 行

将字节序列转换为内部表示。

pub fn fromBytes(bytes: *const [16]u8) Block

参数

bytes: *const [16]u8

std.crypto.aes.soft.Block.invMixColumns

函数

第 272 行

对一个块应用逆 MixColumns 运算。

pub fn invMixColumns(block: Block) Block

参数

block: Block

std.crypto.aes.soft.Block.orBlocks

函数

第 262 行

对两个块的内容执行按位或操作。

pub fn orBlocks(block1: Block, block2: Block) Block

参数

block1: Block
block2: Block

std.crypto.aes.soft.Block.parallel

命名空间

第 292 行

并行处理多个块。

std.crypto.aes.soft.Block.parallel.decryptLastWide

函数

第 347 行

使用同一个末轮密钥并行解密多个数据块。

pub fn decryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block

参数

count: usize
blocks: [count]Block
round_key: Block

std.crypto.aes.soft.Block.parallel.decryptParallel

函数

第 307 行

并行解密多个数据块,各自使用自己的轮密钥。

pub fn decryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block

参数

count: usize
blocks: [count]Block
round_keys: [count]Block

std.crypto.aes.soft.Block.parallel.decryptWide

函数

第 327 行

使用同一轮密钥并行解密多个块。

pub fn decryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block

参数

count: usize
blocks: [count]Block
round_key: Block

std.crypto.aes.soft.Block.parallel.encryptLastWide

函数

第 337 行

使用相同的最后一轮密钥并行加密多个块。

pub fn encryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block

参数

count: usize
blocks: [count]Block
round_key: Block

std.crypto.aes.soft.Block.parallel.encryptParallel

函数

第 297 行

并行加密多个块,每个块使用各自的轮密钥。

pub fn encryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block

参数

count: usize
blocks: [count]Block
round_keys: [count]Block

std.crypto.aes.soft.Block.parallel.encryptWide

函数

第 317 行

使用同一个轮密钥并行加密多个数据块。

pub fn encryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block

参数

count: usize
blocks: [count]Block
round_key: Block

std.crypto.aes.soft.Block.parallel.optimal_parallel_blocks

常量

第 294 行

对所选实现,建议并行执行的 AES 加密/解密次数。

std.crypto.aes.soft.Block.Repr

类型

第 12 行

std.crypto.aes.soft.Block.toBytes

函数

第 29 行

将数据块的内部表示转换为字节序列。

pub fn toBytes(block: Block) [16]u8

参数

block: Block

std.crypto.aes.soft.Block.xorBlocks

函数

第 242 行

对两个数据块的内容应用按位异或操作。

pub fn xorBlocks(block1: Block, block2: Block) Block

参数

block1: Block
block2: Block

std.crypto.aes.soft.Block.xorBytes

函数

第 39 行

将该块与字节序列异或。

pub fn xorBytes(block: Block, bytes: *const [16]u8) [16]u8

参数

block: Block
bytes: *const [16]u8

std.crypto.aes.soft.BlockVec

类型函数

第 360 行

固定大小的 AES 块向量。 所有操作均并行执行,在可用时使用 SIMD 指令。

pub fn BlockVec(comptime blocks_count: comptime_int) type

参数

blocks_count: comptime_int

字段

repr: [native_words]Block

分组向量的内部表示。

std.crypto.aes.soft.BlockVec.andBlocks

函数

第 451 行

对两个块向量的内容执行按位与。

pub fn andBlocks(block_vec1: Self, block_vec2: Self) Self

参数

block_vec1: Self
block_vec2: Self

std.crypto.aes.soft.BlockVec.block_length

常量

第 376 行

分组向量的长度,单位为字节。

usize

std.crypto.aes.soft.BlockVec.decrypt

函数

第 424 行

使用轮密钥向量,对块向量执行逆 AES 操作。

pub fn decrypt(block_vec: Self, inv_round_key_vec: Self) Self

参数

block_vec: Self
inv_round_key_vec: Self

std.crypto.aes.soft.BlockVec.decryptLast

函数

第 433 行

使用末轮密钥向量,对块向量应用逆 AES 操作。

pub fn decryptLast(block_vec: Self, inv_round_key_vec: Self) Self

参数

block_vec: Self
inv_round_key_vec: Self

std.crypto.aes.soft.BlockVec.encrypt

函数

第 406 行

使用轮密钥向量,对块向量应用正向 AES 运算。

pub fn encrypt(block_vec: Self, round_key_vec: Self) Self

参数

block_vec: Self
round_key_vec: Self

std.crypto.aes.soft.BlockVec.encryptLast

函数

第 415 行

使用最后一轮密钥向量,对块向量执行正向 AES 操作。

pub fn encryptLast(block_vec: Self, round_key_vec: Self) Self

参数

block_vec: Self
round_key_vec: Self

std.crypto.aes.soft.BlockVec.fromBytes

函数

第 379 行

将字节序列转换为内部表示。

pub fn fromBytes(bytes: *const [blocks_count * 16]u8) Self

参数

bytes: *const [blocks_count * 16]u8

std.crypto.aes.soft.BlockVec.invMixColumns

函数

第 469 行

对向量中的每个块应用逆 MixColumns 运算。

pub fn invMixColumns(block_vec: Self) Self

参数

block_vec: Self

std.crypto.aes.soft.BlockVec.native_vector_size

常量

第 365 行

目标架构可通过单条指令处理的 AES 块数量。

std.crypto.aes.soft.BlockVec.native_word_size

常量

第 368 行

目标架构可用单条指令处理的 AES 块向量大小,以字节为单位。

std.crypto.aes.soft.BlockVec.native_words

常量

第 370 行

std.crypto.aes.soft.BlockVec.orBlocks

函数

第 460 行

对两个块向量的内容应用按位或操作。

pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self

参数

block_vec1: Self
block_vec2: Self

std.crypto.aes.soft.BlockVec.Self

别名

第 362 行

别名目标 std.crypto.aes.soft.BlockVec

std.crypto.aes.soft.BlockVec.toBytes

函数

第 388 行

将块向量的内部表示转换为字节序列。

pub fn toBytes(block_vec: Self) [blocks_count * 16]u8

参数

block_vec: Self

std.crypto.aes.soft.BlockVec.xorBlocks

函数

第 442 行

对两个块向量的内容应用按位异或操作。

pub fn xorBlocks(block_vec1: Self, block_vec2: Self) Self

参数

block_vec1: Self
block_vec2: Self

std.crypto.aes.soft.BlockVec.xorBytes

函数

第 397 行

将块向量与字节序列异或。

pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8

参数

block_vec: Self
bytes: *const [blocks_count * 16]u8

std.crypto.aes.soft.cache_line_bytes

别名

第 828 行

别名目标 std.atomic.cache_line

std.crypto.aes.soft.generateSbox

函数

第 753 行

fn generateSbox(invert: bool) [256]u8

参数

invert: bool

std.crypto.aes.soft.generateTable

函数

第 787 行

fn generateTable(invert: bool) [4][256]u32

参数

invert: bool

std.crypto.aes.soft.KeySchedule

类型函数

第 479 行

fn KeySchedule(comptime Aes: type) type

参数

Aes: type

字段

round_keys: [rounds + 1]Block

std.crypto.aes.soft.KeySchedule.expandKey

函数

第 491 行

fn expandKey(key: [key_length]u8) Self

参数

key: [key_length]u8

std.crypto.aes.soft.KeySchedule.expandKey.func

函数

第 494 行

fn func(w: u32) u32

参数

w: u32

std.crypto.aes.soft.KeySchedule.invert

函数

第 522 行

反转密钥编排。

pub fn invert(key_schedule: Self) Self

参数

key_schedule: Self

std.crypto.aes.soft.KeySchedule.Self

别名

第 485 行

别名目标 std.crypto.aes.soft.KeySchedule

std.crypto.aes.soft.KeySchedule.words_in_key

常量

第 486 行

std.crypto.aes.soft.math

别名

第 2 行

别名目标 std.math

std.crypto.aes.soft.mem

别名

第 3 行

别名目标 std.mem

std.crypto.aes.soft.mul

函数

第 807 行

fn mul(a: u8, b: u8) u8

参数

a: u8
b: u8

std.crypto.aes.soft.PackedKeys

类型函数

第 546 行

用于位切片实现的已打包正向轮密钥。

fn PackedKeys(comptime rounds: usize) type

参数

rounds: usize

std.crypto.aes.soft.poly

常量

第 731 行

u9

std.crypto.aes.soft.powx

常量

第 734 行

std.crypto.aes.soft.protected

常量

第 8 行

std.crypto.aes.soft.sbox_decrypt

常量

第 748 行

std.crypto.aes.soft.sbox_encrypt

常量

第 746 行

std.crypto.aes.soft.sbox_key_schedule

常量

第 747 行

std.crypto.aes.soft.sbox_lookup

函数

第 830 行

fn sbox_lookup(sbox: *align(64) const [256]u8, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u8

参数

sbox: *align(64) const [256]u8
idx0: u8
idx1: u8
idx2: u8
idx3: u8

std.crypto.aes.soft.side_channels_mitigations

常量

第 6 行

std.crypto.aes.soft.std

别名

第 1 行

别名目标 std

std.crypto.aes.soft.table_decrypt

常量

第 750 行

std.crypto.aes.soft.table_encrypt

常量

第 749 行

std.crypto.aes.soft.table_lookup

函数

第 868 行

fn table_lookup(table: *align(64) const [4][256]u32, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u32

参数

table: *align(64) const [4][256]u32
idx0: u8
idx1: u8
idx2: u8
idx3: u8
源码

std/crypto/aes/soft.zig

const std = @import("../../std.zig");
const math = std.math;
const mem = std.mem;
const bitsliced = @import("bitsliced.zig");

const side_channels_mitigations = std.options.side_channels_mitigations;

const protected = side_channels_mitigations != .none;

/// 单个 AES 数据块。
pub const Block = struct {
    const Repr = [4]u32;

    pub const block_length: usize = 16;

    /// 数据分组的内部表示。
    repr: Repr align(16),

    /// 将字节序列转换为内部表示。
    pub fn fromBytes(bytes: *const [16]u8) Block {
        const s0 = mem.readInt(u32, bytes[0..4], .little);
        const s1 = mem.readInt(u32, bytes[4..8], .little);
        const s2 = mem.readInt(u32, bytes[8..12], .little);
        const s3 = mem.readInt(u32, bytes[12..16], .little);
        return Block{ .repr = Repr{ s0, s1, s2, s3 } };
    }

    /// 将数据块的内部表示转换为字节序列。
    pub fn toBytes(block: Block) [16]u8 {
        var bytes: [16]u8 = undefined;
        mem.writeInt(u32, bytes[0..4], block.repr[0], .little);
        mem.writeInt(u32, bytes[4..8], block.repr[1], .little);
        mem.writeInt(u32, bytes[8..12], block.repr[2], .little);
        mem.writeInt(u32, bytes[12..16], block.repr[3], .little);
        return bytes;
    }

    /// 将该块与字节序列异或。
    pub fn xorBytes(block: Block, bytes: *const [16]u8) [16]u8 {
        const block_bytes = block.toBytes();
        var x: [16]u8 = undefined;
        comptime var i: usize = 0;
        inline while (i < 16) : (i += 1) {
            x[i] = block_bytes[i] ^ bytes[i];
        }
        return x;
    }

    /// 使用轮密钥加密一个块。
    pub fn encrypt(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        var x: [4]u32 = undefined;
        x = table_lookup(&table_encrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 24)));
        var t0 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_encrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 24)));
        var t1 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_encrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 24)));
        var t2 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_encrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 24)));
        var t3 = x[0] ^ x[1] ^ x[2] ^ x[3];

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 使用轮密钥加密一个数据块,*不提供任何侧信道防护*。
    pub fn encryptUnprotected(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        var x: [4]u32 = undefined;
        x = .{
            table_encrypt[0][@as(u8, @truncate(s0))],
            table_encrypt[1][@as(u8, @truncate(s1 >> 8))],
            table_encrypt[2][@as(u8, @truncate(s2 >> 16))],
            table_encrypt[3][@as(u8, @truncate(s3 >> 24))],
        };
        var t0 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_encrypt[0][@as(u8, @truncate(s1))],
            table_encrypt[1][@as(u8, @truncate(s2 >> 8))],
            table_encrypt[2][@as(u8, @truncate(s3 >> 16))],
            table_encrypt[3][@as(u8, @truncate(s0 >> 24))],
        };
        var t1 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_encrypt[0][@as(u8, @truncate(s2))],
            table_encrypt[1][@as(u8, @truncate(s3 >> 8))],
            table_encrypt[2][@as(u8, @truncate(s0 >> 16))],
            table_encrypt[3][@as(u8, @truncate(s1 >> 24))],
        };
        var t2 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_encrypt[0][@as(u8, @truncate(s3))],
            table_encrypt[1][@as(u8, @truncate(s0 >> 8))],
            table_encrypt[2][@as(u8, @truncate(s1 >> 16))],
            table_encrypt[3][@as(u8, @truncate(s2 >> 24))],
        };
        var t3 = x[0] ^ x[1] ^ x[2] ^ x[3];

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 使用最后一轮的轮密钥加密一个分组。
    pub fn encryptLast(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        // 最后一轮直接使用 S 盒,并通过异或产生输出。
        var x: [4]u8 = undefined;
        x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 24)));
        var t0 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 24)));
        var t1 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 24)));
        var t2 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 24)));
        var t3 = mem.readInt(u32, &x, .little);

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 用轮密钥解密一个块。
    pub fn decrypt(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        var x: [4]u32 = undefined;
        x = table_lookup(&table_decrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 24)));
        var t0 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_decrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 24)));
        var t1 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_decrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 24)));
        var t2 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = table_lookup(&table_decrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 24)));
        var t3 = x[0] ^ x[1] ^ x[2] ^ x[3];

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 使用轮密钥解密一个块,*不提供任何侧信道防护*
    pub fn decryptUnprotected(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        var x: [4]u32 = undefined;
        x = .{
            table_decrypt[0][@as(u8, @truncate(s0))],
            table_decrypt[1][@as(u8, @truncate(s3 >> 8))],
            table_decrypt[2][@as(u8, @truncate(s2 >> 16))],
            table_decrypt[3][@as(u8, @truncate(s1 >> 24))],
        };
        var t0 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_decrypt[0][@as(u8, @truncate(s1))],
            table_decrypt[1][@as(u8, @truncate(s0 >> 8))],
            table_decrypt[2][@as(u8, @truncate(s3 >> 16))],
            table_decrypt[3][@as(u8, @truncate(s2 >> 24))],
        };
        var t1 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_decrypt[0][@as(u8, @truncate(s2))],
            table_decrypt[1][@as(u8, @truncate(s1 >> 8))],
            table_decrypt[2][@as(u8, @truncate(s0 >> 16))],
            table_decrypt[3][@as(u8, @truncate(s3 >> 24))],
        };
        var t2 = x[0] ^ x[1] ^ x[2] ^ x[3];
        x = .{
            table_decrypt[0][@as(u8, @truncate(s3))],
            table_decrypt[1][@as(u8, @truncate(s2 >> 8))],
            table_decrypt[2][@as(u8, @truncate(s1 >> 16))],
            table_decrypt[3][@as(u8, @truncate(s0 >> 24))],
        };
        var t3 = x[0] ^ x[1] ^ x[2] ^ x[3];

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 使用最后一轮密钥解密一个块。
    pub fn decryptLast(block: Block, round_key: Block) Block {
        const s0 = block.repr[0];
        const s1 = block.repr[1];
        const s2 = block.repr[2];
        const s3 = block.repr[3];

        // 最后一轮直接使用 S 盒,并通过异或产生输出。
        var x: [4]u8 = undefined;
        x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 24)));
        var t0 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 24)));
        var t1 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 24)));
        var t2 = mem.readInt(u32, &x, .little);
        x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 24)));
        var t3 = mem.readInt(u32, &x, .little);

        t0 ^= round_key.repr[0];
        t1 ^= round_key.repr[1];
        t2 ^= round_key.repr[2];
        t3 ^= round_key.repr[3];

        return Block{ .repr = Repr{ t0, t1, t2, t3 } };
    }

    /// 对两个数据块的内容应用按位异或操作。
    pub fn xorBlocks(block1: Block, block2: Block) Block {
        var x: Repr = undefined;
        comptime var i = 0;
        inline while (i < 4) : (i += 1) {
            x[i] = block1.repr[i] ^ block2.repr[i];
        }
        return Block{ .repr = x };
    }

    /// 对两个块的内容应用按位与操作。
    pub fn andBlocks(block1: Block, block2: Block) Block {
        var x: Repr = undefined;
        comptime var i = 0;
        inline while (i < 4) : (i += 1) {
            x[i] = block1.repr[i] & block2.repr[i];
        }
        return Block{ .repr = x };
    }

    /// 对两个块的内容执行按位或操作。
    pub fn orBlocks(block1: Block, block2: Block) Block {
        var x: Repr = undefined;
        comptime var i = 0;
        inline while (i < 4) : (i += 1) {
            x[i] = block1.repr[i] | block2.repr[i];
        }
        return Block{ .repr = x };
    }

    /// 对一个块应用逆 MixColumns 运算。
    pub fn invMixColumns(block: Block) Block {
        var out: Repr = undefined;
        inline for (0..4) |i| {
            const col = block.repr[i];
            const b0: u8 = @truncate(col);
            const b1: u8 = @truncate(col >> 8);
            const b2: u8 = @truncate(col >> 16);
            const b3: u8 = @truncate(col >> 24);

            const r0 = mul(0x0e, b0) ^ mul(0x0b, b1) ^ mul(0x0d, b2) ^ mul(0x09, b3);
            const r1 = mul(0x09, b0) ^ mul(0x0e, b1) ^ mul(0x0b, b2) ^ mul(0x0d, b3);
            const r2 = mul(0x0d, b0) ^ mul(0x09, b1) ^ mul(0x0e, b2) ^ mul(0x0b, b3);
            const r3 = mul(0x0b, b0) ^ mul(0x0d, b1) ^ mul(0x09, b2) ^ mul(0x0e, b3);

            out[i] = @as(u32, r0) | (@as(u32, r1) << 8) | (@as(u32, r2) << 16) | (@as(u32, r3) << 24);
        }
        return Block{ .repr = out };
    }

    /// 并行处理多个块。
    pub const parallel = struct {
        /// 对所选实现,建议并行执行的 AES 加密/解密次数。
        pub const optimal_parallel_blocks = if (protected) bitsliced.width else 1;

        /// 并行加密多个块,每个块使用各自的轮密钥。
        pub fn encryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].encrypt(round_keys[i]);
            }
            return out;
        }

        /// 并行解密多个数据块,各自使用自己的轮密钥。
        pub fn decryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].decrypt(round_keys[i]);
            }
            return out;
        }

        /// 使用同一个轮密钥并行加密多个数据块。
        pub fn encryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].encrypt(round_key);
            }
            return out;
        }

        /// 使用同一轮密钥并行解密多个块。
        pub fn decryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].decrypt(round_key);
            }
            return out;
        }

        /// 使用相同的最后一轮密钥并行加密多个块。
        pub fn encryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].encryptLast(round_key);
            }
            return out;
        }

        /// 使用同一个末轮密钥并行解密多个数据块。
        pub fn decryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block {
            var i = 0;
            var out: [count]Block = undefined;
            while (i < count) : (i += 1) {
                out[i] = blocks[i].decryptLast(round_key);
            }
            return out;
        }
    };
};

/// 固定大小的 AES 块向量。
/// 所有操作均并行执行,在可用时使用 SIMD 指令。
pub fn BlockVec(comptime blocks_count: comptime_int) type {
    return struct {
        const Self = @This();

        /// 目标架构可通过单条指令处理的 AES 块数量。
        pub const native_vector_size = 1;

        /// 目标架构可用单条指令处理的 AES 块向量大小,以字节为单位。
        pub const native_word_size = native_vector_size * 16;

        const native_words = blocks_count;

        /// 分组向量的内部表示。
        repr: [native_words]Block,

        /// 分组向量的长度,单位为字节。
        pub const block_length: usize = blocks_count * 16;

        /// 将字节序列转换为内部表示。
        pub fn fromBytes(bytes: *const [blocks_count * 16]u8) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = Block.fromBytes(bytes[i * native_word_size ..][0..native_word_size]);
            }
            return out;
        }

        /// 将块向量的内部表示转换为字节序列。
        pub fn toBytes(block_vec: Self) [blocks_count * 16]u8 {
            var out: [blocks_count * 16]u8 = undefined;
            for (0..native_words) |i| {
                out[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].toBytes();
            }
            return out;
        }

        /// 将块向量与字节序列异或。
        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[i * native_word_size ..][0..native_word_size].* = block_vec.repr[i].xorBytes(bytes[i * native_word_size ..][0..native_word_size]);
            }
            return out;
        }

        /// 使用轮密钥向量,对块向量应用正向 AES 运算。
        pub fn encrypt(block_vec: Self, round_key_vec: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec.repr[i].encrypt(round_key_vec.repr[i]);
            }
            return out;
        }

        /// 使用最后一轮密钥向量,对块向量执行正向 AES 操作。
        pub fn encryptLast(block_vec: Self, round_key_vec: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec.repr[i].encryptLast(round_key_vec.repr[i]);
            }
            return out;
        }

        /// 使用轮密钥向量,对块向量执行逆 AES 操作。
        pub fn decrypt(block_vec: Self, inv_round_key_vec: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec.repr[i].decrypt(inv_round_key_vec.repr[i]);
            }
            return out;
        }

        /// 使用末轮密钥向量,对块向量应用逆 AES 操作。
        pub fn decryptLast(block_vec: Self, inv_round_key_vec: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec.repr[i].decryptLast(inv_round_key_vec.repr[i]);
            }
            return out;
        }

        /// 对两个块向量的内容应用按位异或操作。
        pub fn xorBlocks(block_vec1: Self, block_vec2: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec1.repr[i].xorBlocks(block_vec2.repr[i]);
            }
            return out;
        }

        /// 对两个块向量的内容执行按位与。
        pub fn andBlocks(block_vec1: Self, block_vec2: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec1.repr[i].andBlocks(block_vec2.repr[i]);
            }
            return out;
        }

        /// 对两个块向量的内容应用按位或操作。
        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]);
            }
            return out;
        }

        /// 对向量中的每个块应用逆 MixColumns 运算。
        pub fn invMixColumns(block_vec: Self) Self {
            var out: Self = undefined;
            for (0..native_words) |i| {
                out.repr[i] = block_vec.repr[i].invMixColumns();
            }
            return out;
        }
    };
}

fn KeySchedule(comptime Aes: type) type {
    std.debug.assert(Aes.rounds == 10 or Aes.rounds == 14);
    const key_length = Aes.key_bits / 8;
    const rounds = Aes.rounds;

    return struct {
        const Self = @This();
        const words_in_key = key_length / 4;

        round_keys: [rounds + 1]Block,

        // 密钥扩展算法。参见 FIPS-197,图 11。
        fn expandKey(key: [key_length]u8) Self {
            const subw = struct {
                // 对 w 的每个字节应用 sbox_encrypt。
                fn func(w: u32) u32 {
                    const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(w)), @as(u8, @truncate(w >> 8)), @as(u8, @truncate(w >> 16)), @as(u8, @truncate(w >> 24)));
                    return mem.readInt(u32, &x, .little);
                }
            }.func;

            var round_keys: [rounds + 1]Block = undefined;
            comptime var i: usize = 0;
            inline while (i < words_in_key) : (i += 1) {
                round_keys[i / 4].repr[i % 4] = mem.readInt(u32, key[4 * i ..][0..4], .big);
            }
            inline while (i < round_keys.len * 4) : (i += 1) {
                var t = round_keys[(i - 1) / 4].repr[(i - 1) % 4];
                if (i % words_in_key == 0) {
                    t = subw(std.math.rotl(u32, t, 8)) ^ (@as(u32, powx[i / words_in_key - 1]) << 24);
                } else if (words_in_key > 6 and i % words_in_key == 4) {
                    t = subw(t);
                }
                round_keys[i / 4].repr[i % 4] = round_keys[(i - words_in_key) / 4].repr[(i - words_in_key) % 4] ^ t;
            }
            i = 0;
            inline while (i < round_keys.len * 4) : (i += 1) {
                round_keys[i / 4].repr[i % 4] = @byteSwap(round_keys[i / 4].repr[i % 4]);
            }
            return Self{ .round_keys = round_keys };
        }

        /// 反转密钥编排。
        pub fn invert(key_schedule: Self) Self {
            const round_keys = &key_schedule.round_keys;
            var inv_round_keys: [rounds + 1]Block = undefined;
            const total_words = 4 * round_keys.len;
            var i: usize = 0;
            while (i < total_words) : (i += 4) {
                const ei = total_words - i - 4;
                comptime var j: usize = 0;
                inline while (j < 4) : (j += 1) {
                    var rk = round_keys[(ei + j) / 4].repr[(ei + j) % 4];
                    if (i > 0 and i + 4 < total_words) {
                        const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(rk >> 24)), @as(u8, @truncate(rk >> 16)), @as(u8, @truncate(rk >> 8)), @as(u8, @truncate(rk)));
                        const y = table_lookup(&table_decrypt, x[3], x[2], x[1], x[0]);
                        rk = y[0] ^ y[1] ^ y[2] ^ y[3];
                    }
                    inv_round_keys[(i + j) / 4].repr[(i + j) % 4] = rk;
                }
            }
            return Self{ .round_keys = inv_round_keys };
        }
    };
}

/// 用于位切片实现的已打包正向轮密钥。
fn PackedKeys(comptime rounds: usize) type {
    return if (protected) [rounds + 1]bitsliced.State else void;
}

/// 对 `count` 个分组执行位切片运算
fn bitslicedOperation(
    comptime rounds: usize,
    comptime operation: enum { encrypt, decrypt },
    keys: *const [rounds + 1]bitsliced.State,
    comptime count: usize,
    dst: *[16 * count]u8,
    src: *const [16 * count]u8,
) void {
    const w = bitsliced.width;
    const chunks = (count + w - 1) / w;
    inline for (0..chunks) |c| {
        const off: usize = c * w;
        const n: usize = @min(w, count - off);
        const lo = off * 16;
        const hi = lo + n * 16;
        var buf: [w * 16]u8 = undefined;
        @memcpy(buf[0 .. n * 16], src[lo..hi]);
        if (operation == .decrypt) {
            bitsliced.Wide.decrypt(rounds, keys, &buf);
        } else {
            bitsliced.Wide.encrypt(rounds, keys, &buf);
        }
        @memcpy(dst[lo..hi], buf[0 .. n * 16]);
    }
}

/// 使用标准 AES 密钥扩展进行加密的上下文。
pub fn AesEncryptCtx(comptime Aes: type) type {
    std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
    const rounds = Aes.rounds;

    return struct {
        const Self = @This();
        pub const block = Aes.block;
        pub const block_length = block.block_length;
        key_schedule: KeySchedule(Aes),
        packed_keys: PackedKeys(rounds),

        /// 使用给定密钥创建新的加密上下文。
        pub fn init(key: [Aes.key_bits / 8]u8) Self {
            const key_schedule = KeySchedule(Aes).expandKey(key);
            return Self{
                .key_schedule = key_schedule,
                .packed_keys = if (protected) bitsliced.Wide.packKeys(key_schedule.round_keys[0..]) else {},
            };
        }

        /// 加密单个块,不提供任何侧信道防护。
        fn encryptUnprotectedBlock(ctx: Self, dst: *[16]u8, src: *const [16]u8) void {
            const round_keys = ctx.key_schedule.round_keys;
            var t = Block.fromBytes(src).xorBlocks(round_keys[0]);
            inline for (1..rounds) |i| {
                t = t.encryptUnprotected(round_keys[i]);
            }
            t = t.encryptLast(round_keys[rounds]);
            dst.* = t.toBytes();
        }

        /// 加密单个数据块。
        pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void {
            ctx.encryptWide(1, dst, src);
        }

        /// 对单个块执行加密和异或。
        pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void {
            ctx.xorWide(1, dst, src, counter);
        }

        /// 加密多个块,可能利用并行处理。
        pub fn encryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void {
            if (protected) {
                bitslicedOperation(rounds, .encrypt, &ctx.packed_keys, count, dst, src);
            } else {
                for (0..count) |i| {
                    ctx.encryptUnprotectedBlock(dst[16 * i .. 16 * i + 16][0..16], src[16 * i .. 16 * i + 16][0..16]);
                }
            }
        }

        /// 对多个块执行加密及异或,可能利用并行处理。
        pub fn xorWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8, counters: [16 * count]u8) void {
            var ks: [16 * count]u8 = undefined;
            ctx.encryptWide(count, &ks, &counters);
            for (dst, ks, src) |*d, k, s| d.* = k ^ s;
        }
    };
}

/// 使用标准 AES 密钥调度进行解密的上下文。
pub fn AesDecryptCtx(comptime Aes: type) type {
    std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
    const rounds = Aes.rounds;

    return struct {
        const Self = @This();
        pub const block = Aes.block;
        pub const block_length = block.block_length;
        key_schedule: KeySchedule(Aes),
        packed_keys: PackedKeys(rounds),

        /// 从现有加密上下文创建解密上下文。
        pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self {
            return Self{
                .key_schedule = ctx.key_schedule.invert(),
                .packed_keys = ctx.packed_keys,
            };
        }

        /// 使用给定密钥创建新的解密上下文。
        pub fn init(key: [Aes.key_bits / 8]u8) Self {
            const enc_ctx = AesEncryptCtx(Aes).init(key);
            return initFromEnc(enc_ctx);
        }

        /// 解密单个块,不提供任何侧信道防护。
        fn decryptUnprotectedBlock(ctx: Self, dst: *[16]u8, src: *const [16]u8) void {
            const inv_round_keys = ctx.key_schedule.round_keys;
            var t = Block.fromBytes(src).xorBlocks(inv_round_keys[0]);
            inline for (1..rounds) |i| {
                t = t.decryptUnprotected(inv_round_keys[i]);
            }
            t = t.decryptLast(inv_round_keys[rounds]);
            dst.* = t.toBytes();
        }

        /// 解密单个数据块。
        pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void {
            ctx.decryptWide(1, dst, src);
        }

        /// 解密多个数据块,可能利用并行处理。
        pub fn decryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void {
            if (protected) {
                bitslicedOperation(rounds, .decrypt, &ctx.packed_keys, count, dst, src);
            } else {
                for (0..count) |i| {
                    ctx.decryptUnprotectedBlock(dst[16 * i .. 16 * i + 16][0..16], src[16 * i .. 16 * i + 16][0..16]);
                }
            }
        }
    };
}

/// 采用标准密钥扩展的 AES-128。
pub const Aes128 = struct {
    pub const key_bits: usize = 128;
    pub const rounds = ((key_bits - 64) / 32 + 8);
    pub const block = Block;

    /// 创建新的加密上下文。
    pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128) {
        return AesEncryptCtx(Aes128).init(key);
    }

    /// 创建新的解密上下文。
    pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128) {
        return AesDecryptCtx(Aes128).init(key);
    }
};

/// 采用标准密钥编排的 AES-256。
pub const Aes256 = struct {
    pub const key_bits: usize = 256;
    pub const rounds = ((key_bits - 64) / 32 + 8);
    pub const block = Block;

    /// 创建新的加密上下文。
    pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256) {
        return AesEncryptCtx(Aes256).init(key);
    }

    /// 创建新的解密上下文。
    pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256) {
        return AesDecryptCtx(Aes256).init(key);
    }
};

// 常量

// Rijndael 的不可约多项式。
const poly: u9 = 1 << 8 | 1 << 4 | 1 << 3 | 1 << 1 | 1 << 0; // x⁸ + x⁴ + x³ + x + 1

// GF(2) 中 x 的幂对 poly 取模。
const powx = init: {
    var array: [16]u8 = undefined;

    var value = 1;
    for (&array) |*power| {
        power.* = value;
        value = mul(value, 2);
    }

    break :init array;
};

const sbox_encrypt align(64) = generateSbox(false); // 用于加密的 S 盒
const sbox_key_schedule align(64) = generateSbox(false); // 仅用于密钥扩展的 S 盒,使其使用的一级缓存条目与加密所用的 S 盒不同。
const sbox_decrypt align(64) = generateSbox(true); // 解密用 S 盒
const table_encrypt align(64) = generateTable(false); // 用于加密的 4 字节查找表
const table_decrypt align(64) = generateTable(true); // 用于解密的 4 字节查找表

// 生成 S 盒代换值。
fn generateSbox(invert: bool) [256]u8 {
    @setEvalBranchQuota(10000);

    var sbox: [256]u8 = undefined;

    var p: u8 = 1;
    var q: u8 = 1;
    for (sbox) |_| {
        p = mul(p, 3);
        q = mul(q, 0xf6); // 除以 3

        var value: u8 = q ^ 0x63;
        value ^= math.rotl(u8, q, 1);
        value ^= math.rotl(u8, q, 2);
        value ^= math.rotl(u8, q, 3);
        value ^= math.rotl(u8, q, 4);

        if (invert) {
            sbox[value] = p;
        } else {
            sbox[p] = value;
        }
    }

    if (invert) {
        sbox[0x63] = 0x00;
    } else {
        sbox[0x00] = 0x63;
    }

    return sbox;
}

// 生成查找表。
fn generateTable(invert: bool) [4][256]u32 {
    @setEvalBranchQuota(50000);

    var table: [4][256]u32 = undefined;

    for (generateSbox(invert), 0..) |value, index| {
        table[0][index] = math.shl(u32, mul(value, if (invert) 0xb else 0x3), 24);
        table[0][index] |= math.shl(u32, mul(value, if (invert) 0xd else 0x1), 16);
        table[0][index] |= math.shl(u32, mul(value, if (invert) 0x9 else 0x1), 8);
        table[0][index] |= mul(value, if (invert) 0xe else 0x2);

        table[1][index] = math.rotl(u32, table[0][index], 8);
        table[2][index] = math.rotl(u32, table[0][index], 16);
        table[3][index] = math.rotl(u32, table[0][index], 24);
    }

    return table;
}

// 将 a 和 b 作为 GF(2) 多项式相乘,并模 poly 约减。
fn mul(a: u8, b: u8) u8 {
    @setEvalBranchQuota(30000);

    var i: u8 = a;
    var j: u9 = b;
    var s: u9 = 0;

    while (i > 0) : (i >>= 1) {
        if (i & 1 != 0) {
            s ^= j;
        }

        j *= 2;
        if (j & 0x100 != 0) {
            j ^= poly;
        }
    }

    return @as(u8, @truncate(s));
}

const cache_line_bytes = std.atomic.cache_line;

fn sbox_lookup(sbox: *align(64) const [256]u8, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u8 {
    if (side_channels_mitigations == .none) {
        return [4]u8{
            sbox[idx0],
            sbox[idx1],
            sbox[idx2],
            sbox[idx3],
        };
    } else {
        const stride = switch (side_channels_mitigations) {
            .none => unreachable,
            .basic => sbox.len / 4,
            .medium => @min(sbox.len, 2 * cache_line_bytes),
            .full => @min(sbox.len, cache_line_bytes),
        };
        const of0 = idx0 % stride;
        const of1 = idx1 % stride;
        const of2 = idx2 % stride;
        const of3 = idx3 % stride;
        var t: [4][sbox.len / stride]u8 align(64) = undefined;
        var i: usize = 0;
        while (i < t[0].len) : (i += 1) {
            const tx = sbox[i * stride ..];
            t[0][i] = tx[of0];
            t[1][i] = tx[of1];
            t[2][i] = tx[of2];
            t[3][i] = tx[of3];
        }
        std.mem.doNotOptimizeAway(t);
        return [4]u8{
            t[0][idx0 / stride],
            t[1][idx1 / stride],
            t[2][idx2 / stride],
            t[3][idx3 / stride],
        };
    }
}

fn table_lookup(table: *align(64) const [4][256]u32, idx0: u8, idx1: u8, idx2: u8, idx3: u8) [4]u32 {
    if (side_channels_mitigations == .none) {
        return [4]u32{
            table[0][idx0],
            table[1][idx1],
            table[2][idx2],
            table[3][idx3],
        };
    } else {
        const table_len: usize = 256;
        const stride = switch (side_channels_mitigations) {
            .none => unreachable,
            .basic => table_len / 4,
            .medium => @max(1, @min(table_len, 2 * cache_line_bytes / 4)),
            .full => @max(1, @min(table_len, cache_line_bytes / 4)),
        };
        const of0 = idx0 % stride;
        const of1 = idx1 % stride;
        const of2 = idx2 % stride;
        const of3 = idx3 % stride;
        var t: [4][table_len / stride]u32 align(64) = undefined;
        var i: usize = 0;
        while (i < t[0].len) : (i += 1) {
            const tx = table[0][i * stride ..];
            t[0][i] = tx[of0];
            t[1][i] = tx[of1];
            t[2][i] = tx[of2];
            t[3][i] = tx[of3];
        }
        std.mem.doNotOptimizeAway(t);
        return [4]u32{
            t[0][idx0 / stride],
            math.rotl(u32, (&t[1])[idx1 / stride], 8),
            math.rotl(u32, (&t[2])[idx2 / stride], 16),
            math.rotl(u32, (&t[3])[idx3 / stride], 24),
        };
    }
}