-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_core.v
329 lines (218 loc) · 7.46 KB
/
aes_core.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
`timescale 1ns / 1ps
`default_nettype none
module aes_core(
input wire clk,
input wire reset,
input wire enc_dec,
input wire init,
input wire next,
output wire ready,
input wire [255:0] key,
input wire keylen,
input wire [127:0] block,
output wire [127:0] result,
output wire result_valid
);
//-------------------------------------------------------------
// Some constants defination.
//-------------------------------------------------------------
localparam CTRL_IDLE = 2'h0;
localparam CTRL_INIT = 2'h1;
localparam CTRL_NEXT = 2'h2;
//------------------------------------------------------------
// Some Registers definations
//------------------------------------------------------------
reg [1:0] aes_core_ctrl_reg;
reg [1:0] aes_core_ctrl_new;
reg aes_core_ctrl_we;
reg result_valid_reg;
reg result_valid_new;
reg result_valid_we;
reg ready_reg;
reg ready_new;
reg ready_we;
//------------------------------------------------------------
// Some wires declartion
//------------------------------------------------------------
reg init_state;
wire [127:0] round_key;
wire key_ready;
reg enc_next;
wire [3:0] enc_round_num;
wire [127:0] enc_new_block;
wire enc_ready;
wire [31:0] enc_sboxw;
reg dec_next;
wire [3:0] dec_round_num;
wire [127:0] dec_new_block;
wire dec_ready;
reg [127:0] muxed_new_block;
reg [3:0] muxed_round_num;
reg muxed_ready;
wire [31:0] keymem_sboxw;
//-----------------------------------------------------------
//S-Box parameters
//-----------------------------------------------------------
reg [31:0] muxed_sboxw;
wire [31:0] new_sboxw;
//-----------------------------------------------------------
//Model Instantiations
//-----------------------------------------------------------
aes_encode_block enc_block(
.clk(clk),
.reset(reset),
.next(enc_next),
.keylen(keylen),
.round_no(enc_round_num),
.round_key(round_key),
.sbox_word(enc_sboxw),
.new_sbox_word(new_sboxw),
.block(block),
.new_block(enc_new_block),
.ready(enc_ready)
);
aes_decode_block dec_block(
.clk(clk),
.reset(reset),
.next(dec_next),
.keylen(keylen),
.round_no(dec_round_num),
.round_key(round_key),
.block(block),
.new_block(dec_new_block),
.ready(dec_ready)
);
aes_key_schedule key_schedule(
.clk(clk),
.reset(reset),
.key(key),
.keylen(keylen),
.init(init),
.round(muxed_round_num),
.round_key(round_key),
.ready(key_ready),
.sboxw(keymem_sboxw),
.new_sboxw(new_sboxw)
);
aes_sbox s1(
.input_sbox(muxed_sboxw),
.new_sbox(new_sboxw)
);
assign ready = ready_reg;
assign result = muxed_new_block;
assign result_valid = result_valid_reg;
always@(posedge clk or negedge reset)
begin
if(!reset)
begin
result_valid_reg <= 1'b0;
ready_reg <= 1'b1;
aes_core_ctrl_reg <= CTRL_IDLE;
end
else
begin
if(result_valid_we)
result_valid_reg <= result_valid_new;
if(ready_we)
ready_reg <= ready_new;
if(aes_core_ctrl_reg)
aes_core_ctrl_reg <= aes_core_ctrl_new;
end
end
// Determine which of encipher block or
// keygenerator will gain access to
//SBOX network
always@(*)
begin
if(init_state)
muxed_sboxw = keymem_sboxw;
else
muxed_sboxw = enc_sboxw;
end
// Determine which of encipher or decode
// core which will be processed
always@(*)
begin
enc_next = 1'b0;
dec_next = 1'b0;
if(enc_dec)
begin
enc_next = next;
muxed_round_num = enc_round_num;
muxed_new_block = enc_new_block;
muxed_ready = enc_ready;
end
else
begin
dec_next = next;
muxed_round_num = dec_round_num;
muxed_new_block = dec_new_block;
muxed_ready = dec_ready;
end
end
// FSM to control aes_core module
// it connect different submodules
// to the shared resources
always@(*)
begin
init_state = 1'b0;
ready_new = 1'b0;
ready_we = 1'b0;
result_valid_new = 1'b0;
result_valid_we = 1'b0;
aes_core_ctrl_new = CTRL_IDLE;
aes_core_ctrl_we = 1'b0;
case(aes_core_ctrl_reg)
CTRL_IDLE :
begin
if(init)
begin
init_state = 1'b1;
ready_new = 1'b0;
ready_we = 1'b1;
result_valid_new = 1'b0;
result_valid_we = 1'b1;
aes_core_ctrl_new = CTRL_INIT;
aes_core_ctrl_we = 1'b1;
end
else if(next)
begin
init_state = 1'b0;
ready_new = 1'b0;
ready_we = 1'b1;
result_valid_new = 1'b0;
result_valid_we = 1'b1;
aes_core_ctrl_new = CTRL_NEXT;
aes_core_ctrl_we = 1'b1;
end
end
CTRL_INIT :
begin
init_state = 1'b1;
if(key_ready)
begin
ready_new = 1'b1;
ready_we = 1'b1;
aes_core_ctrl_new = CTRL_IDLE;
aes_core_ctrl_we = 1'b1;
end
end
CTRL_NEXT :
begin
init_state = 1'b0;
if(muxed_ready)
begin
ready_new = 1'b1;
ready_we = 1'b1;
result_valid_new = 1'b1;
result_valid_we = 1'b1;
aes_core_ctrl_new = CTRL_IDLE;
aes_core_ctrl_we = 1'b1;
end
end
default:
begin
end
endcase
end
endmodule