2 //#include "./printf.h"
6 //#include <ESP8266WiFi.h>
8 const int size_mesg=256;
13 const int sleepTimeS = 10;
21 const int len=size_mesg/h2;
42 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
44 for(int i=0;i<size_tab;i++) {
45 inv_perm_tabs[tab[i]] = i;
50 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
52 for(int i=0;i<size_tab;i++) {
53 inv_perm_tabs[tab[i]] = i;
60 void rc4key(uchar *key, uchar *sc, int size_DK) {
62 for(int i=0;i<256;i++) {
68 for(int i0=0; i0<256; i0++) {
69 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
78 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
84 for (int i=0;i<len;i++) {
87 for (int it = 0; it < rp; it++) {
89 for(int i0 = 0; i0<len; i0++) {
90 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
99 void prga(uchar *sc, int ldata, uchar *r) {
103 for (int it=0; it<ldata; it++) {
104 i0 = ((i0+1)&0xFE); //%255);
105 j0 = (j0 + sc[i0])&0xFF;
109 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
115 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) {
117 //printArray(seq_out,16);
124 for(int a=0;a<h2;a++) {
125 X[a]=Sbox1[a&0xFF]; //Warning according to the size of h2, we can be outsize of Sbox1[a]
129 for(int it=0;it<len;it++) {
140 for(int a=0;a<h2;a+=4) {
142 X[a+1]=X[Sbox1[a+1]];
143 X[a+2]=X[Sbox1[a+2]];
144 X[a+3]=X[Sbox1[a+3]];
147 for(int a=0;a<h2;a+=4){
156 //printf("fx %d\n",fX[0] );
159 for(int a=0;a<h2;a+=16) {
163 *(int*)&fX[a+12]^=it;
170 for(int a=0;a<h2;a+=4) {
172 fX[a+1]=fX[a+1]^RM1[a+1];
173 fX[a+2]=fX[a+2]^RM1[a+2];
174 fX[a+3]=fX[a+3]^RM1[a+3];
178 for(int a=0;a<h2;a+=4) {
180 fX[a+1]=Sbox2[fX[a+1]];
181 fX[a+2]=Sbox2[fX[a+2]];
182 fX[a+3]=Sbox2[fX[a+3]];
188 for(int a=0;a<h2;a+=4) {
189 fX[a]=fX[a]^seq_in[ind2+a];
190 fX[a+1]=fX[a+1]^seq_in[ind2+a+1];
191 fX[a+2]=fX[a+2]^seq_in[ind2+a+2];
192 fX[a+3]=fX[a+3]^seq_in[ind2+a+3];
197 for(int a=0;a<h2;a+=4) {
198 seq_out[ind1+a]=fX[a];
199 seq_out[ind1+a+1]=fX[a+1];
200 seq_out[ind1+a+2]=fX[a+2];
201 seq_out[ind1+a+3]=fX[a+3];
204 for(int a=0;a<h2;a+=4) {
205 RM1[a]=RM1[PboxRM[a]];
206 RM1[a+1]=RM1[PboxRM[a+1]];
207 RM1[a+2]=RM1[PboxRM[a+2]];
208 RM1[a+3]=RM1[PboxRM[a+3]];
215 // printf("size mes %d\n",size_mesg);
216 // printf("len %d\n",len);
219 //printArray(seq_out,16);
225 void printArray(byte *mes, int n) {
226 for (byte i = 0; i < n; i++) {
227 Serial.print(mes[i]);
236 Serial.begin (57600) ;
239 printf("\n===testng mode\n") ;
249 for(byte i=0;i<64;i++) {
252 rc4key(DK, Sbox1, 8);
255 rc4key(&DK[8], Sbox2, 8);
257 rc4key(&DK[16], sc, 16);
263 rc4keyperm(&DK[32], len, rp, Pbox, 16);
266 rc4keyperm(&DK[48], h2, rp, PboxRM, 16);
268 for(int i=0;i<h2;i++){
273 // WiFi.mode( WIFI_OFF );
275 Serial.println("END OF INIT");
280 // otfly_test256 () ;
288 // Serial.println("ESP8266 in sleep mode");
289 // ESP.deepSleep(sleepTimeS * 1000000, WAKE_RF_DISABLED );
291 // Serial.println("ESP8266 in normal mode");
294 void prekey (int bits)
298 byte plain[size_mesg];
299 byte cipher [size_mesg] ;
300 byte check [size_mesg] ;
303 for(int i=0;i<size_mesg;i++) {
304 plain[i]=random(255);
310 for(int i=0;i<h2;i++){
318 unsigned long ms1 = micros ();
319 encrypt_ctr(plain, cipher,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,1);
320 Serial.print("ONE Encryption took: ");
321 Serial.println(micros() - ms1);
324 encrypt_ctr(cipher, check,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
325 Serial.print("ONE Decryption took: ");
326 Serial.println(micros() - ms1);
328 printf("\n\nPLAIN :");
329 printArray(plain,16*2);
331 printArray(cipher,16*2);
333 printArray(check,16*2);
336 for(int i=0;i<size_mesg;i++) {
338 if(check[i]!=plain[i]) {
342 Serial.print("CHECK ");
343 Serial.println(equal);