1 |
2 |
entactogen |
-- Copyright (c) 2011 Antonio de la Piedra
|
2 |
|
|
|
3 |
|
|
-- This program is free software: you can redistribute it and/or modify
|
4 |
|
|
-- it under the terms of the GNU General Public License as published by
|
5 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
6 |
|
|
-- (at your option) any later version.
|
7 |
|
|
|
8 |
|
|
-- This program is distributed in the hope that it will be useful,
|
9 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11 |
|
|
-- GNU General Public License for more details.
|
12 |
|
|
|
13 |
|
|
-- You should have received a copy of the GNU General Public License
|
14 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
library IEEE;
|
18 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
19 |
|
|
use IEEE.std_logic_ARITH.ALL;
|
20 |
|
|
use IEEE.std_logic_UNSIGNED.ALL;
|
21 |
|
|
|
22 |
|
|
entity key_schedule is
|
23 |
|
|
port(clk : in std_logic;
|
24 |
|
|
rst : in std_logic;
|
25 |
|
|
|
26 |
|
|
load : in std_logic;
|
27 |
|
|
start : in std_logic;
|
28 |
|
|
|
29 |
|
|
key_in : in std_logic_vector(127 downto 0);
|
30 |
|
|
|
31 |
|
|
key_ready : out std_logic;
|
32 |
|
|
key_out : out std_logic_vector(127 downto 0));
|
33 |
|
|
end key_schedule;
|
34 |
|
|
|
35 |
|
|
architecture Behavioral of key_schedule is
|
36 |
|
|
signal w_3_i_s : std_logic_vector(31 downto 0);
|
37 |
|
|
|
38 |
|
|
signal g_sub_0_s : std_logic_vector(7 downto 0);
|
39 |
|
|
signal g_sub_1_s : std_logic_vector(7 downto 0);
|
40 |
|
|
signal g_sub_2_s : std_logic_vector(7 downto 0);
|
41 |
|
|
signal g_sub_3_s : std_logic_vector(7 downto 0);
|
42 |
|
|
|
43 |
|
|
signal count_5 : natural range 0 to 5;
|
44 |
|
|
signal count_10 : natural range 0 to 10;
|
45 |
|
|
|
46 |
|
|
type type_RCON is array (0 to 9) of std_logic_vector(7 downto 0);
|
47 |
|
|
constant rcon : type_RCON := (x"01", x"02", x"04", x"08", x"10", x"20", x"40", x"80", x"1B", x"36");
|
48 |
|
|
|
49 |
|
|
begin
|
50 |
|
|
mod_5_cnt : process(clk, rst, start)
|
51 |
|
|
begin
|
52 |
|
|
if rising_edge(clk) then
|
53 |
|
|
if (rst = '1') then
|
54 |
|
|
count_5 <= 0;
|
55 |
|
|
elsif(start = '1') then
|
56 |
|
|
if (count_5 = 4) then
|
57 |
|
|
count_5 <= 0;
|
58 |
|
|
else
|
59 |
|
|
count_5 <= count_5 + 1;
|
60 |
|
|
end if;
|
61 |
|
|
end if;
|
62 |
|
|
end if;
|
63 |
|
|
end process mod_5_cnt;
|
64 |
|
|
|
65 |
|
|
mod_10_cnt : process(clk, rst, start, count_5)
|
66 |
|
|
begin
|
67 |
|
|
if rising_edge(clk) then
|
68 |
|
|
if (rst = '1') then
|
69 |
|
|
count_10 <= 0;
|
70 |
|
|
elsif(start = '1' and count_5 = 4) then
|
71 |
|
|
if (count_10 = 9) then
|
72 |
|
|
count_10 <= 0;
|
73 |
|
|
else
|
74 |
|
|
count_10 <= count_10 + 1;
|
75 |
|
|
end if;
|
76 |
|
|
end if;
|
77 |
|
|
end if;
|
78 |
|
|
end process mod_10_cnt;
|
79 |
|
|
|
80 |
|
|
gen_sub_keys : process(clk, rst, start, count_5, count_10, load)
|
81 |
|
|
variable w_0_i_tmp_old : std_logic_vector(31 downto 0) := (others => '0');
|
82 |
|
|
variable w_1_i_tmp_old : std_logic_vector(31 downto 0) := (others => '0');
|
83 |
|
|
variable w_2_i_tmp_old : std_logic_vector(31 downto 0) := (others => '0');
|
84 |
|
|
variable w_3_i_tmp_old : std_logic_vector(31 downto 0) := (others => '0');
|
85 |
|
|
|
86 |
|
|
variable tmp_0 : std_logic_vector(31 downto 0) := (others => '0');
|
87 |
|
|
variable tmp_1 : std_logic_vector(31 downto 0) := (others => '0');
|
88 |
|
|
variable tmp_2 : std_logic_vector(31 downto 0) := (others => '0');
|
89 |
|
|
variable tmp_3 : std_logic_vector(31 downto 0) := (others => '0');
|
90 |
|
|
begin
|
91 |
|
|
if rising_edge(clk) then
|
92 |
|
|
if (rst = '1') then
|
93 |
|
|
w_0_i_tmp_old := (others => '0');
|
94 |
|
|
w_1_i_tmp_old := (others => '0');
|
95 |
|
|
w_2_i_tmp_old := (others => '0');
|
96 |
|
|
w_3_i_tmp_old := (others => '0');
|
97 |
|
|
elsif (load = '1') then
|
98 |
|
|
w_0_i_tmp_old := key_in(31 downto 0);
|
99 |
|
|
w_1_i_tmp_old := key_in(63 downto 32);
|
100 |
|
|
w_2_i_tmp_old := key_in(95 downto 64);
|
101 |
|
|
w_3_i_tmp_old := key_in(127 downto 96);
|
102 |
|
|
elsif (start = '1') then
|
103 |
|
|
if (count_5 = 1) then
|
104 |
|
|
tmp_0 := w_0_i_tmp_old xor (g_sub_3_s & g_sub_2_s & g_sub_1_s & (g_sub_0_s xor rcon(count_10)));
|
105 |
|
|
w_0_i_tmp_old := tmp_0;
|
106 |
|
|
elsif (count_5 = 2) then
|
107 |
|
|
tmp_1 := w_1_i_tmp_old xor w_0_i_tmp_old;
|
108 |
|
|
w_1_i_tmp_old := tmp_1;
|
109 |
|
|
elsif (count_5 = 3) then
|
110 |
|
|
tmp_2 := w_2_i_tmp_old xor w_1_i_tmp_old;
|
111 |
|
|
w_2_i_tmp_old := tmp_2;
|
112 |
|
|
elsif (count_5 = 4) then
|
113 |
|
|
tmp_3 := w_3_i_tmp_old xor w_2_i_tmp_old;
|
114 |
|
|
w_3_i_tmp_old := tmp_3;
|
115 |
|
|
end if;
|
116 |
|
|
end if;
|
117 |
|
|
end if;
|
118 |
|
|
|
119 |
|
|
w_3_i_s <= w_3_i_tmp_old;
|
120 |
|
|
|
121 |
|
|
key_out <= tmp_3 & tmp_2 & tmp_1 & tmp_0;
|
122 |
|
|
|
123 |
|
|
end process;
|
124 |
|
|
|
125 |
|
|
key_ready <= '1' when (count_5 = 1 and start = '1') else '0';
|
126 |
|
|
|
127 |
|
|
S_BOX_DUAL_1: entity work.dual_mem(rtl) port map (clk, '0', w_3_i_s(7 downto 0), w_3_i_s(15 downto 8), (others=>'0'), g_sub_3_s, g_sub_0_s);
|
128 |
|
|
S_BOX_DUAL_2: entity work.dual_mem(rtl) port map (clk, '0', w_3_i_s(23 downto 16), w_3_i_s(31 downto 24), (others=>'0'), g_sub_1_s, g_sub_2_s);
|
129 |
|
|
|
130 |
|
|
end Behavioral;
|