#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
-
+#include<string.h>
typedef unsigned char uchar;
}
}
+#define ROR64(x,r) (((x)>>(r))|((x)<<(64-(r))))
+#define ROL64(x,r) (((x)<<(r))|((x)>>(64-(r))))
+#define R(x,y,k) (x=ROR64(x,18), x+=y, x^=k, y=ROL64(y,13), y^=x)
-void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_t *Val, uchar *Sbox1, uchar *Sbox2, int *Pbox, int *Pbox2, uchar *DK, int delta) {
- int update=0;
- for(int nb=0;nb<nb_bloc;nb++) {
+void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_t *Val, uint64_t* Val2,uchar *Sbox1, uchar *Sbox2, uchar * Sbox3, uchar *Sbox4, int *Pbox, int *Pbox2, int *Pbox3, int *Pbox4, uchar *DK, int delta) {
+ int update=0;
+
+
+ for(int nb=0;nb<nb_bloc;nb++) {
+
+ // #pragma omp parallel
for(int j=0;j<bufsize;j++) {
//Val[j]=splitmix64_stateless(Val[j])^Val[Pbox[j]];
//Val[j]=xorshift64(Val[j])^Val[Pbox[j]]; //fail
- Val[j]=xorshift64(Val[j])^Val[Pbox[j]]^Val[Pbox2[j]];
+// Val[j]=xorshift64(Val[j])^Val[Pbox[j]]^Val[Pbox2[j]];
+
+ Val2[j]=xorshift64(Val[j])^Val[Pbox[j]]^Val[Pbox2[j]];
//Val[j]=xoroshiro128plus(&xoro[j])^Val[Pbox[j]];
//Val[j]=jsf(&ctx[j])^Val[Pbox[j]]; //good
//Val[j]=sfc(&sfcd[j])^Val[Pbox[j]]; //good
}
+
+
for(int j=0;j<bufsize;j++) {
+ Val[j]=ROR64(Val2[j],Pbox[j]&63);
cipher[nb*bufsize+j]=Val[j]^plain[nb*bufsize+j];
}
if(update==delta) {
update=0;
- uchar *ptr=(uchar*)Val;
+ /* uchar *ptr=(uchar*)Val;
for(int j=0;j<bufsize*8;j++)
ptr[j]^=Sbox2[Sbox1[ptr[j]+DK[j&63]]];
- rc4keyperm(ptr, bufsize, 1, Pbox, 64);
- //only for xorshift
- rc4keyperm(&ptr[32], bufsize, 1, Pbox2, 64);
-
+ */
+
+ for(int j=0;j<bufsize;j++) {
+ Val[j]=R(Val[j],Val[Pbox[j]],Val[Pbox2[j]]);
+ }
+
+ /*
+ for(int j=0;j<256;j++)
+ Sbox1[j]=Sbox3[Sbox1[j]];
+
+ for(int j=0;j<256;j++)
+ Sbox2[j]=Sbox4[Sbox2[j]];
+
+ for(int j=0;j<256;j++)
+ Sbox3[j]=Sbox4[Sbox3[j]];
+ */
+
+ for(int j=0;j<bufsize;j++)
+ Pbox[j]=Pbox3[Pbox[j]];
+
+ for(int j=0;j<bufsize;j++)
+ Pbox2[j]=Pbox4[Pbox2[j]];
+
+ for(int j=0;j<bufsize;j++)
+ Pbox3[j]=Pbox4[Pbox3[j]];
+
+
- rc4key(ptr, Sbox1, 64);
- rc4key(&ptr[64], Sbox2, 64);
}
else
update++;
}
-int main() {
- printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
+int main(int argc, char **argv) {
+// printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
+ uint nb_test=1;
+
int width;
int height;
uchar *data_R, *data_G, *data_B;
int imsize;
uchar *buffer;
- int size_buf=512;
+ int size_buf=128;
+
+ int lena=0;
+ int h=128;
+ 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
+ }
+
+
+
+
+
- int lena=1;
if(lena==1) {
load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
uchar Sbox1[256];
uchar Sbox2[256];
+ uchar Sbox3[256];
+ uchar Sbox4[256];
rc4key(DK, Sbox1, 8);
rc4key(&DK[8], Sbox2, 8);
+ rc4key(&DK[16], Sbox3, 8);
+ rc4key(&DK[24], Sbox4, 8);
- const int bufsize=128;
- int Pbox[bufsize];
- int Pbox2[bufsize];
- rc4keyperm(&DK[16], bufsize, 1, Pbox, 16);
- rc4keyperm(&DK[32], bufsize, 1, Pbox2, 16);
+ int Pbox[h];
+ int Pbox2[h];
+ int Pbox3[h];
+ int Pbox4[h];
+ rc4keyperm(&DK[16], h, 1, Pbox, 16);
+ rc4keyperm(&DK[32], h, 1, Pbox2, 16);
+ rc4keyperm(&DK[8], h, 1, Pbox3, 16);
+ rc4keyperm(&DK[48], h, 1, Pbox4, 16);
// uint64_t plain[bufsize];
// uint64_t cipher[bufsize];
- ulong2 xoro[bufsize];
-
- ranctx ctx[bufsize];
- sfcctx sfcd[bufsize];
-
- uint64_t Val[bufsize];
- for(int i=0;i<bufsize;i++) {
+ uint64_t Val[h];
+ uint64_t Val2[h];
+ for(int i=0;i<h;i++) {
Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
}
- int delta=8;
+ int delta=64;
- uint nb_test=100;
+
double t=TimeStart();
- int nb_bloc=imsize/(bufsize*8); //8 because we use 64bits numbers
+ int nb_bloc=imsize/(h*8); //8 because we use 64bits numbers
for(uint iter=0;iter<nb_test;iter++) {
- scprng(SEQ, SEQ2, bufsize, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
+ scprng(SEQ, SEQ2, h, nb_bloc, Val,Val2,Sbox1, Sbox2, Sbox3, Sbox4, Pbox, Pbox2, Pbox3, Pbox4, DK, delta);
}
double time=TimeStop(t);
- printf("time %e\n",nb_test*nb_bloc*bufsize*8/time);
+ printf("%e ",nb_test*nb_bloc*h*8/time);
if(lena) {
for(int i=0;i<oneD;i++) {
}
+
//reinit of parameters
+
rc4key(DK, Sbox1, 8);
rc4key(&DK[8], Sbox2, 8);
+ rc4key(&DK[16], Sbox3, 8);
+ rc4key(&DK[24], Sbox4, 8);
- rc4keyperm(&DK[16], bufsize, 1, Pbox, 16);
- rc4keyperm(&DK[32], bufsize, 1, Pbox2, 16);
- for(int i=0;i<bufsize;i++) {
+ rc4keyperm(&DK[16], h, 1, Pbox, 16);
+ rc4keyperm(&DK[32], h, 1, Pbox2, 16);
+ rc4keyperm(&DK[8], h, 1, Pbox3, 16);
+ rc4keyperm(&DK[48], h, 1, Pbox4, 16);
+
+
+ for(int i=0;i<h;i++) {
Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
}
t=TimeStart();
+
+
for(uint iter=0;iter<nb_test;iter++) {
- scprng(SEQ2, SEQ, bufsize, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
+ scprng(SEQ2, SEQ, h, nb_bloc, Val,Val2,Sbox1, Sbox2, Sbox3, Sbox4, Pbox, Pbox2, Pbox3, Pbox4, DK, delta);
}
time=TimeStop(t);
- printf("time %e\n",nb_test*nb_bloc*bufsize*8/time);
+ printf("%e\n",nb_test*nb_bloc*h*8/time);
if(lena) {
for(int i=0;i<oneD;i++) {