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

Subversion Repositories aes_decrypt_fpga

[/] [aes_decrypt_fpga/] [trunk/] [rtl/] [verilog/] [KeyExpand256.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 schengopen
////////////////////////////////////////////////////////////////// ////
2
////                                                                                                                            ////
3
//// AES Decryption Core for FPGA                                                                       ////
4
////                                                                                                                            ////
5
//// This file is part of the AES Decryption Core for FPGA project      ////
6
//// http://www.opencores.org/cores/xxx/                                                        ////
7
////                                                                                                                            ////
8
//// Description                                                                                                        ////
9
//// Implementation of  AES Decryption Core for FPGA according to       ////
10
//// core specification document.                                                                       ////
11
////                                                                                                                            ////
12
//// To Do:                                                                                                             ////
13
//// -                                                                                                                          ////
14
////                                                                                                                            ////
15
//// Author(s):                                                                                                         ////
16
//// - scheng, schengopencores@opencores.org                                            ////
17
////                                                                                                                            ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                                                                                            ////
20
//// Copyright (C) 2009 Authors and OPENCORES.ORG                                       ////
21
////                                                                                                                            ////
22
//// This source file may be used and distributed without                       ////
23
//// restriction provided that this copyright statement is not          ////
24
//// removed from the file and that any derivative work contains        ////
25
//// the original copyright notice and the associated disclaimer.       ////
26
////                                                                                                                            ////
27
//// This source file is free software; you can redistribute it         ////
28
//// and/or modify it under the terms of the GNU Lesser General         ////
29
//// Public License as published by the Free Software Foundation;       ////
30
//// either version 2.1 of the License, or (at your option) any         ////
31
//// later version.                                                                                             ////
32
////                                                                                                                            ////
33
//// This source is distributed in the hope that it will be             ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
36
//// PURPOSE. See the GNU Lesser General Public License for more        ////
37
//// details.                                                                                                           ////
38
////                                                                                                                            ////
39
//// You should have received a copy of the GNU Lesser General          ////
40
//// Public License along with this source; if not, download it         ////
41
//// from http://www.opencores.org/lgpl.shtml                                           ////
42
////                                                                                                                            //// ///
43
///////////////////////////////////////////////////////////////////
44
////                                                                                                                            ////
45
//// 256-bit key expander                                                                                       ////
46
////                                                                                                                            ////
47
//// The key expansion algorithm is described in section 5.2 of the     ////
48
//// FIPS-197 spec. This file implements the case for 256-bit key       ////
49
//// only.                                                                                                                      ////
50
////                                                                                                                            ////
51
////////////////////////////////////////////////////////////////////////
52
 
53
module KeyExpand256(
54
        // 256-bit key expander
55
 
56
        input   [0:255] kt,
57
        input   kt_vld,         // Active high input informing key expander that a valid new key is present at kt.
58
        output  kt_rdy,         // Active high output indicates key expander ready to accept new key
59
 
60
        output  [0:127] rkey,   // Note : rkey is always 128 bit regardless the crypto key length
61
        output  rkey_vld,       // Active high output indicates valid roundkey available at rkey[0:127]
62
        output  rkey_last,      // High for 1 clock cycle, indicates last roundkey available at rkey[0:127].
63
 
64
        input   clk,
65
        input   rst
66
        );
67
 
68
        // Registers holding the calculated roundkeys
69
        logic   [0:31]  w0;
70
        logic   [0:31]  w1;
71
        logic   [0:31]  w2;
72
        logic   [0:31]  w3;
73
        logic   [0:31]  w4;
74
        logic   [0:31]  w5;
75
        logic   [0:31]  w6;
76
        logic   [0:31]  w7;
77
 
78
        logic   [0:3]   keyexp_state;   // Key expansion state machine
79
        logic   [0:7]   Rcon;                   // Round constant. See FIPS-197 section 5.3.
80
        logic   [0:7]   Rcon_1;         // Shadow Rcon. For use to compute next Rcon value in key expansion state machine.
81
 
82
        wire    [0:31]  subword_out;
83
        wire    [0:31]  subword_in;
84
        wire    [0:31]  rotword_out;
85
        wire    [0:31]  rotword_in;
86
 
87
        wire    keyexp_state_0;         // '1' indicates key expansion state machine at state 0 (initial state)
88
        wire    keyexp_state_14;        // '1' indicates key expansion state machine at state 14 (last state)
89
 
90
        // Do not remove the "keep" and "max_fanout" attribute. They are there to force the synthesizer
91
        // to infer independent logic for next_w*, instead of deriving next_w1 from next_w0, ...and
92
        // so on. See the definitions of next_w* below. This is to avoid getting a chain of LUTs, which reduces Fmax.
93
        wire    [0:31]  next_w0;
94
        wire    [0:31]  next_w1;
95
        wire    [0:31]  next_w2;
96
        wire    [0:31]  next_w3;
97
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w4;
98
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w5;
99
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w6;
100
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w7;
101
 
102
 
103
        assign rotword_in = (keyexp_state_0)? kt[224:255] : w7;
104
        RotWord RotWord_u(.din(rotword_in), .dout(rotword_out));
105
 
106
        // keyexp_state[3] = 1 corresponds to states 1,3,5,7,...(odd states), which in turn corresponds to
107
        // the condition ((Nk > 6) && (i mod Nk == 4)) in the key expansion algorithm. See section 5.2 fig.
108
        // 11 of FIPS-197.
109
        assign subword_in = (keyexp_state[3])? rotword_in : rotword_out;
110
        SubWord SubWord_u(.din(subword_in), .dout(subword_out));
111
 
112
        assign next_w0 = (keyexp_state_0)? kt[128+:32] : w4;
113
        assign next_w1 = (keyexp_state_0)? kt[160+:32] : w5;
114
        assign next_w2 = (keyexp_state_0)? kt[192+:32] : w6;
115
        assign next_w3 = (keyexp_state_0)? kt[224+:32] : w7;
116
        assign next_w4 = (keyexp_state_0)? (subword_out ^ {Rcon,24'h000000} ^ kt[0+:32]) : (subword_out ^ {Rcon,24'h000000} ^ w0);
117
        assign next_w5 = (keyexp_state_0)? (subword_out ^ {Rcon,24'h000000} ^ kt[0+:32] ^ kt[32+:32]) : (subword_out ^ {Rcon,24'h000000} ^ w0 ^ w1);
118
        assign next_w6 = (keyexp_state_0)? (subword_out ^ {Rcon,24'h000000} ^ kt[0+:32] ^ kt[32+:32] ^ kt[64+:32]) : (subword_out ^ {Rcon,24'h000000} ^ w0 ^ w1 ^ w2);
119
        assign next_w7 = (keyexp_state_0)? (subword_out ^ {Rcon,24'h000000} ^ kt[0+:32] ^ kt[32+:32] ^ kt[64+:32] ^ kt[96+:32]) : (subword_out ^ {Rcon,24'h000000} ^ w0 ^ w1 ^ w2 ^ w3);
120
 
121
        assign rkey = (keyexp_state_0)? kt[0:127] : {w0,w1,w2,w3};
122
        assign kt_rdy = keyexp_state_0; // Only accept new key in initial state.
123
        assign rkey_vld = ~keyexp_state_0 | kt_vld;
124
        assign rkey_last = keyexp_state_14;
125
        assign keyexp_state_0 = (keyexp_state == 0);
126
        assign keyexp_state_14 = (keyexp_state == 14);
127
 
128
        // Key Expansion state machine
129
        always_ff @(posedge clk)
130
        begin
131
                if (rst)
132
                begin
133
                        keyexp_state <= 0;      // Reset to initial state
134
                        Rcon <= 8'h01;
135
                        Rcon_1 <= 8'h01;
136
                end
137
                else
138
                        unique case (keyexp_state)
139
 
140
                                        if (kt_vld)
141
                                        begin
142
                                                keyexp_state <= keyexp_state + 1;
143
                                                {w0,w1,w2,w3,w4,w5,w6,w7} <= {next_w0, next_w1, next_w2, next_w3, next_w4, next_w5, next_w6, next_w7};
144
                                                Rcon_1 <= Rcon;
145
                                                Rcon <= 0;
146
                                        end
147
                                2,4,6,8,10,12 :
148
                                        // Proceed to next state and update roundkey register
149
                                        begin
150
                                                keyexp_state <= keyexp_state + 1;
151
                                                {w0,w1,w2,w3,w4,w5,w6,w7} <= {next_w0, next_w1, next_w2, next_w3, next_w4, next_w5, next_w6, next_w7};
152
                                                Rcon_1 <= Rcon;
153
                                                Rcon <= 0;
154
                                        end
155
                                1,3,5,7,9,11,13 :
156
                                        // Proceed to next state and update to roundkey register, also
157
                                        // advance Rcon to next value.
158
                                        begin
159
                                                keyexp_state <= keyexp_state + 1;
160
                                                {w0,w1,w2,w3,w4,w5,w6,w7} <= {next_w0, next_w1, next_w2, next_w3, next_w4, next_w5, next_w6, next_w7};
161
                                                Rcon <= (Rcon_1[0])? (Rcon_1 << 1) ^ 8'h1b : (Rcon_1 << 1);
162
 
163
                                        end
164
                                14:     // Wrap back to initial state
165
                                        begin
166
                                                keyexp_state <= 0;
167
                                                Rcon <= 8'h01;
168
                                        end
169
                        endcase
170
        end
171
 
172
endmodule

powered by: WebSVN 2.1.0

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