跳转到内容

std.crypto.aes_siv

标准库Zig 0.17.1官方原文
源码std/crypto/aes_siv.zig

第 1 行

std.crypto.aes_siv.Aes128Siv

类型

第 11 行

std.crypto.aes_siv.Aes256Siv

类型

第 12 行

std.crypto.aes_siv.AesSiv

类型函数

第 47 行

AES-SIV:确定性认证加密,相同消息始终产生相同密文。

作用:加密数据并防止篡改。与多数加密模式不同,AES-SIV 具有确定性:使用相同密钥加密相同消息,始终产生相同密文,除非提供可选的 nonce。

适用场景:

  • 需要确定性加密,例如在加密存储中执行去重。
  • 无法存储或生成 nonce。
  • 密钥封装,即保护密码学密钥。
  • 需要在不解密的情况下搜索加密数据。

不适用的场景:

  • 要求相同明文产生不同密文,此时应使用 AES-GCM 或 AES-GCM-SIV。
  • 需要防范重放攻击的网络协议。

特有功能:

  • 可选 nonce:可以添加 nonce,使加密不再具有确定性,但这并非必需。
  • 多组关联数据:支持关联数据字符串向量,而不限于单个字符串。算法以密码学方式确保各部分正确分隔,防止不同数据划分方式均被认定有效的规范化攻击。

安全属性:

  • 确定性:相同输入始终产生相同输出,可能泄露模式信息。
  • 抵抗 nonce 误用:重复使用 nonce 不会造成灾难性失效。
  • 密钥承诺:密文只能由加密时使用的确切密钥解密。

AES-SIV 的安全属性优于 AES-GCM-SIV,但速度慢得多。

工作原理:结合两把密钥,一把用于认证(S2V),另一把用于加密(CTR 模式)。 总密钥长度是 AES 密钥长度的两倍:AES-128-SIV 为 256 位,AES-256-SIV 为 512 位。

定义于 RFC 5297。

fn AesSiv(comptime Aes: anytype) type

std.crypto.aes_siv.AesSiv.CmacImpl

类型

第 54 行

std.crypto.aes_siv.AesSiv.dbl

函数

第 114 行

RFC 5297 定义的倍增操作。 在 GF(2^128) 中乘以 x,即左移 1 位。 与 CMAC 子密钥生成使用的操作相同。 如果最高有效位置位,则移位后与多项式 0x87 异或。

fn dbl(d: [16]u8) [16]u8

参数

d: [16]u8

std.crypto.aes_siv.AesSiv.decrypt

函数

第 176 行

使用 AES-SIV 解密密文。 m:已解密明文的输出缓冲区 c:要解密的密文 tag:认证标签(合成初始化向量) ad:可选的关联数据(必须与加密时相同) nonce:可选的 nonce(必须与加密时相同) key:组合密钥(大小为 AES 密钥的两倍)

pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) AuthenticationError!void

参数

m: []u8
c: []const u8
tag: [tag_length]u8
ad: ?[]const u8
nonce: ?[]const u8
key: [key_length]u8

错误

AuthenticationFailed AuthenticationError

std.crypto.aes_siv.AesSiv.decryptWithAdVector

函数

第 258 行

使用多个关联数据分量解密密文。 这是 AES-SIV 解密最通用的形式,接受 RFC 5297 规定的最多 126 个关联数据字符串组成的向量。

pub fn decryptWithAdVector(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const []const u8, key: [key_length]u8) AuthenticationError!void

参数

m: []u8
c: []const u8
tag: [tag_length]u8
ad: []const []const u8
key: [key_length]u8

错误

AuthenticationFailed AuthenticationError

std.crypto.aes_siv.AesSiv.encrypt

函数

第 134 行

使用 AES-SIV 加密明文。 c:密文输出缓冲区,大小与明文相同 tag:认证标签,即合成 IV 的输出缓冲区 m:待加密明文 ad:可选关联数据 nonce:可选 nonce;若提供,则作为最后一个关联数据分量加入 key:组合密钥,大小为 AES 密钥的两倍

pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) void

参数

c: []u8
tag: *[tag_length]u8
m: []const u8
ad: ?[]const u8
nonce: ?[]const u8
key: [key_length]u8

std.crypto.aes_siv.AesSiv.encryptWithAdVector

函数

第 223 行

使用多个关联数据分量加密明文。 这是最通用的 AES-SIV 加密形式,接受 RFC 5297 所指定的、 最多包含 126 个关联数据字符串的向量。

