KeyExpansion_x4.vhd 14.9 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
----------------------------------------------------------------------------------
-- COPYRIGHT (c) 2016 ALL RIGHT RESERVED
--
-- COMPANY:					Ruhr-Universitaet Bochum, Chair for Embedded Security
-- AUTHOR:					Pascal Sasdrich
--
-- CREATE DATA:			17/11/2016
-- MODULE NAME:			KeyExpansion
--
--	REVISION:				1.00 - File created
--
-- LICENCE: 				Please look at licence.txt
-- USAGE INFORMATION:	Please look at readme.txt. If licence.txt or readme.txt
--								are missing or	if you have questions regarding the code
--								please contact Pascal Sasdrich (pascal.sasdrich@rub.de)
--								or Amir Moradi (amir.moradi@rub.de).
--
-- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
-- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
-- PARTICULAR PURPOSE.
----------------------------------------------------------------------------------



-- IMPORTS
----------------------------------------------------------------------------------
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;

USE WORK.SKINNYPKG.ALL;



-- ENTITY
----------------------------------------------------------------------------------
ENTITY KeyExpansion_x4 IS
	GENERIC (BS : BLOCK_SIZE := BLOCK_SIZE_128; TS : TWEAK_SIZE := TWEAK_SIZE_3N);
	PORT ( --CLK			: IN  STD_LOGIC;
			 -- CONTROL PORTS --------------------------------
			 --INIT			: IN	STD_LOGIC;
			 -- KEY PORTS ------------------------------------
          KEY			: IN  STD_LOGIC_VECTOR ((GET_TWEAK_SIZE(BS, TS) - 1 - 64) DOWNTO 0);
        ROUND_KEY1	: OUT STD_LOGIC_VECTOR ((GET_TWEAK_SIZE(BS, TS) - 64 - 1) DOWNTO 0);   
        ROUND_KEY2	: OUT STD_LOGIC_VECTOR ((GET_TWEAK_SIZE(BS, TS) - 64 - 1) DOWNTO 0);   
        ROUND_KEY3	: OUT STD_LOGIC_VECTOR ((GET_TWEAK_SIZE(BS, TS) - 64 - 1) DOWNTO 0);   
        ROUND_KEY4	: OUT STD_LOGIC_VECTOR ((GET_TWEAK_SIZE(BS, TS) - 64 - 1) DOWNTO 0));
END KeyExpansion_x4;



-- ARCHITECTURE : MIXED
----------------------------------------------------------------------------------
ARCHITECTURE Mixed OF KeyExpansion_x4 IS

	-- CONSTANTS ------------------------------------------------------------------
	CONSTANT N : INTEGER := GET_BLOCK_SIZE(BS);
	CONSTANT T : INTEGER := GET_TWEAK_SIZE(BS, TS);
	CONSTANT W : INTEGER := GET_WORD_SIZE(BS);
	
	-- SIGNALS --------------------------------------------------------------------
	SIGNAL CURRENT_KEY0, PERMUTED_KEY0, NEXT_KEY0	: STD_LOGIC_VECTOR((T - 64 - 1) DOWNTO 0);
        SIGNAL CURRENT_KEY1, PERMUTED_KEY1, NEXT_KEY1	: STD_LOGIC_VECTOR((T - 64 - 1) DOWNTO 0);
        SIGNAL CURRENT_KEY2, PERMUTED_KEY2, NEXT_KEY2	: STD_LOGIC_VECTOR((T - 64 - 1) DOWNTO 0);
        SIGNAL CURRENT_KEY3, PERMUTED_KEY3, NEXT_KEY3	: STD_LOGIC_VECTOR((T - 64 - 1) DOWNTO 0);

