From: couturie Date: Mon, 16 Apr 2018 04:36:50 +0000 (+0200) Subject: update X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/commitdiff_plain/d252a09c09194eb764275de868606ba944ad0f97?ds=sidebyside update --- diff --git a/OneRoundIoT/openssl/Makefile b/OneRoundIoT/openssl/Makefile index 5a8b638..e463ec3 100644 --- a/OneRoundIoT/openssl/Makefile +++ b/OneRoundIoT/openssl/Makefile @@ -4,7 +4,7 @@ OBJ = pixmap_io.o openssl_evp.o OBJ2 = pixmap_io.o openssl_evp_cmac.o OBJ3 = pixmap_io.o openssl_evp_ccm.o OBJ4 = pixmap_io.o aesccm.o - +OBJ5 = pixmap_io.o openssl_evp_gcm.o openssl_evp: $(OBJ) $(C) -o $@ $^ $(CFLAGS) @@ -18,6 +18,10 @@ openssl_evp_ccm: $(OBJ3) aesccm: $(OBJ4) $(C) -o $@ $^ $(CFLAGS) + +openssl_evp_gcm: $(OBJ5) + $(C) -o $@ $^ $(CFLAGS) + %.o: %.c $(C) -c -o $@ $< diff --git a/OneRoundIoT/openssl/openssl_evp.c b/OneRoundIoT/openssl/openssl_evp.c index 90441f1..77dba80 100644 --- a/OneRoundIoT/openssl/openssl_evp.c +++ b/OneRoundIoT/openssl/openssl_evp.c @@ -62,6 +62,9 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, //t=TimeStart(); //256 //avant ecb + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include "pixmap_io.h" + +typedef unsigned char uchar; + +int nb_test=1; +int ctr=0; + + + + + +double TimeStart() +{ + struct timeval tstart; + gettimeofday(&tstart,0); + return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) ); +} + +double TimeStop(double t) +{ + struct timeval tend; + + gettimeofday(&tend,0); + t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t; + return (t); +} + + +void handleErrors(void) +{ + ERR_print_errors_fp(stderr); + abort(); +} + +int encryptgcm(unsigned char *plaintext, int plaintext_len, unsigned char *aad, + int aad_len, unsigned char *key, unsigned char *iv, int iv_len, + unsigned char *ciphertext, unsigned char *tag) +{ + EVP_CIPHER_CTX *ctx; + + int len; + + int ciphertext_len; + + + /* Create and initialise the context */ + if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); + + /* Initialise the encryption operation. */ + if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL)) + handleErrors(); + + /* Set IV length if default 12 bytes (96 bits) is not appropriate */ + if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL)) + handleErrors(); + for(int i=0;i 0) + { + /* Success */ + plaintext_len += len; + return plaintext_len; + } + else + { + /* Verify failed */ + return -1; + } +} + +/* int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, */ +/* unsigned char *iv, unsigned char *ciphertext, int ctr, int index) */ +/* { */ +/* EVP_CIPHER_CTX *ctx; */ + +/* int len; */ + +/* int ciphertext_len; */ + +/* /\* Create and initialise the context *\/ */ +/* if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); */ + +/* /\* Initialise the encryption operation. IMPORTANT - ensure you use a key */ +/* * and IV size appropriate for your cipher */ +/* * 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(); */ +/* //256 */ +/* //avant ecb */ +/* 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 ecb */ +/* if(ctr) { */ +/* if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) */ +/* handleErrors(); */ +/* } */ +/* else */ +/* if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) */ +/* handleErrors(); */ + +/* /\* Provide the message to be decrypted, and obtain the plaintext output. */ +/* * EVP_DecryptUpdate can be called multiple times if necessary */ +/* *\/ */ + +/* /\* static double time=0; */ +/* double t=0; */ +/* t=TimeStart(); */ +/* *\/ */ +/* for(int i=0;i