OpenCores
URL https://opencores.org/ocsvn/gcm-aes/gcm-aes/trunk

Subversion Repositories gcm-aes

[/] [gcm-aes/] [trunk/] [rtl/] [aes_cipher_top.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tariq786
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  AES Cipher Top Level                                       ////
4
////                                                             ////
5
////                                                             ////
6
////  Author: Rudolf Usselmann                                   ////
7
////          rudi@asics.ws                                      ////
8
////                                                             ////
9
////                                                             ////
10
////  Downloaded from: http://www.opencores.org/cores/aes_core/  ////
11
////                                                             ////
12
/////////////////////////////////////////////////////////////////////
13
////                                                             ////
14
//// Copyright (C) 2000-2002 Rudolf Usselmann                    ////
15
////                         www.asics.ws                        ////
16
////                         rudi@asics.ws                       ////
17
////                                                             ////
18
//// This source file may be used and distributed without        ////
19
//// restriction provided that this copyright statement is not   ////
20
//// removed from the file and that any derivative work contains ////
21
//// the original copyright notice and the associated disclaimer.////
22
////                                                             ////
23
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
24
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
25
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
26
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
27
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
28
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
29
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
30
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
31
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
32
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
33
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
34
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
35
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
36
////                                                             ////
37
/////////////////////////////////////////////////////////////////////
38
 
39
//  CVS Log
40
//
41
//  $Id: aes_cipher_top.v,v 1.1.1.1 2002-11-09 11:22:48 rudi Exp $
42
//
43
//  $Date: 2002-11-09 11:22:48 $
44
//  $Revision: 1.1.1.1 $
45
//  $Author: rudi $
46
//  $Locker:  $
47
//  $State: Exp $
48
//
49
// Change History:
50
//               $Log: not supported by cvs2svn $
51
//
52
//
53
//
54
//
55
//
56
 
57
 
58
`timescale 1 ns/1 ps
59
 
60
module aes_cipher_top(clk, rst, ld, done, key, text_in, text_out);
61
input           clk, rst;
62
input           ld;
63
output          done;
64
input   [127:0]  key;
65
input   [127:0]  text_in;
66
output  [127:0]  text_out;
67
 
68
////////////////////////////////////////////////////////////////////
69
//
70
// Local Wires
71
//
72
 
73
wire    [31:0]   w0, w1, w2, w3;
74
reg     [127:0]  text_in_r;
75
reg     [127:0]  text_out;
76
 
77
 
78
reg     [7:0]    sa00, sa01, sa02, sa03;
79
reg     [7:0]    sa10, sa11, sa12, sa13;
80
reg     [7:0]    sa20, sa21, sa22, sa23;
81
reg     [7:0]    sa30, sa31, sa32, sa33;
82
 
83
wire    [7:0]    sa00_next, sa01_next, sa02_next, sa03_next;
84
wire    [7:0]    sa10_next, sa11_next, sa12_next, sa13_next;
85
wire    [7:0]    sa20_next, sa21_next, sa22_next, sa23_next;
86
wire    [7:0]    sa30_next, sa31_next, sa32_next, sa33_next;
87
 
88
wire    [7:0]    sa00_sub, sa01_sub, sa02_sub, sa03_sub;
89
wire    [7:0]    sa10_sub, sa11_sub, sa12_sub, sa13_sub;
90
wire [7:0]       sa20_sub, sa21_sub, sa22_sub, sa23_sub;
91
wire    [7:0]    sa30_sub, sa31_sub, sa32_sub, sa33_sub;
92
 
93
wire    [7:0]    sa00_sr, sa01_sr, sa02_sr, sa03_sr;
94
wire    [7:0]    sa10_sr, sa11_sr, sa12_sr, sa13_sr;
95
wire    [7:0]    sa20_sr, sa21_sr, sa22_sr, sa23_sr;
96
wire    [7:0]    sa30_sr, sa31_sr, sa32_sr, sa33_sr;
97
 
98
wire    [7:0]    sa00_mc, sa01_mc, sa02_mc, sa03_mc;
99
wire    [7:0]    sa10_mc, sa11_mc, sa12_mc, sa13_mc;
100
wire    [7:0]    sa20_mc, sa21_mc, sa22_mc, sa23_mc;
101
wire    [7:0]    sa30_mc, sa31_mc, sa32_mc, sa33_mc;
102
 
103
reg             done, ld_r;
104
reg     [3:0]    dcnt;
105
 
106
////////////////////////////////////////////////////////////////////
107
//
108
// Misc Logic
109
//
110
 
111
always @(posedge clk or posedge rst)
112
 
113
begin
114
        if(rst) begin dcnt <=  4'h0;     end
115
        else
116
        if(ld)  begin   dcnt <=  4'hb;   end
117
        else
118
        if(|dcnt) begin dcnt <=  dcnt - 4'h1;  end
119
 
120
end
121
 
122
always @(posedge clk) done <=  !(|dcnt[3:1]) & dcnt[0] & !ld;
123
always @(posedge clk) if(ld) text_in_r <=  text_in;
124
always @(posedge clk) ld_r <=  ld;
125
 
126
 
127
////////////////////////////////////////////////////////////////////
128
//
129
// Initial Permutation (AddRoundKey)
130
//
131
 
132
always @(posedge clk)           sa33 <=  ld_r ? text_in_r[007:000] ^ w3[07:00] : sa33_next;
133
always @(posedge clk)           sa23 <=  ld_r ? text_in_r[015:008] ^ w3[15:08] : sa23_next;
134
always @(posedge clk)           sa13 <=  ld_r ? text_in_r[023:016] ^ w3[23:16] : sa13_next;
135
always @(posedge clk)           sa03 <=  ld_r ? text_in_r[031:024] ^ w3[31:24] : sa03_next;
136
always @(posedge clk)           sa32 <=  ld_r ? text_in_r[039:032] ^ w2[07:00] : sa32_next;
137
always @(posedge clk)           sa22 <=  ld_r ? text_in_r[047:040] ^ w2[15:08] : sa22_next;
138
always @(posedge clk)           sa12 <=  ld_r ? text_in_r[055:048] ^ w2[23:16] : sa12_next;
139
always @(posedge clk)           sa02 <=  ld_r ? text_in_r[063:056] ^ w2[31:24] : sa02_next;
140
always @(posedge clk)           sa31 <=  ld_r ? text_in_r[071:064] ^ w1[07:00] : sa31_next;
141
always @(posedge clk)           sa21 <=  ld_r ? text_in_r[079:072] ^ w1[15:08] : sa21_next;
142
always @(posedge clk)           sa11 <=  ld_r ? text_in_r[087:080] ^ w1[23:16] : sa11_next;
143
always @(posedge clk)           sa01 <=  ld_r ? text_in_r[095:088] ^ w1[31:24] : sa01_next;
144
always @(posedge clk)           sa30 <=  ld_r ? text_in_r[103:096] ^ w0[07:00] : sa30_next;
145
always @(posedge clk)           sa20 <=  ld_r ? text_in_r[111:104] ^ w0[15:08] : sa20_next;
146
always @(posedge clk)           sa10 <=  ld_r ? text_in_r[119:112] ^ w0[23:16] : sa10_next;
147
always @(posedge clk)           sa00 <=  ld_r ? text_in_r[127:120] ^ w0[31:24] : sa00_next;
148
 
149
////////////////////////////////////////////////////////////////////
150
//
151
// Modules instantiation
152
//
153
 
154
aes_key_expand_128 u0(
155
        .clk(           clk     ),
156
        .kld(           ld      ),
157
        .key(           key     ),
158
        .wo_0(          w0      ),
159
        .wo_1(          w1      ),
160
        .wo_2(          w2      ),
161
        .wo_3(          w3      )
162
        );
163
 
164
//sbox lookup
165
aes_sbox us00(  .a(     sa00    ), .d(  sa00_sub        ));
166
aes_sbox us01(  .a(     sa01    ), .d(  sa01_sub        ));
167
aes_sbox us02(  .a(     sa02    ), .d(  sa02_sub        ));
168
aes_sbox us03(  .a(     sa03    ), .d(  sa03_sub        ));
169
aes_sbox us10(  .a(     sa10    ), .d(  sa10_sub        ));
170
aes_sbox us11(  .a(     sa11    ), .d(  sa11_sub        ));
171
aes_sbox us12(  .a(     sa12    ), .d(  sa12_sub        ));
172
aes_sbox us13(  .a(     sa13    ), .d(  sa13_sub        ));
173
aes_sbox us20(  .a(     sa20    ), .d(  sa20_sub        ));
174
aes_sbox us21(  .a(     sa21    ), .d(  sa21_sub        ));
175
aes_sbox us22(  .a(     sa22    ), .d(  sa22_sub        ));
176
aes_sbox us23(  .a(     sa23    ), .d(  sa23_sub        ));
177
aes_sbox us30(  .a(     sa30    ), .d(  sa30_sub        ));
178
aes_sbox us31(  .a(     sa31    ), .d(  sa31_sub        ));
179
aes_sbox us32(  .a(     sa32    ), .d(  sa32_sub        ));
180
aes_sbox us33(  .a(     sa33    ), .d(  sa33_sub        ));
181
 
182
////////////////////////////////////////////////////////////////////
183
//
184
// Round Permutations
185
//
186
 
187
assign sa00_sr = sa00_sub;              //
188
assign sa01_sr = sa01_sub;              //no shift
189
assign sa02_sr = sa02_sub;              //
190
assign sa03_sr = sa03_sub;              //
191
 
192
assign sa10_sr = sa11_sub;              //
193
assign sa11_sr = sa12_sub;              // left shift by 1
194
assign sa12_sr = sa13_sub;              //
195
assign sa13_sr = sa10_sub;              //
196
 
197
assign sa20_sr = sa22_sub;              //
198
assign sa21_sr = sa23_sub;              //      left shift by 2
199
assign sa22_sr = sa20_sub;              //
200
assign sa23_sr = sa21_sub;              //
201
 
202
assign sa30_sr = sa33_sub;              //
203
assign sa31_sr = sa30_sub;              // left shift by 3
204
assign sa32_sr = sa31_sub;              //
205
assign sa33_sr = sa32_sub;              //
206
 
207
// mix column operation
208
assign {sa00_mc, sa10_mc, sa20_mc, sa30_mc}  = mix_col(sa00_sr,sa10_sr,sa20_sr,sa30_sr);
209
assign {sa01_mc, sa11_mc, sa21_mc, sa31_mc}  = mix_col(sa01_sr,sa11_sr,sa21_sr,sa31_sr);
210
assign {sa02_mc, sa12_mc, sa22_mc, sa32_mc}  = mix_col(sa02_sr,sa12_sr,sa22_sr,sa32_sr);
211
assign {sa03_mc, sa13_mc, sa23_mc, sa33_mc}  = mix_col(sa03_sr,sa13_sr,sa23_sr,sa33_sr);
212
 
213
//// add round key
214
assign sa00_next = sa00_mc ^ w0[31:24];
215
assign sa01_next = sa01_mc ^ w1[31:24];
216
assign sa02_next = sa02_mc ^ w2[31:24];
217
assign sa03_next = sa03_mc ^ w3[31:24];
218
assign sa10_next = sa10_mc ^ w0[23:16];
219
assign sa11_next = sa11_mc ^ w1[23:16];
220
assign sa12_next = sa12_mc ^ w2[23:16];
221
assign sa13_next = sa13_mc ^ w3[23:16];
222
assign sa20_next = sa20_mc ^ w0[15:08];
223
assign sa21_next = sa21_mc ^ w1[15:08];
224
assign sa22_next = sa22_mc ^ w2[15:08];
225
assign sa23_next = sa23_mc ^ w3[15:08];
226
assign sa30_next = sa30_mc ^ w0[07:00];
227
assign sa31_next = sa31_mc ^ w1[07:00];
228
assign sa32_next = sa32_mc ^ w2[07:00];
229
assign sa33_next = sa33_mc ^ w3[07:00];
230
 
231
always @(posedge clk)
232
 begin
233
          text_out[127:120] <=  sa00_sr ^ w0[31:24];
234
          text_out[095:088] <=  sa01_sr ^ w1[31:24];
235
     text_out[063:056] <=  sa02_sr ^ w2[31:24];
236
          text_out[031:024] <=  sa03_sr ^ w3[31:24];
237
          text_out[119:112] <=  sa10_sr ^ w0[23:16];
238
          text_out[087:080] <=  sa11_sr ^ w1[23:16];
239
          text_out[055:048] <=  sa12_sr ^ w2[23:16];
240
          text_out[023:016] <=  sa13_sr ^ w3[23:16];
241
          text_out[111:104] <=  sa20_sr ^ w0[15:08];
242
          text_out[079:072] <=  sa21_sr ^ w1[15:08];
243
          text_out[047:040] <=  sa22_sr ^ w2[15:08];
244
          text_out[015:008] <=  sa23_sr ^ w3[15:08];
245
          text_out[103:096] <=  sa30_sr ^ w0[07:00];
246
          text_out[071:064] <=  sa31_sr ^ w1[07:00];
247
          text_out[039:032] <=  sa32_sr ^ w2[07:00];
248
          text_out[007:000] <=  sa33_sr ^ w3[07:00];
249
 end
250
////////////////////////////////////////////////////////////////////
251
//
252
// Generic Functions
253
//
254
 
255
function [31:0] mix_col;
256
input   [7:0]    s0,s1,s2,s3;
257
 
258
begin
259
mix_col[31:24]=xtime(s0)^xtime(s1)^s1^s2^s3;
260
mix_col[23:16]=s0^xtime(s1)^xtime(s2)^s2^s3;
261
mix_col[15:08]=s0^s1^xtime(s2)^xtime(s3)^s3;
262
mix_col[07:00]=xtime(s0)^s0^s1^s2^xtime(s3);
263
end
264
endfunction
265
 
266
function [7:0] xtime;
267
input [7:0] b; xtime={b[6:0],1'b0}^(8'h1b&{8{b[7]}});
268
endfunction
269
 
270
 
271
 
272
endmodule
273
 
274
 
275
 

powered by: WebSVN 2.1.0

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