与lcrypto链接的程序比openssl命令慢许多倍 [英] program linked with lcrypto is many times slower than openssl command
问题描述
我有一个简单的C
程序用于aes256加密.它与openssl库(-lcrypto
)链接.该程序的核心是以下几行:
I have a simple C
program for aes256 encryption. It is linked with openssl library (-lcrypto
). The core of the program are following few lines:
AES_set_encrypt_key(key32 ,256 ,&aes_ks3);
while( len = fread( buf ,1 ,4096, fp) ){
if( 4096 != len )
break;
AES_cbc_encrypt(buf ,buf ,len ,&aes_ks3 ,iv ,AES_ENCRYPT);
fwrite(buf ,1 ,len ,wfp);
}
AES_cbc_encrypt(buf ,buf ,len+padding_len ,&aes_ks3, iv,AES_ENCRYPT);
fwrite(buf ,1 ,len+padding_len ,wfp);
我仅使用标准的openssl库函数进行加密(即,我没有使用自己的函数).我可以使用openssl
命令使用相同的密钥和IV来加密相同的文件:
I am only using standard openssl library functions for encryption (ie. I am not using my own functions). I can encrypt same file, using same key and IV with openssl
command:
openssl enc -aes-256-cbc -in FILE.in -out FILE.out -K $key -iv $iv
然后我得到了相同的输出文件(因此验证我的程序可以正常工作).
And I get identical output file (thus verifying that my program works correctly).
但是,我的程序始终运行比openssl
命令慢4-5倍.它们都使用相同的例程,abd都与相同的库链接.
However, my program consistently runs is 4-5 times slower than the openssl
command. They are both using the same routines, abd are both linked with the same library.
那怎么可能?
我如何调查原因?
更新:
以下是使用1)openssl,2)我的程序加密同一文件的实际数字:
Here are the actual numbers for encrypting same file with 1) openssl, 2) my program:
1)openssl:
1) openssl:
real 0m0.238s
user 0m0.196s
sys 0m0.040s
2)我的程序:
real 0m1.006s
user 0m0.964s
sys 0m0.040s
推荐答案
直接调用AES函数,就失去了 EVP层支持AES内在函数,这在支持它们的CPU上有很大的不同.
By calling the AES functions directly, you lose all the optimizations provided by the EVP layer. In particular, the EVP layer supports AES intrinsics, which makes a huge difference on CPUs that support them.
这篇关于与lcrypto链接的程序比openssl命令慢许多倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!