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

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [rtl/] [datapath.v] - Blame information for rev 15

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    AES CORE BLOCK
5
////
6
////
7
////
8 7 redbear
//// This file is part of the APB to I2C project
9 2 redbear
////
10 7 redbear
//// http://www.opencores.org/cores/apbi2c/
11 2 redbear
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// aes128_spec IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): - Felipe Fernandes Da Costa, fefe2560@gmail.com
29
////              Julio Cesar 
30
////
31
///////////////////////////////////////////////////////////////// 
32
////
33
////
34
//// Copyright (C) 2009 Authors and OPENCORES.ORG
35
////
36
////
37
////
38
//// This source file may be used and distributed without
39
////
40
//// restriction provided that this copyright statement is not
41
////
42
//// removed from the file and that any derivative work contains
43
//// the original copyright notice and the associated disclaimer.
44
////
45
////
46
//// This source file is free software; you can redistribute it
47
////
48
//// and/or modify it under the terms of the GNU Lesser General
49
////
50
//// Public License as published by the Free Software Foundation;
51
//// either version 2.1 of the License, or (at your option) any
52
////
53
//// later version.
54
////
55
////
56
////
57
//// This source is distributed in the hope that it will be
58
////
59
//// useful, but WITHOUT ANY WARRANTY; without even the implied
60
////
61
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
62
////
63
//// PURPOSE. See the GNU Lesser General Public License for more
64
//// details.
65
////
66
////
67
////
68
//// You should have received a copy of the GNU Lesser General
69
////
70
//// Public License along with this source; if not, download it
71
////
72
//// from http://www.opencores.org/lgpl.shtml
73
////
74
////
75
///////////////////////////////////////////////////////////////////
76
module datapath
77
(
78
        // OUTPUTS
79
        output [31:0] col_bus,
80
        output [31:0] key_bus,
81
        output [31:0] iv_bus,
82
        output end_aes,
83
        // INPUTS
84
        input [31:0] bus_in,
85
        input [ 1:0] data_type,
86
        input [ 1:0] rk_sel,
87
        input [ 1:0] key_out_sel,
88
        input [ 3:0] round,
89
        input [ 2:0] sbox_sel,
90
        input [ 3:0] iv_en,
91
        input [ 3:0] iv_sel_rd,
92
        input [ 3:0] col_en_host,
93
        input [ 3:0] col_en_cnt_unit,
94
        input [ 3:0] key_host_en,
95
        input [ 3:0] key_en,
96
        input [ 1:0] key_sel_rd,
97
        input [ 1:0] col_sel,
98
        input [ 1:0] col_sel_host,
99
        input end_comp,
100
        input key_sel,
101
        input key_init,
102
        input bypass_rk,
103
        input bypass_key_en,
104
        input first_block,
105
        input last_round,
106
        input iv_cnt_en,
107
        input iv_cnt_sel,
108
        input enc_dec,
109
        input mode_ctr,
110
        input mode_cbc,
111
        input key_gen,
112
        input key_derivation_en,
113
        input rst_n,
114
        input clk
115
);
116
 
117
//`include "include/control_unit_params.vh"
118
//=============================================================================
119
// SBOX SEL
120
//=============================================================================
121
localparam COL_0 = 3'b000;
122
localparam COL_1 = 3'b001;
123
localparam COL_2 = 3'b010;
124
localparam COL_3 = 3'b011;
125
localparam G_FUNCTION = 3'b100;
126
 
127
//=============================================================================
128
// RK_SEL
129
//=============================================================================
130
localparam COL        = 2'b00;
131
localparam MIXCOL_IN  = 2'b01;
132
localparam MIXCOL_OUT = 2'b10;
133
 
134
//=============================================================================
135
// KEY_OUT_SEL
136
//=============================================================================
137
localparam KEY_0 = 2'b00;
138
localparam KEY_1 = 2'b01;
139
localparam KEY_2 = 2'b10;
140
localparam KEY_3 = 2'b11;
141
 
