#include <omp.h>
/*#include <cryptopp/hex.h>
-#include <cryptopp/sha.h>
-#include <cryptopp/osrng.h>
-#include <cryptopp/secblock.h>
+ #include <cryptopp/sha.h>
+ #include <cryptopp/osrng.h>
+ #include <cryptopp/secblock.h>
*/
void rotate(uchar *RM1, uchar *RM2, int size, int n)
{
int i;
- for (i = 0; i< size-n; i++)
- RM2[i+n] = RM1[i];
+ for (i = 0; i< size-n; i++)
+ RM2[i+n] = RM1[i];
- for (i = 0; i< n; i++)
- RM2[i] = RM1[size-n-1+i];
+ for (i = 0; i< n; i++)
+ RM2[i] = RM1[size-n-1+i];
}
void encrypt_ctr(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int enc, int num) {
- uchar X[h2];
- uchar fX[h2*num];
-
-
-
-
-
- for(int a=0;a<h2;a++) {
- X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
- }
int loc_len=len/num;
for(int p=0;p<num;p++) {
int id=omp_get_thread_num();
+ uchar fX[h2];
uchar RM2[h2];
+
for(int a=0;a<h2;a++) {
RM2[a]=RM1[id*h2+a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
}
-
+
+ uchar X[h2];
+
+
+ for(int a=0;a<h2;a+=4) {
+ X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
+ X[a+1]=Sbox1[(a+1)&0xFF];
+ X[a+2]=Sbox1[(a+2)&0xFF];
+ X[a+3]=Sbox1[(a+3)&0xFF];
+ }
+
+
int offset=p*loc_len;
for(int it=offset;it<offset+loc_len;it++) {
- int ind1,ind2;
+ int ind1,ind2;
- //cout<<id<<" "<<it<<endl;
+ //cout<<id<<" "<<it<<endl;
- if(enc) {
- ind1=it*h2;
- ind2=Pbox[it]*h2;
- }
- else {
- ind2=it*h2;
- ind1=Pbox[it]*h2;
- }
+ if(enc) {
+ ind1=it*h2;
+ ind2=Pbox[it]*h2;
+ }
+ else {
+ ind2=it*h2;
+ ind1=Pbox[it]*h2;
+ }
- /*for(int a=0;a<h2;a+=4){
- fX[a]=RM1[X[a]];
- fX[a+1]=RM1[X[a+1]];
- fX[a+2]=RM1[X[a+2]];
- fX[a+3]=RM1[X[a+3]];
- }*/
-
- for(int a=0;a<h2;a+=4){
- fX[id*h2+a]=X[a];
- fX[id*h2+a+1]=X[a+1];
- fX[id*h2+a+2]=X[a+2];
- fX[id*h2+a+3]=X[a+3];
- }
-
-
+ for(int a=0;a<h2;a+=4) {
+ X[a]=X[Sbox1[a]];
+ X[a+1]=X[Sbox1[a+1]];
+ X[a+2]=X[Sbox1[a+2]];
+ X[a+3]=X[Sbox1[a+3]];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=X[a]^RM2[a];
+ fX[a+1]=X[a+1]^RM2[a+1];
+ fX[a+2]=X[a+2]^RM2[a+2];
+ fX[a+3]=X[a+3]^RM2[a+3];
+ }
- /* if(it<513) {
- for(int a=0;a<h2;a++)
- printf("%d ",fX[a]);
- printf("\n");
- }*/
-
- *(int*)&fX[id*h2+0]^=it;
-
- /* if(it<513) {
- for(int a=0;a<h2;a++)
- printf("%d ",fX[a]);
- printf("\n");
- }*/
-
-
-
- /*for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=fX[id*h2+a]^RM1[id*h2+a];
- fX[id*h2+a+1]=fX[id*h2+a+1]^RM1[id*h2+a+1];
- fX[id*h2+a+2]=fX[id*h2+a+2]^RM1[id*h2+a+2];
- fX[id*h2+a+3]=fX[id*h2+a+3]^RM1[id*h2+a+3];
- }*/
-
-
-
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=Sbox2[fX[id*h2+a]];
- fX[id*h2+a+1]=Sbox2[fX[id*h2+a+1]];
- fX[id*h2+a+2]=Sbox2[fX[id*h2+a+2]];
- fX[id*h2+a+3]=Sbox2[fX[id*h2+a+3]];
- }
+ /*for(int a=0;a<h2;a+=4) {
+ fX[a]=Sbox2[fX[a]];
+ fX[a+1]=Sbox2[fX[a+1]];
+ fX[a+2]=Sbox2[fX[a+2]];
+ fX[a+3]=Sbox2[fX[a+3]];
+ }*/
// rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
- for(int a=0;a<h2;a+=4) {
- RM2[a]=RM2[PboxRM[a]];
- RM2[a+1]=RM2[PboxRM[a+1]];
- RM2[a+2]=RM2[PboxRM[a+2]];
- RM2[a+3]=RM2[PboxRM[a+3]];
- }
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
- fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
- fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
- fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
- }
+
+
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=fX[id*h2+a]^seq_in[ind2+a];
- fX[id*h2+a+1]=fX[id*h2+a+1]^seq_in[ind2+a+1];
- fX[id*h2+a+2]=fX[id*h2+a+2]^seq_in[ind2+a+2];
- fX[id*h2+a+3]=fX[id*h2+a+3]^seq_in[ind2+a+3];
- }
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^seq_in[ind2+a];
+ fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
+ fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
+ fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
+ }
- for(int a=0;a<h2;a+=4) {
- seq_out[ind1+a]=fX[id*h2+a];
- seq_out[ind1+a+1]=fX[id*h2+a+1];
- seq_out[ind1+a+2]=fX[id*h2+a+2];
- seq_out[ind1+a+3]=fX[id*h2+a+3];
- }
-
- /*for(int a=0;a<h2;a+=4) {
- RM1[id*h2+a]=RM1[id*h2+PboxRM[a]];
- RM1[id*h2+a+1]=RM1[id*h2+PboxRM[a+1]];
- RM1[id*h2+a+2]=RM1[id*h2+PboxRM[a+2]];
- RM1[id*h2+a+3]=RM1[id*h2+PboxRM[a+3]];
- }
- */
+ for(int a=0;a<h2;a+=4) {
+ seq_out[ind1+a]=fX[a];
+ seq_out[ind1+a+1]=fX[a+1];
+ seq_out[ind1+a+2]=fX[a+2];
+ seq_out[ind1+a+3]=fX[a+3];
+ }
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ RM2[a]=RM2[PboxRM[a]];
+ RM2[a+1]=RM2[PboxRM[a+1]];
+ RM2[a+2]=RM2[PboxRM[a+2]];
+ RM2[a+3]=RM2[PboxRM[a+3]];
+ }
+
+
+ /*for(int a=0;a<h2;a+=4) {
+ RM1[id*h2+a]=RM1[id*h2+PboxRM[a]];
+ RM1[id*h2+a+1]=RM1[id*h2+PboxRM[a+1]];
+ RM1[id*h2+a+2]=RM1[id*h2+PboxRM[a+2]];
+ RM1[id*h2+a+3]=RM1[id*h2+PboxRM[a+3]];
+ }
+ */
- }
+ }
}
}
/* uchar *X=new uchar[h2];
- uchar *fX=new uchar[h2];
- unsigned int *lX=(unsigned int*)X;
- unsigned int *lseq_in=(unsigned int*)seq_in;
+ uchar *fX=new uchar[h2];
+ unsigned int *lX=(unsigned int*)X;
+ unsigned int *lseq_in=(unsigned int*)seq_in;
*/
- uchar X[h2*num];
- uchar fX[h2*num];
- uchar RM2[h2*num];
+
// unsigned int *lX=(unsigned int*)X;
// unsigned int *lseq_in=(unsigned int*)seq_in;
+
+
+ int loc_len=len/num;
-#pragma omp parallel for
- for(int it=0;it<len;it++) {
- int ind1=it*h2;
- int ind2=Pbox[it]*h2;
+#pragma omp parallel for
+ for(int p=0;p<num;p++) {
+
int id=omp_get_thread_num();
-
- for(int a=0;a<h2;a+=4) {
- X[id*h2+a]=seq_in[ind2+a];
- X[id*h2+a+1]=seq_in[ind2+a+1];
- X[id*h2+a+2]=seq_in[ind2+a+2];
- X[id*h2+a+3]=seq_in[ind2+a+3];
- }
- for(int a=0;a<h2;a+=4){
- fX[id*h2+a]=Sbox1[X[id*h2+a]];
- fX[id*h2+a+1]=Sbox1[X[id*h2+a+1]];
- fX[id*h2+a+2]=Sbox1[X[id*h2+a+2]];
- fX[id*h2+a+3]=Sbox1[X[id*h2+a+3]];
+ uchar fX[h2];
+
+
+ uchar RM2[h2];
+ for(int a=0;a<h2;a++) {
+ RM2[a]=RM1[id*h2+a];
}
+
+ int offset=p*loc_len;
-/* 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];
- }*/
- rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
- fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
- fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
- fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
- }
+
+ for(int it=offset;it<offset+loc_len;it++) {
- for(int a=0;a<h2;a+=4) {
- seq_out[ind1+a]=Sbox2[fX[id*h2+a]];
- seq_out[ind1+a+1]=Sbox2[fX[id*h2+a+1]];
- seq_out[ind1+a+2]=Sbox2[fX[id*h2+a+2]];
- seq_out[ind1+a+3]=Sbox2[fX[id*h2+a+3]];
- }
+
- /* for(int a=0;a<h2;a+=4) {
- 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]];
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
- }*/
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=seq_in[ind2+a];
+ fX[a+1]=seq_in[ind2+a+1];
+ fX[a+2]=seq_in[ind2+a+2];
+ fX[a+3]=seq_in[ind2+a+3];
+ }
- }
+ for(int a=0;a<h2;a+=4){
+ fX[a]=Sbox1[fX[a]];
+ fX[a+1]=Sbox1[fX[a+1]];
+ fX[a+2]=Sbox1[fX[a+2]];
+ fX[a+3]=Sbox1[fX[a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^RM2[a];
+ fX[a+1]=fX[a+1]^RM2[a+1];
+ fX[a+2]=fX[a+2]^RM2[a+2];
+ fX[a+3]=fX[a+3]^RM2[a+3];
+ }
+
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ 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]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ RM2[a]=RM2[PboxRM[a]];
+ RM2[a+1]=RM2[PboxRM[a+1]];
+ RM2[a+2]=RM2[PboxRM[a+2]];
+ RM2[a+3]=RM2[PboxRM[a+3]];
+ }
+
+
+
+
+ }
+
+ }
}
+
+
template<int h2>
void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Inv_Sbox1, uchar *Inv_Sbox2, int debug, int num) {
/*uchar *fX=new uchar[h2];
- uchar *Inv_Sbox1=new uchar[256];
- uchar *Inv_Sbox2=new uchar[256];
+ uchar *Inv_Sbox1=new uchar[256];
+ uchar *Inv_Sbox2=new uchar[256];
*/
- uchar fX[h2*num];
- uchar RM2[h2*num];
-#pragma omp parallel for
- for(int it=0;it<len;it++) {
- int ind1=it*h2;
- int ind2=Pbox[it]*h2;
+
+
+
+ int loc_len=len/num;
+
+#pragma omp parallel for
+ for(int p=0;p<num;p++) {
+
int id=omp_get_thread_num();
+ uchar RM2[h2];
+ for(int a=0;a<h2;a++) {
+ RM2[a]=RM1[id*h2+a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
+ }
+ uchar fX[h2];
+
+ int offset=p*loc_len;
+
+ for(int it=offset;it<offset+loc_len;it++) {
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=seq_in[ind1+a];
- fX[id*h2+a+1]=seq_in[ind1+a+1];
- fX[id*h2+a+2]=seq_in[ind1+a+2];
- fX[id*h2+a+3]=seq_in[ind1+a+3];
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=seq_in[ind1+a];
+ fX[a+1]=seq_in[ind1+a+1];
+ fX[a+2]=seq_in[ind1+a+2];
+ fX[a+3]=seq_in[ind1+a+3];
- }
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=Inv_Sbox2[fX[id*h2+a]];
- fX[id*h2+a+1]=Inv_Sbox2[fX[id*h2+a+1]];
- fX[id*h2+a+2]=Inv_Sbox2[fX[id*h2+a+2]];
- fX[id*h2+a+3]=Inv_Sbox2[fX[id*h2+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) {
+ fX[a]=Inv_Sbox2[fX[a]];
+ fX[a+1]=Inv_Sbox2[fX[a+1]];
+ fX[a+2]=Inv_Sbox2[fX[a+2]];
+ fX[a+3]=Inv_Sbox2[fX[a+3]];
}
- for(int a=0;a<h2;a+=4) {
- 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]];
- }*/
- rotate(RM1, &RM2[id*h2], h2, Pbox[it]%h2);
- for(int a=0;a<h2;a+=4) {
- fX[id*h2+a]=fX[id*h2+a]^RM2[id*h2+a];
- fX[id*h2+a+1]=fX[id*h2+a+1]^RM2[id*h2+a+1];
- fX[id*h2+a+2]=fX[id*h2+a+2]^RM2[id*h2+a+2];
- fX[id*h2+a+3]=fX[id*h2+a+3]^RM2[id*h2+a+3];
- }
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^RM2[a];
+ fX[a+1]=fX[a+1]^RM2[a+1];
+ fX[a+2]=fX[a+2]^RM2[a+2];
+ fX[a+3]=fX[a+3]^RM2[a+3];
+ }
- for(int a=0;a<h2;a+=4) {
- seq_out[ind2+a]=Inv_Sbox1[fX[id*h2+a]];
- seq_out[ind2+a+1]=Inv_Sbox1[fX[id*h2+a+1]];
- seq_out[ind2+a+2]=Inv_Sbox1[fX[id*h2+a+2]];
- seq_out[ind2+a+3]=Inv_Sbox1[fX[id*h2+a+3]];
- }
+ for(int a=0;a<h2;a+=4) {
+ seq_out[ind2+a]=Inv_Sbox1[fX[a]];
+ seq_out[ind2+a+1]=Inv_Sbox1[fX[a+1]];
+ seq_out[ind2+a+2]=Inv_Sbox1[fX[a+2]];
+ seq_out[ind2+a+3]=Inv_Sbox1[fX[a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ RM2[a]=RM2[PboxRM[a]];
+ RM2[a+1]=RM2[PboxRM[a+1]];
+ RM2[a+2]=RM2[PboxRM[a+2]];
+ RM2[a+3]=RM2[PboxRM[a+3]];
+ }
- }
+ }
+ }
}
+
int main(int argc, char** argv) {
}
/* 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);
+ 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 num=omp_get_max_threads();
cout<<"num "<<num<<endl;
- uchar RM1[num*(h * h)];
- uchar RM2[num*(h * h)];
- prga(sc, num*(h * h), RM1);
- for(int i=0;i<num*h2;i++) {
- RM2[i]=RM1[i];
- }
- uchar keyp[16];
- for (int i = 48; i < 64; i++)
- keyp[i-48] = DK[i];
+
+ uchar RM1[num*(h * h)];
+ uchar RM2[num*(h * h)];
+ /*for(int i=0;i<num;i++) {
+
+ rc4key(&DK[48+i*16], sc, 16);
+ prga(sc, h2, &RM1[h2*i]);
+ for(int a=0;a<h2;a++) {
+ cout<<(int)RM1[h2*i+a]<<" ";
+ }
+ cout<<endl<<endl;
+ }*/
+
+ rc4key(&DK[48], sc, 16);
+ prga(sc, h2*num, RM1);
+
+ rc4key(&DK[64], sc, 16);
+ prga(sc, h2, RM2);
+
+
+
+
+
// cout<<len<<endl;
int *Pbox=new int[len];
int *PboxRM=new int[h2];
- rc4keyperm(keyp, len, rp, Pbox, 16);
+ rc4keyperm(&DK[48+16*num], len, rp, Pbox, 16);
-// printf("len %d\n",len);
- for(int i=0;i<len;i++) {
-// printf("%d \n",Pbox[i]);
- }
-
+ rc4keyperm(RM2, h2, rp, PboxRM, h2);
+ for(int i=0;i<num*h2;i++) {
+ RM2[i]=RM1[i];
+ }
double time=0;
double t=TimeStart();