pub fn encryptWithAdVector(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const []const u8, key: [key_length]u8) void

参数

c: []u8
tag: *[tag_length]u8
m: []const u8
ad: []const []const u8
key: [key_length]u8

std.crypto.aes_siv.AesSiv.key_length

常量

第 52 行

std.crypto.aes_siv.AesSiv.s2v

函数

第 60 行

S2V,字符串到向量,RFC 5297 第 2.4 节。 使用 CMAC 从密钥与输入字符串派生合成 IV。 此函数实现密码学伪随机函数,将 变长字符串向量映射为固定 128 位输出。

fn s2v(iv: *[16]u8, key: [Aes.key_bits / 8]u8, strings: []const []const u8) void

参数

iv: *[16]u8
key: [Aes.key_bits / 8]u8
strings: []const []const u8

std.crypto.aes_siv.AesSiv.tag_length

常量

第 51 行

std.crypto.aes_siv.assert

别名

第 2 行

别名目标 std.debug.assert

std.crypto.aes_siv.AuthenticationError

别名

第 9 行

别名目标 std.crypto.errors.AuthenticationError

std.crypto.aes_siv.Cmac

别名

第 8 行

别名目标 std.crypto.cmac.Cmac

std.crypto.aes_siv.crypto

别名

第 3 行

别名目标 std.crypto

std.crypto.aes_siv.debug

别名

第 4 行

别名目标 std.debug

std.crypto.aes_siv.htest

常量

第 301 行

std.crypto.aes_siv.math

别名

第 6 行

别名目标 std.math

std.crypto.aes_siv.mem

别名

第 5 行

别名目标 std.mem

std.crypto.aes_siv.modes

别名

第 7 行

别名目标 std.crypto.core.modes

std.crypto.aes_siv.std

别名

第 1 行

别名目标 std

std.crypto.aes_siv.testing

别名

第 302 行

别名目标 std.testing

源码

std/crypto/aes_siv.zig

const std = @import("std");
const assert = std.debug.assert;
const crypto = std.crypto;
const debug = std.debug;
const mem = std.mem;
const math = std.math;
const modes = crypto.core.modes;
const Cmac = @import("cmac.zig").Cmac;
const AuthenticationError = crypto.errors.AuthenticationError;

pub const Aes128Siv = AesSiv(crypto.core.aes.Aes128);
pub const Aes256Siv = AesSiv(crypto.core.aes.Aes256);