142
//=============================================================================
143
// COL_SEL
144
//=============================================================================
145
localparam SHIFT_ROWS = 2'b00;
146
localparam ADD_RK_OUT = 2'b01;
147
localparam INPUT      = 2'b10;
148
 
149
//=============================================================================
150
// KEY_SEL
151
//=============================================================================
152
localparam KEY_HOST = 1'b0;
153
localparam KEY_OUT  = 1'b1;
154
 
155
//=============================================================================
156
// KEY_EN
157
//=============================================================================
158
localparam KEY_DIS  = 4'b0000;
159
localparam EN_KEY_0 = 4'b0001;
160
localparam EN_KEY_1 = 4'b0010;
161
localparam EN_KEY_2 = 4'b0100;
162
localparam EN_KEY_3 = 4'b1000;
163
localparam KEY_ALL  = 4'b1111;
164
 
165
//=============================================================================
166
// COL_EN
167
//=============================================================================
168
localparam COL_DIS  = 4'b0000;
169
localparam EN_COL_0 = 4'b0001;
170
localparam EN_COL_1 = 4'b0010;
171
localparam EN_COL_2 = 4'b0100;
172
localparam EN_COL_3 = 4'b1000;
173
localparam COL_ALL  = 4'b1111;
174
 
175
//=============================================================================
176
// IV_CNT_SEL
177
//=============================================================================
178
localparam IV_CNT = 1'b1;
179
localparam IV_BUS = 1'b0;
180
 
181
//=============================================================================
182
// ENABLES
183
//=============================================================================
184
localparam ENABLE = 1'b1;
185
localparam DISABLE = 1'b0;
186
 
187
reg [31 : 0] col     [0:3];
188
reg [31 : 0] key     [0:3];
189
reg [31 : 0] key_host[0:3];
190
reg [31 : 0] bkp     [0:3];
191
reg [31 : 0] bkp_1   [0:3];
192
reg [31 : 0] iv      [0:3];
193
 
194
reg  [127 : 0] col_in;
195
reg  [ 31 : 0] data_in;
196
reg  [ 31 : 0] add_rd_key_in;
197
reg  [ 31 : 0] sbox_input;
198
reg  [ 31 : 0] key_mux_out;
199
reg  [ 31 : 0] iv_mux_out;
200
reg  [ 31 : 0] bkp_mux_out;
201
 
202
wire [127 : 0] key_in, key_out;
203
wire [127 : 0] sr_input;
204
wire [127 : 0] sr_enc, sr_dec;
205
wire [ 31 : 0] add_rk_out;
206
wire [ 31 : 0] sbox_out_enc;
207
wire [ 31 : 0] sbox_out_dec;
208
wire [ 31 : 0] g_in;
209
wire [ 31 : 0] mix_out_enc;
210
wire [ 31 : 0] mix_out_dec;
211
wire [ 31 : 0] add_rd;
212
wire [ 31 : 0] bus_swap;
213
wire [ 31 : 0] iv_bkp_mux;
214
wire [ 31 : 0] xor_input_bkp_iv;
215
wire [ 31 : 0] sr_input_0;
216
wire [ 31 : 0] sr_input_3;
217
wire [  3 : 0] key_en_sel;
218
wire [  3 : 0] bkp_en;
219
wire [  3 : 0] col_en;
220
wire [  1 : 0] key_mux_sel;
221
wire [  1 : 0] rk_sel_mux;
222
wire [  1 : 0] col_sel_w_bypass;
223
wire [  3 : 0] col_en_w_bypass;
224
wire rk_out_sel;
225
wire add_rk_sel;
226
wire key_sel_mux;
227
wire key1_mux_cnt;
228
wire enc_dec_sbox;
229
 
