SHARE
TWEET

Untitled

a guest Feb 16th, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "crypto.h"
  2.  
  3. #include "crypto.h"
  4.  
  5. void add_round_key(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], uint8_t key[CRYPTO_KEY_SIZE])
  6. {
  7.     // /// INSERT YOUR CODE HERE ///
  8.     state_bs[0] = state_bs[0] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 0) & 0x1) - 1)));
  9.  
  10.     state_bs[1] = state_bs[1] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 1) & 0x1) - 1)));
  11.  
  12.     state_bs[2] = state_bs[2] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 2) & 0x1) - 1)));
  13.  
  14.     state_bs[3] = state_bs[3] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 3) & 0x1) - 1)));
  15.  
  16.     state_bs[4] = state_bs[4] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 4) & 0x1) - 1)));
  17.  
  18.     state_bs[5] = state_bs[5] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 5) & 0x1) - 1)));
  19.  
  20.     state_bs[6] = state_bs[6] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 6) & 0x1) - 1)));
  21.  
  22.     state_bs[7] = state_bs[7] ^  (0xFFFF & (~(((uint16_t)(key[ 0 ] >> 7) & 0x1) - 1)));
  23.  
  24.     state_bs[8] = state_bs[8] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 0) & 0x1) - 1)));
  25.  
  26.     state_bs[9] = state_bs[9] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 1) & 0x1) - 1)));
  27.  
  28.     state_bs[10] = state_bs[10] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 2) & 0x1) - 1)));
  29.  
  30.     state_bs[11] = state_bs[11] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 3) & 0x1) - 1)));
  31.  
  32.     state_bs[12] = state_bs[12] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 4) & 0x1) - 1)));
  33.  
  34.     state_bs[13] = state_bs[13] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 5) & 0x1) - 1)));
  35.  
  36.     state_bs[14] = state_bs[14] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 6) & 0x1) - 1)));
  37.  
  38.     state_bs[15] = state_bs[15] ^  (0xFFFF & (~(((uint16_t)(key[ 1 ] >> 7) & 0x1) - 1)));
  39.  
  40.     state_bs[16] = state_bs[16] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 0) & 0x1) - 1)));
  41.  
  42.     state_bs[17] = state_bs[17] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 1) & 0x1) - 1)));
  43.  
  44.     state_bs[18] = state_bs[18] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 2) & 0x1) - 1)));
  45.  
  46.     state_bs[19] = state_bs[19] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 3) & 0x1) - 1)));
  47.  
  48.     state_bs[20] = state_bs[20] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 4) & 0x1) - 1)));
  49.  
  50.     state_bs[21] = state_bs[21] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 5) & 0x1) - 1)));
  51.  
  52.     state_bs[22] = state_bs[22] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 6) & 0x1) - 1)));
  53.  
  54.     state_bs[23] = state_bs[23] ^  (0xFFFF & (~(((uint16_t)(key[ 2 ] >> 7) & 0x1) - 1)));
  55.  
  56.     state_bs[24] = state_bs[24] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 0) & 0x1) - 1)));
  57.  
  58.     state_bs[25] = state_bs[25] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 1) & 0x1) - 1)));
  59.  
  60.     state_bs[26] = state_bs[26] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 2) & 0x1) - 1)));
  61.  
  62.     state_bs[27] = state_bs[27] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 3) & 0x1) - 1)));
  63.  
  64.     state_bs[28] = state_bs[28] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 4) & 0x1) - 1)));
  65.  
  66.     state_bs[29] = state_bs[29] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 5) & 0x1) - 1)));
  67.  
  68.     state_bs[30] = state_bs[30] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 6) & 0x1) - 1)));
  69.  
  70.     state_bs[31] = state_bs[31] ^  (0xFFFF & (~(((uint16_t)(key[ 3 ] >> 7) & 0x1) - 1)));
  71.  
  72.     state_bs[32] = state_bs[32] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 0) & 0x1) - 1)));
  73.  
  74.     state_bs[33] = state_bs[33] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 1) & 0x1) - 1)));
  75.  
  76.     state_bs[34] = state_bs[34] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 2) & 0x1) - 1)));
  77.  
  78.     state_bs[35] = state_bs[35] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 3) & 0x1) - 1)));
  79.  
  80.     state_bs[36] = state_bs[36] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 4) & 0x1) - 1)));
  81.  
  82.     state_bs[37] = state_bs[37] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 5) & 0x1) - 1)));
  83.  
  84.     state_bs[38] = state_bs[38] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 6) & 0x1) - 1)));
  85.  
  86.     state_bs[39] = state_bs[39] ^  (0xFFFF & (~(((uint16_t)(key[ 4 ] >> 7) & 0x1) - 1)));
  87.  
  88.     state_bs[40] = state_bs[40] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 0) & 0x1) - 1)));
  89.  
  90.     state_bs[41] = state_bs[41] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 1) & 0x1) - 1)));
  91.  
  92.     state_bs[42] = state_bs[42] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 2) & 0x1) - 1)));
  93.  
  94.     state_bs[43] = state_bs[43] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 3) & 0x1) - 1)));
  95.  
  96.     state_bs[44] = state_bs[44] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 4) & 0x1) - 1)));
  97.  
  98.     state_bs[45] = state_bs[45] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 5) & 0x1) - 1)));
  99.  
  100.     state_bs[46] = state_bs[46] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 6) & 0x1) - 1)));
  101.  
  102.     state_bs[47] = state_bs[47] ^  (0xFFFF & (~(((uint16_t)(key[ 5 ] >> 7) & 0x1) - 1)));
  103.  
  104.     state_bs[48] = state_bs[48] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 0) & 0x1) - 1)));
  105.  
  106.     state_bs[49] = state_bs[49] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 1) & 0x1) - 1)));
  107.  
  108.     state_bs[50] = state_bs[50] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 2) & 0x1) - 1)));
  109.  
  110.     state_bs[51] = state_bs[51] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 3) & 0x1) - 1)));
  111.  
  112.     state_bs[52] = state_bs[52] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 4) & 0x1) - 1)));
  113.  
  114.     state_bs[53] = state_bs[53] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 5) & 0x1) - 1)));
  115.  
  116.     state_bs[54] = state_bs[54] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 6) & 0x1) - 1)));
  117.  
  118.     state_bs[55] = state_bs[55] ^  (0xFFFF & (~(((uint16_t)(key[ 6 ] >> 7) & 0x1) - 1)));
  119.  
  120.     state_bs[56] = state_bs[56] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 0) & 0x1) - 1)));
  121.  
  122.     state_bs[57] = state_bs[57] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 1) & 0x1) - 1)));
  123.  
  124.     state_bs[58] = state_bs[58] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 2) & 0x1) - 1)));
  125.  
  126.     state_bs[59] = state_bs[59] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 3) & 0x1) - 1)));
  127.  
  128.     state_bs[60] = state_bs[60] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 4) & 0x1) - 1)));
  129.  
  130.     state_bs[61] = state_bs[61] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 5) & 0x1) - 1)));
  131.  
  132.     state_bs[62] = state_bs[62] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 6) & 0x1) - 1)));
  133.  
  134.     state_bs[63] = state_bs[63] ^  (0xFFFF & (~(((uint16_t)(key[ 7 ] >> 7) & 0x1) - 1)));
  135. }
  136.  
  137. void pbox_layer(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], bs_reg_t out[CRYPTO_IN_SIZE_BIT])
  138. {
  139.     out[0] = state_bs[0];
  140.     out[16] = state_bs[1];
  141.     out[32] = state_bs[2];
  142.     out[48] = state_bs[3];
  143.     out[1] = state_bs[4];
  144.     out[17] = state_bs[5];
  145.     out[33] = state_bs[6];
  146.     out[49] = state_bs[7];
  147.     out[2] = state_bs[8];
  148.     out[18] = state_bs[9];
  149.     out[34] = state_bs[10];
  150.     out[50] = state_bs[11];
  151.     out[3] = state_bs[12];
  152.     out[19] = state_bs[13];
  153.     out[35] = state_bs[14];
  154.     out[51] = state_bs[15];
  155.     out[4] = state_bs[16];
  156.     out[20] = state_bs[17];
  157.     out[36] = state_bs[18];
  158.     out[52] = state_bs[19];
  159.     out[5] = state_bs[20];
  160.     out[21] = state_bs[21];
  161.     out[37] = state_bs[22];
  162.     out[53] = state_bs[23];
  163.     out[6] = state_bs[24];
  164.     out[22] = state_bs[25];
  165.     out[38] = state_bs[26];
  166.     out[54] = state_bs[27];
  167.     out[7] = state_bs[28];
  168.     out[23] = state_bs[29];
  169.     out[39] = state_bs[30];
  170.     out[55] = state_bs[31];
  171.     out[8] = state_bs[32];
  172.     out[24] = state_bs[33];
  173.     out[40] = state_bs[34];
  174.     out[56] = state_bs[35];
  175.     out[9] = state_bs[36];
  176.     out[25] = state_bs[37];
  177.     out[41] = state_bs[38];
  178.     out[57] = state_bs[39];
  179.     out[10] = state_bs[40];
  180.     out[26] = state_bs[41];
  181.     out[42] = state_bs[42];
  182.     out[58] = state_bs[43];
  183.     out[11] = state_bs[44];
  184.     out[27] = state_bs[45];
  185.     out[43] = state_bs[46];
  186.     out[59] = state_bs[47];
  187.     out[12] = state_bs[48];
  188.     out[28] = state_bs[49];
  189.     out[44] = state_bs[50];
  190.     out[60] = state_bs[51];
  191.     out[13] = state_bs[52];
  192.     out[29] = state_bs[53];
  193.     out[45] = state_bs[54];
  194.     out[61] = state_bs[55];
  195.     out[14] = state_bs[56];
  196.     out[30] = state_bs[57];
  197.     out[46] = state_bs[58];
  198.     out[62] = state_bs[59];
  199.     out[15] = state_bs[60];
  200.     out[31] = state_bs[61];
  201.     out[47] = state_bs[62];
  202.     out[63] = state_bs[63];
  203. }
  204.  
  205. void sbox_layer(bs_reg_t state_bs[CRYPTO_IN_SIZE_BIT], bs_reg_t out[CRYPTO_IN_SIZE_BIT])
  206. {
  207.     bs_reg_t in0, in1, in2, in3;
  208.  
  209.     in0 = state_bs[0];
  210.     in1 = state_bs[1];
  211.     in2 = state_bs[2];
  212.     in3 = state_bs[3];
  213.  
  214.     out[0] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  215.     out[1] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  216.     out[2] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  217.     out[3] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  218.  
  219.  
  220.     in0 = state_bs[4];
  221.     in1 = state_bs[5];
  222.     in2 = state_bs[6];
  223.     in3 = state_bs[7];
  224.  
  225.     out[4] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  226.     out[5] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  227.     out[6] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  228.     out[7] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  229.  
  230.  
  231.     in0 = state_bs[8];
  232.     in1 = state_bs[9];
  233.     in2 = state_bs[10];
  234.     in3 = state_bs[11];
  235.  
  236.     out[8] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  237.     out[9] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  238.     out[10] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  239.     out[11] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  240.  
  241.  
  242.     in0 = state_bs[12];
  243.     in1 = state_bs[13];
  244.     in2 = state_bs[14];
  245.     in3 = state_bs[15];
  246.  
  247.     out[12] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  248.     out[13] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  249.     out[14] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  250.     out[15] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  251.  
  252.  
  253.     in0 = state_bs[16];
  254.     in1 = state_bs[17];
  255.     in2 = state_bs[18];
  256.     in3 = state_bs[19];
  257.  
  258.     out[16] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  259.     out[17] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  260.     out[18] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  261.     out[19] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  262.  
  263.  
  264.     in0 = state_bs[20];
  265.     in1 = state_bs[21];
  266.     in2 = state_bs[22];
  267.     in3 = state_bs[23];
  268.  
  269.     out[20] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  270.     out[21] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  271.     out[22] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  272.     out[23] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  273.  
  274.  
  275.     in0 = state_bs[24];
  276.     in1 = state_bs[25];
  277.     in2 = state_bs[26];
  278.     in3 = state_bs[27];
  279.  
  280.     out[24] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  281.     out[25] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  282.     out[26] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  283.     out[27] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  284.  
  285.  
  286.     in0 = state_bs[28];
  287.     in1 = state_bs[29];
  288.     in2 = state_bs[30];
  289.     in3 = state_bs[31];
  290.  
  291.     out[28] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  292.     out[29] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  293.     out[30] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  294.     out[31] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  295.  
  296.  
  297.     in0 = state_bs[32];
  298.     in1 = state_bs[33];
  299.     in2 = state_bs[34];
  300.     in3 = state_bs[35];
  301.  
  302.     out[32] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  303.     out[33] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  304.     out[34] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  305.     out[35] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  306.  
  307.  
  308.     in0 = state_bs[36];
  309.     in1 = state_bs[37];
  310.     in2 = state_bs[38];
  311.     in3 = state_bs[39];
  312.  
  313.     out[36] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  314.     out[37] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  315.     out[38] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  316.     out[39] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  317.  
  318.  
  319.     in0 = state_bs[40];
  320.     in1 = state_bs[41];
  321.     in2 = state_bs[42];
  322.     in3 = state_bs[43];
  323.  
  324.     out[40] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  325.     out[41] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  326.     out[42] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  327.     out[43] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  328.  
  329.  
  330.     in0 = state_bs[44];
  331.     in1 = state_bs[45];
  332.     in2 = state_bs[46];
  333.     in3 = state_bs[47];
  334.  
  335.     out[44] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  336.     out[45] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  337.     out[46] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  338.     out[47] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  339.  
  340.  
  341.     in0 = state_bs[48];
  342.     in1 = state_bs[49];
  343.     in2 = state_bs[50];
  344.     in3 = state_bs[51];
  345.  
  346.     out[48] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  347.     out[49] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  348.     out[50] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  349.     out[51] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  350.  
  351.  
  352.     in0 = state_bs[52];
  353.     in1 = state_bs[53];
  354.     in2 = state_bs[54];
  355.     in3 = state_bs[55];
  356.  
  357.     out[52] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  358.     out[53] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  359.     out[54] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  360.     out[55] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  361.  
  362.  
  363.     in0 = state_bs[56];
  364.     in1 = state_bs[57];
  365.     in2 = state_bs[58];
  366.     in3 = state_bs[59];
  367.  
  368.     out[56] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  369.     out[57] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  370.     out[58] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  371.     out[59] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  372.  
  373.  
  374.     in0 = state_bs[60];
  375.     in1 = state_bs[61];
  376.     in2 = state_bs[62];
  377.     in3 = state_bs[63];
  378.  
  379.     out[60] = ((in0) ^  (in1 & in2) ^ (in2) ^ (in3) );
  380.     out[61] = (in0 & ( (in2 & (in1 ^ in3)) ^ (in3 & in1))) ^ (in3 & (~in2 ^ in1)) ^ in1;
  381.     out[62] = (in0 & ((in1 & ~in3) ^ (in3 & ~in2))) ^ (in3 & ~in1) ^ ~in2;
  382.     out[63] = (in1 & ( (in0 & (in2 ^ in3)) ^ ~in2)) ^ (in0 & ~(in2 & in3)) ^ ~in3;
  383.  
  384. }
  385.  
  386. static void enslice(const uint8_t pt[8 * 16], bs_reg_t state[64]) {
  387.     // covert into 16 bit * 8
  388.     uint8_t i;
  389.     for (i = 0; i < 64; ++i) {
  390.         uint8_t row = i >> 3; // get row
  391.         uint8_t column = i % 8;   // get column
  392.         state[i] = (pt[row + 0] >> column & 0x1) << 0 | (pt[row + 8] >> column & 0x1) << 1 |
  393.                    (pt[row + 16] >> column & 0x1) << 2 | (pt[row + 24] >> column & 0x1) << 3 |
  394.                    (pt[row + 32] >> column & 0x1) << 4 | (pt[row + 40] >> column & 0x1) << 5 |
  395.                    (pt[row + 48] >> column & 0x1) << 6 | (pt[row + 56] >> column & 0x1) << 7 |
  396.                    (pt[row + 64] >> column & 0x1) << 8 | (pt[row + 72] >> column & 0x1) << 9 |
  397.                    (pt[row + 80] >> column & 0x1) << 10 | (pt[row + 88] >> column & 0x1) << 11 |
  398.                    (pt[row + 96] >> column & 0x1) << 12 | (pt[row + 104] >> column & 0x1) << 13 |
  399.                    (pt[row + 112] >> column & 0x1) << 14 | (pt[row + 120] >> column & 0x1) << 15;
  400.     }
  401. }
  402.  
  403. static void unslice(const bs_reg_t state[64], uint8_t pt[128]) {
  404.     // covert back to 8 * 16
  405.     uint8_t i;
  406.     for (i = 0; i < 128; ++i) {
  407.         uint8_t row = i >> 4; // get row
  408.         uint8_t column = i % 16;   // get column
  409.         pt[i] = (state[row + 0] >> column & 0x1) << 0 | (state[row + 16] >> column & 0x1) << 1 |
  410.                 (state[row + 32] >> column & 0x1) << 2 | (state[row + 48] >> column & 0x1) << 3 |
  411.                 (state[row + 64] >> column & 0x1) << 4 | (state[row + 80] >> column & 0x1) << 5 |
  412.                 (state[row + 96] >> column & 0x1) << 6 | (state[row + 112] >> column & 0x1) << 7;
  413.     }
  414. }
  415.  
  416.  
  417. static void update_round_key(uint8_t key[CRYPTO_KEY_SIZE], const uint8_t r)
  418. {
  419.     const uint8_t sbox[16] = {
  420.             0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2,
  421.     };
  422.  
  423.     uint8_t tmp = 0;
  424.     const uint8_t tmp2 = key[2];
  425.     const uint8_t tmp1 = key[1];
  426.     const uint8_t tmp0 = key[0];
  427.  
  428.     // rotate right by 19 bit
  429.     key[0] = key[2] >> 3 | key[3] << 5;
  430.     key[1] = key[3] >> 3 | key[4] << 5;
  431.     key[2] = key[4] >> 3 | key[5] << 5;
  432.     key[3] = key[5] >> 3 | key[6] << 5;
  433.     key[4] = key[6] >> 3 | key[7] << 5;
  434.     key[5] = key[7] >> 3 | key[8] << 5;
  435.     key[6] = key[8] >> 3 | key[9] << 5;
  436.     key[7] = key[9] >> 3 | tmp0 << 5;
  437.     key[8] = tmp0 >> 3   | tmp1 << 5;
  438.     key[9] = tmp1 >> 3   | tmp2 << 5;
  439.  
  440.     // perform sbox lookup on MSbits
  441.     tmp = sbox[key[9] >> 4];
  442.     key[9] &= 0x0F;
  443.     key[9] |= tmp << 4;
  444.  
  445.     // XOR round counter k19 ... k15
  446.     key[1] ^= r << 7;
  447.     key[2] ^= r >> 1;
  448. }
  449.  
  450. void crypto_func(uint8_t pt[CRYPTO_IN_SIZE * BITSLICE_WIDTH], uint8_t key[CRYPTO_KEY_SIZE])
  451. {
  452.     // State buffer and additional backbuffer of same size (you can remove the backbuffer if you do not need it)
  453.     bs_reg_t state[CRYPTO_IN_SIZE_BIT] = {0};
  454.     bs_reg_t bb[CRYPTO_IN_SIZE_BIT];
  455.     //uint8_t i = 1;
  456.  
  457.     // Bring into bitslicing form
  458.     enslice(pt, state);
  459.  
  460.     /// INSERT YOUR CODE HERE ///
  461.     //for(i = 1; i <=31; i++){
  462.     add_round_key(state, key + 2); //state holds the latest output
  463.     sbox_layer(state, bb); //bb now holds the latest output
  464.     pbox_layer(bb, state); //state now holds the latest output
  465.     update_round_key(key, 1);
  466.  
  467.     add_round_key(state, key + 2); //state holds the latest output
  468.     sbox_layer(state, bb); //bb now holds the latest output
  469.     pbox_layer(bb, state); //state now holds the latest output
  470.     update_round_key(key, 2);
  471.  
  472.     add_round_key(state, key + 2); //state holds the latest output
  473.     sbox_layer(state, bb); //bb now holds the latest output
  474.     pbox_layer(bb, state); //state now holds the latest output
  475.     update_round_key(key, 3);
  476.  
  477.     add_round_key(state, key + 2); //state holds the latest output
  478.     sbox_layer(state, bb); //bb now holds the latest output
  479.     pbox_layer(bb, state); //state now holds the latest output
  480.     update_round_key(key, 4);
  481.  
  482.     add_round_key(state, key + 2); //state holds the latest output
  483.     sbox_layer(state, bb); //bb now holds the latest output
  484.     pbox_layer(bb, state); //state now holds the latest output
  485.     update_round_key(key, 5);
  486.  
  487.     add_round_key(state, key + 2); //state holds the latest output
  488.     sbox_layer(state, bb); //bb now holds the latest output
  489.     pbox_layer(bb, state); //state now holds the latest output
  490.     update_round_key(key, 6);
  491.  
  492.     add_round_key(state, key + 2); //state holds the latest output
  493.     sbox_layer(state, bb); //bb now holds the latest output
  494.     pbox_layer(bb, state); //state now holds the latest output
  495.     update_round_key(key, 7);
  496.  
  497.     add_round_key(state, key + 2); //state holds the latest output
  498.     sbox_layer(state, bb); //bb now holds the latest output
  499.     pbox_layer(bb, state); //state now holds the latest output
  500.     update_round_key(key, 8);
  501.  
  502.     add_round_key(state, key + 2); //state holds the latest output
  503.     sbox_layer(state, bb); //bb now holds the latest output
  504.     pbox_layer(bb, state); //state now holds the latest output
  505.     update_round_key(key, 9);
  506.  
  507.     add_round_key(state, key + 2); //state holds the latest output
  508.     sbox_layer(state, bb); //bb now holds the latest output
  509.     pbox_layer(bb, state); //state now holds the latest output
  510.     update_round_key(key, 10);
  511.  
  512.     add_round_key(state, key + 2); //state holds the latest output
  513.     sbox_layer(state, bb); //bb now holds the latest output
  514.     pbox_layer(bb, state); //state now holds the latest output
  515.     update_round_key(key, 11);
  516.  
  517.     add_round_key(state, key + 2); //state holds the latest output
  518.     sbox_layer(state, bb); //bb now holds the latest output
  519.     pbox_layer(bb, state); //state now holds the latest output
  520.     update_round_key(key, 12);
  521.  
  522.     add_round_key(state, key + 2); //state holds the latest output
  523.     sbox_layer(state, bb); //bb now holds the latest output
  524.     pbox_layer(bb, state); //state now holds the latest output
  525.     update_round_key(key, 13);
  526.  
  527.     add_round_key(state, key + 2); //state holds the latest output
  528.     sbox_layer(state, bb); //bb now holds the latest output
  529.     pbox_layer(bb, state); //state now holds the latest output
  530.     update_round_key(key, 14);
  531.  
  532.     add_round_key(state, key + 2); //state holds the latest output
  533.     sbox_layer(state, bb); //bb now holds the latest output
  534.     pbox_layer(bb, state); //state now holds the latest output
  535.     update_round_key(key, 15);
  536.  
  537.     add_round_key(state, key + 2); //state holds the latest output
  538.     sbox_layer(state, bb); //bb now holds the latest output
  539.     pbox_layer(bb, state); //state now holds the latest output
  540.     update_round_key(key, 16);
  541.  
  542.     add_round_key(state, key + 2); //state holds the latest output
  543.     sbox_layer(state, bb); //bb now holds the latest output
  544.     pbox_layer(bb, state); //state now holds the latest output
  545.     update_round_key(key, 17);
  546.  
  547.     add_round_key(state, key + 2); //state holds the latest output
  548.     sbox_layer(state, bb); //bb now holds the latest output
  549.     pbox_layer(bb, state); //state now holds the latest output
  550.     update_round_key(key, 18);
  551.  
  552.     add_round_key(state, key + 2); //state holds the latest output
  553.     sbox_layer(state, bb); //bb now holds the latest output
  554.     pbox_layer(bb, state); //state now holds the latest output
  555.     update_round_key(key, 19);
  556.  
  557.     add_round_key(state, key + 2); //state holds the latest output
  558.     sbox_layer(state, bb); //bb now holds the latest output
  559.     pbox_layer(bb, state); //state now holds the latest output
  560.     update_round_key(key, 20);
  561.  
  562.     add_round_key(state, key + 2); //state holds the latest output
  563.     sbox_layer(state, bb); //bb now holds the latest output
  564.     pbox_layer(bb, state); //state now holds the latest output
  565.     update_round_key(key, 21);
  566.  
  567.     add_round_key(state, key + 2); //state holds the latest output
  568.     sbox_layer(state, bb); //bb now holds the latest output
  569.     pbox_layer(bb, state); //state now holds the latest output
  570.     update_round_key(key, 22);
  571.  
  572.     add_round_key(state, key + 2); //state holds the latest output
  573.     sbox_layer(state, bb); //bb now holds the latest output
  574.     pbox_layer(bb, state); //state now holds the latest output
  575.     update_round_key(key, 23);
  576.  
  577.     add_round_key(state, key + 2); //state holds the latest output
  578.     sbox_layer(state, bb); //bb now holds the latest output
  579.     pbox_layer(bb, state); //state now holds the latest output
  580.     update_round_key(key, 24);
  581.  
  582.     add_round_key(state, key + 2); //state holds the latest output
  583.     sbox_layer(state, bb); //bb now holds the latest output
  584.     pbox_layer(bb, state); //state now holds the latest output
  585.     update_round_key(key, 25);
  586.  
  587.     add_round_key(state, key + 2); //state holds the latest output
  588.     sbox_layer(state, bb); //bb now holds the latest output
  589.     pbox_layer(bb, state); //state now holds the latest output
  590.     update_round_key(key, 26);
  591.  
  592.     add_round_key(state, key + 2); //state holds the latest output
  593.     sbox_layer(state, bb); //bb now holds the latest output
  594.     pbox_layer(bb, state); //state now holds the latest output
  595.     update_round_key(key, 27);
  596.  
  597.     add_round_key(state, key + 2); //state holds the latest output
  598.     sbox_layer(state, bb); //bb now holds the latest output
  599.     pbox_layer(bb, state); //state now holds the latest output
  600.     update_round_key(key, 28);
  601.  
  602.     add_round_key(state, key + 2); //state holds the latest output
  603.     sbox_layer(state, bb); //bb now holds the latest output
  604.     pbox_layer(bb, state); //state now holds the latest output
  605.     update_round_key(key, 29);
  606.  
  607.     add_round_key(state, key + 2); //state holds the latest output
  608.     sbox_layer(state, bb); //bb now holds the latest output
  609.     pbox_layer(bb, state); //state now holds the latest output
  610.     update_round_key(key, 30);
  611.  
  612.     add_round_key(state, key + 2); //state holds the latest output
  613.     sbox_layer(state, bb); //bb now holds the latest output
  614.     pbox_layer(bb, state); //state now holds the latest output
  615.     update_round_key(key, 31);
  616.  
  617.     //}
  618.  
  619.     add_round_key(state, key + 2); //state holds the latest output
  620.  
  621.     // Convert back to normal form
  622.     unslice(state, pt);
  623. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top