#include<math.h>
#include<stdlib.h>
#include<stdio.h>
+#include<string.h>
#include <fstream>
#include <sys/time.h>
int key_size=256;
int nb_test=1;
+int ctr=0;
const int h=16;
const int h2=h*h;
}
+void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
void rc4key(uchar *key, uchar *sc, int size_DK) {
+void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc) {
+
+
+ uchar *X=new uchar[h2];
+ uchar *fX=new uchar[h2];
+ int ind1,ind2;
+
+
+ for(int a=0;a<h2;a++) {
+ X[a]=Sbox1[a];
+ }
+
+
+ for(int it=0;it<len;it++) {
+ if(enc) {
+ ind1=it*h2;
+ ind2=Pbox[it]*h2;
+ }
+ else {
+ ind2=it*h2;
+ ind1=Pbox[it]*h2;
+ }
+
+
+
+ for(int a=0;a<h2;a++){
+ fX[a]=Sbox1[RM1[X[a]]];
+ }
+
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=fX[a]^RM1[a];
+ }
+
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=Sbox2[fX[a]];
+ }
+
+
+ for(int a=0;a<h2;a++) {
+ seq_out[ind1+a]=fX[a]^seq_in[ind2+a];
+ }
+
+ for(int a=0;a<h2;a++) {
+ RM1[a]=RM1[PboxRM[a]];
+ }
+
+
+
+ }
+
+
+}
+
void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
uchar *X=new uchar[h2];
uchar *fX=new uchar[h2];
-
+ unsigned int *lX=(unsigned int*)X;
+ unsigned int *lseq_in=(unsigned int*)seq_in;
+
+
for(int it=0;it<len;it++) {
int ind1=it*h2;
int ind2=Pbox[it]*h2;
- for(int a=0;a<h2;a++) {
+ for(int a=0;a<h2;a+=1) {
X[a]=seq_in[ind2+a];
+/* X[a+1]=seq_in[ind2+a+1];
+ X[a+2]=seq_in[ind2+a+2];
+ X[a+3]=seq_in[ind2+a+3];
+ /* X[a+4]=seq_in[ind2+a+4];
+ X[a+5]=seq_in[ind2+a+5];
+ X[a+6]=seq_in[ind2+a+6];
+ X[a+7]=seq_in[ind2+a+7];*/
}
- for(int a=0;a<h2;a++){
+ for(int a=0;a<h2;a+=1){
fX[a]=Sbox1[X[a]];
+/* fX[a+1]=Sbox1[X[a+1]];
+ fX[a+2]=Sbox1[X[a+2]];
+ fX[a+3]=Sbox1[X[a+3]];
+ /* fX[a+4]=Sbox1[X[a+4]];
+ fX[a+5]=Sbox1[X[a+5]];
+ fX[a+6]=Sbox1[X[a+6]];
+ fX[a+7]=Sbox1[X[a+7]];*/
}
-
- for(int a=0;a<h2;a++) {
+
+ for(int a=0;a<h2;a+=1) {
fX[a]=fX[a]^RM1[a];
+/* fX[a+1]=fX[a+1]^RM1[a+1];
+ fX[a+2]=fX[a+2]^RM1[a+2];
+ fX[a+3]=fX[a+3]^RM1[a+3];
+ /* fX[a+4]=fX[a+4]^RM1[a+4];
+ fX[a+5]=fX[a+5]^RM1[a+5];
+ fX[a+6]=fX[a+6]^RM1[a+6];
+ fX[a+7]=fX[a+7]^RM1[a+7];*/
}
- for(int a=0;a<h2;a++) {
+ for(int a=0;a<h2;a+=1) {
seq_out[ind1+a]=Sbox2[fX[a]];
+/* seq_out[ind1+a+1]=Sbox2[fX[a+1]];
+ seq_out[ind1+a+2]=Sbox2[fX[a+2]];
+ seq_out[ind1+a+3]=Sbox2[fX[a+3]];
+ /* seq_out[ind1+a+4]=Sbox2[fX[a+4]];
+ seq_out[ind1+a+5]=Sbox2[fX[a+5]];
+ seq_out[ind1+a+6]=Sbox2[fX[a+6]];
+ seq_out[ind1+a+7]=Sbox2[fX[a+7]];*/
}
- for(int a=0;a<h2;a++) {
+ for(int a=0;a<h2;a+=1) {
RM1[a]=RM1[PboxRM[a]];
+/* RM1[a+1]=RM1[PboxRM[a+1]];
+ RM1[a+2]=RM1[PboxRM[a+2]];
+ RM1[a+3]=RM1[PboxRM[a+3]];
+ /* RM1[a+4]=RM1[PboxRM[a+4]];
+ RM1[a+5]=RM1[PboxRM[a+5]];
+ RM1[a+6]=RM1[PboxRM[a+6]];
+ RM1[a+7]=RM1[PboxRM[a+7]];*/
}
-
+
}
+
+
}
}
+int main(int argc, char** argv) {
+
-void Dynamickeygenerationnew(uchar *Secretkey, uchar *counter) {
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
+ }
+
+ printf("nb times %d\n",nb_test);
+ printf("ctr %d\n",ctr);
+
+/*
+ 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);
+*/
+
+
+ int seed=time(NULL);
+ cout<<seed<<endl;
+ srand48(seed);
+
+ uchar Secretkey[key_size];
+
+ uchar counter[key_size];
+
+ for(int i=0;i<key_size;i++) {
+ Secretkey[i]=lrand48()&0xFF;
+ counter[i]=lrand48()&0xFF;
+ }
+
+
int size = 64;
uchar DK[size];
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);
for (int i = 0; i < 256 ; i++) {
-// mix[i]=(int)secret_key.BytePtr()[i]^(int)mycounter.BytePtr()[i];
mix[i]=Secretkey[i]^counter[i];
}
-/* byte digest[64];
- SHA512().CalculateDigest(digest, mix, 256);
-*/
-
cout<<"hash "<<endl;
for (int i = 0; i < 64 ; i++) {
int i;
for(i=0;i<nb_test;i++)
{
- encrypt(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
+// encrypt(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
+ encrypt_ctr(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
}
time+=TimeStop(t);
time=0;
t=TimeStart();
for(i=0;i<nb_test;i++) {
- decrypt(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
+// decrypt(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
+ encrypt_ctr(seq2, seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
}
time+=TimeStop(t);
-
-
-}
-
-
-
-int main(int argc, char** argv) {
- cout << "Hello, World!" << endl;
-
-
-
-
- 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);
-
- int seed=time(NULL);
- cout<<seed<<endl;
- srand48(seed);
-
- uchar Secretkey[key_size];
-
- uchar counter[key_size];
-
- for(int i=0;i<key_size;i++) {
- Secretkey[i]=lrand48()&0xFF;
- counter[i]=lrand48()&0xFF;
- }
-
- Dynamickeygenerationnew(Secretkey, counter);
-
return 0;
}
//gcc pixmap_io.c -c
-//gcc openssl_evp_ctr.c pixmap_io.o -o openssl_evp_ctr -I /usr/include/openssl/ -lcrypto -O3 -std=c99
+//gcc openssl_evp.c pixmap_io.o -o openssl_evp -I /usr/include/openssl/ -lcrypto -O3 -std=c99
#include <openssl/conf.h>
typedef unsigned char uchar;
+int nb_test=1;
+int ctr=0;
double TimeStart()
{
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;
//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);
}
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;
//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.
* real application? :-)
*/
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
+ }
+
+ printf("nb times %d\n",nb_test);
+ printf("ctr %d\n",ctr);
+
+
+
+
/* A 256 bit key */
// unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
unsigned char *key = (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
for(i=0;i<nb_test;i++)
{
ciphertext_len = encrypt (plaintext, size, key, iv,
- ciphertext);
+ ciphertext, ctr);
}
time+=TimeStop(t);
{
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
- decryptedtext);
+ decryptedtext,ctr);
}
time+=TimeStop(t);
+++ /dev/null
-//gcc pixmap_io.c -c
-//gcc openssl_evp_ebc.c pixmap_io.o -o openssl_evp_ebc -I /usr/include/openssl/ -lcrypto -O3 -std=c99
-
-
-#include <openssl/conf.h>
-#include <openssl/evp.h>
-#include <openssl/err.h>
-#include <openssl/ssl.h>
-#include <openssl/bio.h>
-#include <string.h>
-#include <sys/time.h>
-#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<oneD;i++) {
- plaintext[i]=data_R[i];
- plaintext[oneD+i]=data_G[i];
- plaintext[2*oneD+i]=data_B[i];
- }
-
-
-
- uchar *ciphertext = malloc(size);
-
- /* Buffer for the decrypted text */
- uchar *decryptedtext = malloc(size);
-
- int decryptedtext_len, ciphertext_len;
-
-
- int ss=0;
- double time=0;
- double t=TimeStart();
-
-
- /* Encrypt the plaintext */
-
-
- int i;
-
- for(i=0;i<nb_test;i++)
- {
- ciphertext_len = encrypt (plaintext, size, key, iv,
- ciphertext);
- }
-
- time+=TimeStop(t);
-
- printf("Time encrypt %f \n",time);
-
-
- for(int i=0;i<oneD;i++) {
- data_R[i]=ciphertext[i];
- data_G[i]=ciphertext[oneD+i];
- data_B[i]=ciphertext[2*oneD+i];
- }
- store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
-
-
- time=0;
- t=0;
- t=TimeStart();
-
- for(int i=0;i<nb_test;i++)
- {
- /* Decrypt the ciphertext */
- decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
- decryptedtext);
- }
-
- time+=TimeStop(t);
-
- printf("Time decrypt %f\n",time);
-
-
- for(int i=0;i<oneD;i++) {
- data_R[i]=decryptedtext[i];
- data_G[i]=decryptedtext[oneD+i];
- data_B[i]=decryptedtext[2*oneD+i];
- }
- store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
-
-
-
- /* Clean up */
- EVP_cleanup();
- ERR_free_strings();
-
- return 0;
-}