Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "crypto.h"
- void add_round_key(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], uint8_t key[CRYPTO_KEY_SIZE])
- {
- // /// INSERT YOUR CODE HERE ///
- state_bs[0] = state_bs[0] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 0) & 0x1) - 1)));
- state_bs[1] = state_bs[1] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 1) & 0x1) - 1)));
- state_bs[2] = state_bs[2] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 2) & 0x1) - 1)));
- state_bs[3] = state_bs[3] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 3) & 0x1) - 1)));
- state_bs[4] = state_bs[4] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 4) & 0x1) - 1)));
- state_bs[5] = state_bs[5] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 5) & 0x1) - 1)));
- state_bs[6] = state_bs[6] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 6) & 0x1) - 1)));
- state_bs[7] = state_bs[7] ^ (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 7) & 0x1) - 1)));
- state_bs[8] = state_bs[8] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 0) & 0x1) - 1)));
- state_bs[9] = state_bs[9] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 1) & 0x1) - 1)));
- state_bs[10] = state_bs[10] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 2) & 0x1) - 1)));
- state_bs[11] = state_bs[11] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 3) & 0x1) - 1)));
- state_bs[12] = state_bs[12] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 4) & 0x1) - 1)));
- state_bs[13] = state_bs[13] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 5) & 0x1) - 1)));
- state_bs[14] = state_bs[14] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 6) & 0x1) - 1)));
- state_bs[15] = state_bs[15] ^ (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 7) & 0x1) - 1)));
- state_bs[16] = state_bs[16] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 0) & 0x1) - 1)));
- state_bs[17] = state_bs[17] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 1) & 0x1) - 1)));
- state_bs[18] = state_bs[18] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 2) & 0x1) - 1)));
- state_bs[19] = state_bs[19] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 3) & 0x1) - 1)));
- state_bs[20] = state_bs[20] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 4) & 0x1) - 1)));
- state_bs[21] = state_bs[21] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 5) & 0x1) - 1)));
- state_bs[22] = state_bs[22] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 6) & 0x1) - 1)));
- state_bs[23] = state_bs[23] ^ (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 7) & 0x1) - 1)));
- state_bs[24] = state_bs[24] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 0) & 0x1) - 1)));
- state_bs[25] = state_bs[25] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 1) & 0x1) - 1)));
- state_bs[26] = state_bs[26] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 2) & 0x1) - 1)));
- state_bs[27] = state_bs[27] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 3) & 0x1) - 1)));
- state_bs[28] = state_bs[28] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 4) & 0x1) - 1)));
- state_bs[29] = state_bs[29] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 5) & 0x1) - 1)));
- state_bs[30] = state_bs[30] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 6) & 0x1) - 1)));
- state_bs[31] = state_bs[31] ^ (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 7) & 0x1) - 1)));
- state_bs[32] = state_bs[32] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 0) & 0x1) - 1)));
- state_bs[33] = state_bs[33] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 1) & 0x1) - 1)));
- state_bs[34] = state_bs[34] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 2) & 0x1) - 1)));
- state_bs[35] = state_bs[35] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 3) & 0x1) - 1)));
- state_bs[36] = state_bs[36] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 4) & 0x1) - 1)));
- state_bs[37] = state_bs[37] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 5) & 0x1) - 1)));
- state_bs[38] = state_bs[38] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 6) & 0x1) - 1)));
- state_bs[39] = state_bs[39] ^ (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 7) & 0x1) - 1)));
- state_bs[40] = state_bs[40] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 0) & 0x1) - 1)));
- state_bs[41] = state_bs[41] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 1) & 0x1) - 1)));
- state_bs[42] = state_bs[42] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 2) & 0x1) - 1)));
- state_bs[43] = state_bs[43] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 3) & 0x1) - 1)));
- state_bs[44] = state_bs[44] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 4) & 0x1) - 1)));
- state_bs[45] = state_bs[45] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 5) & 0x1) - 1)));
- state_bs[46] = state_bs[46] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 6) & 0x1) - 1)));
- state_bs[47] = state_bs[47] ^ (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 7) & 0x1) - 1)));
- state_bs[48] = state_bs[48] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 0) & 0x1) - 1)));
- state_bs[49] = state_bs[49] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 1) & 0x1) - 1)));
- state_bs[50] = state_bs[50] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 2) & 0x1) - 1)));
- state_bs[51] = state_bs[51] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 3) & 0x1) - 1)));
- state_bs[52] = state_bs[52] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 4) & 0x1) - 1)));
- state_bs[53] = state_bs[53] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 5) & 0x1) - 1)));
- state_bs[54] = state_bs[54] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 6) & 0x1) - 1)));
- state_bs[55] = state_bs[55] ^ (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 7) & 0x1) - 1)));
- state_bs[56] = state_bs[56] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 0) & 0x1) - 1)));
- state_bs[57] = state_bs[57] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 1) & 0x1) - 1)));
- state_bs[58] = state_bs[58] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 2) & 0x1) - 1)));
- state_bs[59] = state_bs[59] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 3) & 0x1) - 1)));
- state_bs[60] = state_bs[60] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 4) & 0x1) - 1)));
- state_bs[61] = state_bs[61] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 5) & 0x1) - 1)));
- state_bs[62] = state_bs[62] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 6) & 0x1) - 1)));
- state_bs[63] = state_bs[63] ^ (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 7) & 0x1) - 1)));
- }
- void pbox_layer(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], bs_reg_t out[CRYPTO_IN_SIZE_BIT])
- {
- out[0] = state_bs[0];
- out[16] = state_bs[1];
- out[32] = state_bs[2];
- out[48] = state_bs[3];
- out[1] = state_bs[4];
- out[17] = state_bs[5];
- out[33] = state_bs[6];
- out[49] = state_bs[7];
- out[2] = state_bs[8];
- out[18] = state_bs[9];
- out[34] = state_bs[10];
- out[50] = state_bs[11];
- out[3] = state_bs[12];
- out[19] = state_bs[13];
- out[35] = state_bs[14];
- out[51] = state_bs[15];
- out[4] = state_bs[16];
- out[20] = state_bs[17];
- out[36] = state_bs[18];
- out[52] = state_bs[19];
- out[5] = state_bs[20];
- out[21] = state_bs[21];
- out[37] = state_bs[22];
- out[53] = state_bs[23];
- out[6] = state_bs[24];
- out[22] = state_bs[25];
- out[38] = state_bs[26];
- out[54] = state_bs[27];
- out[7] = state_bs[28];
- out[23] = state_bs[29];
- out[39] = state_bs[30];
- out[55] = state_bs[31];
- out[8] = state_bs[32];
- out[24] = state_bs[33];
- out[40] = state_bs[34];
- out[56] = state_bs[35];
- out[9] = state_bs[36];
- out[25] = state_bs[37];
- out[41] = state_bs[38];
- out[57] = state_bs[39];
- out[10] = state_bs[40];
- out[26] = state_bs[41];
- out[42] = state_bs[42];
- out[58] = state_bs[43];
- out[11] = state_bs[44];
- out[27] = state_bs[45];
- out[43] = state_bs[46];
- out[59] = state_bs[47];
- out[12] = state_bs[48];
- out[28] = state_bs[49];
- out[44] = state_bs[50];
- out[60] = state_bs[51];
- out[13] = state_bs[52];
- out[29] = state_bs[53];
- out[45] = state_bs[54];
- out[61] = state_bs[55];
- out[14] = state_bs[56];
- out[30] = state_bs[57];
- out[46] = state_bs[58];
- out[62] = state_bs[59];
- out[15] = state_bs[60];
- out[31] = state_bs[61];
- out[47] = state_bs[62];
- out[63] = state_bs[63];
- }
- void sbox_layer(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], bs_reg_t out[CRYPTO_IN_SIZE_BIT])
- {
- bs_reg_t in0, in1, in2, in3;
- in0 = state_bs[0];
- in1 = state_bs[1];
- in2 = state_bs[2];
- in3 = state_bs[3];
- out[0] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[1] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[2] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[3] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[4];
- in1 = state_bs[5];
- in2 = state_bs[6];
- in3 = state_bs[7];
- out[4] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[5] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[6] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[7] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[8];
- in1 = state_bs[9];
- in2 = state_bs[10];
- in3 = state_bs[11];
- out[8] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[9] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[10] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[11] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[12];
- in1 = state_bs[13];
- in2 = state_bs[14];
- in3 = state_bs[15];
- out[12] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[13] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[14] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[15] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[16];
- in1 = state_bs[17];
- in2 = state_bs[18];
- in3 = state_bs[19];
- out[16] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[17] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[18] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[19] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[20];
- in1 = state_bs[21];
- in2 = state_bs[22];
- in3 = state_bs[23];
- out[20] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[21] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[22] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[23] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[24];
- in1 = state_bs[25];
- in2 = state_bs[26];
- in3 = state_bs[27];
- out[24] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[25] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[26] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[27] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[28];
- in1 = state_bs[29];
- in2 = state_bs[30];
- in3 = state_bs[31];
- out[28] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[29] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[30] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[31] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[32];
- in1 = state_bs[33];
- in2 = state_bs[34];
- in3 = state_bs[35];
- out[32] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[33] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[34] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[35] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[36];
- in1 = state_bs[37];
- in2 = state_bs[38];
- in3 = state_bs[39];
- out[36] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[37] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[38] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[39] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[40];
- in1 = state_bs[41];
- in2 = state_bs[42];
- in3 = state_bs[43];
- out[40] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[41] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[42] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[43] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[44];
- in1 = state_bs[45];
- in2 = state_bs[46];
- in3 = state_bs[47];
- out[44] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[45] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[46] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[47] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[48];
- in1 = state_bs[49];
- in2 = state_bs[50];
- in3 = state_bs[51];
- out[48] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[49] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[50] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[51] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[52];
- in1 = state_bs[53];
- in2 = state_bs[54];
- in3 = state_bs[55];
- out[52] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[53] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[54] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[55] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[56];
- in1 = state_bs[57];
- in2 = state_bs[58];
- in3 = state_bs[59];
- out[56] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[57] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[58] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[59] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- in0 = state_bs[60];
- in1 = state_bs[61];
- in2 = state_bs[62];
- in3 = state_bs[63];
- out[60] = ((in0) ^ (in1 & in2) ^ (in2) ^ (in3) );
- out[61] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
- out[62] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
- out[63] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
- }
- static void enslice(const uint8_t pt[8 * 16], bs_reg_t state[64]) {
- // covert into 16 bit * 8
- uint8_t i;
- for (i = 0; i < 64; ++i) {
- uint8_t row = i >> 3; // get row
- uint8_t column = i % 8; // get column
- state[i] = (pt[row + 0] >> column & 0x1) << 0 | (pt[row + 8] >> column & 0x1) << 1 |
- (pt[row + 16] >> column & 0x1) << 2 | (pt[row + 24] >> column & 0x1) << 3 |
- (pt[row + 32] >> column & 0x1) << 4 | (pt[row + 40] >> column & 0x1) << 5 |
- (pt[row + 48] >> column & 0x1) << 6 | (pt[row + 56] >> column & 0x1) << 7 |
- (pt[row + 64] >> column & 0x1) << 8 | (pt[row + 72] >> column & 0x1) << 9 |
- (pt[row + 80] >> column & 0x1) << 10 | (pt[row + 88] >> column & 0x1) << 11 |
- (pt[row + 96] >> column & 0x1) << 12 | (pt[row + 104] >> column & 0x1) << 13 |
- (pt[row + 112] >> column & 0x1) << 14 | (pt[row + 120] >> column & 0x1) << 15;
- }
- }
- static void unslice(const bs_reg_t state[64], uint8_t pt[128]) {
- // covert back to 8 * 16
- uint8_t i;
- for (i = 0; i < 64; ++i) {
- uint8_t row = i >> 4; // get row
- uint8_t column = i % 16; // get column
- pt[i] = (state[row + 0] >> column & 0x1) << 0 | (state[row + 16] >> column & 0x1) << 1 |
- (state[row + 32] >> column & 0x1) << 2 | (state[row + 48] >> column & 0x1) << 3 |
- (state[row + 64] >> column & 0x1) << 4 | (state[row + 80] >> column & 0x1) << 5 |
- (state[row + 96] >> column & 0x1) << 6 | (state[row + 112] >> column & 0x1) << 7;
- }
- }
- /**
- * Perform next key schedule step
- * @param key Key register to be updated
- * @param r Round counter
- * @warning For correct function, has to be called with incremented r each time
- * @note You are free to change or optimize this function
- */
- static void update_round_key(uint8_t key[CRYPTO_KEY_SIZE], const uint8_t r)
- {
- const uint8_t sbox[16] = {
- 0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2,
- };
- uint8_t tmp = 0;
- const uint8_t tmp2 = key[2];
- const uint8_t tmp1 = key[1];
- const uint8_t tmp0 = key[0];
- // rotate right by 19 bit
- key[0] = key[2] >> 3 | key[3] << 5;
- key[1] = key[3] >> 3 | key[4] << 5;
- key[2] = key[4] >> 3 | key[5] << 5;
- key[3] = key[5] >> 3 | key[6] << 5;
- key[4] = key[6] >> 3 | key[7] << 5;
- key[5] = key[7] >> 3 | key[8] << 5;
- key[6] = key[8] >> 3 | key[9] << 5;
- key[7] = key[9] >> 3 | tmp0 << 5;
- key[8] = tmp0 >> 3 | tmp1 << 5;
- key[9] = tmp1 >> 3 | tmp2 << 5;
- // perform sbox lookup on MSbits
- tmp = sbox[key[9] >> 4];
- key[9] &= 0x0F;
- key[9] |= tmp << 4;
- // XOR round counter k19 ... k15
- key[1] ^= r << 7;
- key[2] ^= r >> 1;
- }
- void crypto_func(uint8_t pt[CRYPTO_IN_SIZE * BITSLICE_WIDTH], uint8_t key[CRYPTO_KEY_SIZE])
- {
- // State buffer and additional backbuffer of same size (you can remove the backbuffer if you do not need it)
- bs_reg_t state[CRYPTO_IN_SIZE_BIT] = {0};
- bs_reg_t bb[CRYPTO_IN_SIZE_BIT];
- //uint8_t i = 1;
- // Bring into bitslicing form
- enslice(pt, state);
- /// INSERT YOUR CODE HERE ///
- //for(i = 1; i <=31; i++){
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 1);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 2);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 3);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 4);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 5);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 6);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 7);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 8);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 9);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 10);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 11);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 12);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 13);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 14);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 15);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 16);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 17);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 18);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 19);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 20);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 21);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 22);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 23);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 24);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 25);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 26);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 27);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 28);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 29);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 30);
- add_round_key(state, key + 2); //state holds the latest output
- sbox_layer(state, bb); //bb now holds the latest output
- pbox_layer(bb, state); //state now holds the latest output
- update_round_key(key, 31);
- //}
- add_round_key(state, key + 2); //state holds the latest output
- // Convert back to normal form
- unslice(state, pt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement