From 94e90035b1b2f00c5862a03f69a0b23d9b53df51 Mon Sep 17 00:00:00 2001 From: couturie Date: Sun, 18 Feb 2018 15:18:22 +0100 Subject: [PATCH 1/1] add cmac version --- OneRoundIoT/openssl/Makefile | 7 +- OneRoundIoT/openssl/openssl_evp_cmac.c | 351 +++++++++++++++++++++++++ 2 files changed, 357 insertions(+), 1 deletion(-) create mode 100755 OneRoundIoT/openssl/openssl_evp_cmac.c diff --git a/OneRoundIoT/openssl/Makefile b/OneRoundIoT/openssl/Makefile index c3cf5b7..b2f769e 100644 --- a/OneRoundIoT/openssl/Makefile +++ b/OneRoundIoT/openssl/Makefile @@ -1,14 +1,19 @@ C=gcc CFLAGS= -I /usr/include/openssl/ -lcrypto -O3 -std=c99 OBJ = pixmap_io.o openssl_evp.o +OBJ2 = pixmap_io.o openssl_evp_cmac.o openssl_evp: $(OBJ) $(C) -o $@ $^ $(CFLAGS) +openssl_evp_cmac: $(OBJ2) + $(C) -o $@ $^ $(CFLAGS) + %.o: %.c $(C) -c -o $@ $< clean: - rm -rf $(OBJ) openssl_evp + rm -rf $(OBJ) openssl_evp openssl_evp_cmac + diff --git a/OneRoundIoT/openssl/openssl_evp_cmac.c b/OneRoundIoT/openssl/openssl_evp_cmac.c new file mode 100755 index 0000000..ae01f5c --- /dev/null +++ b/OneRoundIoT/openssl/openssl_evp_cmac.c @@ -0,0 +1,351 @@ +//gcc pixmap_io.c -c +//gcc openssl_evp.c pixmap_io.o -o openssl_evp -I /usr/include/openssl/ -lcrypto -O3 -std=c99 + + +#include +#include +#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 encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, + unsigned char *iv, unsigned char *ciphertext, int ctr, int index) +{ + CMAC_CTX *ctx; + + int len; + + int ciphertext_len; + + /* Create and initialise the context */ + if(!(ctx = CMAC_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 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL)) + handleErrors(); + } + else + if(1 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL)) + handleErrors(); + size_t mactlen; +unsigned char mact[16] = {0}; + //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