OpenSSL 在可用的情况下使用 AES-NI。
openssl speed -evp aes-128-cbc
和输出
The 'numbers' are in 1000s of bytes per second processed.
type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes 16384 bytes
aes-128-cbc 531549.19k 969335.21k 1045437.10k 1066826.75k 1054665.39k 1052120.41k
由于您没有使用 AES-NI,您需要将其与软件版本进行比较
OPENSSL_ia32cap=”~0x200000200000000″ openssl speed -elapsed -evp aes-128-cbc
The 'numbers' are in 1000s of bytes per second processed.
type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes 16384 bytes
aes-128-cbc 143802.75k 161369.51k 165049.17k 166054.57k 166262.10k 166461.44k
如果我们比较最后一列,您会发现 AES-NI 比 OpenSSL 软件版本快约 6.3 倍。这意味着你的速度比软件版本慢大约 4 倍。
在许多情况下,编译器优化参数也会影响速度。查看你的编译器手册,如果你使用的是 GCC 那么它们是-O[0..3]
关于代码;
如果您查看 OpenSSL 的 AES 代码,您会发现它们使用预先计算的表 https://github.com/openssl/openssl/blob/master/crypto/aes/aes_core.c这是一种非常常见的技术。
The Subytes
, Shiftrows
and MixColums
都变成了查表。速度差异就是这些。并注意表查找很容易受到缓存定时攻击 https://cr.yp.to/antiforgery/cachetiming-20050414.pdf.