library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
entity TEA is
port(
clk : in std_logic;
input : in std_logic_vector (63 downto 0);
key : in std_logic_vector (127 downto 0);
encript, decript : out std_logic_vector (63 downto 0)
);
end TEA;
architecture TEA of TEA is
signal k0, k1, k2, k3: std_logic_vector(31 downto 0);
signal n: integer:=0;
begin
k0<=key(127 downto 96);
k1<=key(95 downto 64);
k2<=key(63 downto 32);
k3<=key(31 downto 0);
process (clk, input)
variable z, z1, z2,z3,zt,y, y1, y2, y3, yt: std_logic_vector (31 downto 0);
variable sum: std_logic_vector (31 downto 0) ;
variable delta: std_logic_vector (31 downto 0) ;
begin
delta := x"9E3779B9";
sum := x"00000000";
if rising_edge(clk) then
if (n=0) then
y:= input(63 downto 32);
z:= input(31 downto 0);
else if (n=32) then
sum:=delta(26 downto 0) & "00000";
end if;
if (n<32) then
sum:=sum+delta;
z1:= ((z(27 downto 0) &"0000")+k0);
z2:= (z+sum);
z3:= (("00000"& z(31 downto 5))+k1);
zt:= z1 xor z2 xor z3;
y := y+zt;
y1:= ((y(27 downto 0) &"0000")+k2);
y2:= (y+sum);
y3:= (("00000"& y(31 downto 5))+k3);
yt:= y1 xor y2 xor y3;
z := z+yt;
encript <= y&z;
else if (n<64) then
y1:= ((y(27 downto 0) &"0000")+k2);
y2:= (y+sum);
y3:= (("00000"& y(31 downto 5))+k3);
yt:= y1 xor y2 xor y3;
z := z-yt;
z1:= ((z(27 downto 0) &"0000")+k0);
z2:= (z+sum);
z3:= (("00000"& z(31 downto 5))+k1);
zt:= z1 xor z2 xor z3;
y := y-zt;
sum := sum-delta;
Decript <= y&z;
end if;
end if;
n<=n+1;
end if;
end if;
end process;
end TEA;