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

Subversion Repositories apbtoaes128

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

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 key_expander
77
(
78
        // OUTPUTS
79 7 redbear
        output  [127:0] key_out,
80
        output  [ 31:0] g_in,
81 2 redbear
        // 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 7 redbear
localparam integer KEY_WIDTH = 32;
90
localparam integer KEY_NUM   = 4;
91
localparam integer WORD      = 8;
92
localparam integer ROUNDS    = 10;
93 2 redbear
 
94 7 redbear
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 2 redbear
 
100
//=====================================================================================
101
// Key Generation
102
//=====================================================================================
103
generate
104
        genvar i;
105
        for(i = 0; i < KEY_NUM; i = i + 1)
106 14 redbear
        begin:KG
107 2 redbear
                        assign key[KEY_NUM - 1 - i] = key_in[KEY_WIDTH*(i + 1) - 1 : KEY_WIDTH*i];
108 7 redbear
        end
109 2 redbear
endgenerate
110
 
111
//=====================================================================================
112
// Key Out Generation
113
//=====================================================================================
114
generate
115
        genvar j;
116
        for(j = 0; j < KEY_NUM; j = j + 1)
117 14 redbear
        begin:KGO
118 2 redbear
                        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 9 redbear
        end
126 2 redbear
endgenerate
127
 
128
//=====================================================================================
129
// G Function Input Generation
130
//=====================================================================================
131
generate
132
        genvar k;
133 14 redbear
        for(k = 0; k < KEY_NUM; k = k + 1)
134
        begin:GFIG
135 2 redbear
                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];
136 14 redbear
        end
137 2 redbear
endgenerate
138
 
139
generate
140
        genvar l;
141 14 redbear
        for(l = 0; l < KEY_NUM; l = l + 1)
142
        begin:GFIG1
143 2 redbear
                assign g_in[WORD*(l + 1) - 1 : WORD*l] = rot_in[(KEY_NUM + l - 1)%KEY_NUM];
144 14 redbear
        end
145 2 redbear
endgenerate
146
 
147
//=====================================================================================
148
// G Functin Output Processsing
149
//=====================================================================================
150
assign g_func = {g_out[KEY_WIDTH - 1 : KEY_WIDTH - WORD] ^ rc, g_out[KEY_WIDTH - WORD - 1 : 0]};
151
 
152
assign rc = (enc_dec) ? rc_dir : rc_inv;
153
 
154
always @(*)
155
        begin: RC_DIR
156
                integer i;
157
                for(i = 0; i < ROUNDS; i = i + 1)
158
                        if(round == 8)
159
                                rc_dir = 8'h1b;
160
                        else
161
                        if(round == 9)
162
                                rc_dir = 8'h36;
163
                        else
164
                                rc_dir = 8'h01 << round;
165
        end
166
 
167
always @(*)
168
        begin: RC_INV
169
                integer i;
170
                for(i = 0; i < ROUNDS; i = i + 1)
171
                        if(round == 1)
172
                                rc_inv = 8'h1b;
173
                        else
174
                        if(round == 0)
175
                                rc_inv = 8'h36;
176
                        else
177
                                rc_inv = 8'h80 >> (round - 2);
178
        end
179
endmodule

powered by: WebSVN 2.1.0

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