/// AES-SIV:确定性认证加密,相同消息始终产生相同密文。
///
/// 作用:加密数据并防止篡改。与多数加密模式不同,AES-SIV 具有确定性:使用相同密钥加密相同消息,始终产生相同密文,除非提供可选的 nonce。
///
/// 适用场景:
/// - 需要确定性加密,例如在加密存储中执行去重。
/// - 无法存储或生成 nonce。
/// - 密钥封装,即保护密码学密钥。
/// - 需要在不解密的情况下搜索加密数据。
///
/// 不适用的场景:
/// - 要求相同明文产生不同密文,此时应使用 AES-GCM 或 AES-GCM-SIV。
/// - 需要防范重放攻击的网络协议。
///
/// 特有功能:
/// - 可选 nonce:可以添加 nonce,使加密不再具有确定性,但这并非必需。
/// - 多组关联数据:支持关联数据字符串向量,而不限于单个字符串。算法以密码学方式确保各部分正确分隔,防止不同数据划分方式均被认定有效的规范化攻击。
///
/// 安全属性:
/// - 确定性:相同输入始终产生相同输出,可能泄露模式信息。
/// - 抵抗 nonce 误用:重复使用 nonce 不会造成灾难性失效。
/// - 密钥承诺:密文只能由加密时使用的确切密钥解密。
///
/// AES-SIV 的安全属性优于 AES-GCM-SIV,但速度慢得多。
///
/// 工作原理:结合两把密钥,一把用于认证(S2V),另一把用于加密(CTR 模式)。
/// 总密钥长度是 AES 密钥长度的两倍:AES-128-SIV 为 256 位,AES-256-SIV 为 512 位。
///
/// 定义于 RFC 5297。
///
///
///
///
fn AesSiv(comptime Aes: anytype) type {
    debug.assert(Aes.block.block_length == 16);

    return struct {
        pub const tag_length = 16;
        pub const key_length = Aes.key_bits / 8 * 2; // SIV 使用双倍密钥长度

        const CmacImpl = Cmac(Aes);

        /// S2V,字符串到向量,RFC 5297 第 2.4 节。
        /// 使用 CMAC 从密钥与输入字符串派生合成 IV。
        /// 此函数实现密码学伪随机函数,将
        /// 变长字符串向量映射为固定 128 位输出。
        fn s2v(iv: *[16]u8, key: [Aes.key_bits / 8]u8, strings: []const []const u8) void {
            assert(strings.len > 0);
            assert(strings.len <= 127); // S2V 限制

            var d: [16]u8 = undefined;

            // 使用零分组的 CMAC 初始化
            const zero_block: [16]u8 = @splat(0);
            CmacImpl.create(&d, &zero_block, &key);

            // 处理除最后一个以外的所有字符串
            var i: usize = 0;
            while (i < strings.len - 1) : (i += 1) {
                d = dbl(d);
                var tmp: [16]u8 = undefined;
                CmacImpl.create(&tmp, strings[i], &key);
                for (&d, tmp) |*b, t| {
                    b.* ^= t;
                }
            }

            // 处理最后一个字符串
            const sn = strings[strings.len - 1];
            if (sn.len >= 16) {
                // 将 d 与 Sn 的最后 16 字节异或,然后以增量方式将整个 Sn 提交给 CMAC。
                //
                var cmac = CmacImpl.init(&key);
                const prefix = sn.len - 16;
                cmac.update(sn[0..prefix]);

                var tail: [16]u8 = undefined;
                for (&tail, sn[prefix..][0..16], d) |*out, s, db| {
                    out.* = s ^ db;
                }
                cmac.update(&tail);

                cmac.final(iv);
            } else {
                // 填充并异或
                d = dbl(d);
                var padded: [16]u8 = @splat(0);
                @memcpy(padded[0..sn.len], sn);
                padded[sn.len] = 0x80;
                for (&d, padded) |*b, p| {
                    b.* ^= p;
                }
                CmacImpl.create(iv, &d, &key);
            }
        }

        /// RFC 5297 定义的倍增操作。
        /// 在 GF(2^128) 中乘以 x,即左移 1 位。
        /// 与 CMAC 子密钥生成使用的操作相同。
        /// 如果最高有效位置位,则移位后与多项式 0x87 异或。
        fn dbl(d: [16]u8) [16]u8 {
            // 按大端序 128 位整数读取
            const val = mem.readInt(u128, &d, .big);

            // 左移 1 位;若原最高位为 1,则与 0x87 异或。
            const doubled = (val << 1) ^ (0x87 & -%(@as(u128, val >> 127)));

            // 按大端序写回
            var result: [16]u8 = undefined;
            mem.writeInt(u128, &result, doubled, .big);
            return result;
        }

        /// 使用 AES-SIV 加密明文。
        /// `c`:密文输出缓冲区,大小与明文相同
        /// `tag`:认证标签,即合成 IV 的输出缓冲区
        /// `m`:待加密明文
        /// `ad`:可选关联数据
        /// `nonce`:可选 nonce;若提供,则作为最后一个关联数据分量加入
        /// `key`:组合密钥,大小为 AES 密钥的两倍
        pub fn encrypt(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) void {
            debug.assert(c.len == m.len);

            // 将密钥拆为 K1(用于 S2V)和 K2(用于 CTR)
            const k1 = key[0 .. Aes.key_bits / 8];
            const k2 = key[Aes.key_bits / 8 ..];

            // 为 S2V 准备字符串:关联数据的各部分,随后为明文。
            var strings_buf: [128][]const u8 = undefined;
            var strings_len: usize = 0;

            if (ad) |a| {
                strings_buf[strings_len] = a;
                strings_len += 1;
            }
            if (nonce) |n| {
                strings_buf[strings_len] = n;
                strings_len += 1;
            }
            strings_buf[strings_len] = m;
            strings_len += 1;

            // 使用 S2V 计算合成 IV
            s2v(tag, k1.*, strings_buf[0..strings_len]);

            // 清除第 31 和第 63 位,以用作 CTR IV
            var ctr_iv = tag.*;
            ctr_iv[8] &= 0x7f;
            ctr_iv[12] &= 0x7f;

            // 使用 CTR 模式加密明文
            const aes_ctx = Aes.initEnc(k2.*);
            modes.ctr(@TypeOf(aes_ctx), aes_ctx, c, m, ctr_iv, .big);
        }

        /// 使用 AES-SIV 解密密文。
        /// `m`:已解密明文的输出缓冲区
        /// `c`:要解密的密文
        /// `tag`:认证标签(合成初始化向量)
        /// `ad`:可选的关联数据(必须与加密时相同)
        /// `nonce`:可选的 nonce(必须与加密时相同)
        /// `key`:组合密钥(大小为 AES 密钥的两倍)
        pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: ?[]const u8, nonce: ?[]const u8, key: [key_length]u8) AuthenticationError!void {
            assert(c.len == m.len);

            // 将密钥拆为 K1(用于 S2V)和 K2(用于 CTR)
            const k1 = key[0 .. Aes.key_bits / 8];
            const k2 = key[Aes.key_bits / 8 ..];

            // 清除第 31 和第 63 位,以用作 CTR IV
            var ctr_iv = tag;
            ctr_iv[8] &= 0x7f;
            ctr_iv[12] &= 0x7f;

            // 使用 CTR 模式解密密文
            const aes_ctx = Aes.initEnc(k2.*);
            modes.ctr(@TypeOf(aes_ctx), aes_ctx, m, c, ctr_iv, .big);

            // 为 S2V 准备字符串:关联数据的各部分,随后为明文。
            var strings_buf: [128][]const u8 = undefined;
            var strings_len: usize = 0;

            if (ad) |a| {
                strings_buf[strings_len] = a;
                strings_len += 1;
            }
            if (nonce) |n| {
                strings_buf[strings_len] = n;
                strings_len += 1;
            }
            strings_buf[strings_len] = m;
            strings_len += 1;

            // 使用 S2V 校验合成 IV
            var computed_tag: [tag_length]u8 = undefined;
            s2v(&computed_tag, k1.*, strings_buf[0..strings_len]);

            // 验证标签
            const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
            if (!verify) {
                crypto.secureZero(u8, &computed_tag);
                @memset(m, undefined);
                return error.AuthenticationFailed;
            }
        }

        /// 使用多个关联数据分量加密明文。
        /// 这是最通用的 AES-SIV 加密形式,接受 RFC 5297 所指定的、
        /// 最多包含 126 个关联数据字符串的向量。
        pub fn encryptWithAdVector(c: []u8, tag: *[tag_length]u8, m: []const u8, ad: []const []const u8, key: [key_length]u8) void {
            debug.assert(c.len == m.len);
            debug.assert(ad.len <= 126); // AES-SIV 最多支持 126 个关联数据部分。

            // 将密钥拆为 K1(用于 S2V)和 K2(用于 CTR)
            const k1 = key[0 .. Aes.key_bits / 8];
            const k2 = key[Aes.key_bits / 8 ..];

            // 为 S2V 准备字符串:关联数据的各部分,随后为明文。
            var strings_buf: [128][]const u8 = undefined;
            var strings_len: usize = 0;

            for (ad) |a| {
                strings_buf[strings_len] = a;
                strings_len += 1;
            }
            strings_buf[strings_len] = m;
            strings_len += 1;

            // 使用 S2V 计算合成 IV
            s2v(tag, k1.*, strings_buf[0..strings_len]);

            // 清除第 31 和第 63 位,以用作 CTR IV
            var ctr_iv = tag.*;
            ctr_iv[8] &= 0x7f;
            ctr_iv[12] &= 0x7f;

            // 使用 CTR 模式加密明文
            const aes_ctx = Aes.initEnc(k2.*);
            modes.ctr(@TypeOf(aes_ctx), aes_ctx, c, m, ctr_iv, .big);
        }

        /// 使用多个关联数据分量解密密文。
        /// 这是 AES-SIV 解密最通用的形式,接受
        /// RFC 5297 规定的最多 126 个关联数据字符串组成的向量。
        pub fn decryptWithAdVector(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const []const u8, key: [key_length]u8) AuthenticationError!void {
            assert(c.len == m.len);
            assert(ad.len <= 126); // AES-SIV 最多支持 126 个关联数据部分。

            // 将密钥拆为 K1(用于 S2V)和 K2(用于 CTR)
            const k1 = key[0 .. Aes.key_bits / 8];
            const k2 = key[Aes.key_bits / 8 ..];

            // 清除第 31 和第 63 位,以用作 CTR IV
            var ctr_iv = tag;
            ctr_iv[8] &= 0x7f;
            ctr_iv[12] &= 0x7f;

            // 使用 CTR 模式解密密文
            const aes_ctx = Aes.initEnc(k2.*);
            modes.ctr(@TypeOf(aes_ctx), aes_ctx, m, c, ctr_iv, .big);

            // 为 S2V 准备字符串:关联数据的各部分,随后为明文。
            var strings_buf: [128][]const u8 = undefined;
            var strings_len: usize = 0;

            for (ad) |a| {
                strings_buf[strings_len] = a;
                strings_len += 1;
            }
            strings_buf[strings_len] = m;
            strings_len += 1;

            // 使用 S2V 校验合成 IV
            var computed_tag: [tag_length]u8 = undefined;
            s2v(&computed_tag, k1.*, strings_buf[0..strings_len]);

            // 验证标签
            const verify = crypto.timing_safe.eql([tag_length]u8, computed_tag, tag);
            if (!verify) {
                crypto.secureZero(u8, &computed_tag);
                @memset(m, undefined);
                return error.AuthenticationFailed;
            }
        }
    };
}

