X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/4fa9c3d0a87911ef71653cb99aa1c70f633a9473..9e6223f116e23fd4660e9a30b719f500778e740d:/OneRoundIoT/openssl/openssl_evp.c diff --git a/OneRoundIoT/openssl/openssl_evp.c b/OneRoundIoT/openssl/openssl_evp.c index 3c1416f..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; @@ -41,7 +44,7 @@ void handleErrors(void) int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, - unsigned char *iv, unsigned char *ciphertext, int ctr) + unsigned char *iv, unsigned char *ciphertext, int ctr, int index) { EVP_CIPHER_CTX *ctx; @@ -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,39 +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(); + for(int i=0;i ecb - if(ctr) + if(ctr) { if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) handleErrors(); - else + } + else if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) handleErrors(); @@ -129,17 +148,32 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, * EVP_DecryptUpdate can be called multiple times if necessary */ - - if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) - handleErrors(); - plaintext_len = len; +/* static double time=0; + double t=0; + t=TimeStart(); +*/ + 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); +*/ + + /* Finalise the decryption. Further plaintext bytes may be written at * this stage. */ if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors(); plaintext_len += len; + } + + time_decrypt+=TimeStop(t); + /* Clean up */ EVP_CIPHER_CTX_free(ctx); @@ -153,15 +187,24 @@ int main (int argc, char** argv) * real application? :-) */ - for(int i=1; i