1 |
2 |
pfulgoni |
|
2 |
|
|
--------------------------------------------------------------------------------
|
3 |
|
|
-- Designer: Paolo Fulgoni <pfulgoni@opencores.org>
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 09/14/2007
|
6 |
7 |
pfulgoni |
-- Last Update: 04/09/2008
|
7 |
2 |
pfulgoni |
-- Project Name: camellia-vhdl
|
8 |
|
|
-- Description: FL and FL^-1 functions, only for 128-bit key en/decryption
|
9 |
|
|
--
|
10 |
|
|
-- Copyright (C) 2007 Paolo Fulgoni
|
11 |
|
|
-- This file is part of camellia-vhdl.
|
12 |
|
|
-- camellia-vhdl is free software; you can redistribute it and/or modify
|
13 |
|
|
-- it under the terms of the GNU General Public License as published by
|
14 |
|
|
-- the Free Software Foundation; either version 3 of the License, or
|
15 |
|
|
-- (at your option) any later version.
|
16 |
|
|
-- camellia-vhdl is distributed in the hope that it will be useful,
|
17 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
|
|
-- GNU General Public License for more details.
|
20 |
|
|
-- You should have received a copy of the GNU General Public License
|
21 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22 |
|
|
--
|
23 |
|
|
-- The Camellia cipher algorithm is 128 bit cipher developed by NTT and
|
24 |
|
|
-- Mitsubishi Electric researchers.
|
25 |
|
|
-- http://info.isl.ntt.co.jp/crypt/eng/camellia/
|
26 |
|
|
--------------------------------------------------------------------------------
|
27 |
|
|
library IEEE;
|
28 |
|
|
use IEEE.std_logic_1164.all;
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
entity FL128 is
|
32 |
|
|
generic (
|
33 |
|
|
fl_ke_offset : INTEGER; -- encryption
|
34 |
|
|
fl_ke_shift : INTEGER;
|
35 |
|
|
fli_ke_offset : INTEGER;
|
36 |
|
|
fli_ke_shift : INTEGER;
|
37 |
|
|
fl_kd_offset : INTEGER; -- decryption
|
38 |
|
|
fl_kd_shift : INTEGER;
|
39 |
|
|
fli_kd_offset : INTEGER;
|
40 |
|
|
fli_kd_shift : INTEGER
|
41 |
|
|
);
|
42 |
|
|
port(
|
43 |
|
|
reset : in STD_LOGIC;
|
44 |
|
|
clk : in STD_LOGIC;
|
45 |
|
|
fl_in : in STD_LOGIC_VECTOR (0 to 63);
|
46 |
|
|
fli_in : in STD_LOGIC_VECTOR (0 to 63);
|
47 |
|
|
k : in STD_LOGIC_VECTOR (0 to 255);
|
48 |
|
|
dec : in STD_LOGIC;
|
49 |
|
|
fl_out : out STD_LOGIC_VECTOR (0 to 63);
|
50 |
|
|
fli_out : out STD_LOGIC_VECTOR (0 to 63)
|
51 |
|
|
);
|
52 |
|
|
end FL128;
|
53 |
|
|
|
54 |
|
|
architecture RTL of FL128 is
|
55 |
|
|
|
56 |
|
|
signal fl_in_l : STD_LOGIC_VECTOR (0 to 31);
|
57 |
|
|
signal fl_in_r : STD_LOGIC_VECTOR (0 to 31);
|
58 |
|
|
signal fli_in_l : STD_LOGIC_VECTOR (0 to 31);
|
59 |
|
|
signal fli_in_r : STD_LOGIC_VECTOR (0 to 31);
|
60 |
|
|
|
61 |
|
|
signal tmp_fl_ke : STD_LOGIC_VECTOR (0 to 127); -- encryption
|
62 |
|
|
signal tmp_fli_ke : STD_LOGIC_VECTOR (0 to 127);
|
63 |
|
|
signal tmp_fl_kd : STD_LOGIC_VECTOR (0 to 127); -- decryption
|
64 |
|
|
signal tmp_fli_kd : STD_LOGIC_VECTOR (0 to 127);
|
65 |
|
|
|
66 |
|
|
signal fl_k_l : STD_LOGIC_VECTOR (0 to 31);
|
67 |
|
|
signal fl_k_r : STD_LOGIC_VECTOR (0 to 31);
|
68 |
|
|
signal fli_k_l : STD_LOGIC_VECTOR (0 to 31);
|
69 |
|
|
signal fli_k_r : STD_LOGIC_VECTOR (0 to 31);
|
70 |
|
|
|
71 |
|
|
signal fl_a1 : STD_LOGIC_VECTOR (0 to 31);
|
72 |
|
|
signal fl_a2 : STD_LOGIC_VECTOR (0 to 31);
|
73 |
|
|
signal fl_b1 : STD_LOGIC_VECTOR (0 to 31);
|
74 |
|
|
signal fl_b2 : STD_LOGIC_VECTOR (0 to 31);
|
75 |
|
|
signal fli_a1 : STD_LOGIC_VECTOR (0 to 31);
|
76 |
|
|
signal fli_a2 : STD_LOGIC_VECTOR (0 to 31);
|
77 |
|
|
signal fli_b1 : STD_LOGIC_VECTOR (0 to 31);
|
78 |
|
|
signal fli_b2 : STD_LOGIC_VECTOR (0 to 31);
|
79 |
|
|
|
80 |
|
|
-- registers
|
81 |
|
|
signal reg_fl_in : STD_LOGIC_VECTOR (0 to 63);
|
82 |
|
|
signal reg_fli_in : STD_LOGIC_VECTOR (0 to 63);
|
83 |
|
|
|
84 |
|
|
begin
|
85 |
|
|
|
86 |
|
|
REG : process(reset, clk)
|
87 |
|
|
begin
|
88 |
|
|
|
89 |
|
|
if (reset = '1') then
|
90 |
|
|
reg_fl_in <= (others=>'0');
|
91 |
|
|
reg_fli_in <= (others=>'0');
|
92 |
|
|
else
|
93 |
7 |
pfulgoni |
if (rising_edge(clk)) then -- rising clock edge
|
94 |
2 |
pfulgoni |
reg_fl_in <= fl_in;
|
95 |
|
|
reg_fli_in <= fli_in;
|
96 |
|
|
end if;
|
97 |
|
|
end if;
|
98 |
|
|
end process;
|
99 |
|
|
|
100 |
|
|
--FL function
|
101 |
|
|
fl_in_l <= reg_fl_in(0 to 31);
|
102 |
|
|
fl_in_r <= reg_fl_in(32 to 63);
|
103 |
|
|
|
104 |
|
|
tmp_fl_ke <= k(fl_ke_offset+fl_ke_shift to fl_ke_offset+127) &
|
105 |
|
|
k(fl_ke_offset to fl_ke_offset+fl_ke_shift-1);
|
106 |
|
|
tmp_fl_kd <= k(fl_kd_offset+fl_kd_shift to fl_kd_offset+127) &
|
107 |
|
|
k(fl_kd_offset to fl_kd_offset+fl_kd_shift-1);
|
108 |
|
|
fl_k_l <= tmp_fl_ke(0 to 31) when dec='0' else tmp_fl_kd(64 to 95);
|
109 |
|
|
fl_k_r <= tmp_fl_ke(32 to 63) when dec='0' else tmp_fl_kd(96 to 127);
|
110 |
|
|
|
111 |
|
|
fl_a1 <= fl_in_l and fl_k_l;
|
112 |
|
|
fl_a2 <= (fl_a1(1 to 31) & fl_a1(0)) xor fl_in_r;
|
113 |
|
|
|
114 |
|
|
fl_b1 <= fl_a2 or fl_k_r;
|
115 |
|
|
fl_b2 <= fl_in_l xor fl_b1;
|
116 |
|
|
|
117 |
|
|
fl_out <= fl_b2 & fl_a2;
|
118 |
|
|
|
119 |
|
|
--FL^-1 function
|
120 |
|
|
fli_in_l <= reg_fli_in(0 to 31);
|
121 |
|
|
fli_in_r <= reg_fli_in(32 to 63);
|
122 |
|
|
|
123 |
|
|
tmp_fli_ke <= k(fli_ke_offset+fli_ke_shift to fli_ke_offset+127) &
|
124 |
|
|
k(fli_ke_offset to fli_ke_offset+fli_ke_shift-1);
|
125 |
|
|
tmp_fli_kd <= k(fli_kd_offset+fli_kd_shift to fli_kd_offset+127) &
|
126 |
|
|
k(fli_kd_offset to fli_kd_offset+fli_kd_shift-1);
|
127 |
|
|
fli_k_l <= tmp_fli_ke(64 to 95) when dec='0' else tmp_fli_kd(0 to 31);
|
128 |
|
|
fli_k_r <= tmp_fli_ke(96 to 127) when dec='0' else tmp_fli_kd(32 to 63);
|
129 |
|
|
|
130 |
|
|
fli_a1 <= fli_in_r or fli_k_r;
|
131 |
|
|
fli_a2 <= fli_in_l xor fli_a1;
|
132 |
|
|
|
133 |
|
|
fli_b1 <= fli_a2 and fli_k_l;
|
134 |
|
|
fli_b2 <= (fli_b1(1 to 31) & fli_b1(0)) xor fli_in_r;
|
135 |
|
|
|
136 |
|
|
fli_out <= fli_a2 & fli_b2;
|
137 |
|
|
|
138 |
|
|
end RTL;
|