OpenCores
URL https://opencores.org/ocsvn/cryptopan_core/cryptopan_core/trunk

Subversion Repositories cryptopan_core

[/] [cryptopan_core/] [trunk/] [rtl/] [cryptopan_unit.vhd] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tonyb33
--
2
-- This file is part of the Crypto-PAn core.
3
--
4
-- Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
5
-- Authors: Anthony Blake (tonyb33@opencores.org)
6
--          
7
-- All rights reserved.
8
--
9
-- This code has been developed by the University of Waikato WAND 
10
-- research group. For further information please see http://www.wand.net.nz/
11
--
12
-- This source file 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 2 of the License, or
15
-- (at your option) any later version.
16
--
17
-- This source is distributed in the hope that it will be useful,
18
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
19
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
-- GNU General Public License for more details.
21
--
22
-- You should have received a copy of the GNU General Public License
23
-- along with libtrace; if not, write to the Free Software
24
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
--
26
 
27
library ieee;
28
use ieee.std_logic_1164.all;
29
use ieee.std_logic_unsigned.all;
30
 
31
use work.cryptopan.all;
32
 
33
entity cryptopan_unit is
34
 
35
  port (
36
    clk      : in  std_logic;
37
    reset    : in  std_logic;
38
    ready    : out std_logic;
39
    key      : in  std_logic_vector(255 downto 0);
40
    key_wren : in  std_logic;
41
    ip_in    : in  std_logic_vector(31 downto 0);
42
    ip_wren  : in  std_logic;
43
    ip_out   : out std_logic_vector(31 downto 0);
44
    ip_dv    : out std_logic
45
    );
46
 
47
end cryptopan_unit;
48
 
49
architecture rtl of cryptopan_unit is
50
 
51
  component aes_encrypt_unit
52
    port (
53
      key_in    : in  std_logic_vector(127 downto 0);
54
      key_wren  : in  std_logic;
55
      ready     : out std_logic;
56
      data_in   : in  std_logic_vector(127 downto 0);
57
      data_wren : in  std_logic;
58
      data_dv   : out std_logic;
59
      data_out  : out std_logic_vector(127 downto 0);
60
      clk       : in  std_logic;
61
      reset     : in  std_logic);
62
  end component;
63
 
64
  signal aes_din, aes_dout         : std_logic_vector(127 downto 0);
65
  signal aes_din_wren, aes_dout_dv : std_logic;
66
  signal ready_int                 : std_logic;
67
  signal m_pad                     : std_logic_vector(127 downto 0);
68
 
69
  signal ip_reg      : std_logic_vector(31 downto 0);
70
  signal read_ip_reg : std_logic_vector(31 downto 0);
71
 
72
  type states is (INIT, INITWAIT, IDLE, BUSY);
73
  signal state : states;
74
 
75
  type read_states is (INIT, IDLE, BUSY);
76
  signal read_state : read_states;
77
 
78
  type output_states is (IDLE, BUSY);
79
  signal output_state : output_states;
80
 
81
--  signal shift_counter : std_logic_vector(31 downto 0);
82
  signal output_counter : std_logic_vector(4 downto 0);
83
 
84
 
85
  signal first4bytes_pad   : std_logic_vector(31 downto 0);
86
  signal first4bytes_input : std_logic_vector(31 downto 0);
87
 
88
  signal mask_onehot     : std_logic_vector(31 downto 0);
89
  signal mask_onehot_inv : std_logic_vector(31 downto 0);
90
 
91
  signal ip_out_int : std_logic_vector(31 downto 0);
92
 
93
begin
94
 
95
  mask_onehot_inv <= not mask_onehot;
96
 
97
  with state select
98
    ready <=
99
    '1' when IDLE,
100
    '0' when others;
101
 
102
  first4bytes_pad   <= m_pad(127 downto 96);
103
  first4bytes_input <= (ip_reg and mask_onehot_inv) or (first4bytes_pad and mask_onehot);
