std.crypto.aes.aesni.Aes128
命名空间
采用标准密钥扩展的 AES-128。
std.crypto.aes.aesni.Aes128.block
别名
std.crypto.aes.aesni.Aes128.initDec
函数
创建新的解密上下文。
pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128)
参数
key: [key_bits / 8]u8
std.crypto.aes.aesni.Aes128.initEnc
函数
创建新的加密上下文。
pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128)
参数
key: [key_bits / 8]u8
std.crypto.aes.aesni.Aes128.key_bits
常量
usize
std.crypto.aes.aesni.Aes128.rounds
常量
std.crypto.aes.aesni.Aes256
命名空间
采用标准密钥编排的 AES-256。
std.crypto.aes.aesni.Aes256.block
别名
std.crypto.aes.aesni.Aes256.initDec
函数
创建新的解密上下文。
pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256)
参数
key: [key_bits / 8]u8
std.crypto.aes.aesni.Aes256.initEnc
函数
创建新的加密上下文。
pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256)
参数
key: [key_bits / 8]u8
std.crypto.aes.aesni.Aes256.key_bits
常量
usize
std.crypto.aes.aesni.Aes256.rounds
常量
std.crypto.aes.aesni.AesDecryptCtx
类型函数
使用标准 AES 密钥调度进行解密的上下文。
pub fn AesDecryptCtx(comptime Aes: type) type
参数
Aes: type
字段
key_schedule: KeySchedule(Aes)
std.crypto.aes.aesni.AesDecryptCtx.block
常量
std.crypto.aes.aesni.AesDecryptCtx.block_length
常量
std.crypto.aes.aesni.AesDecryptCtx.decrypt
函数
解密单个数据块。
pub fn decrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void
参数
std.crypto.aes.aesni.AesDecryptCtx.decryptWide
函数
解密多个数据块,可能利用并行处理。
pub fn decryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void
参数
std.crypto.aes.aesni.AesDecryptCtx.init
函数
使用给定密钥创建新的解密上下文。
pub fn init(key: [Aes.key_bits / 8]u8) Self
参数
key: [Aes.key_bits / 8]u8
std.crypto.aes.aesni.AesDecryptCtx.initFromEnc
函数
从现有加密上下文创建解密上下文。
pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self
参数
ctx: AesEncryptCtx(Aes)
std.crypto.aes.aesni.AesDecryptCtx.Self
别名
std.crypto.aes.aesni.AesEncryptCtx
类型函数
使用标准 AES 密钥扩展进行加密的上下文。
pub fn AesEncryptCtx(comptime Aes: type) type
参数
Aes: type
字段
key_schedule: KeySchedule(Aes)
std.crypto.aes.aesni.AesEncryptCtx.block
常量
std.crypto.aes.aesni.AesEncryptCtx.block_length
常量
std.crypto.aes.aesni.AesEncryptCtx.encrypt
函数
加密单个数据块。
pub fn encrypt(ctx: Self, dst: *[16]u8, src: *const [16]u8) void
参数
std.crypto.aes.aesni.AesEncryptCtx.encryptWide
函数
加密多个块,可能利用并行处理。
pub fn encryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void
参数
std.crypto.aes.aesni.AesEncryptCtx.init
函数
使用给定密钥创建新的加密上下文。
pub fn init(key: [Aes.key_bits / 8]u8) Self
参数
key: [Aes.key_bits / 8]u8
std.crypto.aes.aesni.AesEncryptCtx.Self
别名
std.crypto.aes.aesni.AesEncryptCtx.xor
函数
对单个块执行加密和异或。
pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void
参数
std.crypto.aes.aesni.AesEncryptCtx.xorWide
函数
对多个块执行加密及异或,可能利用并行处理。
pub fn xorWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8, counters: [16 * count]u8) void
参数
std.crypto.aes.aesni.Block
容器
单个 AES 数据块。
字段
repr: Repr数据分组的内部表示。
std.crypto.aes.aesni.Block.andBlocks
函数
对两个块的内容应用按位与操作。
pub fn andBlocks(block1: Block, block2: Block) Block
参数
std.crypto.aes.aesni.Block.block_length
常量
AES 分组的长度,单位为字节。
usize
std.crypto.aes.aesni.Block.decrypt
函数
用轮密钥解密一个块。
pub fn decrypt(block: Block, inv_round_key: Block) Block
参数
std.crypto.aes.aesni.Block.decryptLast
函数
使用最后一轮密钥解密一个块。
pub fn decryptLast(block: Block, inv_round_key: Block) Block
参数
std.crypto.aes.aesni.Block.encrypt
函数
使用轮密钥加密一个块。
pub fn encrypt(block: Block, round_key: Block) Block
参数
std.crypto.aes.aesni.Block.encryptLast
函数
使用最后一轮的轮密钥加密一个分组。
pub fn encryptLast(block: Block, round_key: Block) Block
参数
std.crypto.aes.aesni.Block.fromBytes
函数
将字节序列转换为内部表示。
pub fn fromBytes(bytes: *const [16]u8) Block
参数
bytes: *const [16]u8
std.crypto.aes.aesni.Block.invMixColumns
函数
对一个块应用逆 MixColumns 运算。
pub fn invMixColumns(block: Block) Block
参数
block: Block
std.crypto.aes.aesni.Block.orBlocks
函数
对两个块的内容执行按位或操作。
pub fn orBlocks(block1: Block, block2: Block) Block
参数
std.crypto.aes.aesni.Block.parallel
命名空间
并行处理多个块。
std.crypto.aes.aesni.Block.parallel.cpu
别名
别名目标 std.Target.x86.cpu
std.crypto.aes.aesni.Block.parallel.decryptLastWide
函数
使用同一个末轮密钥并行解密多个数据块。
pub fn decryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.decryptParallel
函数
并行解密多个数据块,各自使用自己的轮密钥。
pub fn decryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.decryptWide
函数
使用同一轮密钥并行解密多个块。
pub fn decryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.encryptLastWide
函数
使用相同的最后一轮密钥并行加密多个块。
pub fn encryptLastWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.encryptParallel
函数
并行加密多个块,每个块使用各自的轮密钥。
pub fn encryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.encryptWide
函数
使用同一个轮密钥并行加密多个数据块。
pub fn encryptWide(comptime count: usize, blocks: [count]Block, round_key: Block) [count]Block
参数
std.crypto.aes.aesni.Block.parallel.optimal_parallel_blocks
常量
对所选实现,建议并行执行的 AES 加密/解密次数。
std.crypto.aes.aesni.Block.Repr
常量
std.crypto.aes.aesni.Block.toBytes
函数
将数据块的内部表示转换为字节序列。
pub fn toBytes(block: Block) [16]u8
参数
block: Block
std.crypto.aes.aesni.Block.xorBlocks
函数
对两个数据块的内容应用按位异或操作。
pub fn xorBlocks(block1: Block, block2: Block) Block
参数
std.crypto.aes.aesni.Block.xorBytes
函数
将该块与字节序列异或。
pub fn xorBytes(block: Block, bytes: *const [16]u8) [16]u8
参数
block: Block
bytes: *const [16]u8
std.crypto.aes.aesni.BlockVec
类型函数
固定大小的 AES 块向量。 所有操作均并行执行,在可用时使用 SIMD 指令。
pub fn BlockVec(comptime blocks_count: comptime_int) type
参数
blocks_count: comptime_int
字段
repr: [native_words]Repr分组向量的内部表示。
std.crypto.aes.aesni.BlockVec.andBlocks
函数
对两个块向量的内容执行按位与。
pub fn andBlocks(block_vec1: Self, block_vec2: Self) Self
参数
std.crypto.aes.aesni.BlockVec.block_length
常量
分组向量的长度,单位为字节。
usize
std.crypto.aes.aesni.BlockVec.decrypt
函数
使用轮密钥向量,对块向量执行逆 AES 操作。
pub fn decrypt(block_vec: Self, inv_round_key_vec: Self) Self
参数
std.crypto.aes.aesni.BlockVec.decryptLast
函数
使用末轮密钥向量,对块向量应用逆 AES 操作。
pub fn decryptLast(block_vec: Self, inv_round_key_vec: Self) Self
参数
std.crypto.aes.aesni.BlockVec.encrypt
函数
使用轮密钥向量,对块向量应用正向 AES 运算。
pub fn encrypt(block_vec: Self, round_key_vec: Self) Self
参数
std.crypto.aes.aesni.BlockVec.encryptLast
函数
使用最后一轮密钥向量,对块向量执行正向 AES 操作。
pub fn encryptLast(block_vec: Self, round_key_vec: Self) Self
参数
std.crypto.aes.aesni.BlockVec.fromBytes
函数
将字节序列转换为内部表示。
pub fn fromBytes(bytes: *const [blocks_count * 16]u8) Self
参数
bytes: *const [blocks_count * 16]u8
std.crypto.aes.aesni.BlockVec.invMixColumns
函数
对向量中的每个块应用逆 MixColumns 运算。
pub fn invMixColumns(block_vec: Self) Self
参数
block_vec: Self
std.crypto.aes.aesni.BlockVec.native_vector_size
常量
目标架构可通过单条指令处理的 AES 块数量。
std.crypto.aes.aesni.BlockVec.native_word_size
常量
目标架构可用单条指令处理的 AES 块向量大小,以字节为单位。
std.crypto.aes.aesni.BlockVec.native_words
常量
std.crypto.aes.aesni.BlockVec.orBlocks
函数
对两个块向量的内容应用按位或操作。
pub fn orBlocks(block_vec1: Self, block_vec2: Self) Self
参数
std.crypto.aes.aesni.BlockVec.Repr
常量
std.crypto.aes.aesni.BlockVec.Self
别名
std.crypto.aes.aesni.BlockVec.toBytes
函数
将块向量的内部表示转换为字节序列。
pub fn toBytes(block_vec: Self) [blocks_count * 16]u8
参数
block_vec: Self
std.crypto.aes.aesni.BlockVec.xorBlocks
函数
对两个块向量的内容应用按位异或操作。
pub fn xorBlocks(block_vec1: Self, block_vec2: Self) Self
参数
std.crypto.aes.aesni.BlockVec.xorBytes
函数
将块向量与字节序列异或。
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.aesni.builtin
常量
std.crypto.aes.aesni.debug
别名
别名目标 std.debug
std.crypto.aes.aesni.has_avx512f
常量
std.crypto.aes.aesni.has_vaes
常量
std.crypto.aes.aesni.KeySchedule
类型函数
fn KeySchedule(comptime Aes: type) type
参数
Aes: type
字段
round_keys: [rounds + 1]Block
std.crypto.aes.aesni.KeySchedule.drc
函数
fn drc(comptime second: bool, comptime rc: u8, t: Repr, tx: Repr) Repr
参数
std.crypto.aes.aesni.KeySchedule.expand128
函数
fn expand128(t1: *Block) Self
参数
t1: *Block
std.crypto.aes.aesni.KeySchedule.expand256
函数
fn expand256(t1: *Block, t2: *Block) Self
参数
std.crypto.aes.aesni.KeySchedule.invert
函数
反转密钥编排。
pub fn invert(key_schedule: Self) Self
参数
key_schedule: Self
std.crypto.aes.aesni.KeySchedule.Repr
常量
std.crypto.aes.aesni.KeySchedule.Self
别名
std.crypto.aes.aesni.mem
别名
别名目标 std.mem
std.crypto.aes.aesni.std
别名
别名目标 std
源码
std/crypto/aes/aesni.zig
const std = @import("../../std.zig");
const builtin = @import("builtin");
const mem = std.mem;
const debug = std.debug;
const has_vaes = builtin.cpu.arch == .x86_64 and builtin.cpu.has(.x86, .vaes);
const has_avx512f = builtin.cpu.arch == .x86_64 and builtin.zig_backend != .stage2_x86_64 and builtin.cpu.has(.x86, .avx512f);
/// 单个 AES 数据块。
pub const Block = struct {
const Repr = @Vector(2, u64);
/// AES 分组的长度,单位为字节。
pub const block_length: usize = 16;
/// 数据分组的内部表示。
repr: Repr,
/// 将字节序列转换为内部表示。
pub fn fromBytes(bytes: *const [16]u8) Block {
const repr = mem.bytesToValue(Repr, bytes);
return Block{ .repr = repr };
}
/// 将数据块的内部表示转换为字节序列。
pub fn toBytes(block: Block) [16]u8 {
return mem.toBytes(block.repr);
}
/// 将该块与字节序列异或。
pub fn xorBytes(block: Block, bytes: *const [16]u8) [16]u8 {
const x = block.repr ^ fromBytes(bytes).repr;
return mem.toBytes(x);
}
/// 使用轮密钥加密一个块。
pub fn encrypt(block: Block, round_key: Block) Block {
return Block{
.repr = asm (
\\ vaesenc %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block.repr),
[rk] "x" (round_key.repr),
),
};
}
/// 使用最后一轮的轮密钥加密一个分组。
pub fn encryptLast(block: Block, round_key: Block) Block {
return Block{
.repr = asm (
\\ vaesenclast %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block.repr),
[rk] "x" (round_key.repr),
),
};
}
/// 用轮密钥解密一个块。
pub fn decrypt(block: Block, inv_round_key: Block) Block {
return Block{
.repr = asm (
\\ vaesdec %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block.repr),
[rk] "x" (inv_round_key.repr),
),
};
}
/// 使用最后一轮密钥解密一个块。
pub fn decryptLast(block: Block, inv_round_key: Block) Block {
return Block{
.repr = asm (
\\ vaesdeclast %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block.repr),
[rk] "x" (inv_round_key.repr),
),
};
}
/// 对两个数据块的内容应用按位异或操作。
pub fn xorBlocks(block1: Block, block2: Block) Block {
return Block{ .repr = block1.repr ^ block2.repr };
}
/// 对两个块的内容应用按位与操作。
pub fn andBlocks(block1: Block, block2: Block) Block {
return Block{ .repr = block1.repr & block2.repr };
}
/// 对两个块的内容执行按位或操作。
pub fn orBlocks(block1: Block, block2: Block) Block {
return Block{ .repr = block1.repr | block2.repr };
}
/// 对一个块应用逆 MixColumns 运算。
pub fn invMixColumns(block: Block) Block {
return Block{
.repr = asm (
\\ vaesimc %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block.repr),
),
};
}
/// 并行处理多个块。
pub const parallel = struct {
const cpu = std.Target.x86.cpu;
/// 对所选实现,建议并行执行的 AES 加密/解密次数。
pub const optimal_parallel_blocks = switch (builtin.cpu.model) {
&cpu.westmere, &cpu.goldmont => 3,
&cpu.cannonlake, &cpu.skylake, &cpu.skylake_avx512, &cpu.tremont, &cpu.goldmont_plus, &cpu.cascadelake => 4,
&cpu.icelake_client, &cpu.icelake_server, &cpu.tigerlake, &cpu.rocketlake, &cpu.alderlake => 6,
&cpu.haswell, &cpu.broadwell => 7,
&cpu.sandybridge, &cpu.ivybridge => 8,
&cpu.znver1, &cpu.znver2, &cpu.znver3, &cpu.znver4 => 8,
else => 8,
};
/// 并行加密多个块,每个块使用各自的轮密钥。
pub fn encryptParallel(comptime count: usize, blocks: [count]Block, round_keys: [count]Block) [count]Block {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 {
comptime var i = 0;
var out: [count]Block = undefined;
inline 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 = w: {
if (has_avx512f and blocks_count % 4 == 0) break :w 4;
if (has_vaes and blocks_count % 2 == 0) break :w 2;
break :w 1;
};
/// 目标架构可用单条指令处理的 AES 块向量大小,以字节为单位。
pub const native_word_size = native_vector_size * 16;
const native_words = blocks_count / native_vector_size;
const Repr = @Vector(native_vector_size * 2, u64);
/// 分组向量的内部表示。
repr: [native_words]Repr,
/// 分组向量的长度,单位为字节。
pub const block_length: usize = blocks_count * 16;
/// 将字节序列转换为内部表示。
pub fn fromBytes(bytes: *const [blocks_count * 16]u8) Self {
var out: Self = undefined;
inline for (0..native_words) |i| {
out.repr[i] = mem.bytesToValue(Repr, 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;
inline for (0..native_words) |i| {
out[i * native_word_size ..][0..native_word_size].* = mem.toBytes(block_vec.repr[i]);
}
return out;
}
/// 将块向量与字节序列异或。
pub fn xorBytes(block_vec: Self, bytes: *const [blocks_count * 16]u8) [blocks_count * 16]u8 {
var x: Self = undefined;
inline for (0..native_words) |i| {
x.repr[i] = block_vec.repr[i] ^ mem.bytesToValue(Repr, bytes[i * native_word_size ..][0..native_word_size]);
}
return x.toBytes();
}
/// 使用轮密钥向量,对块向量应用正向 AES 运算。
pub fn encrypt(block_vec: Self, round_key_vec: Self) Self {
var out: Self = undefined;
inline for (0..native_words) |i| {
out.repr[i] = asm (
\\ vaesenc %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block_vec.repr[i]),
[rk] "x" (round_key_vec.repr[i]),
);
}
return out;
}
/// 使用最后一轮密钥向量,对块向量执行正向 AES 操作。
pub fn encryptLast(block_vec: Self, round_key_vec: Self) Self {
var out: Self = undefined;
inline for (0..native_words) |i| {
out.repr[i] = asm (
\\ vaesenclast %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block_vec.repr[i]),
[rk] "x" (round_key_vec.repr[i]),
);
}
return out;
}
/// 使用轮密钥向量,对块向量执行逆 AES 操作。
pub fn decrypt(block_vec: Self, inv_round_key_vec: Self) Self {
var out: Self = undefined;
inline for (0..native_words) |i| {
out.repr[i] = asm (
\\ vaesdec %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block_vec.repr[i]),
[rk] "x" (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;
inline for (0..native_words) |i| {
out.repr[i] = asm (
\\ vaesdeclast %[rk], %[in], %[out]
: [out] "=x" (-> Repr),
: [in] "x" (block_vec.repr[i]),
[rk] "x" (inv_round_key_vec.repr[i]),
);
}
return out;
}
/// 对两个块向量的内容应用按位异或操作。
pub fn xorBlocks(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];
}
return out;
}
/// 对两个块向量的内容执行按位与。
pub fn andBlocks(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];
}
return out;
}
/// 对两个块向量的内容应用按位或操作。
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];
}
return out;
}
/// 对向量中的每个块应用逆 MixColumns 运算。
pub fn invMixColumns(block_vec: Self) Self {
var out_bytes: [blocks_count * 16]u8 = undefined;
const in_bytes = block_vec.toBytes();
inline for (0..blocks_count) |i| {
const block = Block.fromBytes(in_bytes[i * 16 ..][0..16]);
out_bytes[i * 16 ..][0..16].* = block.invMixColumns().toBytes();
}
return fromBytes(&out_bytes);
}
};
}
fn KeySchedule(comptime Aes: type) type {
std.debug.assert(Aes.rounds == 10 or Aes.rounds == 14);
const rounds = Aes.rounds;
return struct {
const Self = @This();
const Repr = Aes.block.Repr;
round_keys: [rounds + 1]Block,
fn drc(comptime second: bool, comptime rc: u8, t: Repr, tx: Repr) Repr {
var s: Repr = undefined;
var ts: Repr = undefined;
return asm (
\\ vaeskeygenassist %[rc], %[t], %[s]
\\ vpslldq $4, %[tx], %[ts]
\\ vpxor %[ts], %[tx], %[r]
\\ vpslldq $8, %[r], %[ts]
\\ vpxor %[ts], %[r], %[r]
\\ vpshufd %[mask], %[s], %[ts]
\\ vpxor %[ts], %[r], %[r]
: [r] "=&x" (-> Repr),
[s] "=&x" (s),
[ts] "=&x" (ts),
: [rc] "n" (rc),
[t] "x" (t),
[tx] "x" (tx),
[mask] "n" (@as(u8, if (second) 0xaa else 0xff)),
);
}
fn expand128(t1: *Block) Self {
var round_keys: [11]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32, 64, 128, 27, 54 };
inline for (rcs, 0..) |rc, round| {
round_keys[round] = t1.*;
t1.repr = drc(false, rc, t1.repr, t1.repr);
}
round_keys[rcs.len] = t1.*;
return Self{ .round_keys = round_keys };
}
fn expand256(t1: *Block, t2: *Block) Self {
var round_keys: [15]Block = undefined;
const rcs = [_]u8{ 1, 2, 4, 8, 16, 32 };
round_keys[0] = t1.*;
inline for (rcs, 0..) |rc, round| {
round_keys[round * 2 + 1] = t2.*;
t1.repr = drc(false, rc, t2.repr, t1.repr);
round_keys[round * 2 + 2] = t1.*;
t2.repr = drc(true, rc, t1.repr, t2.repr);
}
round_keys[rcs.len * 2 + 1] = t2.*;
t1.repr = drc(false, 64, t2.repr, t1.repr);
round_keys[rcs.len * 2 + 2] = t1.*;
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;
inv_round_keys[0] = round_keys[rounds];
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
inv_round_keys[i] = Block{
.repr = asm (
\\ vaesimc %[rk], %[inv_rk]
: [inv_rk] "=x" (-> Repr),
: [rk] "x" (round_keys[rounds - i].repr),
),
};
}
inv_round_keys[rounds] = round_keys[0];
return Self{ .round_keys = inv_round_keys };
}
};
}
/// 使用标准 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),
/// 使用给定密钥创建新的加密上下文。
pub fn init(key: [Aes.key_bits / 8]u8) Self {
var t1 = Block.fromBytes(key[0..16]);
const key_schedule = if (Aes.key_bits == 128) ks: {
break :ks KeySchedule(Aes).expand128(&t1);
} else ks: {
var t2 = Block.fromBytes(key[16..32]);
break :ks KeySchedule(Aes).expand256(&t1, &t2);
};
return Self{
.key_schedule = key_schedule,
};
}
/// 加密单个数据块。
pub fn encrypt(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]);
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
t = t.encrypt(round_keys[i]);
}
t = t.encryptLast(round_keys[rounds]);
dst.* = t.toBytes();
}
/// 对单个块执行加密和异或。
pub fn xor(ctx: Self, dst: *[16]u8, src: *const [16]u8, counter: [16]u8) void {
const round_keys = ctx.key_schedule.round_keys;
var t = Block.fromBytes(&counter).xorBlocks(round_keys[0]);
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
t = t.encrypt(round_keys[i]);
}
t = t.encryptLast(round_keys[rounds]);
dst.* = t.xorBytes(src);
}
/// 加密多个块,可能利用并行处理。
pub fn encryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void {
const round_keys = ctx.key_schedule.round_keys;
var ts: [count]Block = undefined;
comptime var j = 0;
inline while (j < count) : (j += 1) {
ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xorBlocks(round_keys[0]);
}
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
ts = Block.parallel.encryptWide(count, ts, round_keys[i]);
}
ts = Block.parallel.encryptLastWide(count, ts, round_keys[i]);
j = 0;
inline while (j < count) : (j += 1) {
dst[16 * j .. 16 * j + 16].* = ts[j].toBytes();
}
}
/// 对多个块执行加密及异或,可能利用并行处理。
pub fn xorWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8, counters: [16 * count]u8) void {
const round_keys = ctx.key_schedule.round_keys;
var ts: [count]Block = undefined;
comptime var j = 0;
inline while (j < count) : (j += 1) {
ts[j] = Block.fromBytes(counters[j * 16 .. j * 16 + 16][0..16]).xorBlocks(round_keys[0]);
}
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
ts = Block.parallel.encryptWide(count, ts, round_keys[i]);
}
ts = Block.parallel.encryptLastWide(count, ts, round_keys[i]);
j = 0;
inline while (j < count) : (j += 1) {
dst[16 * j .. 16 * j + 16].* = ts[j].xorBytes(src[16 * j .. 16 * j + 16]);
}
}
};
}
/// 使用标准 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),
/// 从现有加密上下文创建解密上下文。
pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self {
return Self{
.key_schedule = ctx.key_schedule.invert(),
};
}
/// 使用给定密钥创建新的解密上下文。
pub fn init(key: [Aes.key_bits / 8]u8) Self {
const enc_ctx = AesEncryptCtx(Aes).init(key);
return initFromEnc(enc_ctx);
}
/// 解密单个数据块。
pub fn decrypt(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]);
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
t = t.decrypt(inv_round_keys[i]);
}
t = t.decryptLast(inv_round_keys[rounds]);
dst.* = t.toBytes();
}
/// 解密多个数据块,可能利用并行处理。
pub fn decryptWide(ctx: Self, comptime count: usize, dst: *[16 * count]u8, src: *const [16 * count]u8) void {
const inv_round_keys = ctx.key_schedule.round_keys;
var ts: [count]Block = undefined;
comptime var j = 0;
inline while (j < count) : (j += 1) {
ts[j] = Block.fromBytes(src[j * 16 .. j * 16 + 16][0..16]).xorBlocks(inv_round_keys[0]);
}
comptime var i = 1;
inline while (i < rounds) : (i += 1) {
ts = Block.parallel.decryptWide(count, ts, inv_round_keys[i]);
}
ts = Block.parallel.decryptLastWide(count, ts, inv_round_keys[i]);
j = 0;
inline while (j < count) : (j += 1) {
dst[16 * j .. 16 * j + 16].* = ts[j].toBytes();
}
}
};
}
/// 采用标准密钥扩展的 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);
}
};