1 |
3 |
entactogen |
|
2 |
|
|
-- Copyright (c) 2013 Antonio de la Piedra
|
3 |
|
|
|
4 |
|
|
-- This program is free software: you can redistribute it and/or modify
|
5 |
|
|
-- it under the terms of the GNU General Public License as published by
|
6 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
7 |
|
|
-- (at your option) any later version.
|
8 |
|
|
|
9 |
|
|
-- This program is distributed in the hope that it will be useful,
|
10 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
|
|
-- GNU General Public License for more details.
|
13 |
|
|
|
14 |
|
|
-- You should have received a copy of the GNU General Public License
|
15 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
|
|
|
17 |
2 |
entactogen |
library IEEE;
|
18 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
19 |
|
|
|
20 |
|
|
entity des_loop is
|
21 |
|
|
port(clk : in std_logic;
|
22 |
|
|
rst : in std_logic;
|
23 |
|
|
mode : in std_logic; -- 0 encrypt, 1 decrypt
|
24 |
|
|
key_in : in std_logic_vector(63 downto 0);
|
25 |
|
|
key_pre_w_in : in std_logic_vector(63 downto 0);
|
26 |
|
|
key_pos_w_in : in std_logic_vector(63 downto 0);
|
27 |
|
|
blk_in : in std_logic_vector(63 downto 0);
|
28 |
|
|
blk_out : out std_logic_vector(63 downto 0));
|
29 |
|
|
end des_loop;
|
30 |
|
|
|
31 |
|
|
architecture Behavioral of des_loop is
|
32 |
|
|
|
33 |
|
|
signal after_ip_s : std_logic_vector(63 downto 0);
|
34 |
|
|
signal after_ip_minus_one_s : std_logic_vector(63 downto 0);
|
35 |
|
|
signal after_f_s : std_logic_vector(31 downto 0);
|
36 |
|
|
signal final_s : std_logic_vector(63 downto 0);
|
37 |
|
|
|
38 |
|
|
component des_round is
|
39 |
|
|
port(clk : in std_logic;
|
40 |
|
|
l_0 : in std_logic_vector(31 downto 0);
|
41 |
|
|
r_0 : in std_logic_vector(31 downto 0);
|
42 |
|
|
k_i : in std_logic_vector(47 downto 0);
|
43 |
|
|
l_1 : out std_logic_vector(31 downto 0);
|
44 |
|
|
r_1 : out std_logic_vector(31 downto 0));
|
45 |
|
|
end component;
|
46 |
|
|
|
47 |
|
|
component key_schedule is
|
48 |
|
|
port(clk : in std_logic;
|
49 |
|
|
rst : in std_logic;
|
50 |
|
|
mode : in std_logic; -- 0 encrypt, 1 decrypt
|
51 |
|
|
key : in std_logic_vector(63 downto 0);
|
52 |
|
|
key_out : out std_logic_vector(47 downto 0));
|
53 |
|
|
end component;
|
54 |
|
|
|
55 |
|
|
signal key_s : std_logic_vector(47 downto 0);
|
56 |
|
|
|
57 |
|
|
signal l_0_s : std_logic_vector(31 downto 0);
|
58 |
|
|
signal l_1_s : std_logic_vector(31 downto 0);
|
59 |
|
|
signal l_2_s : std_logic_vector(31 downto 0);
|
60 |
|
|
signal l_3_s : std_logic_vector(31 downto 0);
|
61 |
|
|
signal l_4_s : std_logic_vector(31 downto 0);
|
62 |
|
|
signal l_5_s : std_logic_vector(31 downto 0);
|
63 |
|
|
signal l_6_s : std_logic_vector(31 downto 0);
|
64 |
|
|
signal l_7_s : std_logic_vector(31 downto 0);
|
65 |
|
|
signal l_8_s : std_logic_vector(31 downto 0);
|
66 |
|
|
signal l_9_s : std_logic_vector(31 downto 0);
|
67 |
|
|
signal l_10_s : std_logic_vector(31 downto 0);
|
68 |
|
|
signal l_11_s : std_logic_vector(31 downto 0);
|
69 |
|
|
signal l_12_s : std_logic_vector(31 downto 0);
|
70 |
|
|
signal l_13_s : std_logic_vector(31 downto 0);
|
71 |
|
|
signal l_14_s : std_logic_vector(31 downto 0);
|
72 |
|
|
signal l_15_s : std_logic_vector(31 downto 0);
|
73 |
|
|
signal l_16_s : std_logic_vector(31 downto 0);
|
74 |
|
|
|
75 |
|
|
signal r_0_s : std_logic_vector(31 downto 0);
|
76 |
|
|
signal r_1_s : std_logic_vector(31 downto 0);
|
77 |
|
|
signal r_2_s : std_logic_vector(31 downto 0);
|
78 |
|
|
signal r_3_s : std_logic_vector(31 downto 0);
|
79 |
|
|
signal r_4_s : std_logic_vector(31 downto 0);
|
80 |
|
|
signal r_5_s : std_logic_vector(31 downto 0);
|
81 |
|
|
signal r_6_s : std_logic_vector(31 downto 0);
|
82 |
|
|
signal r_7_s : std_logic_vector(31 downto 0);
|
83 |
|
|
signal r_8_s : std_logic_vector(31 downto 0);
|
84 |
|
|
signal r_9_s : std_logic_vector(31 downto 0);
|
85 |
|
|
signal r_10_s : std_logic_vector(31 downto 0);
|
86 |
|
|
signal r_11_s : std_logic_vector(31 downto 0);
|
87 |
|
|
signal r_12_s : std_logic_vector(31 downto 0);
|
88 |
|
|
signal r_13_s : std_logic_vector(31 downto 0);
|
89 |
|
|
signal r_14_s : std_logic_vector(31 downto 0);
|
90 |
|
|
signal r_15_s : std_logic_vector(31 downto 0);
|
91 |
|
|
signal r_16_s : std_logic_vector(31 downto 0);
|
92 |
|
|
|
93 |
|
|
signal k_0_s : std_logic_vector(47 downto 0);
|
94 |
|
|
signal k_1_s : std_logic_vector(47 downto 0);
|
95 |
|
|
signal k_2_s : std_logic_vector(47 downto 0);
|
96 |
|
|
signal k_3_s : std_logic_vector(47 downto 0);
|
97 |
|
|
signal k_4_s : std_logic_vector(47 downto 0);
|
98 |
|
|
signal k_5_s : std_logic_vector(47 downto 0);
|
99 |
|
|
signal k_6_s : std_logic_vector(47 downto 0);
|
100 |
|
|
signal k_7_s : std_logic_vector(47 downto 0);
|
101 |
|
|
signal k_8_s : std_logic_vector(47 downto 0);
|
102 |
|
|
signal k_9_s : std_logic_vector(47 downto 0);
|
103 |
|
|
signal k_10_s : std_logic_vector(47 downto 0);
|
104 |
|
|
signal k_11_s : std_logic_vector(47 downto 0);
|
105 |
|
|
signal k_12_s : std_logic_vector(47 downto 0);
|
106 |
|
|
signal k_13_s : std_logic_vector(47 downto 0);
|
107 |
|
|
signal k_14_s : std_logic_vector(47 downto 0);
|
108 |
|
|
signal k_15_s : std_logic_vector(47 downto 0);
|
109 |
|
|
|
110 |
|
|
signal rst_s : std_logic;
|
111 |
|
|
|
112 |
|
|
signal blk_in_s : std_logic_vector(63 downto 0);
|
113 |
|
|
signal blk_out_s : std_logic_vector(63 downto 0);
|
114 |
|
|
|
115 |
|
|
begin
|
116 |
|
|
|
117 |
|
|
pr_rst_delay : process(clk, rst)
|
118 |
|
|
begin
|
119 |
|
|
if rising_edge(clk) then
|
120 |
|
|
rst_s <= rst;
|
121 |
|
|
end if;
|
122 |
|
|
end process;
|
123 |
|
|
|
124 |
|
|
-- IP
|
125 |
|
|
|
126 |
|
|
blk_in_s <= (blk_in xor key_pre_w_in) when mode = '0' else (blk_in xor key_pos_w_in);
|
127 |
|
|
|
128 |
|
|
pr_seq: process(clk, rst_s, blk_in_s)
|
129 |
|
|
begin
|
130 |
|
|
if rst_s = '1' then
|
131 |
|
|
l_0_s <= blk_in_s(6) & blk_in_s(14) & blk_in_s(22) & blk_in_s(30) & blk_in_s(38) & blk_in_s(46) & blk_in_s(54) & blk_in_s(62) &
|
132 |
|
|
blk_in_s(4) & blk_in_s(12) & blk_in_s(20) & blk_in_s(28) & blk_in_s(36) & blk_in_s(44) & blk_in_s(52) & blk_in_s(60) &
|
133 |
|
|
blk_in_s(2) & blk_in_s(10) & blk_in_s(18) & blk_in_s(26) & blk_in_s(34) & blk_in_s(42) & blk_in_s(50) & blk_in_s(58) &
|
134 |
|
|
blk_in_s(0) & blk_in_s(8) & blk_in_s(16) & blk_in_s(24) & blk_in_s(32) & blk_in_s(40) & blk_in_s(48) & blk_in_s(56);
|
135 |
|
|
|
136 |
|
|
r_0_s <= blk_in_s(7) & blk_in_s(15) & blk_in_s(23) & blk_in_s(31) & blk_in_s(39) & blk_in_s(47) & blk_in_s(55) & blk_in_s(63) &
|
137 |
|
|
blk_in_s(5) & blk_in_s(13) & blk_in_s(21) & blk_in_s(29) & blk_in_s(37) & blk_in_s(45) & blk_in_s(53) & blk_in_s(61) &
|
138 |
|
|
blk_in_s(3) & blk_in_s(11) & blk_in_s(19) & blk_in_s(27) & blk_in_s(35) & blk_in_s(43) & blk_in_s(51) & blk_in_s(59) &
|
139 |
|
|
blk_in_s(1) & blk_in_s(9) & blk_in_s(17) & blk_in_s(25) & blk_in_s(33) & blk_in_s(41) & blk_in_s(49) & blk_in_s(57);
|
140 |
|
|
elsif rising_edge(clk) then
|
141 |
|
|
l_0_s <= l_1_s;
|
142 |
|
|
r_0_s <= r_1_s;
|
143 |
|
|
end if;
|
144 |
|
|
end process;
|
145 |
|
|
|
146 |
|
|
DES_ROUND_0 : des_round port map (clk, l_0_s, r_0_s, k_0_s, l_1_s, r_1_s);
|
147 |
|
|
|
148 |
|
|
final_s <= r_1_s & l_1_s;
|
149 |
|
|
|
150 |
|
|
blk_out_s <= final_s(24) & final_s(56) & final_s(16) & final_s(48) & final_s(8) & final_s(40) & final_s(0) & final_s(32) &
|
151 |
|
|
final_s(25) & final_s(57) & final_s(17) & final_s(49) & final_s(9) & final_s(41) & final_s(1) & final_s(33) &
|
152 |
|
|
final_s(26) & final_s(58) & final_s(18) & final_s(50) & final_s(10) & final_s(42) & final_s(2) & final_s(34) &
|
153 |
|
|
final_s(27) & final_s(59) & final_s(19) & final_s(51) & final_s(11) & final_s(43) & final_s(3) & final_s(35) &
|
154 |
|
|
final_s(28) & final_s(60) & final_s(20) & final_s(52) & final_s(12) & final_s(44) & final_s(4) & final_s(36) &
|
155 |
|
|
final_s(29) & final_s(61) & final_s(21) & final_s(53) & final_s(13) & final_s(45) & final_s(5) & final_s(37) &
|
156 |
|
|
final_s(30) & final_s(62) & final_s(22) & final_s(54) & final_s(14) & final_s(46) & final_s(6) & final_s(38) &
|
157 |
|
|
final_s(31) & final_s(63) & final_s(23) & final_s(55) & final_s(15) & final_s(47) & final_s(7) & final_s(39);
|
158 |
|
|
|
159 |
|
|
blk_out <= (blk_out_s xor key_pos_w_in) when mode = '0' else (blk_out_s xor key_pre_w_in);
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
KEY_SCHEDULE_0 : key_schedule port map (clk, rst, mode, key_in, k_0_s);
|
163 |
|
|
|
164 |
|
|
end Behavioral;
|
165 |
|
|
|