230
reg [31 : 0] sbox_pp2;
231
reg [ 3 : 0] col_en_cnt_unit_pp1;
232
reg [ 3 : 0] col_en_cnt_unit_pp2;
233
reg [ 3 : 0] key_en_pp1;
234
reg [ 3 : 0] round_pp1;
235
reg [ 1 : 0] col_sel_pp1;
236
reg [ 1 : 0] col_sel_pp2;
237
reg [ 1 : 0] key_out_sel_pp1;
238
reg [ 1 : 0] key_out_sel_pp2;
239
reg [ 1 : 0] rk_sel_pp1;
240
reg [ 1 : 0] rk_sel_pp2;
241
reg key_sel_pp1;
242
reg rk_out_sel_pp1, rk_out_sel_pp2;
243
reg last_round_pp1, last_round_pp2;
244 9 redbear
//reg end_aes_pp2,end_aes_pp1;//end_aes_pp2;
245 2 redbear
 
246
assign key_bus = key_mux_out;
247
assign iv_bus = iv_mux_out;
248
 
249
// Input Swap Unit
250
data_swap SWAP_IN
251
(
252
        .data_swap( bus_swap  ),
253
        .data_in  ( bus_in    ),
254
        .swap_type( data_type )
255
);
256
 
257
// Output Swap Unit
258
data_swap SWAP_OUT
259
(
260
        .data_swap( col_bus    ),
261
        .data_in  ( sbox_input ),
262
        .swap_type( data_type  )
263
);
264
 
265
// IV and BKP Muxs
266 15 redbear
always@(iv_mux_out or bkp_mux_out or col_en or iv_sel_rd or iv[0] or iv[1] or iv[2] or iv[3] or bkp[0] or bkp[1] or bkp[2] or bkp[3])
267 2 redbear
        begin: IV_BKP_MUX
268
                integer i;
