int main(){ printf("Please give the value of the sbox output\n"); int sbox[16]; int i,j; for(i=0;i<16;i++){ scanf("%d",&sbox[i]); //printf("%d\t",sbox[i]); } data input[16]; data output[16]; for(i=0;i<16;i++){ input[i] = convertToData(i); output[i] = convertToData(sbox[i]); } int diffMatrix[16][16]; for(i=0;i<16;i++){ for(j=0;j<16;j++){ diffMatrix[i][j]=matrixEntry(i,j,input,output); printf("%d\t",diffMatrix[i][j]); } printf("\n"); } printf("\n"); }
//Function used for applying reverse substitution function using sboxes bitString substitution(bitString k,bitString a){ int i; int sbox1[16] = {3,8,15,1,10,6,5,11,14,13,4,2,7,0,9,12}; int sbox2[16] = {15,12,2,7,9,0,5,10,1,11,14,8,6,13,3,4}; int sbox3[16] = {8,6,7,9,3,12,10,15,13,1,14,4,0,11,5,2}; int sbox4[16] = {0,15,11,8,12,9,6,3,13,1,2,4,10,7,5,14}; //int sbox5[16] = {0,15,11,8,12,9,6,3,13,1,2,4,10,7,5,14}; int sbox5[16] = {1,15,8,3,12,0,11,6,2,5,4,10,9,14,7,13}; data m[8];int e[8];data d[8]; for(i=0;i<8;i++){ m[i]= bitStringToData(k,4*i); e[i]= sbox5[converttoint(m[i])]; d[i]= convertToData(e[i]); } //BitString used to select the correct combination of sboxes bitString b = intToBitstring(e[0],e[1],e[2],e[3],e[4],e[5],e[6],e[7]); data initial[8];//Divide the input bitstring into 8 parts of 4 bits each int middle[8]; //Pass the each 4-bit box into corresponding sbox and get the new output data final[8]; //Converting the integer to bitstring format for(i=0;i<8;i++){ initial[i] = bitStringToData(a,4*i); if(d[i].a3==0&&d[i].a2==0) middle[i]=getIndex(sbox1,converttoint(initial[i])); if(d[i].a3==0&&d[i].a2==1) middle[i]=getIndex(sbox2,converttoint(initial[i])); if(d[i].a3==1&&d[i].a2==0) middle[i]=getIndex(sbox3,converttoint(initial[i])); if(d[i].a3==1&&d[i].a2==1) middle[i]=getIndex(sbox4,converttoint(initial[i])); final[i]= convertToData(middle[i]); }
int matrixEntry(int a, int b,data* input, data* output){ int results[16],i,count=0; data temp1 = convertToData(a); for(i=0;i<16;i++){ data x = input[i]; data y = output[i]; data x1 = XORdata(x,temp1); int x1Num = converttoint(x1); data y1 = output[x1Num]; data res = XORdata(y,y1); if(converttoint(res)==b) count++; } return count; }
//Function used for converting set of 8 integers to a bitstring bitString intToBitstring(int e1,int e2,int e3,int e4,int e5,int e6,int e7,int e8){ data d[8]; d[0] = convertToData(e1); d[1] = convertToData(e2); d[2] = convertToData(e3); d[3] = convertToData(e4); d[4] = convertToData(e5); d[5] = convertToData(e6); d[6] = convertToData(e7); d[7] = convertToData(e8); bitString b; int i,j; for(i=0;i<8;i++){ b.a[4*i] = d[i].a3; b.a[4*i+1] = d[i].a2; b.a[4*i+2] = d[i].a1; b.a[4*i+3] = d[i].a0; } return b; }
int main(void) { int i; for (i = 0; i < 800; i++) { receivedData[i] = 0; } // init IO DDRB |= (1 << OUTPUT_PIN); // Set the output pin DDRC &= ~(1 << INPUT_PIN); // Set the input pin PORTC |= (1 << INPUT_PIN); // Enable pull-up resistor /*TCCR0A |= (1 << WGM01); // Set the Timer Mode to CTC OCR0A = 0x7D; // Set the value that you want to count to TIMSK0 |= (1 << OCIE0A); //Set the ISR COMPA vect sei(); //enable interrupts TCCR0B |= (1 << CS02); // set prescaler to 256 and start the timer*/ /* USART INIT START */ USART0Init(); stdout =& usart0_output; stdin =& usart0_input; /* USART INIT STOP */ SPIInit(); // Init the SPI bus //char input[100] = "Now, this is a story all about how My life got flipped-turned upside down"; char input[100] = ""; printf("\n\n\n\nSTART CODE WITH THIS INPUT: %s\n\n", input); // Print the input string uint8_t* dataToSend = fillDataArray(input); // Convert data to base2 unsigned int time; time = (1000 / OUTPUT_BAUDRATE) * dataLength; printf("Baudtime: %i\n", 1000/OUTPUT_BAUDRATE); printf("Time needed to send: %i ms\n", time); sendData(dataToSend); printf("datalength: %i\n", dataLength); char* received = convertToData(receivedData, dataLength); // Convert base2 to data if (received[0] == -1) { printf("Error detected...\n"); } else { printf("\nReceived data: %s\n\n", received); // Print the received data } printf("Entering loop now....\n"); PORTB &= ~(1 << OUTPUT_PIN); while (1) { // Main loop } }