const htest = @import("test.zig");
const testing = std.testing;

test "AES-SIV double operation" {
    const AesSivTest = AesSiv(crypto.core.aes.Aes128);

    // RFC 5297 中的测试向量
    const input = [_]u8{ 0x0e, 0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
    const expected = [_]u8{ 0x1c, 0x08, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c };

    const result = AesSivTest.dbl(input);
    try testing.expectEqualSlices(u8, &expected, &result);
}

test "AES-SIV double operation with MSB set" {
    const AesSivTest = AesSiv(crypto.core.aes.Aes128);

    const input = [_]u8{ 0xe0, 0x40, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0 };
    const expected = [_]u8{ 0xc0, 0x80, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe1, 0x01, 0x21, 0x41, 0x61, 0x81, 0xa1, 0x47 };

    const result = AesSivTest.dbl(input);
    try testing.expectEqualSlices(u8, &expected, &result);
}

test "Aes128Siv - RFC 5297 Test Vector A.1" {
    // 来自 RFC 5297 附录 A.1 的测试向量
    const key = [_]u8{
        0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
        0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
    };
    const ad = [_]u8{
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
        0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
    };
    const plaintext = [_]u8{
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
    };

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    // 使用向量 API 测试 RFC 合规性
    const ad_components = [_][]const u8{&ad};
    Aes128Siv.encryptWithAdVector(&ciphertext, &tag, &plaintext, &ad_components, key);

    // RFC 5297 中的预期值
    try htest.assertEqual("85632d07c6e8f37f950acd320a2ecc93", &tag);
    try htest.assertEqual("40c02b9690c4dc04daef7f6afe5c", &ciphertext);

    // 测试解密
    var decrypted: [plaintext.len]u8 = undefined;
    try Aes128Siv.decryptWithAdVector(&decrypted, &ciphertext, tag, &ad_components, key);
    try testing.expectEqualSlices(u8, &plaintext, &decrypted);
}

test "Aes128Siv - RFC 5297 Test Vector A.2" {
    // 来自 RFC 5297 附录 A.2 的测试向量
    const key: [32]u8 = .{
        0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78,
        0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70,
        0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
        0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
    };
    const ad1 = [_]u8{
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
        0xde, 0xad, 0xda, 0xda, 0xde, 0xad, 0xda, 0xda,
        0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
        0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
    };
    const ad2 = [_]u8{
        0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80,
        0x90, 0xa0,
    };
    const nonce: [16]u8 = .{
        0x09, 0xf9, 0x11, 0x02, 0x9d, 0x74, 0xe3, 0x5b,
        0xd8, 0x41, 0x56, 0xc5, 0x63, 0x56, 0x88, 0xc0,
    };
    const plaintext = [_]u8{
        0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
        0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x6c, 0x61,
        0x69, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x74,
        0x6f, 0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70,
        0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20,
        0x53, 0x49, 0x56, 0x2d, 0x41, 0x45, 0x53,
    };

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    Aes128Siv.encryptWithAdVector(&ciphertext, &tag, &plaintext, &.{ &ad1, &ad2, &nonce }, key);

    // RFC 5297 中的预期值
    try htest.assertEqual("7bdb6e3b432667eb06f4d14bff2fbd0f", &tag);
    try htest.assertEqual("cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829ea64ad544a272e9c485b62a3fd5c0d", &ciphertext);
}

test "Aes128Siv - empty plaintext" {
    const key: [32]u8 = @splat(0x42);
    const plaintext = "";
    const ad = "additional data";

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);

    var decrypted: [plaintext.len]u8 = undefined;
    try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key);
}