BEGIN

	-- REGISTER -------------------------------------------------------------------
	--R : ENTITY work.ScanFF
	--GENERIC MAP (SIZE => T)
	--PORT MAP (
	--	CLK 	=> CLK,
	--	SE		=> INIT,
	--	D		=> NEXT_KEY,
	--	DS		=> KEY,
	--	Q		=> CURRENT_KEY
	--);
  -------------------------------------------------------------------------------

	-- TWEAKEY ARRAY PERMUTATIONS : TK1 -------------------------------------------
  TK1 : IF TS = TWEAK_SIZE_1N OR TS = TWEAK_SIZE_2N OR TS = TWEAK_SIZE_3N GENERATE
    

    NEXT_KEY0((T - 64 - 1) DOWNTO (T - 1 * N)) <= x"0000000000000000"; 
          -- PERMUTATION -------------------------------------------------------------
          P1 : ENTITY work.HPermutation
            GENERIC MAP (BS => BS) PORT MAP (
            KEY ((T - 64 - 1) DOWNTO (T - 1 * N)), 
            PERMUTED_KEY1((T - 64 - 1) DOWNTO (T - 1 * N))
            ); 
		
          -- NO LFSR -----------------------------------------------------------------
    NEXT_KEY1((T - 64 - 1) DOWNTO (T - 1 * N)) <= PERMUTED_KEY1((T - 64 - 1) DOWNTO (T - 1 * N));

    NEXT_KEY2((T - 64 - 1) DOWNTO (T - 1 * N)) <= x"0000000000000000"; 

          -- PERMUTATION -------------------------------------------------------------
          P3 : ENTITY work.HPermutation
            GENERIC MAP (BS => BS) PORT MAP (
            NEXT_KEY1 ((T - 64 - 1) DOWNTO (T - 1 * N)), 
            PERMUTED_KEY3((T - 64 - 1) DOWNTO (T - 1 * N))
            ); 
		
		-- NO LFSR -----------------------------------------------------------------
		NEXT_KEY3((T - 64 - 1) DOWNTO (T - 1 * N)) <= PERMUTED_KEY3((T - 64 - 1) DOWNTO (T - 1 * N));
	          
	END GENERATE;
	-------------------------------------------------------------------------------
	
	-- TWEAKEY ARRAY PERMUTATIONS : TK2 -------------------------------------------
	TK2 : IF TS = TWEAK_SIZE_2N OR TS = TWEAK_SIZE_3N GENERATE 
	
		-- PERMUTATION -------------------------------------------------------------
		PP : ENTITY work.Permutation
		GENERIC MAP (BS => BS) PORT MAP (
			KEY ((T - 1 * N - 1) DOWNTO (T - 2 * N)), 
			PERMUTED_KEY0((T - 1 * N - 1) DOWNTO (T - 2 * N))
		); 
		-- PERMUTATION -------------------------------------------------------------
		PP1 : ENTITY work.Permutation
		GENERIC MAP (BS => BS) PORT MAP (
			NEXT_KEY0 ((T - 1 * N - 1) DOWNTO (T - 2 * N)), 
			PERMUTED_KEY1((T - 1 * N - 1) DOWNTO (T - 2 * N))
		); 
		-- PERMUTATION -------------------------------------------------------------
		PP2 : ENTITY work.Permutation
		GENERIC MAP (BS => BS) PORT MAP (
			NEXT_KEY1 ((T - 1 * N - 1) DOWNTO (T - 2 * N)), 
			PERMUTED_KEY2((T - 1 * N - 1) DOWNTO (T - 2 * N))
		); 
		-- PERMUTATION -------------------------------------------------------------
		PP3 : ENTITY work.Permutation
		GENERIC MAP (BS => BS) PORT MAP (
			NEXT_KEY2 ((T - 1 * N - 1) DOWNTO (T - 2 * N)), 
			PERMUTED_KEY3((T - 1 * N - 1) DOWNTO (T - 2 * N))
		); 
		
		-- LFSR --------------------------------------------------------------------
		LFSR2 : FOR I IN 0 TO 3 GENERATE
                  NEXT_KEY0((T + W * (I + 13) - 2 * N - 1) DOWNTO (T + W * (I + 12) - 2 * N)) <= PERMUTED_KEY0((T + W * (I + 13) - 2 * N - 2) DOWNTO (T + W * (I + 12) - 2 * N)) & (PERMUTED_KEY0(T + W * (I + 13) - 2 * N - 1) XOR PERMUTED_KEY0(T + W * (I + 13) - 2 * N - (W / 4) - 1));
                  NEXT_KEY1((T + W * (I + 13) - 2 * N - 1) DOWNTO (T + W * (I + 12) - 2 * N)) <= PERMUTED_KEY1((T + W * (I + 13) - 2 * N - 2) DOWNTO (T + W * (I + 12) - 2 * N)) & (PERMUTED_KEY1(T + W * (I + 13) - 2 * N - 1) XOR PERMUTED_KEY1(T + W * (I + 13) - 2 * N - (W / 4) - 1));
                  NEXT_KEY2((T + W * (I + 13) - 2 * N - 1) DOWNTO (T + W * (I + 12) - 2 * N)) <= PERMUTED_KEY2((T + W * (I + 13) - 2 * N - 2) DOWNTO (T + W * (I + 12) - 2 * N)) & (PERMUTED_KEY2(T + W * (I + 13) - 2 * N - 1) XOR PERMUTED_KEY2(T + W * (I + 13) - 2 * N - (W / 4) - 1));
                  NEXT_KEY3((T + W * (I + 13) - 2 * N - 1) DOWNTO (T + W * (I + 12) - 2 * N)) <= PERMUTED_KEY3((T + W * (I + 13) - 2 * N - 2) DOWNTO (T + W * (I + 12) - 2 * N)) & (PERMUTED_KEY3(T + W * (I + 13) - 2 * N - 1) XOR PERMUTED_KEY3(T + W * (I + 13) - 2 * N - (W / 4) - 1));
                  
                  NEXT_KEY0((T + W * (I +  9) - 2 * N - 1) DOWNTO (T + W * (I +  8) - 2 * N)) <= PERMUTED_KEY0((T + W * (I +  9) - 2 * N - 2) DOWNTO (T + W * (I +  8) - 2 * N)) & (PERMUTED_KEY0(T + W * (I +  9) - 2 * N - 1) XOR PERMUTED_KEY0(T + W * (I +  9) - 2 * N - (W / 4) - 1));
                  NEXT_KEY1((T + W * (I +  9) - 2 * N - 1) DOWNTO (T + W * (I +  8) - 2 * N)) <= PERMUTED_KEY1((T + W * (I +  9) - 2 * N - 2) DOWNTO (T + W * (I +  8) - 2 * N)) & (PERMUTED_KEY1(T + W * (I +  9) - 2 * N - 1) XOR PERMUTED_KEY1(T + W * (I +  9) - 2 * N - (W / 4) - 1));
                  NEXT_KEY2((T + W * (I +  9) - 2 * N - 1) DOWNTO (T + W * (I +  8) - 2 * N)) <= PERMUTED_KEY2((T + W * (I +  9) - 2 * N - 2) DOWNTO (T + W * (I +  8) - 2 * N)) & (PERMUTED_KEY2(T + W * (I +  9) - 2 * N - 1) XOR PERMUTED_KEY2(T + W * (I +  9) - 2 * N - (W / 4) - 1));
                  NEXT_KEY3((T + W * (I +  9) - 2 * N - 1) DOWNTO (T + W * (I +  8) - 2 * N)) <= PERMUTED_KEY3((T + W * (I +  9) - 2 * N - 2) DOWNTO (T + W * (I +  8) - 2 * N)) & (PERMUTED_KEY3(T + W * (I +  9) - 2 * N - 1) XOR PERMUTED_KEY3(T + W * (I +  9) - 2 * N - (W / 4) - 1));
                  
                  NEXT_KEY0((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N)) <= PERMUTED_KEY0((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N));
                  NEXT_KEY1((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N)) <= PERMUTED_KEY1((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N));
                  NEXT_KEY2((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N)) <= PERMUTED_KEY2((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N));
                  NEXT_KEY3((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N)) <= PERMUTED_KEY3((T + W * (I +  5) - 2 * N - 1) DOWNTO (T + W * (I +  4) - 2 * N));
                  
                  NEXT_KEY0((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N)) <= PERMUTED_KEY0((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N));
                  NEXT_KEY1((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N)) <= PERMUTED_KEY1((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N));
                  NEXT_KEY2((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N)) <= PERMUTED_KEY2((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N));
                  NEXT_KEY3((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N)) <= PERMUTED_KEY3((T + W * (I +  1) - 2 * N - 1) DOWNTO (T + W * (I +  0) - 2 * N));		
		END GENERATE;
		
	END GENERATE;
	-------------------------------------------------------------------------------
	
	-- TWEAKEY ARRAY PERMUTATIONS : TK3 -------------------------------------------
	TK3 : IF TS = TWEAK_SIZE_3N GENERATE 
	
          -- PERMUTATION -------------------------------------------------------------
          PP0 : ENTITY work.Permutation
            GENERIC MAP (BS => BS) PORT MAP (
              KEY ((T - 2 * N - 1) DOWNTO (T - 3 * N)),
              PERMUTED_KEY0((T - 2 * N - 1) DOWNTO (T - 3 * N))
          ); 
          -- PERMUTATION -------------------------------------------------------------
          PP1 : ENTITY work.Permutation
            GENERIC MAP (BS => BS) PORT MAP (
              NEXT_KEY0 ((T - 2 * N - 1) DOWNTO (T - 3 * N)),
              PERMUTED_KEY1((T - 2 * N - 1) DOWNTO (T - 3 * N))
          ); 
	-- PERMUTATION -------------------------------------------------------------
          PP2 : ENTITY work.Permutation
            GENERIC MAP (BS => BS) PORT MAP (
              NEXT_KEY1 ((T - 2 * N - 1) DOWNTO (T - 3 * N)),
              PERMUTED_KEY2((T - 2 * N - 1) DOWNTO (T - 3 * N))
          ); 
	-- PERMUTATION -------------------------------------------------------------
          PP3 : ENTITY work.Permutation
            GENERIC MAP (BS => BS) PORT MAP (
              NEXT_KEY2 ((T - 2 * N - 1) DOWNTO (T - 3 * N)),
              PERMUTED_KEY3((T - 2 * N - 1) DOWNTO (T - 3 * N))
          ); 
	
		-- LFSR --------------------------------------------------------------------
		LFSR3 : FOR I IN 0 TO 3 GENERATE
                  NEXT_KEY0((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N)) <= (PERMUTED_KEY0(T + W * (I + 12) - 3 * N) XOR PERMUTED_KEY0(T + W * (I + 13) - 3 * N - (W / 4))) & PERMUTED_KEY0((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N + 1));
                  NEXT_KEY1((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N)) <= (PERMUTED_KEY1(T + W * (I + 12) - 3 * N) XOR PERMUTED_KEY1(T + W * (I + 13) - 3 * N - (W / 4))) & PERMUTED_KEY1((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N + 1));
                  NEXT_KEY2((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N)) <= (PERMUTED_KEY2(T + W * (I + 12) - 3 * N) XOR PERMUTED_KEY2(T + W * (I + 13) - 3 * N - (W / 4))) & PERMUTED_KEY2((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N + 1));
                  NEXT_KEY3((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N)) <= (PERMUTED_KEY3(T + W * (I + 12) - 3 * N) XOR PERMUTED_KEY3(T + W * (I + 13) - 3 * N - (W / 4))) & PERMUTED_KEY3((T + W * (I + 13) - 3 * N - 1) DOWNTO (T + W * (I + 12) - 3 * N + 1));
                  
                  NEXT_KEY0((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N)) <= (PERMUTED_KEY0(T + W * (I +  8) - 3 * N) XOR PERMUTED_KEY0(T + W * (I +  9) - 3 * N - (W / 4))) & PERMUTED_KEY0((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N + 1));
                  NEXT_KEY1((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N)) <= (PERMUTED_KEY1(T + W * (I +  8) - 3 * N) XOR PERMUTED_KEY1(T + W * (I +  9) - 3 * N - (W / 4))) & PERMUTED_KEY1((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N + 1));
                  NEXT_KEY2((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N)) <= (PERMUTED_KEY2(T + W * (I +  8) - 3 * N) XOR PERMUTED_KEY2(T + W * (I +  9) - 3 * N - (W / 4))) & PERMUTED_KEY2((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N + 1));
                  NEXT_KEY3((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N)) <= (PERMUTED_KEY3(T + W * (I +  8) - 3 * N) XOR PERMUTED_KEY3(T + W * (I +  9) - 3 * N - (W / 4))) & PERMUTED_KEY3((T + W * (I +  9) - 3 * N - 1) DOWNTO (T + W * (I +  8) - 3 * N + 1));
                  
                  NEXT_KEY0((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N)) <= PERMUTED_KEY0((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N));
                  NEXT_KEY1((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N)) <= PERMUTED_KEY1((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N));
                  NEXT_KEY2((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N)) <= PERMUTED_KEY2((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N));
                  NEXT_KEY3((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N)) <= PERMUTED_KEY3((T + W * (I +  5) - 3 * N - 1) DOWNTO (T + W * (I +  4) - 3 * N));
                  
                  NEXT_KEY0((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N)) <= PERMUTED_KEY0((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N));
                  NEXT_KEY1((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N)) <= PERMUTED_KEY1((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N));
                  NEXT_KEY2((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N)) <= PERMUTED_KEY2((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N));
                  NEXT_KEY3((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N)) <= PERMUTED_KEY3((T + W * (I +  1) - 3 * N - 1) DOWNTO (T + W * (I +  0) - 3 * N));		
		END GENERATE;
		
	END GENERATE;
	-------------------------------------------------------------------------------
	
    -- KEY OUTPUT -----------------------------------------------------------------
    ROUND_KEY1 <= NEXT_KEY0;
    ROUND_KEY2 <= NEXT_KEY1;
    ROUND_KEY3 <= NEXT_KEY2;
    ROUND_KEY4 <= NEXT_KEY3;
    -------------------------------------------------------------------------------
	
END Mixed;