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, for 128/192/256-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 FL256 is
|
32 |
|
|
generic (
|
33 |
|
|
fl_ke128_offset : INTEGER; -- 128bit encryption
|
34 |
|
|
fl_ke128_shift : INTEGER;
|
35 |
|
|
fli_ke128_offset : INTEGER;
|
36 |
|
|
fli_ke128_shift : INTEGER;
|
37 |
|
|
fl_kd128_offset : INTEGER; -- 128bit decryption
|
38 |
|
|
fl_kd128_shift : INTEGER;
|
39 |
|
|
fli_kd128_offset : INTEGER;
|
40 |
|
|
fli_kd128_shift : INTEGER;
|
41 |
|
|
fl_ke256_offset : INTEGER; -- 192/256bit encryption
|
42 |
|
|
fl_ke256_shift : INTEGER;
|
43 |
|
|
fli_ke256_offset : INTEGER;
|
44 |
|
|
fli_ke256_shift : INTEGER;
|
45 |
|
|
fl_kd256_offset : INTEGER; -- 192/256bit decryption
|
46 |
|
|
fl_kd256_shift : INTEGER;
|
47 |
|
|
fli_kd256_offset : INTEGER;
|
48 |
|
|
fli_kd256_shift : INTEGER
|
49 |
|
|
);
|
50 |
|
|
port(
|
51 |
|
|
reset : in STD_LOGIC;
|
52 |
|
|
clk : in STD_LOGIC;
|
53 |
|
|
fl_in : in STD_LOGIC_VECTOR (0 to 63);
|
54 |
|
|
fli_in : in STD_LOGIC_VECTOR (0 to 63);
|
55 |
|
|
k : in STD_LOGIC_VECTOR (0 to 511);
|
56 |
|
|
k_len : in STD_LOGIC_VECTOR (0 to 1);
|
57 |
|
|
dec : in STD_LOGIC;
|
58 |
|
|
fl_out : out STD_LOGIC_VECTOR (0 to 63);
|
59 |
|
|
fli_out : out STD_LOGIC_VECTOR (0 to 63)
|
60 |
|
|
);
|
61 |
|
|
end FL256;
|
62 |
|
|
|
63 |
|
|
architecture RTL of FL256 is
|
64 |
|
|
|
65 |
|
|
signal fl_in_l : STD_LOGIC_VECTOR (0 to 31);
|
66 |
|
|
signal fl_in_r : STD_LOGIC_VECTOR (0 to 31);
|
67 |
|
|
signal fli_in_l : STD_LOGIC_VECTOR (0 to 31);
|
68 |
|
|
signal fli_in_r : STD_LOGIC_VECTOR (0 to 31);
|
69 |
|
|
|
70 |
|
|
signal fl_ke128 : STD_LOGIC_VECTOR (0 to 127); -- 128bit encryption
|
71 |
|
|
signal fli_ke128 : STD_LOGIC_VECTOR (0 to 127);
|
72 |
|
|
signal fl_kd128 : STD_LOGIC_VECTOR (0 to 127); -- 128bit decryption
|
73 |
|
|
signal fli_kd128 : STD_LOGIC_VECTOR (0 to 127);
|
74 |
|
|
signal fl_ke256 : STD_LOGIC_VECTOR (0 to 127); -- 192/256bit encryption
|
75 |
|
|
signal fli_ke256 : STD_LOGIC_VECTOR (0 to 127);
|
76 |
|
|
signal fl_kd256 : STD_LOGIC_VECTOR (0 to 127); -- 192/256bit decryption
|
77 |
|
|
signal fli_kd256 : STD_LOGIC_VECTOR (0 to 127);
|
78 |
|
|
signal fl_k_l : STD_LOGIC_VECTOR (0 to 31);
|
79 |
|
|
signal fl_k_r : STD_LOGIC_VECTOR (0 to 31);
|
80 |
|
|
signal fli_k_l : STD_LOGIC_VECTOR (0 to 31);
|
81 |
|
|
signal fli_k_r : STD_LOGIC_VECTOR (0 to 31);
|
82 |
|
|
|
83 |
|
|
signal fl_a1 : STD_LOGIC_VECTOR (0 to 31);
|
84 |
|
|
signal fl_a2 : STD_LOGIC_VECTOR (0 to 31);
|
85 |
|
|
signal fl_b1 : STD_LOGIC_VECTOR (0 to 31);
|
86 |
|
|
signal fl_b2 : STD_LOGIC_VECTOR (0 to 31);
|
87 |
|
|
signal fli_a1 : STD_LOGIC_VECTOR (0 to 31);
|
88 |
|
|
signal fli_a2 : STD_LOGIC_VECTOR (0 to 31);
|
89 |
|
|
signal fli_b1 : STD_LOGIC_VECTOR (0 to 31);
|
90 |
|
|
signal fli_b2 : STD_LOGIC_VECTOR (0 to 31);
|
91 |
|
|
|
92 |
|
|
-- registers
|
93 |
|
|
signal reg_fl_in : STD_LOGIC_VECTOR (0 to 63);
|
94 |
|
|
signal reg_fli_in : STD_LOGIC_VECTOR (0 to 63);
|
95 |
|
|
|
96 |
|
|
begin
|
97 |
|
|
|
98 |
|
|
REG : process(reset, clk)
|
99 |
|
|
begin
|
100 |
|
|
|
101 |
|
|
if (reset = '1') then
|
102 |
|
|
reg_fl_in <= (others=>'0');
|
103 |
|
|
reg_fli_in <= (others=>'0');
|
104 |
|
|
else
|
105 |
7 |
pfulgoni |
if (rising_edge(clk)) then -- rising clock edge
|
106 |
2 |
pfulgoni |
reg_fl_in <= fl_in;
|
107 |
|
|
reg_fli_in <= fli_in;
|
108 |
|
|
end if;
|
109 |
|
|
end if;
|
110 |
|
|
end process;
|
111 |
|
|
|
112 |
|
|
--FL function
|
113 |
|
|
fl_in_l <= reg_fl_in(0 to 31);
|
114 |
|
|
fl_in_r <= reg_fl_in(32 to 63);
|
115 |
|
|
|
116 |
|
|
fl_ke128 <= k(fl_ke128_offset+fl_ke128_shift to fl_ke128_offset+127) &
|
117 |
|
|
k(fl_ke128_offset to fl_ke128_offset+fl_ke128_shift-1);
|
118 |
|
|
fl_kd128 <= k(fl_kd128_offset+fl_kd128_shift to fl_kd128_offset+127) &
|
119 |
|
|
k(fl_kd128_offset to fl_kd128_offset+fl_kd128_shift-1);
|
120 |
|
|
fl_ke256 <= k(fl_ke256_offset+fl_ke256_shift to fl_ke256_offset+127) &
|
121 |
|
|
k(fl_ke256_offset to fl_ke256_offset+fl_ke256_shift-1);
|
122 |
|
|
fl_kd256 <= k(fl_kd256_offset+fl_kd256_shift to fl_kd256_offset+127) &
|
123 |
|
|
k(fl_kd256_offset to fl_kd256_offset+fl_kd256_shift-1);
|
124 |
|
|
fl_k_l <= fl_ke128(0 to 31) when dec='0' and k_len(0)='0' else
|
125 |
|
|
fl_kd128(64 to 95) when dec='1' and k_len(0)='0' else
|
126 |
|
|
fl_ke256(0 to 31) when dec='0' and k_len(0)='1' else
|
127 |
|
|
fl_kd256(64 to 95);
|
128 |
|
|
fl_k_r <= fl_ke128(32 to 63) when dec='0' and k_len(0)='0' else
|
129 |
|
|
fl_kd128(96 to 127) when dec='1' and k_len(0)='0' else
|
130 |
|
|
fl_ke256(32 to 63) when dec='0' and k_len(0)='1' else
|
131 |
|
|
fl_kd256(96 to 127);
|
132 |
|
|
|
133 |
|
|
fl_a1 <= fl_in_l and fl_k_l;
|
134 |
|
|
fl_a2 <= (fl_a1(1 to 31) & fl_a1(0)) xor fl_in_r;
|
135 |
|
|
|
136 |
|
|
fl_b1 <= fl_a2 or fl_k_r;
|
137 |
|
|
fl_b2 <= fl_in_l xor fl_b1;
|
138 |
|
|
|
139 |
|
|
fl_out <= fl_b2 & fl_a2;
|
140 |
|
|
|
141 |
|
|
--FL^-1 function
|
142 |
|
|
fli_in_l <= reg_fli_in(0 to 31);
|
143 |
|
|
fli_in_r <= reg_fli_in(32 to 63);
|
144 |
|
|
|
145 |
|
|
fli_ke128 <= k(fli_ke128_offset+fli_ke128_shift to fli_ke128_offset+127) &
|
146 |
|
|
k(fli_ke128_offset to fli_ke128_offset+fli_ke128_shift-1);
|
147 |
|
|
fli_kd128 <= k(fli_kd128_offset+fli_kd128_shift to fli_kd128_offset+127) &
|
148 |
|
|
k(fli_kd128_offset to fli_kd128_offset+fli_kd128_shift-1);
|
149 |
|
|
fli_ke256 <= k(fli_ke256_offset+fli_ke256_shift to fli_ke256_offset+127) &
|
150 |
|
|
k(fli_ke256_offset to fli_ke256_offset+fli_ke256_shift-1);
|
151 |
|
|
fli_kd256 <= k(fli_kd256_offset+fli_kd256_shift to fli_kd256_offset+127) &
|
152 |
|
|
k(fli_kd256_offset to fli_kd256_offset+fli_kd256_shift-1);
|
153 |
|
|
fli_k_l <= fli_ke128(64 to 95) when dec='0' and k_len(0)='0' else
|
154 |
|
|
fli_kd128(0 to 31) when dec='1' and k_len(0)='0' else
|
155 |
|
|
fli_ke256(64 to 95) when dec='0' and k_len(0)='1' else
|
156 |
|
|
fli_kd256(0 to 31);
|
157 |
|
|
fli_k_r <= fli_ke128(96 to 127) when dec='0' and k_len(0)='0' else
|
158 |
|
|
fli_kd128(32 to 63) when dec='1' and k_len(0)='0' else
|
159 |
|
|
fli_ke256(96 to 127) when dec='0' and k_len(0)='1' else
|
160 |
|
|
fli_kd256(32 to 63);
|
161 |
|
|
|
162 |
|
|
fli_a1 <= fli_in_r or fli_k_r;
|
163 |
|
|
fli_a2 <= fli_in_l xor fli_a1;
|
164 |
|
|
|
165 |
|
|
fli_b1 <= fli_a2 and fli_k_l;
|
166 |
|
|
fli_b2 <= (fli_b1(1 to 31) & fli_b1(0)) xor fli_in_r;
|
167 |
|
|
|
168 |
|
|
fli_out <= fli_a2 & fli_b2;
|
169 |
|
|
|
170 |
|
|
end RTL;
|