test "Aes128Siv - empty plaintext without associated data" {
    const key: [Aes128Siv.key_length]u8 = @splat(0x42);
    var ciphertext: [0]u8 = undefined;
    var tag: [Aes128Siv.tag_length]u8 = undefined;

    Aes128Siv.encrypt(&ciphertext, &tag, "", null, null, key);

    try testing.expectEqual([_]u8{
        0x23, 0xef, 0x43, 0xc1, 0x08, 0x55, 0x68, 0x98,
        0x27, 0x56, 0xaf, 0x5c, 0x9f, 0xda, 0x7e, 0xab,
    }, tag);
}

test "Aes128Siv - with nonce" {
    const key: [32]u8 = @splat(0x69);
    const nonce: [16]u8 = @splat(0x42);
    const plaintext = "Hello, AES-SIV!";
    const ad = "metadata";

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, &nonce, key);

    var decrypted: [plaintext.len]u8 = undefined;
    try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, &nonce, key);
    try testing.expectEqualSlices(u8, plaintext, &decrypted);
}

test "Aes256Siv - basic functionality" {
    const key: [64]u8 = @splat(0x96);
    const plaintext = "Test message for AES-256-SIV";
    const ad1 = "header";
    const ad2 = "more data";

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    // 使用向量 API 测试多个关联数据分量
    const ad_components = [_][]const u8{ ad1, ad2 };
    Aes256Siv.encryptWithAdVector(&ciphertext, &tag, plaintext, &ad_components, key);

    var decrypted: [plaintext.len]u8 = undefined;
    try Aes256Siv.decryptWithAdVector(&decrypted, &ciphertext, tag, &ad_components, key);
    try testing.expectEqualSlices(u8, plaintext, &decrypted);
}

