📄 twofish.vhd
字号:
-- Twofish.vhd-- Copyright (C) 2006 Spyros Ninos---- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by-- the Free Software Foundation; either version 2 of the License, or-- (at your option) any later version.-- -- This program is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU General Public License for more details.-- -- You should have received a copy of the GNU General Public License-- along with this library; see the file COPYING. If not, write to:-- -- Free Software Foundation-- 59 Temple Place - Suite 330-- Boston, MA 02111-1307, USA.-- description : this file includes all the components necessary to perform symmetric encryption-- with the twofish 128 bit block cipher. Within there are four main parts of the file.-- the first part is the twofish crypto primitives which are independent of the key-- input length, the second part is the 128 bit key input components, the third part -- is the 192 bit key components and finaly the 256 bit key input components---- ====================================================== ---- ====================================================== ---- ---- first part: key input independent component primitives ---- ---- ====================================================== ---- ====================================================== ---- -- q0--library ieee;Use ieee.std_logic_1164.all;entity q0 isport ( in_q0 : in std_logic_vector(7 downto 0); out_q0 : out std_logic_vector(7 downto 0) );end q0;architecture q0_arch of q0 is -- declaring internal signals signal a0,b0, a1,b1, a2,b2, a3,b3, a4,b4 : std_logic_vector(3 downto 0); signal b0_ror4, a0_times_8, b2_ror4, a2_times_8 : std_logic_vector(3 downto 0);-- beginning of the architecture descriptionbegin -- little endian b0 <= in_q0(3 downto 0); a0 <= in_q0(7 downto 4); a1 <= a0 XOR b0; -- signal b0 is ror4'ed by 1 bit b0_ror4(2 downto 0) <= b0(3 downto 1); b0_ror4(3) <= b0(0); -- 8*a0 = 2^3*a0= a0 << 3 a0_times_8(2 downto 0) <= (others => '0'); a0_times_8(3) <= a0(0); b1 <= a0 XOR b0_ror4 XOR a0_times_8; -- -- t0 table -- with a1 select a2 <= "1000" when "0000", -- 8 "0001" when "0001", -- 1 "0111" when "0010", -- 7 "1101" when "0011", -- D "0110" when "0100", -- 6 "1111" when "0101", -- F "0011" when "0110", -- 3 "0010" when "0111", -- 2 "0000" when "1000", -- 0 "1011" when "1001", -- B "0101" when "1010", -- 5 "1001" when "1011", -- 9 "1110" when "1100", -- E "1100" when "1101", -- C "1010" when "1110", -- A "0100" when others; -- 4 -- -- t1 table -- with b1 select b2 <= "1110" when "0000", -- E "1100" when "0001", -- C "1011" when "0010", -- B "1000" when "0011", -- 8 "0001" when "0100", -- 1 "0010" when "0101", -- 2 "0011" when "0110", -- 3 "0101" when "0111", -- 5 "1111" when "1000", -- F "0100" when "1001", -- 4 "1010" when "1010", -- A "0110" when "1011", -- 6 "0111" when "1100", -- 7 "0000" when "1101", -- 0 "1001" when "1110", -- 9 "1101" when others; -- D a3 <= a2 XOR b2; -- signal b2 is ror4'ed by 1 bit b2_ror4(2 downto 0) <= b2(3 downto 1); b2_ror4(3) <= b2(0); -- 8*a2 = 2^3*a2=a2<<3 a2_times_8(2 downto 0) <= (others => '0'); a2_times_8(3) <= a2(0); b3 <= a2 XOR b2_ror4 XOR a2_times_8; -- -- t0 table -- with a3 select a4 <= "1011" when "0000", -- B "1010" when "0001", -- A "0101" when "0010", -- 5 "1110" when "0011", -- E "0110" when "0100", -- 6 "1101" when "0101", -- D "1001" when "0110", -- 9 "0000" when "0111", -- 0 "1100" when "1000", -- C "1000" when "1001", -- 8 "1111" when "1010", -- F "0011" when "1011", -- 3 "0010" when "1100", -- 2 "0100" when "1101", -- 4 "0111" when "1110", -- 7 "0001" when others; -- 1 -- -- t1 table -- with b3 select b4 <= "1101" when "0000", -- D "0111" when "0001", -- 7 "1111" when "0010", -- F "0100" when "0011", -- 4 "0001" when "0100", -- 1 "0010" when "0101", -- 2 "0110" when "0110", -- 6 "1110" when "0111", -- E "1001" when "1000", -- 9 "1011" when "1001", -- B "0011" when "1010", -- 3 "0000" when "1011", -- 0 "1000" when "1100", -- 8 "0101" when "1101", -- 5 "1100" when "1110", -- C "1010" when others; -- A -- the output of q0 out_q0 <= b4 & a4;end q0_arch;-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ---- ---- new component ---- ---- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ------ q1--library ieee;Use ieee.std_logic_1164.all;entity q1 isport ( in_q1 : in std_logic_vector(7 downto 0); out_q1 : out std_logic_vector(7 downto 0) );end q1;-- architecture descriptionarchitecture q1_arch of q1 is -- declaring the internal signals signal a0,b0, a1,b1, a2,b2, a3,b3, a4,b4 : std_logic_vector(3 downto 0); signal b0_ror4, a0_times_8, b2_ror4, a2_times_8 : std_logic_vector(3 downto 0);-- begin the architecture descriptionbegin -- little endian b0 <= in_q1(3 downto 0); a0 <= in_q1(7 downto 4); a1 <= a0 XOR b0; -- signal b0 is ror4'ed by 1 bit b0_ror4(2 downto 0) <= b0(3 downto 1); b0_ror4(3) <= b0(0); -- 8*a0 = 2^3*a0=a0<<3 a0_times_8(2 downto 0) <= (others => '0'); a0_times_8(3) <= a0(0); b1 <= a0 XOR b0_ror4 XOR a0_times_8; -- -- t0 table -- with a1 select a2 <= "0010" when "0000", -- 2 "1000" when "0001", -- 8 "1011" when "0010", -- b "1101" when "0011", -- d "1111" when "0100", -- f "0111" when "0101", -- 7 "0110" when "0110", -- 6 "1110" when "0111", -- e "0011" when "1000", -- 3 "0001" when "1001", -- 1 "1001" when "1010", -- 9 "0100" when "1011", -- 4 "0000" when "1100", -- 0 "1010" when "1101", -- a "1100" when "1110", -- c "0101" when others; -- 5 -- -- t1 table -- with b1 select b2 <= "0001" when "0000", -- 1 "1110" when "0001", -- e "0010" when "0010", -- 2 "1011" when "0011", -- b "0100" when "0100", -- 4 "1100" when "0101", -- c "0011" when "0110", -- 3 "0111" when "0111", -- 7 "0110" when "1000", -- 6 "1101" when "1001", -- d "1010" when "1010", -- a "0101" when "1011", -- 5 "1111" when "1100", -- f "1001" when "1101", -- 9 "0000" when "1110", -- 0 "1000" when others; -- 8 a3 <= a2 XOR b2; -- signal b2 is ror4'ed by 1 bit b2_ror4(2 downto 0) <= b2(3 downto 1); b2_ror4(3) <= b2(0); -- 8*a2 = 2^3*a2=a2<<3 a2_times_8(2 downto 0) <= (others => '0'); a2_times_8(3) <= a2(0); b3 <= a2 XOR b2_ror4 XOR a2_times_8; -- -- t0 table -- with a3 select a4 <= "0100" when "0000", -- 4 "1100" when "0001", -- c "0111" when "0010", -- 7 "0101" when "0011", -- 5 "0001" when "0100", -- 1 "0110" when "0101", -- 6 "1001" when "0110", -- 9 "1010" when "0111", -- a "0000" when "1000", -- 0 "1110" when "1001", -- e "1101" when "1010", -- d "1000" when "1011", -- 8 "0010" when "1100", -- 2 "1011" when "1101", -- b "0011" when "1110", -- 3 "1111" when others; -- f -- -- t1 table -- with b3 select b4 <= "1011" when "0000", -- b "1001" when "0001", -- 9 "0101" when "0010", -- 5 "0001" when "0011", -- 1 "1100" when "0100", -- c "0011" when "0101", -- 3 "1101" when "0110", -- d "1110" when "0111", -- e "0110" when "1000", -- 6 "0100" when "1001", -- 4 "0111" when "1010", -- 7 "1111" when "1011", -- f "0010" when "1100", -- 2 "0000" when "1101", -- 0 "1000" when "1110", -- 8 "1010" when others; -- a -- output of q1 out_q1 <= b4 & a4;end q1_arch;-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ---- ---- new component ---- ---- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ------ ef multiplier--library ieee;use ieee.std_logic_1164.all;entity mul_ef isport ( in_ef : in std_logic_vector(7 downto 0); out_ef : out std_logic_vector(7 downto 0) );end mul_ef;architecture mul_ef_arch of mul_ef isbegin out_ef(0) <= in_ef(2) XOR in_ef(1) XOR in_ef(0); out_ef(1) <= in_ef(3) XOR in_ef(2) XOR in_ef(1) XOR in_ef(0); out_ef(2) <= in_ef(4) XOR in_ef(3) XOR in_ef(2) XOR in_ef(1) XOR in_ef(0); out_ef(3) <= in_ef(5) XOR in_ef(4) XOR in_ef(3) XOR in_ef(0); out_ef(4) <= in_ef(6) XOR in_ef(5) XOR in_ef(4) XOR in_ef(1); out_ef(5) <= in_ef(7) XOR in_ef(6) XOR in_ef(5) XOR in_ef(1) XOR in_ef(0); out_ef(6) <= in_ef(7) XOR in_ef(6) XOR in_ef(0); out_ef(7) <= in_ef(7) XOR in_ef(1) XOR in_ef(0);end mul_ef_arch;-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ---- ---- new component ---- ---- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ------ 5b multiplier--library ieee;use ieee.std_logic_1164.all;entity mul_5b isport ( in_5b : in std_logic_vector(7 downto 0); out_5b : out std_logic_vector(7 downto 0) );end mul_5b;architecture mul_5b_arch of mul_5b isbegin out_5b(0) <= in_5b(2) XOR in_5b(0); out_5b(1) <= in_5b(3) XOR in_5b(1) XOR in_5b(0); out_5b(2) <= in_5b(4) XOR in_5b(2) XOR in_5b(1); out_5b(3) <= in_5b(5) XOR in_5b(3) XOR in_5b(0); out_5b(4) <= in_5b(6) XOR in_5b(4) XOR in_5b(1) XOR in_5b(0); out_5b(5) <= in_5b(7) XOR in_5b(5) XOR in_5b(1); out_5b(6) <= in_5b(6) XOR in_5b(0); out_5b(7) <= in_5b(7) XOR in_5b(1);end mul_5b_arch;-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ---- ---- new component ---- ---- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ------ mds--library ieee;use ieee.std_logic_1164.all;entity mds isport ( y0, y1, y2, y3 : in std_logic_vector(7 downto 0); z0, z1, z2, z3 : out std_logic_vector(7 downto 0) );end mds;-- architecture description of mds componentarchitecture mds_arch of mds is -- we declare the multiplier by ef component mul_ef port ( in_ef : in std_logic_vector(7 downto 0); out_ef : out std_logic_vector(7 downto 0) ); end component; -- we declare the multiplier by 5b component mul_5b port ( in_5b : in std_logic_vector(7 downto 0); out_5b : out std_logic_vector(7 downto 0) ); end component; -- we declare the multiplier's outputs signal y0_ef, y0_5b, y1_ef, y1_5b, y2_ef, y2_5b, y3_ef, y3_5b : std_logic_vector(7 downto 0);begin -- we perform the signal multiplication y0_times_ef: mul_ef port map ( in_ef => y0, out_ef => y0_ef ); y0_times_5b: mul_5b port map ( in_5b => y0, out_5b => y0_5b ); y1_times_ef: mul_ef port map ( in_ef => y1, out_ef => y1_ef ); y1_times_5b: mul_5b port map ( in_5b => y1, out_5b => y1_5b ); y2_times_ef: mul_ef port map ( in_ef => y2, out_ef => y2_ef ); y2_times_5b: mul_5b port map ( in_5b => y2, out_5b => y2_5b ); y3_times_ef: mul_ef port map ( in_ef => y3, out_ef => y3_ef ); y3_times_5b: mul_5b port map ( in_5b => y3, out_5b => y3_5b ); -- we perform the addition of the partial results in order to receive -- the table output -- z0 = y0*01 + y1*ef + y2*5b + y3*5b , opoy + bazoyme XOR z0 <= y0 XOR y1_ef XOR y2_5b XOR y3_5b; -- z1 = y0*5b + y1*ef + y2*ef + y3*01 z1 <= y0_5b XOR y1_ef XOR y2_ef XOR y3; -- z2 = y0*ef + y1*5b + y2*01 +y3*ef z2 <= y0_ef XOR y1_5b XOR y2 XOR y3_ef; -- z3 = y0*ef + y1*01 + y2*ef + y3*5b z3 <= y0_ef XOR y1 XOR y2_ef XOR y3_5b;end mds_arch;-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ---- ---- new component --
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -