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

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [rtl/] [sBox_8.v] - Blame information for rev 2

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
//// This file is part of the APB to I2C project
9
////
10
//// http://www.opencores.org/cores/apbi2c/
11
////
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
 
77
//Reference: A Very Compact Rijndael S-box, D. Canright
78
 
79
module sBox_8
80
(
81
  //OUTPUTS
82
  output [7:0] sbox_out_enc, // Direct SBOX
83
        output [7:0] sbox_out_dec, // Inverse SBOX
84
  //INPUTS
85
  input  [7:0] sbox_in,
86
        input enc_dec,
87
        input clk
88
);
89
//`include "include/sbox_functions.vf"
90
 
91
// Functions used by SBOX Logic
92
// For more detail, see "A Very Compact Rijndael  S-Box" by D. Canright
93
localparam ENC = 1;
94
localparam DEC = 0;
95
 
96
function [1:0] gf_sq_2;
97
        input [1:0] in;
98
        begin
99
        gf_sq_2 = {in[0], in[1]};
100
        end
101
endfunction
102
 
103
function [1:0] gf_sclw_2;
104
        input [1:0] in;
105
        begin
106
        gf_sclw_2 = {^in, in[1]};
107
  end
108
endfunction
109
 
110
function [1:0] gf_sclw2_2;
111
        input [1:0] in;
112
        begin
113
        gf_sclw2_2 = {in[0], ^in};
114
  end
115
endfunction
116
 
117
function [1:0] gf_muls_2;
118
        input [1:0] in1, in2;
119
  input in3, in4;
120
        begin
121
        gf_muls_2 = ( ~(in1 & in2) ) ^ ( {2{~(in3 & in4)}} );
122
  end
123
endfunction
124
 
125
function [1:0] gf_muls_scl_2;
126
  input [1:0] in1, in2;
127
  input in3, in4;
128
        reg [1:0] nand_in1_in2;
129
  reg nand_in3_in4;
130
        begin
131
        nand_in1_in2 = ~(in1 & in2);
132
        nand_in3_in4 = ~(in3 & in4);
133
        gf_muls_scl_2 = {nand_in3_in4 ^ nand_in1_in2[0], ^nand_in1_in2};
134
  end
135
endfunction
136
 
137
function [3:0] gf_inv_4;
138
  input [3:0] in;
139
        reg [1:0] in_hg;
140
        reg [1:0] in_lw;
141
        reg [1:0] out_gf_mul_2;
142
        reg [1:0] out_gf_mul_3;
143
        reg [1:0] out_gf_sq2_3;
144
        reg [1:0] in_sq2_3;
145
        reg xor_in_hg, xor_in_lw;
146
        begin
147
                in_hg = in[3:2];
148
                in_lw = in[1:0];
149
                xor_in_hg = ^in_hg;
150
                xor_in_lw = ^in_lw;
151
                in_sq2_3 = {~(in_hg[1] | in_lw[1]) ^ (~(xor_in_hg & xor_in_lw)), ~(xor_in_hg | xor_in_lw) ^ (~(in_hg[0] & in_lw[0]))};
152
 
153
        out_gf_sq2_3 = gf_sq_2(in_sq2_3);
154
                out_gf_mul_2 = gf_muls_2(out_gf_sq2_3, in_lw, ^out_gf_sq2_3, xor_in_lw);
155
                out_gf_mul_3 = gf_muls_2(out_gf_sq2_3, in_hg, ^out_gf_sq2_3, xor_in_hg);
156
 
157
                gf_inv_4 = {out_gf_mul_2, out_gf_mul_3};
158
 end
159
endfunction
160
 
161
 
162
function [3:0] gf_sq_scl_4;
163
  input [3:0] in;
164
        reg [1:0] in_hg;
165
        reg [1:0] in_lw;
166
        reg [1:0] out_gf_sq2_1;
167
        reg [1:0] out_gf_sq2_2;
168
        reg [1:0] out_gf_sclw2_1;
169
        begin
170
                in_hg = in[3:2];
171
                in_lw = in[1:0];
172
 
