SHARE
TWEET

Untitled

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