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

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [rtl/] [key_expander.v] - Blame information for rev 3

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 3 redbear
//// This file is part of the APB to AES128 project
9 2 redbear
////
10 3 redbear
//// http://www.opencores.org/cores/apbtoaes128/
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 key_expander
77
(
78
        // OUTPUTS
79
        output [127:0] key_out,
80
        output [ 31:0] g_in,
81
        // INPUTS
82
        input  [ 31:0] g_out,
83
        input  [127:0] key_in,
84
        input  [  3:0] round,
85
        input add_w_out,
86
        input enc_dec
87
);
88
 
89
localparam KEY_WIDTH = 32;
90
localparam KEY_NUM   = 4;
91
localparam WORD      = 8;
92
localparam ROUNDS    = 10;
93
 
94
wire [KEY_WIDTH - 1 : 0] key   [0 : KEY_NUM - 1];
95
wire [     WORD - 1 : 0] rot_in[0 : KEY_NUM - 1];
96
wire [KEY_WIDTH - 1 : 0] g_func;
97
reg  [     WORD - 1 : 0] rc_dir, rc_inv;
98
wire [     WORD - 1 : 0] rc;
99
 
100
//=====================================================================================
101
// Key Generation
102
//=====================================================================================
103
generate
104
        genvar i;
105
        for(i = 0; i < KEY_NUM; i = i + 1)
106
                begin
107
                        assign key[KEY_NUM - 1 - i] = key_in[KEY_WIDTH*(i + 1) - 1 : KEY_WIDTH*i];
108
                end
109
endgenerate
110
 
111
//=====================================================================================
112
// Key Out Generation
113
//=====================================================================================
114
generate
115
        genvar j;
116
        for(j = 0; j < KEY_NUM; j = j + 1)
117
                begin
118
                        if(j == 0)
119
                                assign key_out[KEY_WIDTH*(KEY_NUM - j) - 1 : KEY_WIDTH*(KEY_NUM - j - 1)] = key[j] ^ g_func;
120
                        else
121
                                if(j == 1)
122
                                        assign key_out[KEY_WIDTH*(KEY_NUM - j) - 1 : KEY_WIDTH*(KEY_NUM - j - 1)] = (add_w_out) ? key[j] ^ key[j - 1] ^ g_func : key[j] ^ key[j - 1];
123
                                else
124
                                        assign key_out[KEY_WIDTH*(KEY_NUM - j) - 1 : KEY_WIDTH*(KEY_NUM - j - 1)] = key[j] ^ key[j - 1];
125
                end
126
endgenerate
127
 
128
//=====================================================================================
129
// G Function Input Generation
130
//=====================================================================================
131
generate
132
        genvar k;
133
        for(k = 0; k < KEY_NUM; k = k + 1)
134
                assign rot_in[k] = (enc_dec) ? key[KEY_NUM - 1][WORD*(k + 1) - 1 : WORD*k] : key[KEY_NUM - 1][WORD*(k + 1) - 1 : WORD*k] ^ key[KEY_NUM - 2][WORD*(k + 1) - 1 : WORD*k];
135
endgenerate
136
 
137
generate
138
        genvar l;
139
        for(l = 0; l < KEY_NUM; l = l + 1)
140
                assign g_in[WORD*(l + 1) - 1 : WORD*l] = rot_in[(KEY_NUM + l - 1)%KEY_NUM];
141
endgenerate
142
 
143
//=====================================================================================
144
// G Functin Output Processsing
145
//=====================================================================================
146
assign g_func = {g_out[KEY_WIDTH - 1 : KEY_WIDTH - WORD] ^ rc, g_out[KEY_WIDTH - WORD - 1 : 0]};
147
 
148
assign rc = (enc_dec) ? rc_dir : rc_inv;
149
 
150
always @(*)
151
        begin: RC_DIR
152
                integer i;
153
                for(i = 0; i < ROUNDS; i = i + 1)
154
                        if(round == 8)
155
                                rc_dir = 8'h1b;
156
                        else
157
                        if(round == 9)
158
                                rc_dir = 8'h36;
159
                        else
160
                                rc_dir = 8'h01 << round;
161
        end
162
 
163
always @(*)
164
        begin: RC_INV
165
                integer i;
166
                for(i = 0; i < ROUNDS; i = i + 1)
167
                        if(round == 1)
168
                                rc_inv = 8'h1b;
169
                        else
170
                        if(round == 0)
171
                                rc_inv = 8'h36;
172
                        else
173
                                rc_inv = 8'h80 >> (round - 2);
174
        end
175
endmodule

powered by: WebSVN 2.1.0

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