173
                out_gf_sq2_1 = gf_sq_2(in_hg ^ in_lw );
174
                out_gf_sq2_2 = gf_sq_2(in_lw);
175
                out_gf_sclw2_1 = gf_sclw_2(out_gf_sq2_2);
176
 
177
                gf_sq_scl_4 = {out_gf_sq2_1, out_gf_sclw2_1};
178
        end
179
endfunction
180
 
181
 
182
function [3:0] gf_muls_4;
183
  input [3:0] in1;
184
  input [3:0] in2;
185
        reg [1:0] in1_hg;
186
        reg [1:0] in1_lw;
187
        reg [1:0] in2_hg;
188
        reg [1:0] in2_lw;
189
        reg [1:0] xor_in1_hl;
190
        reg [1:0] xor_in2_hl;
191
        reg [1:0] out_gf_mul_1;
192
        reg [1:0] out_gf_mul_2;
193
        reg [1:0] out_gf_mul_scl_1;
194
        begin
195
                in1_hg = in1[3:2];
196
                in1_lw = in1[1:0];
197
                in2_hg = in2[3:2];
198
                in2_lw = in2[1:0];
199
                xor_in1_hl = in1_hg ^ in1_lw;
200
                xor_in2_hl = in2_hg ^ in2_lw;
201
 
202
                out_gf_mul_1 = gf_muls_2(in1_hg, in2_hg, in1[3] ^ in1[2], in2[3] ^ in2[2]);
203
                out_gf_mul_2 = gf_muls_2(in1_lw, in2_lw, in1[1] ^ in1[0], in2[1] ^ in2[0]);
204
                out_gf_mul_scl_1 = gf_muls_scl_2(xor_in1_hl, xor_in2_hl, ^xor_in1_hl, ^xor_in2_hl);
205
 
206
        gf_muls_4 = {out_gf_mul_1 ^ out_gf_mul_scl_1,  out_gf_mul_2 ^ out_gf_mul_scl_1};
207
        end
208
endfunction
209
 
210
function [3:0] gf_inv_8_stage1;
211
  input [7:0] in;
212
        reg [3:0] in_hg;
213
        reg [3:0] in_lw;
214
        reg [3:0] out_gf_mul4_2;
215
        reg [3:0] out_gf_mul4_3;
216
        reg [3:0] out_gf_inv4_2;
217
        reg c1, c2, c3;
218
        begin
219
                in_hg = in[7:4];
220
                in_lw = in[3:0];
221
 
222
                c1 = ~((in_hg[3] ^ in_hg[2]) & (in_lw[3] ^ in_lw[2]));
223
                c2 = ~((in_hg[2] ^ in_hg[0]) & (in_lw[2] ^ in_lw[0]));
224
                c3 = ~((^in_hg) & (^in_lw));
225
 
226
                gf_inv_8_stage1 =
227
                                 {(~((in_hg[2] ^ in_hg[0]) | (in_lw[2] ^ in_lw[0])) ^ (~(in_hg[3] & in_lw[3]))) ^ c1 ^ c3,
228
          (~((in_hg[3] ^ in_hg[1]) | (in_lw[3] ^ in_lw[1])) ^ (~(in_hg[2] & in_lw[2]))) ^ c1 ^ c2,
229
          (~((in_hg[1] ^ in_hg[0]) | (in_lw[1] ^ in_lw[0])) ^ (~(in_hg[1] & in_lw[1]))) ^ c2 ^ c3,
230
          ((~(in_hg[0] | in_lw[0])) ^ (~((in_hg[1] ^ in_hg[0]) & (in_lw[1] ^ in_lw[0])))) ^ (~((in_hg[3] ^ in_hg[1]) & (in_lw[3] ^ in_lw[1]))) ^ c2};
231
        end
232
endfunction
233
 
234
function [7:0] gf_inv_8_stage2;
235
  input [7:0] in;
236
        input [3:0] c;
237
        reg [3:0] in_hg;
238
        reg [3:0] in_lw;
239
        reg [3:0] out_gf_mul4_2;
240
        reg [3:0] out_gf_mul4_3;
241
        reg [3:0] out_gf_inv4_2;
