cryptography ip-cores in vhdl / verilog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
17 KiB

10 years ago
10 years ago
  1. -- ======================================================================
  2. -- AES encryption/decryption
  3. -- package file with functions
  4. -- Copyright (C) 2011 Torsten Meissner
  5. -------------------------------------------------------------------------
  6. -- This program is free software; you can redistribute it and/or modify
  7. -- it under the terms of the GNU General Public License as published by
  8. -- the Free Software Foundation; either version 2 of the License, or
  9. -- (at your option) any later version.
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. -- GNU General Public License for more details.
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program; if not, write to the Free Software
  16. -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. -- ======================================================================
  18. -- aes implementation
  19. -- key length: 128 bit -> Nk = 4
  20. -- data width: 128 bit -> Nb = 4
  21. -- round number Nr = 10
  22. library ieee;
  23. use ieee.std_logic_1164.all;
  24. use ieee.numeric_std.all;
  25. package aes_pkg is
  26. -- components
  27. component aes_enc is
  28. generic (
  29. design_type : string := "ITER"
  30. );
  31. port (
  32. reset_i : in std_logic;
  33. clk_i : in std_logic;
  34. key_i : in std_logic_vector(0 to 127);
  35. data_i : in std_logic_vector(0 to 127);
  36. valid_i : in std_logic;
  37. accept_o : out std_logic;
  38. data_o : out std_logic_vector(0 to 127);
  39. valid_o : out std_logic;
  40. accept_i : in std_logic
  41. );
  42. end component aes_enc;
  43. component aes_dec is
  44. generic (
  45. design_type : string := "ITER"
  46. );
  47. port (
  48. reset_i : in std_logic;
  49. clk_i : in std_logic;
  50. key_i : in std_logic_vector(0 to 127);
  51. data_i : in std_logic_vector(0 to 127);
  52. valid_i : in std_logic;
  53. accept_o : out std_logic;
  54. data_o : out std_logic_vector(0 to 127);
  55. valid_o : out std_logic;
  56. accept_i : in std_logic
  57. );
  58. end component aes_dec;
  59. -- constants for AES128
  60. constant c_nk : natural := 4; -- key size
  61. constant c_nb : natural := 4; -- number of bytes
  62. constant c_nr : natural := 10; -- number of rounds
  63. subtype t_rounds is natural range 0 to c_nr + 1;
  64. subtype t_key_rounds is natural range c_nk to c_nb * (c_nr + 1);
  65. type t_datatable1d is array (0 to 3) of std_logic_vector(7 downto 0);
  66. type t_datatable2d is array (0 to 3) of t_datatable1d;
  67. type t_stable1d is array (0 to 15) of std_logic_vector(7 downto 0);
  68. type t_stable2d is array (0 to 15) of t_stable1d;
  69. type t_key is array (0 to 3) of std_logic_vector(31 downto 0);
  70. type t_rcon is array (0 to 9) of t_datatable1d;
  71. constant c_sbox : t_stable2d := (
  72. -- 0 1 2 3 4 5 6 7 8 9 A B C D E F
  73. (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"), -- 0
  74. (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"), -- 1
  75. (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"), -- 2
  76. (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"), -- 3
  77. (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"), -- 4
  78. (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"), -- 5
  79. (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"), -- 6
  80. (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"), -- 7
  81. (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"), -- 8
  82. (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"), -- 9
  83. (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"), -- A
  84. (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"), -- B
  85. (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"), -- C
  86. (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"), -- D
  87. (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"), -- E
  88. (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")); -- F
  89. constant c_sbox_invers : t_stable2d := (
  90. -- 0 1 2 3 4 5 6 7 8 9 A B C D E F
  91. (x"52", x"09", x"6a", x"d5", x"30", x"36", x"a5", x"38", x"bf", x"40", x"a3", x"9e", x"81", x"f3", x"d7", x"fb"), -- 0
  92. (x"7c", x"e3", x"39", x"82", x"9b", x"2f", x"ff", x"87", x"34", x"8e", x"43", x"44", x"c4", x"de", x"e9", x"cb"), -- 1
  93. (x"54", x"7b", x"94", x"32", x"a6", x"c2", x"23", x"3d", x"ee", x"4c", x"95", x"0b", x"42", x"fa", x"c3", x"4e"), -- 2
  94. (x"08", x"2e", x"a1", x"66", x"28", x"d9", x"24", x"b2", x"76", x"5b", x"a2", x"49", x"6d", x"8b", x"d1", x"25"), -- 3
  95. (x"72", x"f8", x"f6", x"64", x"86", x"68", x"98", x"16", x"d4", x"a4", x"5c", x"cc", x"5d", x"65", x"b6", x"92"), -- 4
  96. (x"6c", x"70", x"48", x"50", x"fd", x"ed", x"b9", x"da", x"5e", x"15", x"46", x"57", x"a7", x"8d", x"9d", x"84"), -- 5
  97. (x"90", x"d8", x"ab", x"00", x"8c", x"bc", x"d3", x"0a", x"f7", x"e4", x"58", x"05", x"b8", x"b3", x"45", x"06"), -- 6
  98. (x"d0", x"2c", x"1e", x"8f", x"ca", x"3f", x"0f", x"02", x"c1", x"af", x"bd", x"03", x"01", x"13", x"8a", x"6b"), -- 7
  99. (x"3a", x"91", x"11", x"41", x"4f", x"67", x"dc", x"ea", x"97", x"f2", x"cf", x"ce", x"f0", x"b4", x"e6", x"73"), -- 8
  100. (x"96", x"ac", x"74", x"22", x"e7", x"ad", x"35", x"85", x"e2", x"f9", x"37", x"e8", x"1c", x"75", x"df", x"6e"), -- 9
  101. (x"47", x"f1", x"1a", x"71", x"1d", x"29", x"c5", x"89", x"6f", x"b7", x"62", x"0e", x"aa", x"18", x"be", x"1b"), -- A
  102. (x"fc", x"56", x"3e", x"4b", x"c6", x"d2", x"79", x"20", x"9a", x"db", x"c0", x"fe", x"78", x"cd", x"5a", x"f4"), -- B
  103. (x"1f", x"dd", x"a8", x"33", x"88", x"07", x"c7", x"31", x"b1", x"12", x"10", x"59", x"27", x"80", x"ec", x"5f"), -- C
  104. (x"60", x"51", x"7f", x"a9", x"19", x"b5", x"4a", x"0d", x"2d", x"e5", x"7a", x"9f", x"93", x"c9", x"9c", x"ef"), -- D
  105. (x"a0", x"e0", x"3b", x"4d", x"ae", x"2a", x"f5", x"b0", x"c8", x"eb", x"bb", x"3c", x"83", x"53", x"99", x"61"), -- E
  106. (x"17", x"2b", x"04", x"7e", x"ba", x"77", x"d6", x"26", x"e1", x"69", x"14", x"63", x"55", x"21", x"0c", x"7d"));-- F
  107. constant c_rcon : t_rcon := (
  108. (x"01", x"00", x"00", x"00"),
  109. (x"02", x"00", x"00", x"00"),
  110. (x"04", x"00", x"00", x"00"),
  111. (x"08", x"00", x"00", x"00"),
  112. (x"10", x"00", x"00", x"00"),
  113. (x"20", x"00", x"00", x"00"),
  114. (x"40", x"00", x"00", x"00"),
  115. (x"80", x"00", x"00", x"00"),
  116. (x"1B", x"00", x"00", x"00"),
  117. (x"36", x"00", x"00", x"00"));
  118. type t_mode is (ENCRYPT, DECRYPT);
  119. function bytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector;
  120. function invbytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector;
  121. function subbytes (input : in t_datatable2d) return t_datatable2d;
  122. function invsubbytes (input : in t_datatable2d) return t_datatable2d;
  123. function shiftrow (input : t_datatable2d) return t_datatable2d;
  124. function invshiftrow (input : t_datatable2d) return t_datatable2d;
  125. function mixcolumns (input : t_datatable2d) return t_datatable2d;
  126. function invmixcolumns (input : t_datatable2d) return t_datatable2d;
  127. function sortdata (input : std_logic_vector(127 downto 0)) return t_datatable2d;
  128. function gmul (a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector;
  129. function addroundkey (input : in t_datatable2d; key : in t_key) return t_datatable2d;
  130. function subword (input : in t_datatable1d) return t_datatable1d;
  131. function rotword (input : in t_datatable1d) return t_datatable1d;
  132. function set_state (input : in std_logic_vector(0 to 127)) return t_datatable2d;
  133. function get_state (input : in t_datatable2d) return std_logic_vector;
  134. function to_string(input : t_datatable2d) return string;
  135. end package aes_pkg;
  136. package body aes_pkg is
  137. function sortdata (input : std_logic_vector(127 downto 0)) return t_datatable2d is
  138. variable v_datamatrix : t_datatable2d;
  139. begin
  140. for outdex in 0 to 3 loop
  141. for index in 0 to 3 loop
  142. v_datamatrix(index)(outdex) := input(outdex*32+(index+1)*7 downto outdex*32+index*8);
  143. end loop;
  144. end loop;
  145. return v_datamatrix;
  146. end function sortdata;
  147. function bytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector is
  148. begin
  149. return(c_sbox(to_integer(unsigned(input(7 downto 4))))(to_integer(unsigned(input(3 downto 0)))));
  150. end function bytesub;
  151. function invbytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector is
  152. begin
  153. return(c_sbox_invers(to_integer(unsigned(input(7 downto 4))))(to_integer(unsigned(input(3 downto 0)))));
  154. end function invbytesub;
  155. function subbytes (input : in t_datatable2d) return t_datatable2d is
  156. variable v_data : t_datatable2d;
  157. begin
  158. for column in 0 to 3 loop
  159. for row in 0 to 3 loop
  160. v_data(row)(column) := c_sbox(to_integer(unsigned(input(row)(column)(7 downto 4))))(to_integer(unsigned(input(row)(column)(3 downto 0))));
  161. end loop;
  162. end loop;
  163. return v_data;
  164. end function subbytes;
  165. function invsubbytes (input : in t_datatable2d) return t_datatable2d is
  166. variable v_data : t_datatable2d;
  167. begin
  168. for column in 0 to 3 loop
  169. for row in 0 to 3 loop
  170. v_data(row)(column) := c_sbox_invers(to_integer(unsigned(input(row)(column)(7 downto 4))))(to_integer(unsigned(input(row)(column)(3 downto 0))));
  171. end loop;
  172. end loop;
  173. return v_data;
  174. end function invsubbytes;
  175. function shiftrow (input : t_datatable2d) return t_datatable2d is
  176. variable v_datamatrix : t_datatable2d;
  177. begin
  178. -- copy input in internal matrix
  179. v_datamatrix := input;
  180. -- 2nd row
  181. v_datamatrix(1)(0) := input(1)(1);
  182. v_datamatrix(1)(1) := input(1)(2);
  183. v_datamatrix(1)(2) := input(1)(3);
  184. v_datamatrix(1)(3) := input(1)(0);
  185. -- 3rd row
  186. v_datamatrix(2)(0) := input(2)(2);
  187. v_datamatrix(2)(1) := input(2)(3);
  188. v_datamatrix(2)(2) := input(2)(0);
  189. v_datamatrix(2)(3) := input(2)(1);
  190. -- 4rd row
  191. v_datamatrix(3)(0) := input(3)(3);
  192. v_datamatrix(3)(1) := input(3)(0);
  193. v_datamatrix(3)(2) := input(3)(1);
  194. v_datamatrix(3)(3) := input(3)(2);
  195. -- return manipulated internal matrix
  196. return v_datamatrix;
  197. end function shiftrow;
  198. function invshiftrow (input : t_datatable2d) return t_datatable2d is
  199. variable v_datamatrix : t_datatable2d;
  200. begin
  201. -- copy input in internal matrix
  202. v_datamatrix := input;
  203. -- 2nd row
  204. v_datamatrix(1)(0) := input(1)(3);
  205. v_datamatrix(1)(1) := input(1)(0);
  206. v_datamatrix(1)(2) := input(1)(1);
  207. v_datamatrix(1)(3) := input(1)(2);
  208. -- 3rd row
  209. v_datamatrix(2)(0) := input(2)(2);
  210. v_datamatrix(2)(1) := input(2)(3);
  211. v_datamatrix(2)(2) := input(2)(0);
  212. v_datamatrix(2)(3) := input(2)(1);
  213. -- 4rd row
  214. v_datamatrix(3)(0) := input(3)(1);
  215. v_datamatrix(3)(1) := input(3)(2);
  216. v_datamatrix(3)(2) := input(3)(3);
  217. v_datamatrix(3)(3) := input(3)(0);
  218. -- return manipulated internal matrix
  219. return v_datamatrix;
  220. end function invshiftrow;
  221. -- trivial algorithmus to multiply two bytes in the GF(2^8) finite field defined
  222. -- by the polynomial x^8 + x^4 + x^3 + x + 1
  223. -- taken from http://www.codeplanet.eu/tutorials/cpp/51-advanced-encryption-standard.html
  224. -- and ported to vhdl
  225. function gmul (a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector is
  226. variable v_a, v_b : std_logic_vector(7 downto 0);
  227. variable v_data : std_logic_vector(7 downto 0) := (others => '0');
  228. variable v_hi_bit_set : boolean;
  229. begin
  230. v_a := a;
  231. v_b := b;
  232. for index in 0 to 7 loop
  233. if(v_b(0) = '1') then
  234. v_data := v_data xor v_a;
  235. end if;
  236. v_hi_bit_set := v_a(7) = '1';
  237. v_a := v_a(6 downto 0) & '0';
  238. if (v_hi_bit_set) then
  239. v_a := v_a xor x"1B";
  240. end if;
  241. v_b := '0' & v_b(7 downto 1);
  242. end loop;
  243. return v_data;
  244. end function gmul;
  245. -- matrix columns manipulation
  246. function mixcolumns (input : t_datatable2d) return t_datatable2d is
  247. variable v_data : t_datatable2d;
  248. begin
  249. for column in 0 to 3 loop
  250. v_data(0)(column) := gmul(x"02", input(0)(column)) xor gmul(x"03", input(1)(column)) xor input(2)(column) xor input(3)(column);
  251. v_data(1)(column) := input(0)(column) xor gmul(x"02", input(1)(column)) xor gmul(x"03",input(2)(column)) xor input(3)(column);
  252. v_data(2)(column) := input(0)(column) xor input(1)(column) xor gmul(x"02",input(2)(column)) xor gmul(x"03",input(3)(column));
  253. v_data(3)(column) := gmul(x"03", input(0)(column)) xor input(1)(column) xor input(2)(column) xor gmul(x"02",input(3)(column));
  254. end loop;
  255. return v_data;
  256. end function mixcolumns;
  257. -- matrix columns manipulation
  258. function invmixcolumns (input : t_datatable2d) return t_datatable2d is
  259. variable v_data : t_datatable2d;
  260. begin
  261. for column in 0 to 3 loop
  262. v_data(0)(column) := gmul(x"0E", input(0)(column)) xor gmul(x"0B", input(1)(column)) xor gmul(x"0D", input(2)(column)) xor gmul(x"09", input(3)(column));
  263. v_data(1)(column) := gmul(x"09", input(0)(column)) xor gmul(x"0E", input(1)(column)) xor gmul(x"0B", input(2)(column)) xor gmul(x"0D", input(3)(column));
  264. v_data(2)(column) := gmul(x"0D", input(0)(column)) xor gmul(x"09", input(1)(column)) xor gmul(x"0E", input(2)(column)) xor gmul(x"0B", input(3)(column));
  265. v_data(3)(column) := gmul(x"0B", input(0)(column)) xor gmul(x"0D", input(1)(column)) xor gmul(x"09", input(2)(column)) xor gmul(x"0E", input(3)(column));
  266. end loop;
  267. return v_data;
  268. end function invmixcolumns;
  269. function addroundkey (input : in t_datatable2d; key : in t_key) return t_datatable2d is
  270. variable v_data : t_datatable2d;
  271. variable v_key : t_datatable1d;
  272. begin
  273. for column in 0 to 3 loop
  274. v_key := (key(column)(31 downto 24), key(column)(23 downto 16), key(column)(15 downto 8), key(column)(7 downto 0));
  275. for row in 0 to 3 loop
  276. v_data(row)(column) := input(row)(column) xor v_key(row);
  277. end loop;
  278. end loop;
  279. return v_data;
  280. end function addroundkey;
  281. function subword (input : in t_datatable1d) return t_datatable1d is
  282. variable v_data : t_datatable1d;
  283. begin
  284. for i in 0 to 3 loop
  285. v_data(i) := c_sbox(to_integer(unsigned(input(i)(7 downto 4))))(to_integer(unsigned(input(i)(3 downto 0))));
  286. end loop;
  287. return v_data;
  288. end function subword;
  289. function rotword (input : in t_datatable1d) return t_datatable1d is
  290. begin
  291. return(input(1), input(2), input(3), input(0));
  292. end function rotword;
  293. function set_state (input : in std_logic_vector(0 to 127)) return t_datatable2d is
  294. variable v_data : t_datatable2d;
  295. begin
  296. for column in 0 to 3 loop
  297. for row in 0 to 3 loop
  298. v_data(row)(column) := input(row*8+column*32 to row*8+column*32+7);
  299. end loop;
  300. end loop;
  301. return v_data;
  302. end function set_state;
  303. function get_state (input : in t_datatable2d) return std_logic_vector is
  304. begin
  305. return input(0)(0) & input(1)(0) & input(2)(0) & input(3)(0) &
  306. input(0)(1) & input(1)(1) & input(2)(1) & input(3)(1) &
  307. input(0)(2) & input(1)(2) & input(2)(2) & input(3)(2) &
  308. input(0)(3) & input(1)(3) & input(2)(3) & input(3)(3);
  309. end function get_state;
  310. function to_string(input : t_datatable2d) return string is
  311. begin
  312. return '(' & to_hstring(input(0)(0)) & ',' & to_hstring(input(0)(1)) & ',' & to_hstring(input(0)(2)) & ',' & to_hstring(input(0)(3)) & ')' & LF &
  313. '(' & to_hstring(input(1)(0)) & ',' & to_hstring(input(1)(1)) & ',' & to_hstring(input(1)(2)) & ',' & to_hstring(input(1)(3)) & ')' & LF &
  314. '(' & to_hstring(input(2)(0)) & ',' & to_hstring(input(2)(1)) & ',' & to_hstring(input(2)(2)) & ',' & to_hstring(input(2)(3)) & ')' & LF &
  315. '(' & to_hstring(input(3)(0)) & ',' & to_hstring(input(3)(1)) & ',' & to_hstring(input(3)(2)) & ',' & to_hstring(input(3)(3)) & ')';
  316. end function to_string;
  317. end package body aes_pkg;