test "Aes128Siv - demonstrating optional parameters" {
    const key: [32]u8 = @splat(0x77);

    // 测试 1:无附加数据、无 nonce(完全确定性)
    {
        const plaintext = "Deterministic encryption";
        var ciphertext: [plaintext.len]u8 = undefined;
        var tag: [16]u8 = undefined;

        Aes128Siv.encrypt(&ciphertext, &tag, plaintext, null, null, key);

        var decrypted: [plaintext.len]u8 = undefined;
        try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, null, null, key);
        try testing.expectEqualSlices(u8, plaintext, &decrypted);
    }

    // 测试 2:带 AD,不含 nonce
    {
        const plaintext = "With associated data";
        const ad = "some context";
        var ciphertext: [plaintext.len]u8 = undefined;
        var tag: [16]u8 = undefined;

        Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);

        var decrypted: [plaintext.len]u8 = undefined;
        try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key);
        try testing.expectEqualSlices(u8, plaintext, &decrypted);
    }

    // 测试 3:无 AD,带 nonce
    {
        const plaintext = "Nonce-based encryption";
        const nonce: [12]u8 = @splat(0x01);
        var ciphertext: [plaintext.len]u8 = undefined;
        var tag: [16]u8 = undefined;

        Aes128Siv.encrypt(&ciphertext, &tag, plaintext, null, &nonce, key);

        var decrypted: [plaintext.len]u8 = undefined;
        try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, null, &nonce, key);
        try testing.expectEqualSlices(u8, plaintext, &decrypted);
    }

    // 测试 4:同时包含附加数据与 nonce
    {
        const plaintext = "Full featured";
        const ad = "context";
        const nonce: [16]u8 = @splat(0x02);
        var ciphertext: [plaintext.len]u8 = undefined;
        var tag: [16]u8 = undefined;

        Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, &nonce, key);

        var decrypted: [plaintext.len]u8 = undefined;
        try Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, &nonce, key);
        try testing.expectEqualSlices(u8, plaintext, &decrypted);
    }
}

test "Aes128Siv - authentication failure" {
    const key: [32]u8 = @splat(0x13);
    const plaintext = "Secret message";
    const ad = "";

    var ciphertext: [plaintext.len]u8 = undefined;
    var tag: [16]u8 = undefined;

    Aes128Siv.encrypt(&ciphertext, &tag, plaintext, ad, null, key);

    // 破坏标签
    tag[0] ^= 0x01;

    var decrypted: [plaintext.len]u8 = undefined;
    try testing.expectError(error.AuthenticationFailed, Aes128Siv.decrypt(&decrypted, &ciphertext, tag, ad, null, key));
}