X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/bdb76e61a70ac6da625d817bf7179e36436fa6cb..3ed2f4b380f0d9f49ff45bf6accf8a55c511a353:/OneRoundIoT/openssl/openssl_evp.c?ds=sidebyside diff --git a/OneRoundIoT/openssl/openssl_evp.c b/OneRoundIoT/openssl/openssl_evp.c index 6860932..0a4b949 100644 --- a/OneRoundIoT/openssl/openssl_evp.c +++ b/OneRoundIoT/openssl/openssl_evp.c @@ -16,6 +16,9 @@ typedef unsigned char uchar; int nb_test=1; int ctr=0; +double time_encrypt=0; +double time_decrypt=0; + double TimeStart() { struct timeval tstart; @@ -49,6 +52,9 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, int ciphertext_len; + + + /* Create and initialise the context */ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); @@ -57,58 +63,47 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits */ - //static double time=0; - //double t=0; - //t=TimeStart(); + + double t=TimeStart(); + //256 //avant ecb - for(int i=0;i<nb_test;i++) + for(int i=0;i<nb_test;i++) { - - if(ctr) { - if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) - handleErrors(); - } - else + + if(ctr) { + if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) + handleErrors(); + } + else if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) handleErrors(); - - //time+=TimeStop(t); - //printf("Time init %f\n",time); - - + + //time+=TimeStop(t); + //printf("Time init %f\n",time); + + // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx); // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize); - - - /* Provide the message to be encrypted, and obtain the encrypted output. - * EVP_EncryptUpdate can be called multiple times if necessary - */ - -/* - static double time=0; - double t=0; - t=TimeStart(); -*/ - - if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) + + + /* Provide the message to be encrypted, and obtain the encrypted output. + * EVP_EncryptUpdate can be called multiple times if necessary + */ + + if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; - -/* time+=TimeStop(t); - // if(index==nb_test-1) - printf("Time encrypt %f\n",time); - -*/ - - - /* Finalise the encryption. Further ciphertext bytes may be written at - * this stage. - */ - if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); - ciphertext_len += len; + /* Finalise the encryption. Further ciphertext bytes may be written at + * this stage. + */ + if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); + ciphertext_len += len; } + + time_encrypt+=TimeStop(t); + /* Clean up */ EVP_CIPHER_CTX_free(ctx); @@ -135,6 +130,8 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, //256 + double t=TimeStart(); + for(int i=0;i<nb_test;i++) { @@ -173,7 +170,9 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors(); plaintext_len += len; } - + + + time_decrypt+=TimeStop(t); /* Clean up */ EVP_CIPHER_CTX_free(ctx); @@ -278,9 +277,8 @@ int main (int argc, char** argv) */ - double time_encrypt=0; - double time_decrypt=0; - double t=TimeStart(); + + /* Encrypt the plaintext */ @@ -294,10 +292,10 @@ int main (int argc, char** argv) ciphertext, ctr, i ); } - time_encrypt+=TimeStop(t); + // printf("Time encrypt %f\n",time); - printf("%f\t",(double)imsize*nb_test/time_encrypt); + printf("%e\t",(double)imsize*nb_test/time_encrypt); if(lena) { for(int i=0;i<oneD;i++) { @@ -309,9 +307,6 @@ int main (int argc, char** argv) } - t=0; - t=TimeStart(); - //for(int i=0;i<nb_test;i++) { /* Decrypt the ciphertext */ @@ -319,10 +314,8 @@ int main (int argc, char** argv) decryptedtext,ctr, i); } - time_decrypt+=TimeStop(t); - //printf("Time decrypt %f\n",time); - printf("%f\t",(double)imsize*nb_test/time_decrypt); + printf("%e\t",(double)imsize*nb_test/time_decrypt); if(lena) { for(int i=0;i<oneD;i++) {