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); 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).
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.
How is that possible?
How can I investigate why?
UPDATE:
Here are the actual numbers for encrypting same file with 1) openssl, 2) my program:
1) openssl:
real 0m0.238s user 0m0.196s sys 0m0.040s 2) my program:
real 0m1.006s user 0m0.964s sys 0m0.040s
RW_SIZEandFREAD_COUNT? Without any more information I would probably bet that's where your bottleneck is.RW_SIZEandFREAD_COUNTby their corresponding numbers.