2 //#include "./printf.h"
3 //#include <ESP8266WiFi.h>
6 const int size_mesg=256; // size of plain-message
7 const int sleepTimeS = 10;
8 const int h=4; // size of each block
9 const int nblocks=size_mesg/h; // number of blocks
18 //--------------------------------------------------------------------
19 //Function: KSA algorithm of RC4
20 //--------------------------------------------------------------------
21 void rc4key(uchar *key, uchar *sc, int size_DK) {
23 for(int i=0;i<256;i++) {
29 for(int i0=0; i0<256; i0++) {
30 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
38 //--------------------------------------------------------------------
39 //Function: Modified KSA of RC4
40 //--------------------------------------------------------------------
41 void rc4keyperm(uchar *key,int nblocks, int rp,int *sc, int size_DK) {
44 for (int i=0;i<nblocks;i++) {
47 for (int it = 0; it < rp; it++) {
49 for(int i0 = 0; i0<nblocks; i0++) {
50 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% nblocks;
59 //--------------------------------------------------------------------
60 //Function: PRGA of RC4
61 //--------------------------------------------------------------------
62 void prga(uchar *sc, int ldata, uchar *r) {
66 for (int it=0; it<ldata; it++) {
67 i0 = ((i0+1)&0xFE); //%255);
68 j0 = (j0 + sc[i0])&0xFF;
72 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
76 //--------------------------------------------------------------------
77 //Function: Binary diffusion operation (h to h)
78 //--------------------------------------------------------------------
79 void diff(uchar *Y, uchar *X, int h) {
82 Y[0] = X[1]^X[2]^X[3];
83 Y[1] = X[0]^X[2]^X[3];
84 Y[2] = X[0]^X[1]^X[3];
85 Y[3] = X[0]^X[1]^X[2];
88 Y[0] = X[0]^X[2]^X[3]^X[5]^X[6]^X[7];
89 Y[1] = X[0]^X[1]^X[3]^X[4]^X[6]^X[7];
90 Y[2] = X[0]^X[1]^X[2]^X[4]^X[5]^X[7];
91 Y[3] = X[1]^X[2]^X[3]^X[4]^X[5]^X[6];
92 Y[4] = X[0]^X[1]^X[5]^X[6]^X[7];
93 Y[5] = X[1]^X[2]^X[4]^X[6]^X[7];
94 Y[6] = X[2]^X[3]^X[4]^X[5]^X[7];
95 Y[7] = X[0]^X[3]^X[4]^X[5]^X[6];
99 Y[0] = X[3] ^ X[4] ^ X[6] ^ X[8] ^ X[9] ^ X[13] ^ X[14];
100 Y[1] = X[2] ^ X[5] ^ X[7] ^ X[8] ^ X[9] ^ X[12] ^ X[15];
101 Y[2] = X[1] ^ X[4] ^ X[6] ^ X[10] ^ X[11] ^ X[12] ^ X[15];
102 Y[3] = X[0] ^ X[5] ^ X[7] ^ X[10] ^ X[11] ^ X[13] ^ X[14];
103 Y[4] = X[0] ^ X[2] ^ X[5] ^ X[8] ^ X[11] ^ X[14] ^ X[15];
104 Y[5] = X[1] ^ X[3] ^ X[4] ^ X[9] ^ X[10] ^ X[14] ^ X[15];
105 Y[6] = X[0] ^ X[2] ^ X[7] ^ X[9] ^ X[10] ^ X[12] ^ X[13];
106 Y[7] = X[1] ^ X[3] ^ X[6] ^ X[8] ^ X[11] ^ X[12] ^ X[13];
107 Y[8] = X[0] ^ X[1] ^ X[4] ^ X[7] ^ X[10] ^ X[13] ^ X[15];
108 Y[9] = X[0] ^ X[1] ^ X[5] ^ X[6] ^ X[11] ^ X[12] ^ X[14];
109 Y[10] = X[2] ^ X[3] ^ X[5] ^ X[6] ^ X[8] ^ X[13] ^ X[15];
110 Y[11] = X[2] ^ X[3] ^ X[4] ^ X[7] ^ X[9] ^ X[12] ^ X[14];
111 Y[12] = X[1] ^ X[2] ^ X[6] ^ X[7] ^ X[9] ^ X[11] ^ X[12];
112 Y[13] = X[0] ^ X[3] ^ X[6] ^ X[7] ^ X[8] ^ X[10] ^ X[13];
113 Y[14] = X[0] ^ X[3] ^ X[4] ^ X[5] ^ X[9] ^ X[11] ^ X[14];
114 Y[15] = X[1] ^ X[2] ^ X[4] ^ X[5] ^ X[8] ^ X[10] ^ X[15];
119 Y[0]=X[0]^X[1]^X[2]^X[3]^X[4]^X[7]^X[8]^X[10]^X[12]^X[15]^X[16]^X[17]^X[18]^X[20]^X[21]^X[24]^X[25]^X[28]^X[30];
120 Y[1]=X[0]^ X[1]^X[2]^X[3]^X[5]^X[6]^X[9]^X[11]^X[13]^X[14]^X[16]^X[17]^X[19]^X[20]^X[21]^ X[24]^X[25]^X[29]^X[31];
121 Y[2]=X[0]^X[1]^X[2]^X[3]^X[5]^X[6]^X[8]^X[10]^X[13]^X[14]^X[16]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[28]^X[30];
122 Y[3]=X[0]^X[1]^X[2]^X[3]^X[4]^X[7]^X[9]^X[11]^X[12]^X[15]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[29]^X[31];
123 Y[4]=X[0]^X[3]^X[5]^X[6]^X[7]^X[10]^X[11]^ X[12]^X[13]^X[14]^ X[15]^X[16]^X[19]^X[21]^X[23]^ X[25]^X[27]^X[30]^X[31];
124 Y[5]=X[1]^X[2]^X[4]^X[6]^X[7]^X[10]^X[11]^X[12]^X[13]^X[14]^X[16 ]^X[17]^X[18]^X[20]^X[22]^X[24]^X[26]^X[30]^X[31];
125 Y[6]=X[1]^X[2]^X[4]^X[5]^X[7]^X[8]^X[9]^X[12]^X[13]^X[14]^X[15]^ X[17]^X[18]^X[21]^X[23]^X[25]^X[27]^X[28]^X[29];
126 Y[7]=X[0]^X[3]^X[4]^X[5]^X[6]^X[9 ]^X[9]^X[12]^X[13]^X[14]^X[15]^X[16]^X[19]^X[20]^X[22]^X[24]^X[26]^X[28]^X[29];
127 Y[8]=X[0]^X[2]^X[6]^X[7]^X[8]^X[10]^X[11]^X[14]^X[15]^X[16]^X[18]^X[21]^X[22]^X[25]^X[26];
128 Y[9]=X[1]^ X[3]^X[6]^X[7]^X[9]^X[10]^X[11]^X[14]^X[15]^X[17]^X[19]^X[20]^X[23]^X[24]^X[27];
129 Y[10]=X[0]^X[2]^X[4]^X[5]^X[8]^X[9]^X[10]^X[12]^X[13]^X[16]^X[18]^X[20]^X[23]^ X[24]^X[27];
130 Y[11]=X[1]^X[3]^X[4]^X[5]^X[8]^X[9]^X[11]^X[12]^X[13]^X[17]^X[19]^X[21]^X[22]^X[25]^X[26];
131 Y[12]=X[0]^X[3]^X[4]^X[5]^X[6]^X[7]^X[10]^X[11]^X[13]^X[14]^X[15]^X[16]^X[19]^X[21]^X[23]^X[25]^X[27]^X[30]^X[31];
132 Y[13]=X[1]^X[2]^X[4]^X[5]^X[6]^X[7]^X[10]^X[11]^X[12]^X[14]^X[15]^X[17]^ X[18]^X[20]^X[22]^X[24]^X[26]^X[30]^X[31];
133 Y[14]=X[1]^X[2]^X[4]^X[5]^X[6]^X[7]^X[8]^X[9]^X[12]^X[13]^X[15]^X[17]^X[18]^X[21]^X[23]^X[25]^X[27]^X[28]^X[29];
134 Y[15]=X[0]^X[3]^X[4]^X[5]^X[6]^X[7]^X[8]^X[9]^X[12]^X[13]^X[14]^X[16]^X[19]^X[20]^X[22]^ X[24]^X[26]^X[28]^X[29];
135 Y[16]=X[0]^X[1]^X[2]^X[4]^X[8 ]^X[8]^X[10]^X[13 ]^X[15]^X[16]^X[17]^X[18]^X[19]^X[20]^X[21]^X[24]^X[25]^X[28]^X[30];
136 Y[17]=X[0]^X[1]^X[3]^X[5]^X[6]^X[9]^X[11]^X[13]^X[14]^X[16]^X[17]^X[18]^X[19]^X[20]^X[21]^X[24]^X[25]^X[29]^X[31];
137 Y[18]=X[0]^X[2]^X[3]^X[5]^X[6]^X[8]^X[10]^X[13]^X[14]^X[16]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[27]^X[28]^X[30];
138 Y[19]=X[1]^X[2]^X[3]^X[4]^X[7]^X[9]^X[11]^X[12]^X[15]^X[16]^X[17]^X[18]^X[19]^X[22]^X[23]^X[26]^X[28 ]^X[29]^X[31];
139 Y[20]=X[0]^X[1]^X[5]^X[7]^X[10 ]^X[10]^X[13]^X[15]^X[16]^X[17]^X[20]^X[21]^X[23]^X[29]^X[30];
140 Y[21]=X[0]^X[1]^X[4]^X[6]^X[8]^X[11]^X[12]^X[14]^X[16]^X[17]^X[20]^X[21]^X[22]^X[28]^X[31];
141 Y[22]=X[2]^X[3]^X[5]^X[7]^X[8]^X[11]^X[13]^X[15]^X[18]^X[19]^X[21]^X[22]^X[23]^X[28]^X[31];
142 Y[23]=X[2]^X[3]^X[4]^X[6]^X[9]^X[10]^X[12]^X[14]^ X[18]^X[19]^X[20]^X[22]^X[23]^X[29]^X[30];
143 Y[24]=X[0]^X[1]^X[5]^X[7]^X[9]^X[10]^X[13]^X[15]^X[16]^X[17]^X[24]^X[25]^X[27]^X[29]^X[30];
144 Y[25]=X[0]^X[1]^X[4]^X[6]^X[8]^X[11]^X[12]^X[14]^X[16]^X[17]^X[24]^X[25]^X[26]^X[28]^X[31];
145 Y[26]=X[2]^X[3]^X[5]^X[7]^X[8]^X[11]^X[13]^X[15]^X[18]^X[19]^X[25]^X[26]^X[27]^X[28]^ X[31];
146 Y[27]=X[2]^X[3]^X[4]^X[6]^X[9]^X[10]^X[12]^X[14]^X[18]^X[19]^X[24]^X[26]^X[27]^X[29]^X[30];
147 Y[28]=X[0]^X[2]^X[6]^X[7]^X[14]^X[15]^X[16]^X[18]^X[21]^X[22]^X[25]^X[26]^X[28]^X[30]^X[31];
148 Y[29]=X[2]^X[3]^X[6]^X[7]^X[14]^X[15]^X[17]^X[19]^X[20]^X[23]^X[24]^X[27]^X[29]^X[30]^X[31];
149 Y[30]=X[1]^X[2]^X[4]^X[5]^X[12]^X[13]^X[16]^X[18]^X[20]^X[23]^X[24]^X[27]^X[28]^X[29]^X[30];
150 Y[31]=X[2]^X[3]^X[4]^X[5]^X[12]^X[13]^X[17]^X[19]^X[21]^X[22]^X[25]^X[26]^X[28]^X[29]^X[31];
156 //--------------------------------------------------------------------
157 //Function: Proposed hash function
158 //--------------------------------------------------------------------
159 void hash_DSD_BIN(uchar* seq_in, uchar* Hashval,int nblocks, int *PboxRM, uchar *Sbox1, int h) {
160 // Goal: Calculate the hash value
161 // Output: RM (hash value)
166 for(int it=0;it<nblocks;it++) {
170 // Mix with previous hash val
172 for(int a=0;a<h;a+=4) {
173 fX[a]=Hashval[a]^seq_in[ind2+a];
174 fX[a+1]=Hashval[a+1]^seq_in[ind2+a+1];
175 fX[a+2]=Hashval[a+2]^seq_in[ind2+a+2];
176 fX[a+3]=Hashval[a+3]^seq_in[ind2+a+3];
179 // First Diffusion Operation
183 // Substitution Operation
184 for(int a=0;a<h;a+=4) {
185 fX[a]=Sbox1[fX2[a]]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
186 fX[a+1]=Sbox1[fX2[a+1]];
187 fX[a+2]=Sbox1[fX2[a+2]];
188 fX[a+3]=Sbox1[fX2[a+3]];
191 // Second Diffusion Operation
195 // update RM and mix it with hashed block
196 for(int a=0;a<h;a+=4) {
197 Hashval[a]=fX2[a]^Hashval[PboxRM[a]];
198 Hashval[a+1]=fX2[a+1]^Hashval[PboxRM[a+1]];
199 Hashval[a+2]=fX2[a+2]^Hashval[PboxRM[a+2]];
200 Hashval[a+3]=fX2[a+3]^Hashval[PboxRM[a+3]];
210 //--------------------------------------------------------------------
211 //Function: to get the mean of a vector
212 //--------------------------------------------------------------------
213 float mean(int m, int a[]) {
217 return((float)sum/m);
221 //--------------------------------------------------------------------
222 //Function: to print a vector of bytes
223 //--------------------------------------------------------------------
224 void printArray(byte *mes, int n) {
225 for (byte i = 0; i < n; i++) {
226 Serial.print(mes[i]);
232 //--------------------------------------------------------------------
233 //Function: Construction of initial layer
234 //--------------------------------------------------------------------
238 Serial.begin (57600) ;
241 printf("\n===testng mode\n") ;
246 for(byte i=0;i<64;i++) {
251 rc4key(DK, Sbox1, 8);
252 // Creation of initial hash value (keyed hash), equals to zero if it is unkeyed
253 rc4key(&DK[8], sc, 8);
254 prga(sc, h, Hashval);
256 rc4keyperm(&DK[16], h, rp, PboxRM, 8);
257 Serial.println("END OF INIT");
259 //rc4key(&DK[16], sc, 8);
260 //prga(sc, h, Hashval2);
264 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
273 // Serial.println("ESP8266 in sleep mode");
274 // ESP.deepSleep(sleepTimeS * 1000000, WAKE_RF_DISABLED );
276 // Serial.println("ESP8266 in normal mode");
279 //--------------------------------------------------------------------
281 //--------------------------------------------------------------------
282 //mean(float m, int a[])
283 void prekey (int bits)
286 byte plain[size_mesg];
287 byte plain2[size_mesg];
288 //byte hashValue [h] ;
289 // byte hashValue [h]
293 int timecompute[itk];
296 for (int it=0;it<itk;it++){
297 //%%%%%%%%%%%%% Initialization of seed%%%%%%%%%%%%%%
298 int seed=random(2^32);
302 //%%%%%%%%%%%%% Generation of plain message %%%%%%%%%%%%%%
303 for(int i=0;i<size_mesg;i++) {
304 plain[i]=random(255);
306 //printf("\n\noriginal key :");
307 //printArray(Hashval,h);
310 // copy of hashval to hashval2 (it is the output of the hash function)
311 for(int i=0;i<h;i++){
312 Hashval2[i]=Hashval[i];
315 //printf("\n\nKey after copy :");
316 //printArray(Hashval2,h);
318 unsigned long ms1 = micros ();
319 hash_DSD_BIN(plain, Hashval,nblocks,PboxRM,Sbox1,h);
320 Serial.print("Hash time: ");
321 Serial.println(micros() - ms1);
323 printf("\n\nOriginal PLAIN :");
324 printArray(plain,16*2);
325 printf("\n Original Hash value:");
326 printArray(Hashval,h);
328 // Modify message with a random index
330 for(int i=0;i<size_mesg;i++){
335 int indx=random(size_mesg);
336 Serial.println("Index");
337 Serial.println(indx);
338 plain2[indx]=plain2[indx]+1;
339 unsigned long ms3 = micros ();
340 hash_DSD_BIN(plain2, Hashval2,nblocks,PboxRM,Sbox1,h);
341 timecompute[it]=micros() - ms3;
342 Serial.print("Hash time: ");
343 Serial.println(timecompute[it]);
345 printf("\n\nModify PLAIN :");
346 printArray(plain,16*2);
347 printf("\n Modify Hash value:");
348 printArray(Hashval2,h);
352 for(int i=0;i<size_mesg;i++) {
354 if(Hashval2[i]!=Hashval[i]) {
359 Serial.print("CHECK ");
360 Serial.println(equal);
365 timee=mean(itk, timecompute) ;
366 Serial.println ("Mean Time");
367 Serial.println(timee);
371 // encrypt_ctr(cipher, check,nblocks,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
372 hash_DSD_BIN(plain2, Hashval,nblocks,PboxRM,Sbox1,h);
373 Serial.print("ONE Decryption took: ");
374 Serial.println(micros() - ms1);*/
377 //printf("\nCHECK :");
378 //printArray(check,16*2);
383 //--------------------------------------------------------------------
385 //--------------------------------------------------------------------