AEGIS 是一个基于 AES 的认证加密算法族,其速度显著快于广泛使用的算法如 AES-GCM 和 CHACHA20-POLY1305。PHP 8.4 中的 Sodium 扩展在编译时链接了 libsodium 1.0.19 或更高版本的情况下,支持 AEGIS-128L 和 AEGIS-256 加密算法。
AEGIS 家族中的两种加密算法 AEGIS-128L 和 AEGIS-256 的速度比 AES-GCM 快 2-3 倍,比 CHACHA20-POLY1305 算法快 3-4 倍。它们利用了 x86_64 和 aarch64(64 位 ARM 架构)CPU 架构上的硬件 AES 加速功能。
AEGIS 论文提供了关于这些算法内部工作原理的详细信息。
PHP 上的 AEGIS 可用性
当满足以下条件时,PHP 中可用 AEGIS 家族的加密算法:
- PHP 8.4 版本及更高版本 且
- Sodium 扩展使用
libsodium1.0.19 及更高版本 编译 且 - 在
x86_64或aarch64CPU 架构上运行
要检查 PHP 是否可用 AEGIS,请检查以下任一 AEGIS 函数是否存在:
if (function_exists('\sodium_crypto_aead_aegis128l_encrypt')) {
// AEGIS available
}请参阅 PHP 8.4: Sodium:
AEGIS-128LandAEGIS-256support,了解 PHP 8.4 中添加的所有新 AEGIS 函数和常量。
AEGIS-128L
AEGIS-128L 理论上可以加密长度低于 2^64 位的数据,并使用 128 位密钥。它是 Sodium 扩展中唯一使用 128 位密钥的对称加密算法,而其他算法均使用 256 位密钥。
它还需要一个 128 位的 nonce 值,该值必须在解密时提供。
以下是生成密钥和 nonce、使用附加数据加密数据以及解密的示例:
// generate a random key of sufficient length (16 bytes)
// This value must not be public.
$key = sodium_crypto_aead_aegis128l_keygen();
// Generate random nonce value of SODIUM_CRYPTO_AEAD_AEGIS128L_NPUBBYTES length (16 bytes).
// This value should be stored along the encrypted text, but is not required to be private
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_AEGIS128L_NPUBBYTES);
// Additional data. This can be a user ID, email address, or empty.
$additional_data = '';
// Message to encrypt
$message = 'Hello';
// Encrypt
$ciphertext = sodium_crypto_aead_aegis128l_encrypt($message, $additional_data, $nonce, $key);
// Decrypt
$decryptedMessage = sodium_crypto_aead_aegis128l_decrypt($ciphertext, $additional_data, $nonce, $key); // "Hello"AEGIS-256
AEGIS-256 使用 256 位密钥,可以加密长度低于 2^64 位的数据。除了一些例外情况,AEGIS-256 的计算强度比 AEGIS-128L 大约高 20%。
以下是生成密钥和 nonce,并使用附加数据将纯文本消息加密为密文再解密回明文的一个示例:
// generate a random key of sufficient length (32 bytes)
// This value must not be public.
$key = sodium_crypto_aead_aegis256_keygen();
// Generate random nonce value of SODIUM_CRYPTO_AEAD_AEGIS128L_NPUBBYTES length (32 bytes).
// This value should be stored along the encrypted text, but is not required to be private
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_AEGIS256_NPUBBYTES);
// Additional data. This can be a user ID, email address, or empty.
$additional_data = '';
// Message to encrypt
$message = 'Hello';
// Encrypt
$ciphertext = sodium_crypto_aead_aegis256_encrypt($message, $additional_data, $nonce, $key);
// Decrypt
$decryptedMessage = sodium_crypto_aead_aegis256_decrypt($ciphertext, $additional_data, $nonce, $key); // "Hello"AEGIS 与 AES-GCM 和 CHACHA20-POLY1305 的性能对比
AEGIS 家族的加密算法加密速率比当前推荐的 AES-GCM 算法快约两倍到三倍。例如,中等消费级硬件使用 AES-GCM 加密数据的速率约为 2.3 GB/秒,而 AEGIS 家族算法的加密速率为 4.5-5.0 GB/秒。
以下基准测试结果来自在 AMD Ryzen 4800H CPU(一款带有 AES-NI CPU 指令的 x86_64 CPU)上运行的基准测试。
对于数据速率基准测试,使用 Sodium 扩展支持的五种算法对 20 MB 随机字节块进行加密,并取 100 次迭代的平均值。这排除了生成 nonce 和密钥所花费的时间。
每秒操作数基准测试加密了 1 KB 数据,取 100 万次迭代的平均值。与数据速率基准测试类似,它也排除了 RNG(随机数生成器)的时间。
| Algorithm | Data Rate (GB/sec) | Operations/sec |
|---|---|---|
| aes256gcm | 2.31 GB/sec | 1,168,300 ops/sec |
| chacha20poly1305 | 1.29 GB/sec | 738,411 ops/sec |
| chacha20poly1305_ietf | 1.28 GB/sec | 746,409 ops/sec |
| xchacha20poly1305 | 1.28 GB/sec | 692,764 ops/sec |
| aegis128l | 4.99 GB/sec | 1,925,310 ops/sec |
| aegis256 | 4.61 GB/sec | 1,771,924 ops/sec |


此处所有 AES 算法在具有 AES-NI 指令的 CPU 上将更快。主要是低端移动设备和其他低功耗设备(如嵌入式设备)将不具备 AES-NI 指令。然而,PHP 运行的大多数服务器很可能使用的是能够达到上述基准测试结果所示速度的 CPU。