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 mix_columns
|
77 |
|
|
(
|
78 |
|
|
// OUTPUTS
|
79 |
|
|
output [31:0] mix_out_enc,
|
80 |
|
|
output [31:0] mix_out_dec,
|
81 |
|
|
// INPUTS
|
82 |
|
|
input [31:0] mix_in
|
83 |
|
|
);
|
84 |
|
|
|
85 |
7 |
redbear |
localparam integer SIZE = 32;
|
86 |
|
|
localparam integer WORD_SIZE = 8;
|
87 |
|
|
localparam integer NUM_WORDS = 4;
|
88 |
2 |
redbear |
|
89 |
|
|
wire [WORD_SIZE - 1 : 0] col [0 : NUM_WORDS - 1];
|
90 |
|
|
wire [WORD_SIZE - 1 : 0] sum_p[0 : NUM_WORDS - 1];
|
91 |
|
|
wire [WORD_SIZE - 1 : 0] y [0 : NUM_WORDS - 2];
|
92 |
|
|
|
93 |
|
|
//=====================================================================================
|
94 |
|
|
// Multiplication by 02 in GF(2^8)
|
95 |
|
|
//=====================================================================================
|
96 |
|
|
function [7:0] aes_mult_02;
|
97 |
|
|
input [7:0] data_in;
|
98 |
|
|
begin
|
99 |
|
|
aes_mult_02 = (data_in << 1) ^ {8{data_in[7]}} & 8'h1b;
|
100 |
|
|
end
|
101 |
|
|
endfunction
|
102 |
|
|
|
103 |
|
|
//=====================================================================================
|
104 |
|
|
// Multiplication by 04 in GF(2^8)
|
105 |
|
|
//=====================================================================================
|
106 |
|
|
function [7:0] aes_mult_04;
|
107 |
|
|
input [7:0] data_in;
|
108 |
|
|
begin
|
109 |
|
|
aes_mult_04 = ((data_in << 2) ^ {8{data_in[6]}} & 8'h1b) ^ {8{data_in[7]}} & 8'h36;
|
110 |
|
|
end
|
111 |
|
|
endfunction
|
112 |
|
|
|
113 |
|
|
//=====================================================================================
|
114 |
|
|
// Word to Byte transformation
|
115 |
|
|
//=====================================================================================
|
116 |
|
|
generate
|
117 |
|
|
genvar i;
|
118 |
14 |
redbear |
for(i = 0 ; i < NUM_WORDS; i = i + 1)
|
119 |
|
|
begin:WBT
|
120 |
2 |
redbear |
assign col[i] = mix_in[WORD_SIZE*(i + 1) - 1: WORD_SIZE*i];
|
121 |
14 |
redbear |
end
|
122 |
2 |
redbear |
endgenerate
|
123 |
|
|
|
124 |
|
|
//=====================================================================================
|
125 |
|
|
// Direct Mix Column Operation
|
126 |
|
|
//=====================================================================================
|
127 |
|
|
generate
|
128 |
|
|
genvar j;
|
129 |
|
|
for(j = 0; j < NUM_WORDS; j = j + 1)
|
130 |
14 |
redbear |
begin:DMCO
|
131 |
2 |
redbear |
assign sum_p[j] = col[(j + 1)%NUM_WORDS] ^ col[(j + 2)%NUM_WORDS] ^ col[(j + 3)%NUM_WORDS];
|
132 |
|
|
assign mix_out_enc[ WORD_SIZE*(j + 1) - 1 : WORD_SIZE*j] = aes_mult_02(col[j] ^ col[(j + NUM_WORDS - 1)%NUM_WORDS]) ^ sum_p[j];
|
133 |
|
|
end
|
134 |
|
|
endgenerate
|
135 |
|
|
|
136 |
|
|
//=====================================================================================
|
137 |
|
|
// Inverse Mix Column Operation
|
138 |
|
|
//=====================================================================================
|
139 |
|
|
assign y[0] = aes_mult_04(col[2] ^ col[0]);
|
140 |
|
|
assign y[1] = aes_mult_04(col[3] ^ col[1]);
|
141 |
|
|
assign y[2] = aes_mult_02( y[1] ^ y[0]);
|
142 |
|
|
assign mix_out_dec = mix_out_enc ^ {2{y[2] ^ y[1], y[2] ^ y[0]}};
|
143 |
|
|
|
144 |
|
|
endmodule
|