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.

221 lines
10 KiB

  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. library ieee;
  19. use ieee.std_logic_1164.all;
  20. use ieee.numeric_std.all;
  21. library accellera_ovl_vhdl;
  22. use accellera_ovl_vhdl.std_ovl.all;
  23. use accellera_ovl_vhdl.std_ovl_procs.all;
  24. package aes_pkg is
  25. -- OVL configuration
  26. constant ovl_proj_controls : ovl_ctrl_record := (
  27. -- generate statement controls
  28. xcheck_ctrl => OVL_ON,
  29. implicit_xcheck_ctrl => OVL_ON,
  30. init_msg_ctrl => OVL_ON,
  31. init_count_ctrl => OVL_ON,
  32. assert_ctrl => OVL_ON,
  33. cover_ctrl => OVL_ON,
  34. global_reset_ctrl => OVL_ON,
  35. finish_ctrl => OVL_ON,
  36. gating_ctrl => OVL_ON,
  37. -- user configurable library constants
  38. max_report_error => 4,
  39. max_report_cover_point => 15,
  40. runtime_after_fatal => "150 ns ",
  41. -- default values for common generics
  42. severity_level_default => OVL_SEVERITY_DEFAULT,
  43. property_type_default => OVL_PROPERTY_DEFAULT,
  44. msg_default => ovl_set_msg(""), -- OVL_MSG_DEFAULT
  45. coverage_level_default => OVL_COVER_DEFAULT,
  46. clock_edge_default => OVL_CLOCK_EDGE_DEFAULT,
  47. reset_polarity_default => OVL_RESET_POLARITY_DEFAULT,
  48. gating_type_default => OVL_GATING_TYPE_DEFAULT);
  49. type t_datatable1d is array (0 to 3) of std_logic_vector(7 downto 0);
  50. type t_datatable2d is array (0 to 3) of t_datatable1d;
  51. type t_stable1d is array (0 to 15) of std_logic_vector(7 downto 0);
  52. type t_stable2d is array (0 to 15) of t_stable1d;
  53. constant c_sbox : t_stable2d := (
  54. -- 0 1 2 3 4 5 6 7 8 9 A B C D E F
  55. (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
  56. (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
  57. (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
  58. (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
  59. (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
  60. (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
  61. (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
  62. (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
  63. (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
  64. (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
  65. (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
  66. (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
  67. (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
  68. (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
  69. (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
  70. (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
  71. constant c_sbox_invers : t_stable2d := (
  72. -- 0 1 2 3 4 5 6 7 8 9 A B C D E F
  73. (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
  74. (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
  75. (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
  76. (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
  77. (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
  78. (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
  79. (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
  80. (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
  81. (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
  82. (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
  83. (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
  84. (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
  85. (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
  86. (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
  87. (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
  88. (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
  89. function bytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector;
  90. function invbytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector;
  91. function shiftrow (input : t_datatable2d) return t_datatable2d;
  92. function invshiftrow (input : t_datatable2d) return t_datatable2d;
  93. function mixcolumns (input : t_datatable2d; column : natural) return t_datatable2d;
  94. function sortdata (input : std_logic_vector(127 downto 0)) return t_datatable2d;
  95. function gmul (a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector;
  96. end package aes_pkg;
  97. package body aes_pkg is
  98. function sortdata (input : std_logic_vector(127 downto 0)) return t_datatable2d is
  99. variable v_datamatrix : t_datatable2d;
  100. begin
  101. for outdex in 0 to 3 loop
  102. for index in 0 to 3 loop
  103. v_datamatrix(index)(outdex) := input(outdex*32+(index+1)*7 downto outdex*32+index*8);
  104. end loop;
  105. end loop;
  106. return v_datamatrix;
  107. end function sortdata;
  108. function bytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector is
  109. begin
  110. return(c_sbox(to_integer(unsigned(input(7 downto 4))))(to_integer(unsigned(input(3 downto 0)))));
  111. end function bytesub;
  112. function invbytesub (input : std_logic_vector(7 downto 0)) return std_logic_vector is
  113. begin
  114. return(c_sbox_invers(to_integer(unsigned(input(7 downto 4))))(to_integer(unsigned(input(3 downto 0)))));
  115. end function invbytesub;
  116. function shiftrow (input : t_datatable2d) return t_datatable2d is
  117. variable v_datamatrix : t_datatable2d;
  118. begin
  119. -- copy input in internal matrix
  120. v_datamatrix := input;
  121. -- 2nd row
  122. v_datamatrix(1)(1) := input(1)(0);
  123. v_datamatrix(1)(2) := input(1)(1);
  124. v_datamatrix(1)(3) := input(1)(2);
  125. -- 3rd row
  126. v_datamatrix(2)(2) := input(2)(0);
  127. v_datamatrix(2)(3) := input(2)(1);
  128. -- 4rd row
  129. v_datamatrix(3)(3) := input(3)(0);
  130. -- return manipulated internal matrix
  131. return v_datamatrix;
  132. end function shiftrow;
  133. function invshiftrow (input : t_datatable2d) return t_datatable2d is
  134. variable v_datamatrix : t_datatable2d;
  135. begin
  136. -- copy input in internal matrix
  137. v_datamatrix := input;
  138. -- 2nd row
  139. v_datamatrix(1)(0) := input(1)(1);
  140. v_datamatrix(1)(1) := input(1)(2);
  141. v_datamatrix(1)(2) := input(1)(3);
  142. -- 3rd row
  143. v_datamatrix(2)(0) := input(2)(2);
  144. v_datamatrix(2)(1) := input(2)(3);
  145. -- 4rd row
  146. v_datamatrix(3)(0) := input(3)(3);
  147. -- return manipulated internal matrix
  148. return v_datamatrix;
  149. end function invshiftrow;
  150. -- trivial algorithmus to multiply two bytes in the 8 bit galois field
  151. -- algorithmus in c taken from http://www.samiam.org/galois.html and rewritten in vhdl
  152. function gmul (a : std_logic_vector(7 downto 0); b : std_logic_vector(7 downto 0)) return std_logic_vector is
  153. variable v_a, v_b : std_logic_vector(7 downto 0);
  154. variable v_data : std_logic_vector(7 downto 0) := (others => '0');
  155. variable v_hi_bit_set : std_logic := '0';
  156. begin
  157. v_a := a;
  158. v_b := b;
  159. for index in 0 to 7 loop
  160. if(v_b(0) = '1') then
  161. v_data := v_data xor v_a;
  162. end if;
  163. v_hi_bit_set := a(7);
  164. v_a := v_a(6 downto 0) & '0';
  165. if(v_hi_bit_set = '1') then
  166. v_a := v_a xor x"01";
  167. end if;
  168. v_b := '0' & v_b(7 downto 1);
  169. end loop;
  170. return v_data;
  171. end function gmul;
  172. -- matrix columns manipulation
  173. -- 02 03 01 01
  174. -- 01 02 03 01
  175. -- 01 01 02 03
  176. -- 03 01 01 02
  177. function mixcolumns (input : t_datatable2d; column : natural) return t_datatable2d is
  178. variable v_data : t_datatable2d;
  179. begin
  180. end function mixcolumns;
  181. end package body aes_pkg;