X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/429e289335b33339b56a05c986f26c73f555fe65..e8291c5548e7edd0beb647e84f4a4585f32b5265:/OneRoundIoT/openssl/openssl_evp.c?ds=inline diff --git a/OneRoundIoT/openssl/openssl_evp.c b/OneRoundIoT/openssl/openssl_evp.c index 55ba25e..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(); @@ -58,51 +64,46 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, * IV size for *most* modes is the same as the block size. For AES this * is 128 bits */ + double t=TimeStart(); + //256 //avant ecb - if(ctr) { - if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) - handleErrors(); - } - else + 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(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) handleErrors(); - + + //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(); -*/ - for(int i=0;i<nb_test;i++) - { - + + + /* 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; + /* 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+=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; - + time_encrypt+=TimeStop(t); + /* Clean up */ EVP_CIPHER_CTX_free(ctx); @@ -129,6 +130,11 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, //256 + double t=TimeStart(); + + for(int i=0;i<nb_test;i++) + { + //avant => ecb if(ctr) { if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) @@ -146,12 +152,12 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, double t=0; t=TimeStart(); */ - for(int i=0;i<nb_test;i++) - { + + plaintext_len = 0; if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) handleErrors(); plaintext_len = len; - } + /* time+=TimeStop(t); // if(index==nb_test-1) printf("Time decrypt %f\n",time); @@ -163,8 +169,10 @@ 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); @@ -226,7 +234,8 @@ int main (int argc, char** argv) // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B); } else { - width=height=size_buf; + width=size_buf; + height=size_buf; imsize=width*height; buffer=malloc(imsize*sizeof(uchar)); for(int i=0;i<imsize;i++) { @@ -237,7 +246,7 @@ int main (int argc, char** argv) int oneD=width*height; - uchar *plaintext = malloc(imsize); + uchar *plaintext = malloc(imsize+1000); //add that for cbc if(lena) { for(int i=0;i<oneD;i++) { plaintext[i]=data_R[i]; @@ -254,10 +263,10 @@ int main (int argc, char** argv) - uchar *ciphertext = malloc(imsize); + uchar *ciphertext = malloc(imsize+1000); //add that for cbc /* Buffer for the decrypted text */ - uchar *decryptedtext = malloc(imsize); + uchar *decryptedtext = malloc(imsize+1000); //add that for cbc int decryptedtext_len, ciphertext_len; @@ -268,8 +277,8 @@ int main (int argc, char** argv) */ - double time=0; - double t=TimeStart(); + + /* Encrypt the plaintext */ @@ -283,9 +292,10 @@ int main (int argc, char** argv) ciphertext, ctr, i ); } - time+=TimeStop(t); - printf("Time encrypt %f\n",time); + +// printf("Time encrypt %f\n",time); + printf("%e\t",(double)imsize*nb_test/time_encrypt); if(lena) { for(int i=0;i<oneD;i++) { @@ -297,10 +307,6 @@ int main (int argc, char** argv) } - time=0; - t=0; - t=TimeStart(); - //for(int i=0;i<nb_test;i++) { /* Decrypt the ciphertext */ @@ -308,9 +314,8 @@ int main (int argc, char** argv) decryptedtext,ctr, i); } - time+=TimeStop(t); - - printf("Time decrypt %f\n",time); + //printf("Time decrypt %f\n",time); + printf("%e\t",(double)imsize*nb_test/time_decrypt); if(lena) { for(int i=0;i<oneD;i++) {