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

Subversion Repositories aes_decrypt_fpga

[/] [aes_decrypt_fpga/] [trunk/] [rtl/] [verilog/] [KeyExpand128.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
//// 128-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 128-bit key       ////
49
//// only.                                                                                                                      ////
50
////                                                                                                                            ////
51
////////////////////////////////////////////////////////////////////////
52
 
53
module KeyExpand128(
54
        // 128-bit key expander
55
 
56
        input   [0:127] 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,
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 current roundkey
69
        logic   [0:31]  w0;
70
        logic   [0:31]  w1;
71
        logic   [0:31]  w2;
72
        logic   [0:31]  w3;
73
 
74
        logic   [0:3]   keyexp_state;   // Key expansion state machine.
75
        logic   [0:7]   Rcon;                   // Round constant. See FIPS-197 section 5.3.
76
 
77
        wire    [0:31]  subword_out;
78
        wire    [0:31]  rotword_out;
79
        wire    [0:31]  w0_feed;
80
        wire    [0:31]  w1_feed;
81
        wire    [0:31]  w2_feed;
82
        wire    [0:31]  w3_feed;
83
 
84
        wire    keyexp_state_0;         // '1' indicates key expansion state machine at state 0 (initial state)
85
        wire    keyexp_state_10;        // '1' indicates key expansion state machine at state 10 (last state)
86
 
87
        // Do not remove the "keep" and "max_fanout" attribute. They are there to force the synthesizer
88
        // to infer independent logic for next_w0-3, instead of deriving next_w1 from next_w0, ...and
89
        // so on. See the definitions of next_w* below. This is to avoid getting a chain of LUTs, which reduces Fmax.
90
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w0;
91
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w1;
92
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w2;
93
        (* keep = "true", max_fanout = 1 *) wire        [0:31]  next_w3;
94
 
95
        assign w0_feed = (keyexp_state_0)? kt[0+:32] : w0;
96
        assign w1_feed = (keyexp_state_0)? kt[32+:32] : w1;
97
        assign w2_feed = (keyexp_state_0)? kt[64+:32] : w2;
98
        assign w3_feed = (keyexp_state_0)? kt[96+:32] : w3;
99
 
100
        RotWord RotWord_u(.din(w3_feed), .dout(rotword_out));
101
        SubWord SubWord_u(.din(rotword_out), .dout(subword_out));
102
 
103
        assign next_w0 = subword_out ^ {Rcon,24'h000000} ^ w0_feed;
104
        assign next_w1 = subword_out ^ {Rcon,24'h000000} ^ w0_feed ^ w1_feed;
105
        assign next_w2 = subword_out ^ {Rcon,24'h000000} ^ w0_feed ^ w1_feed ^ w2_feed;
106
        assign next_w3 = subword_out ^ {Rcon,24'h000000} ^ w0_feed ^ w1_feed ^ w2_feed ^ w3_feed;
107
 
108
        assign rkey = (keyexp_state_0)? kt : {w0,w1,w2,w3};
109
        assign kt_rdy = keyexp_state_0; // Only accept new key in initial state.
110
        assign rkey_vld = ~keyexp_state_0 | kt_vld;
111
        assign rkey_last = keyexp_state_10;
112
        assign keyexp_state_0 = (keyexp_state == 0);
113
        assign keyexp_state_10 = (keyexp_state == 10);
114
 
115
        // Key Expansion state machine
116
        always_ff @(posedge clk)
117
        begin
118
                if (rst)
119
                begin
120
                        keyexp_state <= 0;      // Reset to initial state
121
                        Rcon <= 8'h01;
122
                end
123
                else
124
                        unique case (keyexp_state)
125
 
126
                                        if (kt_vld)
127
                                        begin
128
                                                keyexp_state <= keyexp_state + 1;
129
                                                {w0,w1,w2,w3} <= {next_w0, next_w1, next_w2, next_w3};
130
                                                Rcon <= (Rcon[0])? (Rcon << 1) ^ 8'h1b : (Rcon << 1);   // Advance to next Rcon value.
131
                                        end
132
                                1,2,3,4,5,6,7,8,9 :
133
                                        // Proceed to next state and update roundkey register
134
                                        begin
135
                                                keyexp_state <= keyexp_state + 1;
136
                                                {w0,w1,w2,w3} <= {next_w0, next_w1, next_w2, next_w3};
137
                                                Rcon <= (Rcon[0])? (Rcon << 1) ^ 8'h1b : (Rcon << 1);   // Advance to next Rcon value.
138
                                        end
139
                                10:     // Wrap back to initial state and update roundkey register
140
                                        begin
141
                                                keyexp_state <= 0;
142
                                                {w0,w1,w2,w3} <= {next_w0, next_w1, next_w2, next_w3};
143
                                                Rcon <= 8'h01;
144
                                        end
145
                        endcase
146
        end
147
 
148
endmodule

powered by: WebSVN 2.1.0

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