269
                iv_mux_out  = {32{1'b0}};
270
                bkp_mux_out = {32{1'b0}};
271
                for(i = 0; i < 4; i = i + 1)
272 14 redbear
                        begin:IVBKP
273 2 redbear
                                if(col_en[i] | iv_sel_rd[i])
274
                                        begin
275
                                                iv_mux_out  = iv[i];
276
                                                bkp_mux_out = bkp[i];
277
                                        end
278
                        end
279
        end
280
 
281
assign iv_bkp_mux = (first_block && !mode_ctr) ? iv_mux_out : bkp_mux_out;
282
 
283
assign xor_input_bkp_iv = ((enc_dec && !mode_ctr) ? bus_swap : add_rk_out) ^ iv_bkp_mux;
284
 
285
always @(*)
286
        begin
287
                data_in = {32{1'b0}};
288
                case(1'b1)
289
                        mode_cbc:
290
                                data_in = (enc_dec || last_round) ? xor_input_bkp_iv : bus_swap;
291
                        mode_ctr:
292
                                data_in = (last_round) ? xor_input_bkp_iv : iv_mux_out;
293
                        default:
294
                                data_in = bus_swap;
295
                endcase
296
        end
297
 
298
assign bkp_en = ( {4{ mode_cbc && last_round && enc_dec}} & col_en_cnt_unit_pp2) |
299
                ( {4{(mode_cbc && !enc_dec) || mode_ctr}} & col_en_host     );
300
 
301
 
302
// IV and BKP Registers
303
generate
304
        genvar l;
305 7 redbear
 
306
        for(l = 0; l < 4;l=l+1)
307 14 redbear
        begin:IV_BKP_REGISTERS
308 2 redbear
                always @(posedge clk, negedge rst_n)
309 7 redbear
                begin
310 2 redbear
                                if(!rst_n)
311 7 redbear
                                begin
312 2 redbear
                                                iv[l]    <= {32{1'b0}};
313
                                                bkp[l]   <= {32{1'b0}};
314
                                                bkp_1[l] <= {32{1'b0}};
315 7 redbear
                                end
316 2 redbear
                                else
317 7 redbear
                                begin
318 2 redbear
                                                if(l == 3)
319 7 redbear
                                                begin
320 2 redbear
                                                                if(iv_en[l] || iv_cnt_en)
321 9 redbear
                                                                begin
322
                                                                        /*
323
                                                                        if(mode_ctr)
324
                                                                                iv[l] <= (iv_cnt_sel) ? iv[l] + 1'b1 : bus_in;
325
 
326
                                                                        else
327
                                                                                iv[l] <= (iv_cnt_sel) ? iv[l] : bus_in;
328
                                                                        */
329
 
330 8 redbear
                                                                        iv[l] <= (iv_cnt_sel) ? iv[l] : bus_in;
331 9 redbear
                                                                end
332 7 redbear
                                                end
333 2 redbear
                                                else
334 7 redbear
                                                begin
335 2 redbear
                                                                if(iv_en[l])
336
                                                                        iv[l] <= bus_in;
337 7 redbear
                                                end
338 2 redbear
 
339
                                        if(bkp_en[l])
340 7 redbear
                                                //bkp[l] <= (mode_ctr) ? bus_swap : ((mode_cbc && enc_dec) ? col_in : bkp_1[l]);
341 8 redbear
                                        bkp[l] <= (mode_ctr) ? bus_swap : ((mode_cbc && enc_dec)? col_in[32*(l + 1) - 1 : 32*l] : bkp_1[l]);
342 2 redbear
 
343
                                        if(bkp_en[l])
344 7 redbear
                                                bkp_1[l] <= col_in[32*(l + 1) - 1 : 32*l];
345
                                end
346 2 redbear
                        end
347 7 redbear
 
348
                end
349 2 redbear
endgenerate
350
 
351
assign col_sel_w_bypass = (bypass_rk) ? col_sel : col_sel_pp2;
352
 
353
// Columns Input Multiplexors
354
always @(*)
355
        begin
356
                col_in = {128{1'b0}};
357
                case(col_sel_w_bypass)
358
                        SHIFT_ROWS:
359
                                col_in = (enc_dec) ? sr_enc : sr_dec;
360
                        ADD_RK_OUT:
361
                                col_in = {4{add_rk_out}};
362
                        INPUT:
363
                                col_in = {4{data_in}};
364
                endcase
365
        end
366
 
367
assign col_en_w_bypass = (bypass_rk) ? col_en_cnt_unit : col_en_cnt_unit_pp2;
368
assign col_en = col_en_host | col_en_w_bypass;
369
 
370
// Columns Definition
371
generate
372
        genvar i;
373 14 redbear
        for(i = 0; i < 4; i = i + 1)
374
        begin:CD
375 2 redbear
                always @(posedge clk, negedge rst_n)
376
                        begin
377
                                if(!rst_n)
378
                                        col[3 - i] <= {32{1'b0}};
379
                                else
380
                                if(col_en[3 - i])
381
                                        col[3 - i] <= col_in[32*(i + 1) - 1 : 32*i];
382 14 redbear
                        end
383
        end
384 2 redbear
endgenerate
385
 
386
// Shift Rows Operation
387
assign sr_input_3 = (enc_dec) ? add_rk_out : col[3];
388
assign sr_input_0 = (enc_dec) ? col[0] : add_rk_out;
389
assign sr_input = {sr_input_0, col[1], col[2], sr_input_3};
390
 
391
shift_rows SHIFT_ROW
392
(
393
        .data_out_enc  ( sr_enc   ),
394
        .data_out_dec  ( sr_dec   ),
395
  .data_in       ( sr_input )
396
);
397
 
398
//SBOX Input Multiplexor
399 15 redbear
always @(sbox_input or sbox_sel or col_sel_host or col[0] or col[1] or col[2] or col[3] or g_in)
400 2 redbear
        begin
401
                sbox_input = {32{1'b0}};
402
                case(sbox_sel | col_sel_host)
403
                        COL_0:
404
                                sbox_input = col[0];
405
                        COL_1:
406
                                sbox_input = col[1];
407
                        COL_2:
408
                                sbox_input = col[2];
409
                        COL_3:
410
                                sbox_input = col[3];
411
                        G_FUNCTION:
412
                                sbox_input = g_in;
413
                endcase
414
        end
415
 
416
// 32 bits SBOX
417
assign enc_dec_sbox = enc_dec | key_gen;
418
 sBox SBOX
419
  (
420
    .sbox_out_enc ( sbox_out_enc ),
421
                .sbox_out_dec ( sbox_out_dec ),
422
    .sbox_in      ( sbox_input   ),
423
    .enc_dec      ( enc_dec_sbox ),
424
                .clk          ( clk          )
425
  );
426
 
427
// Second stage of pipeline
428
always @(posedge clk)
429
        begin
430
                sbox_pp2 <= (enc_dec || mode_ctr) ? sbox_out_enc : sbox_out_dec ^ key_mux_out;
431
        end
432
 
433
assign key_en_sel  = (bypass_key_en) ? key_en : key_en_pp1;
434
assign key_sel_mux = (bypass_key_en) ? key_sel : key_sel_pp1;
435
 
436
// Key registers
437
generate
438
        genvar j;
439 14 redbear
        for(j = 0; j < 4; j = j + 1)
440
        begin:KR
441 2 redbear
                always @(posedge clk, negedge rst_n)
442
                        begin
443
                                if(!rst_n)
444
                                        begin
445
                                                key_host[3 - j] <= {32{1'b0}};
446
                                                key[3 - j] <= {32{1'b0}};
447
                                        end
448
                                else
449
                                        begin
450
                                                if(key_host_en[3 - j] || key_derivation_en)
451
                                                        key_host[3 - j] <= (key_derivation_en) ? key[3 - j] : bus_in;
452
 
453
                                                if(key_en_sel[3 - j] || key_init || key_host_en[3 - j])
454
                                                        key[3 - j] <= (key_sel_mux) ? key_out[32*(j + 1) - 1 : 32*j] : ( (key_host_en[3 - j]) ? bus_in : key_host[3 - j] );
455
                                        end
456 14 redbear
                        end
457
        end
458 2 redbear
endgenerate
459
 
460
assign key_in = {key[0], key[1], key[2], key[3]};
461
 
462
assign key1_mux_cnt = bypass_key_en & enc_dec;
463
 
464
key_expander KEY_EXPANDER
465
(
466
        .key_out   ( key_out       ),
467
        .g_in              ( g_in          ),
468
        .g_out     ( sbox_out_enc  ),
469
        .key_in    ( key_in        ),
470
        .round     ( round_pp1     ),
471
        .add_w_out ( key1_mux_cnt  ),
472
        .enc_dec   ( enc_dec | key_gen)
473
);
474
 
475
assign key_mux_sel = (bypass_key_en) ? key_out_sel : ( (enc_dec | mode_ctr) ? key_out_sel_pp2 : key_out_sel_pp1 );
476
 
477
// Key Expander Mux
478 15 redbear
always @(key_mux_out or key_mux_sel or key_sel_rd or key[0] or key[1] or key[2] or key[3])
479 2 redbear
        begin
480
                key_mux_out = {32{1'b0}};
481
                case(key_mux_sel | key_sel_rd)
482
                        KEY_0:
483
                                key_mux_out = key[0];
484
                        KEY_1:
485
                                key_mux_out = key[1];
486
                        KEY_2:
487
                                key_mux_out = key[2];
488
                        KEY_3:
489
                                key_mux_out = key[3];
490
                endcase
491
        end
492
 
493
  mix_columns MIX_COL
494
  (
495
    .mix_out_enc ( mix_out_enc  ),
496
                .mix_out_dec ( mix_out_dec  ),
497
    .mix_in      ( sbox_pp2     )
498
  );
499
 
500
assign rk_sel_mux = (bypass_rk) ? rk_sel : rk_sel_pp2;
501
 
502
always @(*)
503
        begin
504
                add_rd_key_in = {32{1'b0}};
505
                case(rk_sel_mux)
506
                        COL:
507
                                add_rd_key_in = sbox_input;
508
                        MIXCOL_IN:
509
                                add_rd_key_in = sbox_pp2;
510
                        MIXCOL_OUT:
511
                                add_rd_key_in = mix_out_enc;
512
                endcase
513
        end
514
 
515
// Add Round Key
516
assign add_rd = add_rd_key_in ^ key_mux_out;
517
 
518
assign rk_out_sel = (enc_dec | mode_ctr | bypass_rk);
519
 
520
assign add_rk_sel = (bypass_rk) ? rk_out_sel : rk_out_sel_pp2;
521
 
522
assign add_rk_out = (add_rk_sel) ? add_rd : (last_round_pp2 ? sbox_pp2 : mix_out_dec);
523
 
524 9 redbear
assign end_aes = end_comp;
525 2 redbear
 
526
// Pipeline Registers for Control Signals
527
always @(posedge clk, negedge rst_n)
528
        begin
529
                if(!rst_n)
530
                        begin
531 7 redbear
                                //end_aes_pp1 <= DISABLE;
532
                                //end_aes_pp2 <= DISABLE;
533 2 redbear
 
534
                                col_sel_pp1 <= INPUT;
535
                                col_sel_pp2 <= INPUT;
536
 
537
                                col_en_cnt_unit_pp1 <= COL_DIS;
538
                                col_en_cnt_unit_pp2 <= COL_DIS;
539
 
540
                                key_sel_pp1 <= KEY_HOST;
541
                                key_en_pp1  <= KEY_DIS;
542
 
543
                                round_pp1 <= 4'b0000;
544
 
545
                                key_out_sel_pp1 <= KEY_0;
546
                                key_out_sel_pp2 <= KEY_0;
547
 
548
                                rk_sel_pp1 <= COL;
549
                                rk_sel_pp2 <= COL;
550
 
551
                                rk_out_sel_pp1 <= 1'b1;
552
                                rk_out_sel_pp2 <= 1'b1;
553
 
554
                                last_round_pp1 <= 1'b1;
555
                                last_round_pp2 <= 1'b0;
556
                        end
557
                else
558
                        begin
559
                                col_sel_pp1 <= col_sel;
560
                                col_sel_pp2 <= col_sel_pp1;
561
 
562
                                if(!bypass_rk)
563
                                        begin
564
                                                col_en_cnt_unit_pp1 <= col_en_cnt_unit;
565
                                                col_en_cnt_unit_pp2 <= col_en_cnt_unit_pp1;
566
                                        end
567
 
568
                                key_sel_pp1 <= key_sel;
569
 
570
                                if(!bypass_key_en)
571
                                        key_en_pp1 <= key_en;
572
 
573
                                round_pp1 <= round;
574
 
575
                                key_out_sel_pp1 <= key_out_sel;
576
                                key_out_sel_pp2 <= key_out_sel_pp1;
577
 
578
                                rk_sel_pp1 <= rk_sel;
579
                                rk_sel_pp2 <= rk_sel_pp1;
580
 
581
                                rk_out_sel_pp1 <= rk_out_sel;
582
                                rk_out_sel_pp2 <= rk_out_sel_pp1;
583
 
584
                                last_round_pp1 <= last_round;
585
                                last_round_pp2 <= last_round_pp1;
586
 
587 7 redbear
                                //end_aes_pp1 <= end_comp;
588 9 redbear
                                //end_aes_pp2 <= end_aes_pp1;
589 2 redbear
                        end
590
        end
591
endmodule

powered by: WebSVN 2.1.0

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