242
        reg c1, c2, c3;
243
        begin
244
                in_hg = in[7:4];
245
                in_lw = in[3:0];
246
 
247
                out_gf_inv4_2 = gf_inv_4(c);
248
                out_gf_mul4_2 = gf_muls_4(out_gf_inv4_2, in_lw);
249
                out_gf_mul4_3 = gf_muls_4(out_gf_inv4_2, in_hg);
250
 
251
                gf_inv_8_stage2 = {out_gf_mul4_2, out_gf_mul4_3};
252
        end
253
endfunction
254
 
255
function [15:0] isomorphism;
256
  input [7:0] in;
257
  reg r1, r2, r3, r4, r5, r6, r7, r8, r9;
258
  reg [7:0] enc, dec;
259
  begin
260
    r1 = in[7]  ^ in[5];
261
    r2 = in[7] ~^ in[4];
262
    r3 = in[6]  ^ in[0];
263
    r4 = in[5] ~^ r3;
264
    r5 = in[4]  ^ r4;
265
    r6 = in[3]  ^ in[0];
266
    r7 = in[2]  ^ r1;
267
    r8 = in[1]  ^ r3;
268
    r9 = in[3]  ^ r8;
269
 
270
    enc = {r7 ~^ r8, r5, in[1] ^ r4, r1 ~^ r3, in[1] ^ r2 ^ r6, ~in[0], r4, in[2] ~^ r9};
271
    dec = {r2, in[4] ^ r8, in[6] ^ in[4], r9, in[6] ~^ r2, r7, in[4] ^ r6, in[1] ^ r5};
272
 
273
    isomorphism = {enc, dec};
274
  end
275
endfunction
276
 
277
function [7:0] isomorphism_inv;
278
  input [7:0] in;
279
        input op_type;
280
  reg r1, r2, r3, r4, r5, r6, r7, r8, r9, r10;
281
  begin
282
    r1  = in[7]  ^ in[3];
283
    r2  = in[6]  ^ in[4];
284
    r3  = in[6]  ^ in[0];
285
    r4  = in[5] ~^ in[3];
286
    r5  = in[5] ~^ r1;
287
    r6  = in[5] ~^ in[1];
288
    r7  = in[4] ~^ r6;
289
    r8  = in[2]  ^ r4;
290
    r9  = in[1]  ^ r2;
291
    r10 = r3     ^ r5;
292
 
293
                if(op_type == ENC)
294
        isomorphism_inv = {r4, r1, r3, r5, r2 ^ r5, r3 ^ r8, r7, r9};
295
                else
296
        isomorphism_inv = {in[4] ~^ in[1], in[1] ^ r10, in[2] ^ r10, in[6] ~^ in[1], r8 ^ r9, in[7] ~^ r7, r6, ~in[2]};
297
 
298
  end
299
endfunction
300
 
301
 
302
 
303
 
304
wire [7:0] base_new_enc, base_new_dec, base_new;
305
wire [7:0] base_enc, base_dec;
306
wire [3:0] out_gf_inv8_stage1;
307
wire [7:0] out_gf_inv8_1;
308
wire [7:0] out_gf_inv8_2;
309
 
310
reg [3:0] out_gf_pp;
311
reg [7:0] base_new_pp;
312
 
313
assign {base_new_enc, base_new_dec} = isomorphism(sbox_in);
314
 
315
assign base_new = ~(enc_dec ? base_new_enc : base_new_dec);
316
assign out_gf_inv8_stage1 = gf_inv_8_stage1(base_new);
317
 
318
always @(posedge clk)
319
        begin
320
                out_gf_pp <= out_gf_inv8_stage1;
321
                base_new_pp <= base_new;
322
        end
323
 
324
assign out_gf_inv8_1 = gf_inv_8_stage2(base_new_pp, out_gf_pp);
325
 
326
assign sbox_out_enc = ~isomorphism_inv(out_gf_inv8_1, ENC);
327
assign sbox_out_dec = ~isomorphism_inv(out_gf_inv8_1, DEC);
328
 
329
endmodule

powered by: WebSVN 2.1.0

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