+#include <iostream>
+#include <list>
+#include<math.h>
+#include<stdlib.h>
+#include<stdio.h>
+#include<string.h>
+#include <fstream>
+#include <sys/time.h>
+
+
+
+extern "C" {
+ int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
+ void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
+}
+
+
+//using namespace CryptoPP;
+using namespace std;
+
+
+int key_size=256;
+int nb_test=1;
+int ctr=1;
+
+
+
+
+
+
+
+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 inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
+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) {
+
+ for(int i=0;i<256;i++) {
+ sc[i]=i;
+ }
+
+
+ uchar j0 = 0;
+ for(int i0=0; i0<256; i0++) {
+ j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0 ];
+ sc[j0] = tmp;
+ }
+}
+
+
+
+void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
+
+ //sc=1:len;
+
+
+
+ for (int i=0;i<len;i++) {
+ sc[i]=i;
+ }
+ for (int it = 0; it < rp; it++) {
+ int j0 = 1;
+ for(int i0 = 0; i0<len; i0++) {
+ j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
+ int tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ }
+
+ }
+}
+
+void prga(uchar *sc, int ldata, uchar *r) {
+ uchar i0=0;
+ uchar j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = ((i0+1)&0xFE); //%255);
+ j0 = (j0 + sc[i0])&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[(sc[i0]+sc[j0])&0xFF];
+ }
+}
+
+
+template<int h>
+void myhash(uchar *X, uchar* RM2, uchar *Sbox2) {
+ uchar Y[h];
+ uchar X2[h];
+
+ for(int k=0;k<h;k++) {
+
+
+ for(int a=0;a<h;a+=4) {
+ X2[a]=RM2[a]^X[k*h+a];
+ X2[a+1]=RM2[a+1]^X[k*h+a+1];
+ X2[a+2]=RM2[a+2]^X[k*h+a+2];
+ X2[a+3]=RM2[a+3]^X[k*h+a+3];
+ }
+
+
+ Y[0]=X2[0]^X2[h-1];
+ Y[1]=Y[0]^X2[0];
+ Y[2]=Y[1]^X2[1];
+ Y[3]=Y[2]^X2[2];
+ for(int a=4;a<h;a+=4) {
+ Y[a]=Y[a-1]^X2[a-1];
+ Y[a+1]=Y[a]^X2[a];
+ Y[a+2]=Y[a+1]^X2[a+1];
+ Y[a+3]=Y[a+2]^X2[a+2];
+ }
+
+
+ for(int a=0;a<h;a+=4) {
+ Y[a]=Sbox2[Y[a]];
+ Y[a+1]=Sbox2[Y[a+1]];
+ Y[a+2]=Sbox2[Y[a+2]];
+ Y[a+3]=Sbox2[Y[a+3]];
+ }
+
+
+
+ RM2[h-1]=Y[h-1]^Y[0];
+ RM2[h-2]=RM2[h-1]^Y[h-1];
+ RM2[h-3]=RM2[h-2]^Y[h-2];
+ RM2[h-4]=RM2[h-3]^Y[h-3];
+ for(int a=h-4;a>0;a-=4) {
+ RM2[a-1]=RM2[a]^Y[a];
+ RM2[a-2]=RM2[a-1]^Y[a-1];
+ RM2[a-3]=RM2[a-2]^Y[a-2];
+ RM2[a-4]=RM2[a-3]^Y[a-3];
+ }
+ }
+}
+
+
+
+
+
+
+template<int h2, int h>
+void encrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, uchar *Sbox3, int debug) {
+
+
+ uchar X[h2];
+ uchar Y[h2];
+ uchar fX[h2];
+ uchar gY[h2];
+
+ for(int it=0;it<len;it++) {
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+ for(int a=0;a<h2;a+=4) {
+ X[a]=seq[ind1+a];
+ X[a+1]=seq[ind1+a+1];
+ X[a+2]=seq[ind1+a+2];
+ X[a+3]=seq[ind1+a+3];
+
+ }
+
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ Y[a]=seq[ind2+a];
+ Y[a+1]=seq[ind2+a+1];
+ Y[a+2]=seq[ind2+a+2];
+ Y[a+3]=seq[ind2+a+3];
+
+ }
+
+
+ for(int a=0;a<h2;a+=4){
+ 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]];
+ }
+
+ for(int a=0;a<h2;a+=4){
+ gY[a]=Sbox2[Y[a]];
+ gY[a+1]=Sbox2[Y[a+1]];
+ gY[a+2]=Sbox2[Y[a+2]];
+ gY[a+3]=Sbox2[Y[a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^RM1[a]^Y[a];
+ fX[a+1]=fX[a+1]^RM1[a+1]^Y[a+1];
+ fX[a+2]=fX[a+2]^RM1[a+2]^Y[a+2];
+ fX[a+3]=fX[a+3]^RM1[a+3]^Y[a+3];
+ }
+ for(int a=0;a<h2;a+=4){
+ gY[a]=gY[a]^RM2[a];
+ gY[a+1]=gY[a+1]^RM2[a+1];
+ gY[a+2]=gY[a+2]^RM2[a+2];
+ gY[a+3]=gY[a+3]^RM2[a+3];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind1+a]=Sbox2[fX[a]];
+ seq[ind1+a+1]=Sbox2[fX[a+1]];
+ seq[ind1+a+2]=Sbox2[fX[a+2]];
+ seq[ind1+a+3]=Sbox2[fX[a+3]];
+ }
+ for(int a=0;a<h2;a+=4){
+ seq[ind2+a]=Sbox1[gY[a]];
+ seq[ind2+a+1]=Sbox1[gY[a+1]];
+ seq[ind2+a+2]=Sbox1[gY[a+2]];
+ seq[ind2+a+3]=Sbox1[gY[a+3]];
+ }
+ }
+
+
+ for(int it=0;it<len;it++) {
+ int ind1=it*h2;
+ for(int a=0;a<h2;a+=4) {
+ X[a]=seq[ind1+a];
+ X[a+1]=seq[ind1+a+1];
+ X[a+2]=seq[ind1+a+2];
+ X[a+3]=seq[ind1+a+3];
+
+ }
+ myhash<h>(X, RM3, Sbox3);
+ }
+
+}
+
+
+template<int h2, int h>
+void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uchar *Sbox3, int debug) {
+
+
+ uchar fX[h2];
+ uchar gY[h2];
+ uchar X[h2];
+
+ for(int it=0;it<len;it++) {
+ int ind1=it*h2;
+ for(int a=0;a<h2;a+=4) {
+ X[a]=seq[ind1+a];
+ X[a+1]=seq[ind1+a+1];
+ X[a+2]=seq[ind1+a+2];
+ X[a+3]=seq[ind1+a+3];
+
+ }
+ myhash<h>(X, RM3, Sbox3);
+ }
+
+
+
+
+ for(int it=len-1;it>=0;it--) {
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=Inv_Sbox2[seq[ind1+a]];
+ fX[a+1]=Inv_Sbox2[seq[ind1+a+1]];
+ fX[a+2]=Inv_Sbox2[seq[ind1+a+2]];
+ fX[a+3]=Inv_Sbox2[seq[ind1+a+3]];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ 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];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ gY[a]=Inv_Sbox1[seq[ind2+a]];
+ gY[a+1]=Inv_Sbox1[seq[ind2+a+1]];
+ gY[a+2]=Inv_Sbox1[seq[ind2+a+2]];
+ gY[a+3]=Inv_Sbox1[seq[ind2+a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ gY[a]=Inv_Sbox2[gY[a]^RM2[a]];
+ gY[a+1]=Inv_Sbox2[gY[a+1]^RM2[a+1]];
+ gY[a+2]=Inv_Sbox2[gY[a+2]^RM2[a+2]];
+ gY[a+3]=Inv_Sbox2[gY[a+3]^RM2[a+3]];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^gY[a];
+ fX[a+1]=fX[a+1]^gY[a+1];
+ fX[a+2]=fX[a+2]^gY[a+2];
+ fX[a+3]=fX[a+3]^gY[a+3];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind1+a]=Inv_Sbox1[fX[a]];
+ seq[ind1+a+1]=Inv_Sbox1[fX[a+1]];
+ seq[ind1+a+2]=Inv_Sbox1[fX[a+2]];
+ seq[ind1+a+3]=Inv_Sbox1[fX[a+3]];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind2+a]=gY[a];
+ seq[ind2+a+1]=gY[a+1];
+ seq[ind2+a+2]=gY[a+2];
+ seq[ind2+a+3]=gY[a+3];
+ }
+ }
+
+
+}
+
+
+
+int main(int argc, char** argv) {
+
+
+ int h=32;
+ int lena=0;
+ int size_buf=1;
+ int impb=-1;
+ int tgpb=-1;
+
+
+ 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],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+ if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
+ if(strncmp(argv[i],"impb",4)==0) impb = atoi(&(argv[i][4])); //Use Lena or buffer
+ if(strncmp(argv[i],"tgpb",4)==0) tgpb = atoi(&(argv[i][4])); //Use Lena or buffer
+ }
+
+/* printf("nb times %d\n",nb_test);
+ printf("ctr %d\n",ctr);
+ printf("h %d\n",h);
+ printf("lena %d\n",lena);
+ printf("size_buf %d\n",size_buf);
+*/
+ int h2=h*h;
+
+
+
+ int seed=12;//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 width;
+ int height;
+
+ uchar *data_R, *data_G, *data_B;
+ int imsize;
+ uchar *buffer;
+
+ if(lena==1) {
+ load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+// load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
+ imsize=width*height*3;
+// load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
+ }
+ else {
+ width=height=size_buf;
+ imsize=width*height;
+ buffer=new uchar[imsize];
+ for(int i=0;i<imsize;i++) {
+ buffer[i]=lrand48();
+ }
+ }
+
+
+
+
+
+ uchar* seq= new uchar[imsize];
+ uchar* seq2= new uchar[imsize];
+
+ int oneD=width*height;
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ seq[i]=data_R[i];
+ seq[oneD+i]=data_G[i];
+ seq[2*oneD+i]=data_B[i];
+ }
+ }
+ else {
+ for(int i=0;i<oneD;i++) {
+ seq[i]=buffer[i];
+ }
+ }
+
+
+
+
+
+ int total_len=imsize;
+ int rp=1;
+ int len= total_len/h2;
+
+
+
+ uchar *mix=new uchar[256];
+
+
+
+
+ for (int i = 0; i < 256 ; i++) {
+ mix[i]=Secretkey[i]^counter[i];
+ }
+
+
+// cout<<"hash "<<endl;
+ for (int i = 0; i < 64 ; i++) {
+// DK[i]=digest[i];
+ DK[i]=mix[i];
+ }
+
+
+
+ int *Pbox=new int[len];
+ uchar Sbox1[256];
+ uchar Sbox2[256];
+ uchar Sbox3[256];
+ uchar Inv_Sbox1[256];
+ uchar Inv_Sbox2[256];
+ uchar sc[256];
+ uchar RM1[h2];
+ uchar RM2[h2];
+ uchar RM3[h];
+ uchar RM4[h];
+
+
+
+ double time_encrypt=0;
+ double time_decrypt=0;
+
+
+ double t=TimeStart();
+ rc4key(DK, Sbox1, 8);
+
+
+ rc4key(&DK[8], Sbox2, 8);
+ rc4key(&DK[16], Sbox3, 8);
+
+ rc4key(&DK[24], sc, 16);
+
+
+ prga(sc, h2, RM1);
+ rc4key(&DK[48], sc, 16);
+
+
+ prga(sc, h2, RM2);
+
+
+ rc4keyperm(&DK[64], len, rp, Pbox, 16);
+
+
+ rc4key(&DK[80], sc, 16);
+
+
+ prga(sc, h, RM3);
+
+ //time+=TimeStop(t);
+ //cout<<"Time initializaton "<<time<<endl;
+
+
+
+ for(int i=0;i<h;i++)
+ printf("%d ",RM3[i]);
+ printf("\n");
+
+
+
+
+
+
+
+ for(int i=0;i<h;i++){
+ RM4[i]=RM3[i];
+ }
+
+
+ inverse_tables(Sbox1,256,Inv_Sbox1);
+ inverse_tables(Sbox2,256,Inv_Sbox2);
+
+
+
+
+
+ time_encrypt=0;
+ t=TimeStart();
+
+ int i;
+ switch(h) {
+ case 4:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<4*4,4>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+
+ }
+ break;
+ case 8:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<8*8,8>(seq, len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+ }
+ break;
+ case 16:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<16*16,16>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+ }
+ break;
+ case 32:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<32*32,32>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+ }
+ break;
+ case 64:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<64*64,64>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+ }
+ break;
+ case 128:
+ for(i=0;i<nb_test;i++)
+ {
+ encrypt<128*128,128>(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,Sbox3,0);
+ }
+ break;
+ }
+
+
+
+
+ time_encrypt+=TimeStop(t);
+ //cout<<"Time encrypt "<<
+ cout<<(double)imsize*nb_test/time_encrypt<<"\t";
+
+
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=seq[i];
+ data_G[i]=seq[oneD+i];
+ data_B[i]=seq[2*oneD+i];
+ }
+ store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
+ }
+
+ cout<<"TAG 1"<<endl;
+ for(int i=0;i<h;i++){
+ cout<<(int)RM3[i]<<" ";
+ }
+
+
+ if(impb>=0) {
+ seq[impb]++;
+ }
+
+ if(tgpb>=0 && tgpb<h) {
+ RM4[tgpb]++;
+ }
+
+
+ time_decrypt=0;
+ t=TimeStart();
+ switch(h) {
+ case 4:
+ for(i=0;i<nb_test;i++) {
+ decrypt<4*4,4>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ case 8:
+ for(i=0;i<nb_test;i++) {
+ decrypt<8*8,8>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ case 16:
+ for(i=0;i<nb_test;i++) {
+ decrypt<16*16,16>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ case 32:
+ for(i=0;i<nb_test;i++) {
+ decrypt<32*32,32>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ case 64:
+ for(i=0;i<nb_test;i++) {
+ decrypt<64*64,64>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ case 128:
+ for(i=0;i<nb_test;i++) {
+ decrypt<128*128,128>(seq,len,RM1,RM2,RM4,Pbox,Inv_Sbox1,Inv_Sbox2,Sbox3,0);
+ }
+ break;
+ }
+
+ time_decrypt+=TimeStop(t);
+ //cout<<"Time decrypt "
+ cout<<(double)imsize*nb_test/time_decrypt<<"\t";
+
+
+ cout<<"\nTAG 2"<<endl;
+ for(int i=0;i<h;i++){
+ cout<<(int)RM4[i]<<" ";
+ }
+
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=seq[i];
+ data_G[i]=seq[oneD+i];
+ data_B[i]=seq[2*oneD+i];
+ }
+ store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
+ }
+ else {
+ bool equal=true;
+ for(int i=0;i<imsize;i++) {
+ //cout<<(int)buffer[i]<<endl;
+ if(buffer[i]!=seq[i]) {
+ equal=false;
+ }
+ }
+// cout<<"RESULT CORRECT: "<<equal<<endl;
+ }
+
+
+ cout<<endl;
+ return 0;
+}
+
+
+
+
+
+
+
+
+
+
+