OpenSSL是Linux系统中最常用的加密工具包之一,合理优化可以显著提升加密/解密性能。以下是针对Linux系统优化OpenSSL加密性能的综合方案:
# 检查CPU是否支持AES-NI
grep -m1 -o aes /proc/cpuinfo
# 确认OpenSSL是否启用了AES-NI
openssl speed -evp aes-128-cbc
# 如果显示"aes"字样表示已启用
# 查看可用引擎
openssl engine -t -c
# 启用Intel QuickAssist技术(QAT)等硬件加速
openssl speed -engine qat -evp aes-128-cbc
# 优先使用AES-GCM而不是CBC模式
openssl speed -evp aes-256-gcm
# 使用更高效的椭圆曲线(如X25519而不是NIST P-256)
openssl speed x25519
编辑/etc/ssl/openssl.cnf
:
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
CipherString = HIGH:!aNULL:!MD5:!RC4:!3DES
Options = ServerPreference,PrioritizeChaCha
# 提高加密操作的内存限制
echo "vm.swappiness = 10" >> /etc/sysctl.conf
sysctl -p
# 调整TCP参数优化TLS性能
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
# 将OpenSSL进程绑定到特定CPU核心
taskset -c 0,1 openssl speed -multi 2 aes-256-cbc
# 提高OpenSSL进程优先级
nice -n -10 openssl speed aes-256-cbc
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl \
-Wa,--noexecstack -march=native -O3 -fPIC -DOPENSSL_USE_NODELETE \
enable-ec_nistp_64_gcc_128 enable-tls1_3 no-ssl3 no-comp no-idea \
no-weak-ssl-ciphers no-shared
make -j$(nproc)
make install
# 测试AES性能
openssl speed -evp aes-256-gcm
# 测试TLS握手性能
openssl s_time -connect example.com:443 -new -www / -CApath /etc/ssl/certs
# 监控加密操作的CPU使用
perf stat openssl speed aes-256-cbc
# 使用openssl-speed进行详细测试
openssl-speed -elapsed -seconds 10
通过以上优化措施,可以显著提升Linux系统上OpenSSL的加密性能,特别是在高负载场景下效果更为明显。