2 //#include "./printf.h"
6 //#include <ESP8266WiFi.h>
8 const int size_mesg=16;
13 const int sleepTimeS = 10;
21 const int len=size_mesg/h2;
26 typedef unsigned int uint;
36 unsigned long int x, y;
39 typedef struct ulong2 ulong2;
40 typedef unsigned long int ulong;
44 uint xorshift32(const uint t)
46 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
57 uint pcg32_random_r(ulong2* rng)
59 // pcg32_random_t *rng=(pcg32_random_t*)rng2;
60 ulong oldstate = rng->x;
61 // Advance internal state
62 rng->x = oldstate * 6364136223846793005ULL + (rng->y|1);
63 // Calculate output function (XSH RR), uses old state for max ILP
64 uint xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
65 uint rot = oldstate >> 59u;
66 return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
72 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
74 for(int i=0;i<size_tab;i++) {
75 inv_perm_tabs[tab[i]] = i;
80 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
82 for(int i=0;i<size_tab;i++) {
83 inv_perm_tabs[tab[i]] = i;
90 void rc4key(uchar *key, uchar *sc, int size_DK) {
92 for(int i=0;i<256;i++) {
98 for(int i0=0; i0<256; i0++) {
99 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
108 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
114 for (int i=0;i<len;i++) {
117 for (int it = 0; it < rp; it++) {
119 for(int i0 = 0; i0<len; i0++) {
120 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
129 void prga(uchar *sc, int ldata, uchar *r) {
133 for (int it=0; it<ldata; it++) {
134 i0 = ((i0+1)&0xFE); //%255);
135 j0 = (j0 + sc[i0])&0xFF;
139 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
145 void encrypt_ctr(const uchar* seq_in, uchar *seq_out, int len,uchar* RM1,const int *Pbox, const int *PboxRM, const uchar *Sbox1, const uchar *Sbox2, int enc) {
147 //printArray(seq_out,16);
154 for(int a=0;a<h2;a++) {
155 X[a]=Sbox1[a]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
159 for(int it=0;it<len;it++) {
170 for(int a=0;a<h2;a+=4) {
172 X[a+1]=X[Sbox1[a+1]];
173 X[a+2]=X[Sbox1[a+2]];
174 X[a+3]=X[Sbox1[a+3]];
177 for(int a=0;a<h2;a+=4){
186 //printf("fx %d\n",fX[0] );
189 for(int a=0;a<h2;a+=16) {
193 *(int*)&fX[a+12]^=it;
200 for(int a=0;a<h2;a+=4) {
202 fX[a+1]=fX[a+1]^RM1[a+1];
203 fX[a+2]=fX[a+2]^RM1[a+2];
204 fX[a+3]=fX[a+3]^RM1[a+3];
208 for(int a=0;a<h2;a+=4) {
210 fX[a+1]=Sbox2[fX[a+1]];
211 fX[a+2]=Sbox2[fX[a+2]];
212 fX[a+3]=Sbox2[fX[a+3]];
218 for(int a=0;a<h2;a+=4) {
219 fX[a]=fX[a]^seq_in[ind2+a];
220 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
221 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
222 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
227 for(int a=0;a<h2;a+=4) {
228 seq_out[ind1+a]=fX[a];
229 seq_out[ind1+a+1]=fX[a+1];
230 seq_out[ind1+a+2]=fX[a+2];
231 seq_out[ind1+a+3]=fX[a+3];
234 for(int a=0;a<h2;a+=4) {
235 RM1[a]=RM1[PboxRM[a]];
236 RM1[a+1]=RM1[PboxRM[a+1]];
237 RM1[a+2]=RM1[PboxRM[a+2]];
238 RM1[a+3]=RM1[PboxRM[a+3]];
245 // printf("size mes %d\n",size_mesg);
246 // printf("len %d\n",len);
249 //printArray(seq_out,16);
255 void printArray(byte *mes, int n) {
256 for (byte i = 0; i < n; i++) {
257 Serial.print(mes[i]);
266 Serial.begin (57600) ;
269 printf("\n===testng mode\n") ;
283 for(byte i=0;i<64;i++) {
286 rc4key(DK, Sbox1, 8);
289 rc4key(&DK[8], Sbox2, 8);
291 rc4key(&DK[16], sc, 16);
297 rc4keyperm(&DK[32], len, rp, Pbox, 16);
300 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
302 for(int i=0;i<h2;i++){
307 // WiFi.mode( WIFI_OFF );
309 Serial.println("END OF INIT");
314 // otfly_test256 () ;
322 // Serial.println("ESP8266 in sleep mode");
323 // ESP.deepSleep(sleepTimeS * 1000000, WAKE_RF_DISABLED );
325 // Serial.println("ESP8266 in normal mode");
328 void prekey (int bits)
332 unsigned long msms = micros ();
336 for(int i=0;i<100;i++) {
338 //res=xorshift32(res);
339 //res=pcg32_random_r(&pcg);
342 unsigned long res2=micros() - msms;
343 Serial.print("rand");
344 Serial.println(res2);
349 byte plain[size_mesg];
350 byte cipher [size_mesg] ;
351 byte check [size_mesg] ;
354 for(int i=0;i<size_mesg;i++) {
355 plain[i]=random(255);
361 for(int i=0;i<h2;i++){
368 printArray(Sbox1,16);
369 printArray(Sbox2,16);
370 //printArray(Pbox,16);
373 unsigned long ms1 = micros ();
374 encrypt_ctr(plain, cipher,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
375 Serial.print("ONE Encryption took: ");
376 Serial.println(micros() - ms1);
379 encrypt_ctr(cipher, check,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
380 Serial.print("ONE Decryption took: ");
381 Serial.println(micros() - ms1);
383 printf("\n\nPLAIN :");
384 printArray(plain,16);
386 printArray(cipher,16);
388 printArray(check,16);
391 for(int i=0;i<size_mesg;i++) {
393 if(check[i]!=plain[i]) {
397 Serial.print("CHECK ");
398 Serial.println(equal);