104
 
105
 
106
  LOAD_UNIT_LOGIC : process (clk, reset)
107
  begin
108
    if reset = '1' then
109
      state        <= INIT;
110
      aes_din_wren <= '0';
111
      aes_din      <= (others => '0');
112
      mask_onehot  <= (others => '0');
113
      ip_reg       <= (others => '0');
114
    elsif clk'event and clk = '1' then
115
      mask_onehot  <= (others => '1');
116
      aes_din_wren <= '0';
117
 
118
      if state = INIT and ready_int = '1' then
119
        aes_din      <= key(255 downto 128);
120
        aes_din_wren <= '1';
121
        state        <= INITWAIT;
122
      elsif state = INITWAIT and aes_dout_dv = '1' then
123
        state        <= IDLE;
124
      elsif state = IDLE and ip_wren = '1' then
125
        state        <= BUSY;
126
        ip_reg       <= ip_in;
127
      elsif state = BUSY then
128
 
129
        if mask_onehot(0) = '1' then
130
          aes_din_wren <= '1';
131
          aes_din      <= first4bytes_input & m_pad(95 downto 0);
132
        else
133
          state        <= IDLE;
134
        end if;
135
 
136
        mask_onehot(31)  <= '0';
137
        for i in 30 downto 0 loop
138
          mask_onehot(i) <= mask_onehot(i+1);
139
        end loop;
140
 
141
      end if;
142
 
143
    end if;
144
  end process LOAD_UNIT_LOGIC;
145
 
146
 
147
  READ_UNIT_LOGIC : process (clk, reset)
148
  begin
149
    if reset = '1' then
150
      m_pad          <= (others => '0');
151
 
152
      read_state     <= INIT;
153
      ip_out         <= (others => '0');
154
      ip_dv          <= '0';
155
      output_state   <= IDLE;
156
      read_ip_reg    <= (others => '0');
157
      output_counter <= (others => '1');
158
    elsif clk'event and clk = '1' then
159
 
160
      ip_dv <= '0';
161
 
162
      if read_state = INIT then
163
        if aes_dout_dv = '1' then
164
          m_pad      <= aes_dout;
165
          read_state <= IDLE;
166
        end if;
167
 
168
 
169
      elsif read_state = IDLE then
170
 
171
        if aes_dout_dv = '1' then
172
          if output_counter = "11111" then
173
            read_ip_reg <= ip_reg;
174
          end if;
175
          output_counter <= output_counter - 1;
176
 
177
          ip_out_int <= ip_out_int(30 downto 0) & aes_dout(127);
178
 
179
        end if;
180
        if output_counter = "00000" then
181
          output_state <= BUSY;
182
        end if;
183
 
184
      end if;
185
 
186
      if output_state = BUSY then
187
        output_state <= IDLE;
188
        ip_dv        <= '1';
189
        ip_out       <= ip_out_int xor read_ip_reg;
190
      end if;
191
 
192
    end if;
193
  end process READ_UNIT_LOGIC;
194
 
195
-- OUTPUT_UNIT_LOGIC : process (clk, reset)
196
-- begin
197
-- if reset = '1' then
198
-- ip_out <= (others => '0');
199
-- ip_dv <= '0';
200
-- output_state <= IDLE;
201
-- elsif clk'event and clk = '1' then
202
 
203
-- end if;
204
-- end process OUTPUT_UNIT_LOGIC;
205
 
206
  AES0 : aes_encrypt_unit
207
    port map (
208
      key_in    => key(127 downto 0),
209
      key_wren  => key_wren,
210
      ready     => ready_int,
211
      data_in   => aes_din,
212
      data_wren => aes_din_wren,
213
      data_dv   => aes_dout_dv,
214
      data_out  => aes_dout,
215
      clk       => clk,
216
      reset     => reset);
217
 
218
 
219
 
220
end rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.