Advertisement
Guest User

aes_keygen

a guest
Oct 26th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.84 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date: 26.10.2016 15:45:35
  6. -- Design Name:
  7. -- Module Name: aes_roundkeygen - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool Versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20.  
  21.  
  22. library IEEE;
  23. use IEEE.STD_LOGIC_1164.ALL;
  24. use ieee.numeric_std.all;
  25.  
  26. -- Uncomment the following library declaration if using
  27. -- arithmetic functions with Signed or Unsigned values
  28. --use IEEE.NUMERIC_STD.ALL;
  29.  
  30. -- Uncomment the following library declaration if instantiating
  31. -- any Xilinx leaf cells in this code.
  32. --library UNISIM;
  33. --use UNISIM.VComponents.all;
  34.  
  35.  
  36. entity aes_roundkeygen is
  37. Port (clk: in std_logic;
  38. RoundKey: in std_logic_vector(0 to 127);
  39. output: out std_logic_vector(0 to 127));
  40. end aes_roundkeygen;
  41.  
  42. architecture Behavioral of aes_roundkeygen is
  43.  
  44. type sbox1 is array (integer range 0 to 15, integer range 0 to 15) of std_logic_vector(7 downto 0);
  45. constant SBOX: sbox1 := ( ( x"63" , x"7c" , x"77" , x"7b" , x"f2" , x"6b" , x"6f" , x"c5" , x"30" , x"01" , x"67" , x"2b" , x"fe" , x"d7", x"ab" , x"76") ,
  46. ( x"ca" , x"82" , x"c9" , x"7d" , x"fa" , x"59" , x"47" , x"f0" , x"ad" , x"d4" , x"a2" , x"af" , x"9c" , x"a4", x"72" , x"c0") ,
  47. ( x"b7" , x"fd" , x"93" , x"26" , x"36" , x"3f" , x"f7" , x"cc" , x"34" , x"a5" , x"e5" , x"f1" , x"71" , x"d8", x"31" , x"15") ,
  48. ( x"04" , x"c7" , x"23" , x"c3" , x"18" , x"96" , x"05" , x"9a" , x"07" , x"12" , x"80" , x"e2" , x"eb" , x"27", x"b2" , x"75") ,
  49. ( x"09" , x"83" , x"2c" , x"1a" , x"1b" , x"6e" , x"5a" , x"a0" , x"52" , x"3b" , x"d6" , x"b3" , x"29" , x"e3", x"2f" , x"84") ,
  50. ( x"53" , x"d1" , x"00" , x"ed" , x"20" , x"fc" , x"b1" , x"5b" , x"6a" , x"cb" , x"be" , x"39" , x"4a" , x"4c", x"58" , x"cf") ,
  51. ( x"d0" , x"ef" , x"aa" , x"fb" , x"43" , x"4d" , x"33" , x"85" , x"45" , x"f9" , x"02" , x"7f" , x"50" , x"3c", x"9f" , x"a8") ,
  52. ( x"51" , x"a3" , x"40" , x"8f" , x"92" , x"9d" , x"38" , x"f5" , x"bc" , x"b6" , x"da" , x"21" , x"10" , x"ff", x"f3" , x"d2") ,
  53. ( x"cd" , x"0c" , x"13" , x"ec" , x"5f" , x"97" , x"44" , x"17" , x"c4" , x"a7" , x"7e" , x"3d" , x"64" , x"5d", x"19" , x"73") ,
  54. ( x"60" , x"81" , x"4f" , x"dc" , x"22" , x"2a" , x"90" , x"88" , x"46" , x"ee" , x"b8" , x"14" , x"de" , x"5e", x"0b" , x"db") ,
  55. ( x"e0" , x"32" , x"3a" , x"0a" , x"49" , x"06" , x"24" , x"5c" , x"c2" , x"d3" , x"ac" , x"62" , x"91" , x"95", x"e4" , x"79") ,
  56. ( x"e7" , x"c8" , x"37" , x"6d" , x"8d" , x"d5" , x"4e" , x"a9" , x"6c" , x"56" , x"f4" , x"ea" , x"65" , x"7a", x"ae" , x"08") ,
  57. ( x"ba" , x"78" , x"25" , x"2e" , x"1c" , x"a6" , x"b4" , x"c6" , x"e8" , x"dd" , x"74" , x"1f" , x"4b" , x"bd", x"8b" , x"8a") ,
  58. ( x"70" , x"3e" , x"b5" , x"66" , x"48" , x"03" , x"f6" , x"0e" , x"61" , x"35" , x"57" , x"b9" , x"86" , x"c1", x"1d" , x"9e") ,
  59. ( x"e1" , x"f8" , x"98" , x"11" , x"69" , x"d9" , x"8e" , x"94" , x"9b" , x"1e" , x"87" , x"e9" , x"ce" , x"55", x"28" , x"df") ,
  60. ( x"8c" , x"a1" , x"89" , x"0d" , x"bf" , x"e6" , x"42" , x"68" , x"41" , x"99" , x"2d" , x"0f" , x"b0" , x"54", x"bb" , x"16"));
  61.  
  62.  
  63. begin
  64.  
  65. Keygen : process(clk)
  66. variable round : integer range 1 to 10;
  67. variable nextRound : std_logic_vector(0 to 127) := (others => '0');
  68. -- variable rcon : std_logic_vector(0 to 31) := (others => '0');
  69.  
  70. begin
  71. if(rising_edge(clk)) then
  72.  
  73. case round is
  74. when 1 =>
  75.  
  76. -- ROTWORD
  77. nextRound(0 to 23) := RoundKey(104 to 127);
  78. nextRound(24 to 31) := RoundKey(96 to 103);
  79.  
  80. -- SUBBYTE (SBOX)
  81. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  82. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  83. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  84. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  85.  
  86. -- XOR
  87. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00000001";
  88. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  89. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  90. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  91. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  92. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  93. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  94.  
  95. --output
  96. output <= nextRound(0 to 127);
  97.  
  98. when 2 =>
  99.  
  100. -- ROTWORD
  101. nextRound(0 to 23) := RoundKey(104 to 127);
  102. nextRound(24 to 31) := RoundKey(96 to 103);
  103.  
  104. -- SUBBYTE (SBOX)
  105. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  106. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  107. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  108. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  109.  
  110. -- XOR
  111. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00000010";
  112. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  113. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  114. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  115. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  116. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  117. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  118.  
  119. --output
  120. output <= nextRound(0 to 127);
  121.  
  122. when 3 =>
  123.  
  124. -- ROTWORD
  125. nextRound(0 to 23) := RoundKey(104 to 127);
  126. nextRound(24 to 31) := RoundKey(96 to 103);
  127.  
  128. -- SUBBYTE (SBOX)
  129. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  130. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  131. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  132. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  133.  
  134. -- XOR
  135. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00000100";
  136. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  137. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  138. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  139. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  140. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  141. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  142.  
  143. --output
  144. output <= nextRound(0 to 127);
  145.  
  146. when 4 =>
  147.  
  148. -- ROTWORD
  149. nextRound(0 to 23) := RoundKey(104 to 127);
  150. nextRound(24 to 31) := RoundKey(96 to 103);
  151.  
  152. -- SUBBYTE (SBOX)
  153. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  154. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  155. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  156. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  157.  
  158. -- XOR
  159. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00001000";
  160. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  161. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  162. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  163. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  164. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  165. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  166.  
  167. --output
  168. output <= nextRound(0 to 127);
  169.  
  170. when 5 =>
  171.  
  172. -- ROTWORD
  173. nextRound(0 to 23) := RoundKey(104 to 127);
  174. nextRound(24 to 31) := RoundKey(96 to 103);
  175.  
  176. -- SUBBYTE (SBOX)
  177. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  178. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  179. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  180. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  181.  
  182. -- XOR
  183. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00010000";
  184. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  185. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  186. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  187. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  188. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  189. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  190.  
  191. --output
  192. output <= nextRound(0 to 127);
  193.  
  194. when 6 =>
  195.  
  196. -- ROTWORD
  197. nextRound(0 to 23) := RoundKey(104 to 127);
  198. nextRound(24 to 31) := RoundKey(96 to 103);
  199.  
  200. -- SUBBYTE (SBOX)
  201. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  202. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  203. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  204. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  205.  
  206. -- XOR
  207. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "00100000";
  208. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  209. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  210. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  211. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  212. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  213. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  214.  
  215. --output
  216. output <= nextRound(0 to 127);
  217.  
  218. when 7 =>
  219.  
  220. -- ROTWORD
  221. nextRound(0 to 23) := RoundKey(104 to 127);
  222. nextRound(24 to 31) := RoundKey(96 to 103);
  223.  
  224. -- SUBBYTE (SBOX)
  225. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  226. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  227. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  228. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  229.  
  230. -- XOR
  231. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "01000000";
  232. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  233. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  234. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  235. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  236. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  237. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  238.  
  239. --output
  240. output <= nextRound(0 to 127);
  241.  
  242. when 8 =>
  243.  
  244. -- ROTWORD
  245. nextRound(0 to 23) := RoundKey(104 to 127);
  246. nextRound(24 to 31) := RoundKey(96 to 103);
  247.  
  248. -- SUBBYTE (SBOX)
  249. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  250. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  251. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  252. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  253.  
  254. -- XOR
  255. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "10000000";
  256. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  257. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  258. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  259. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  260. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  261. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  262.  
  263. --output
  264. output <= nextRound(0 to 127);
  265.  
  266. when 9 =>
  267.  
  268. -- ROTWORD
  269. nextRound(0 to 23) := RoundKey(104 to 127);
  270. nextRound(24 to 31) := RoundKey(96 to 103);
  271.  
  272. -- SUBBYTE (SBOX)
  273. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  274. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  275. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  276. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  277.  
  278. -- XOR
  279. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "11011000";
  280. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  281. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  282. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  283. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  284. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  285. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  286.  
  287. --output
  288. output <= nextRound(0 to 127);
  289.  
  290. when 10 =>
  291.  
  292. -- ROTWORD
  293. nextRound(0 to 23) := RoundKey(104 to 127);
  294. nextRound(24 to 31) := RoundKey(96 to 103);
  295.  
  296. -- SUBBYTE (SBOX)
  297. nextRound(0 to 7) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(0 to 3))),to_integer(unsigned(nextRound(4 to 7)))));
  298. nextRound(8 to 15) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(8 to 11))),to_integer(unsigned(nextRound(12 to 15)))));
  299. nextRound(16 to 23) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(16 to 19))),to_integer(unsigned(nextRound(20 to 23)))));
  300. nextRound(24 to 31) := std_logic_vector(SBOX(to_integer(unsigned(nextRound(24 to 27))),to_integer(unsigned(nextRound(28 to 31)))));
  301.  
  302. -- XOR
  303. nextRound(0 to 7) := nextRound(0 to 7) xor RoundKey(0 to 7) xor "01101100";
  304. nextRound(8 to 15) := nextRound(8 to 15) xor RoundKey(8 to 15);
  305. nextRound(16 to 23) := nextRound(16 to 23) xor RoundKey(16 to 23);
  306. nextRound(24 to 31) := nextRound(24 to 31) xor RoundKey(24 to 31);
  307. nextRound(32 to 63) := nextRound(0 to 31) xor RoundKey(32 to 63);
  308. nextRound(64 to 95) := nextRound(32 to 63) xor RoundKey(64 to 95);
  309. nextRound(96 to 127) := nextRound(64 to 95) xor RoundKey(96 to 127);
  310.  
  311. --output
  312. output <= nextRound(0 to 127);
  313.  
  314. when others => null;
  315.  
  316. end case;
  317.  
  318. end if;
  319. end process Keygen;
  320.  
  321. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement