From: couturie Date: Fri, 18 Aug 2017 12:29:18 +0000 (+0200) Subject: ,ew X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/commitdiff_plain/4fa9c3d0a87911ef71653cb99aa1c70f633a9473?ds=inline;hp=--cc ,ew --- 4fa9c3d0a87911ef71653cb99aa1c70f633a9473 diff --git a/OneRoundIoT/OneRound/one_round_new.cpp b/OneRoundIoT/OneRound/one_round_new.cpp index a222855..59210a8 100644 --- a/OneRoundIoT/OneRound/one_round_new.cpp +++ b/OneRoundIoT/OneRound/one_round_new.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,7 @@ using namespace std; int key_size=256; int nb_test=1; +int ctr=0; const int h=16; const int h2=h*h; @@ -66,6 +68,14 @@ void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) { } +void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) { + + for(int i=0;i10000) { + printf("nb tests is not correct\n"); + exit(0); + } + else + printf("nb tests = %d\n\n",nb_test); +*/ + + + int seed=time(NULL); + cout<10000) { - printf("nb tests is not correct\n"); - exit(0); - } - else - printf("nb tests = %d\n\n",nb_test); - - int seed=time(NULL); - cout< @@ -13,6 +13,8 @@ typedef unsigned char uchar; +int nb_test=1; +int ctr=0; double TimeStart() { @@ -39,7 +41,7 @@ void handleErrors(void) int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, - unsigned char *iv, unsigned char *ciphertext) + unsigned char *iv, unsigned char *ciphertext, int ctr) { EVP_CIPHER_CTX *ctx; @@ -58,8 +60,12 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, //256 //avant ecb - if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) - handleErrors(); + 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(); // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx); // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize); @@ -92,7 +98,7 @@ int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, } int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, - unsigned char *iv, unsigned char *plaintext) + unsigned char *iv, unsigned char *plaintext, int ctr) { EVP_CIPHER_CTX *ctx; @@ -112,7 +118,11 @@ int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, //256 //avant => ecb - if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) + 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. @@ -143,6 +153,17 @@ int main (int argc, char** argv) * real application? :-) */ + for(int i=1; i10000) { - printf("nb tests is not correct\n"); - exit(0); - } - else - printf("nb tests = %d\n\n",nb_test); - - - /* Buffer for ciphertext. Ensure the buffer is long enough for the * ciphertext which may be longer than the plaintext, dependant on the * algorithm and mode @@ -217,7 +224,7 @@ int main (int argc, char** argv) for(i=0;i -#include -#include -#include -#include -#include -#include -#include "pixmap_io.h" - -typedef unsigned char uchar; - - -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) -{ - 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 */ - - //256 - //avant ecb - if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv)) - handleErrors(); - -// 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 - */ - - 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; - - /* Clean up */ - EVP_CIPHER_CTX_free(ctx); - - return ciphertext_len; -} - -int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, - unsigned char *iv, unsigned char *plaintext) -{ - EVP_CIPHER_CTX *ctx; - - int len; - - int plaintext_len; - - /* Create and initialise the context */ - if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); - - /* Initialise the decryption 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 */ - - //256 - - //avant => ecb - if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv)) - handleErrors(); - - /* Provide the message to be decrypted, and obtain the plaintext output. - * EVP_DecryptUpdate can be called multiple times if necessary - */ - - - if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) - handleErrors(); - plaintext_len = len; - - /* 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; - - /* Clean up */ - EVP_CIPHER_CTX_free(ctx); - - return plaintext_len; -} - - -int main (int argc, char** argv) -{ - /* Set up the key and iv. Do I need to say to not hard code these in a - * real application? :-) - */ - - /* A 256 bit key */ -// unsigned char *key = (unsigned char *)"01234567890123456789012345678901"; - unsigned char *key = (unsigned char *)"0123456789012345"; - - /* A 128 bit IV */ - unsigned char *iv = (unsigned char *)"0123456789012345"; - - /* Message to be encrypted */ - - int nb_test=1; - if(argc==2) - nb_test=atoi(argv[1]); - if(nb_test<=0 || nb_test>10000) { - printf("nb tests is not correct\n"); - exit(0); - } - else - printf("nb tests = %d\n\n",nb_test); - - - - /* Buffer for ciphertext. Ensure the buffer is long enough for the - * ciphertext which may be longer than the plaintext, dependant on the - * algorithm and mode - */ - - int width; - int height; - uchar *data_R, *data_G, *data_B; - load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B); - - -// load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B); -// load_RGB_pixmap("4096.ppm", &width, &height, &data_R, &data_G, &data_B); - int size=width*height*3; - - int oneD=width*height; - uchar *plaintext = malloc(size); - - - for(int i=0;i