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

Subversion Repositories aes_beh_model

[/] [aes_beh_model/] [trunk/] [bench/] [verilog/] [aes_beh_model_encrypt_tb.sv] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 schengopen
////////////////////////////////////////////////////////////////////////
2
////                                                                                                                            ////
3
//// This file is part of the AES SystemVerilog Behavioral                      ////
4
//// Model project                                                                                                      ////
5
//// http://www.opencores.org/cores/aes_beh_model/                                      ////
6
////                                                                                                                            ////
7
//// Description                                                                                                        ////
8
//// Implementation of AES SystemVerilog Behavioral                                     ////
9
//// Model according to AES Behavioral Model specification document.////
10
////                                                                                                                            ////
11
//// To Do:                                                                                                                     ////
12
//// -                                                                                                                          ////
13
////                                                                                                                            ////
14
//// Author(s):                                                                                                         ////
15
//// - scheng, schengopencores@opencores.org                                            ////
16
////                                                                                                                            ////
17
////////////////////////////////////////////////////////////////////////
18
////                                                                                                                            ////
19
//// Copyright (C) 2009 Authors and OPENCORES.ORG                                       ////
20
////                                                                                                                            ////
21
//// This source file may be used and distributed without                       ////
22
//// restriction provided that this copyright statement is not          ////
23
//// removed from the file and that any derivative work contains        ////
24
//// the original copyright notice and the associated disclaimer.       ////
25
////                                                                                                                            ////
26
//// This source file is free software; you can redistribute it         ////
27
//// and/or modify it under the terms of the GNU Lesser General         ////
28
//// Public License as published by the Free Software Foundation;       ////
29
//// either version 2.1 of the License, or (at your option) any         ////
30
//// later version.                                                                                                     ////
31
////                                                                                                                            ////
32
//// This source is distributed in the hope that it will be                     ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
35
//// PURPOSE. See the GNU Lesser General Public License for more        ////
36
//// details.                                                                                                           ////
37
////                                                                                                                            ////
38
//// You should have received a copy of the GNU Lesser General          ////
39
//// Public License along with this source; if not, download it         ////
40
//// from http://www.opencores.org/lgpl.shtml                                           ////
41
////                                                                                                                            ////
42
////////////////////////////////////////////////////////////////////////
43
 
44
// This is a testbench for verification of the aes encryption model against
45
// selected test vectors in FIPS-197, SP800-38a, and AESAVS. 128/192/256-bit
46
// encryption models are tested. Each model is tested with the following
47
// vectors
48
//
49
// - FIPS-197 sample vector test. FIPS-197 appendix C.
50
// - ECB-AES128/192/256.Encrypt sample vector test. SP800-38a appendix F.
51
// - GFSbox Known Answer Test vectors. AESAVS appendix B.
52
// - KeySbox Known Answer Test. AESAVS appendix C.
53
// - VarTxt Known Answer Test. AESAVS appendix D.
54
// - VarKey Known Answer Test. AESAVS appendix E.
55
 
56
module aes_beh_model_encrypt_tb;
57
 
58
        // Include source file for AES behavioral model
59
        `include "aes_beh_model.sv"
60
 
61
        aes128_encrypt_t encrypt128;
62
        aes192_encrypt_t encrypt192;
63
        aes256_encrypt_t encrypt256;
64
 
65
        logic   [0:127] ct;
66
 
67
        // Test vector file
68
        `include "aes_vec.sv"
69
 
70
        initial
71
        begin
72
                //--------------------------------------------------------------------------------
73
                //
74
                // Start of 128-bit model test
75
                //
76
                //--------------------------------------------------------------------------------
77
 
78
                encrypt128 = new;
79
 
80
                // FIPS-197 128-bit Sample Vector Test
81
                // FIPS-197 appendix C.1
82
                $display("\nFIPS-197 128-bit Sample Vector Test");
83
                for (int k=0; k<`FIPS197_128_VEC_SIZE; k++)
84
                begin
85
                        encrypt128.KeyExpand(FIPS197_128_kt[k]);
86
                        encrypt128.LoadPt(FIPS197_128_pt[k]);
87
                        encrypt128.run(0);
88
                        ct = encrypt128.GetState();
89
                        $display("kt=%h pt=%h ct=%h expected=%h",FIPS197_128_kt[k],FIPS197_128_pt[k],ct,FIPS197_128_ct[k]);
90
                        if (ct != FIPS197_128_ct[k])
91
                        begin
92
                                $display("***Mismatch");
93
                                $stop;
94
                        end
95
                end
96
                $display("FIPS-197 128-bit Sample Vector Test finished");
97
 
98
                // ECB-AES128.Encrypt sample vector test
99
                // SP800-38a appendix F.1.1
100
                $display("\nECB-AES128.Encrypt sample vector test");
101
                for (int k=0; k<`ECB_ENCRYPT_128_VEC_SIZE; k++)
102
                begin
103
                        encrypt128.KeyExpand(ECBEncrypt_128_kt);
104
                        encrypt128.LoadPt(ECBEncrypt_128_pt[k]);
105
                        encrypt128.run(0);
106
                        ct = encrypt128.GetState();
107
                        $display("kt=%h pt=%h ct=%h expected=%h",ECBEncrypt_128_kt,ECBDecrypt_128_pt[k],ct,ECBDecrypt_128_ct[k]);
108
                        if (ct != ECBEncrypt_128_ct[k])
109
                        begin
110
                                $display("***Mismatch");
111
                                $stop;
112
                        end
113
                end
114
                $display("ECB-AES128.Encrypt sample vector test finished");
115
 
116
                // 128-bit GFSbox Known Answer Test vectors.
117
                // AESAVS appendix B.1
118
                $display("\n128-bit GFSbox Known Answer Test");
119
                for (int k=0; k<`GFSbox_128_VEC_SIZE; k++)
120
                begin
121
                        encrypt128.KeyExpand(GFSbox_128_kt);
122
                        encrypt128.LoadPt(GFSbox_128_pt[k]);
123
                        encrypt128.run(0);
124
                        ct = encrypt128.GetState();
125
                        $display("kt=%h pt=%h ct=%h expected=%h",GFSbox_128_kt,GFSbox_128_pt[k],ct,GFSbox_128_ct[k]);
126
                        if (ct != GFSbox_128_ct[k])
127
                        begin
128
                                $display("***Mismatch");
129
                                $stop;
130
                        end
131
                end
132
                $display("128-bit GFSbox Known Answer Test finished");
133
 
134
                // KeySbox Known Answer Test. AESAVS appendix C.1.
135
                $display("\n128-bit KeySbox Known Answer Test");
136
                for (int k=0; k<`KEYSBOX_128_VEC_SIZE; k++)
137
                begin
138
                        encrypt128.KeyExpand(KeySbox_128_kt[k]);
139
                        encrypt128.LoadPt(KeySbox_128_pt);
140
                        encrypt128.run(0);
141
                        ct = encrypt128.GetState();
142
                        $display("kt=%h pt=%h ct=%h expected=%h",KeySbox_128_kt[k],KeySbox_128_pt,ct,KeySbox_128_ct[k]);
143
                        if (ct != KeySbox_128_ct[k])
144
                        begin
145
                                $display("***Mismatch");
146
                                $stop;
147
                        end
148
                end
149
                $display("128-bit KeySbox Known Answer Test finished");
150
 
151
                // VarTxt Known Answer Test. AESAVS appendix D.1.
152
                $display("\n128-bit VarTxt Known Answer Test");
153
                for (int k=0; k<`VARTXT_128_VEC_SIZE; k++)
154
                begin
155
                        encrypt128.KeyExpand(VarTxt_128_kt);
156
                        encrypt128.LoadPt(VarTxt_128_pt[k]);
157
                        encrypt128.run(0);
158
                        ct = encrypt128.GetState();
159
                        $display("kt=%h pt=%h ct=%h expected=%h",VarTxt_128_kt,VarTxt_128_pt[k],ct,VarTxt_128_ct[k]);
160
                        if (ct != VarTxt_128_ct[k])
161
                        begin
162
                                $display("***Mismatch");
163
                                $stop;
164
                        end
165
                end
166
                $display("128-bit VarTxt Known Answer Test finished");
167
 
168
                // VarKey Known Answer Test. AESAVS appendix E.1.
169
                $display("\n128-bit VarKey Known Answer Test");
170
                for (int k=0; k<`VARKEY_128_VEC_SIZE; k++)
171
                begin
172
                        encrypt128.KeyExpand(VarKey_128_kt[k]);
173
                        encrypt128.LoadPt(VarKey_128_pt);
174
                        encrypt128.run(0);
175
                        ct = encrypt128.GetState();
176
                        $display("kt=%h pt=%h ct=%h expected=%h",VarKey_128_kt[k],VarKey_128_pt[k],ct,VarKey_128_ct[k]);
177
                        if (ct != VarKey_128_ct[k])
178
                        begin
179
                                $display("***Mismatch");
180
                                $stop;
181
                        end
182
                end
183
                $display("128-bit VarKey Known Answer Test finished");
184
 
185
                //--------------------------------------------------------------------------------
186
                //
187
                // Start of 192-bit model test
188
                //
189
                //--------------------------------------------------------------------------------
190
 
191
                encrypt192 = new;
192
 
193
                // FIPS-197 192-bit Sample Vector Test
194
                // FIPS-197 appendix C.2
195
                $display("\nFIPS-197 192-bit Sample Vector Test");
196
                for (int k=0; k<`FIPS197_192_VEC_SIZE; k++)
197
                begin
198
                        encrypt192.KeyExpand(FIPS197_192_kt[k]);
199
                        encrypt192.LoadPt(FIPS197_192_pt[k]);
200
                        encrypt192.run(0);
201
                        ct = encrypt192.GetState();
202
                        $display("kt=%h pt=%h ct=%h expected=%h",FIPS197_192_kt[k],FIPS197_192_pt[k],ct,FIPS197_192_ct[k]);
203
                        if (ct != FIPS197_192_ct[k])
204
                        begin
205
                                $display("***Mismatch");
206
                                $stop;
207
                        end
208
                end
209
                $display("FIPS-197 192-bit Sample Vector Test finished");
210
 
211
                // ECB-AES192.Encrypt sample vector test
212
                // SP800-38a appendix F.1.3
213
                $display("\nECB-AES192.Encrypt sample vector test");
214
                for (int k=0; k<`ECB_ENCRYPT_192_VEC_SIZE; k++)
215
                begin
216
                        encrypt192.KeyExpand(ECBEncrypt_192_kt);
217
                        encrypt192.LoadPt(ECBEncrypt_192_pt[k]);
218
                        encrypt192.run(0);
219
                        ct = encrypt192.GetState();
220
                        $display("kt=%h pt=%h ct=%h expected=%h",ECBDecrypt_192_kt,ECBDecrypt_192_pt[k],ct,ECBDecrypt_192_ct[k]);
221
                        if (ct != ECBEncrypt_192_ct[k])
222
                        begin
223
                                $display("***Mismatch");
224
                                $stop;
225
                        end
226
                end
227
                $display("ECB-AES192.Encrypt sample vector test finished");
228
 
229
                // 192-bit GFSbox Known Answer Test vectors.
230
                // AESAVS appendix B.2
231
                $display("\n192-bit GFSbox Known Answer Test");
232
                for (int k=0; k<`GFSbox_192_VEC_SIZE; k++)
233
                begin
234
                        encrypt192.KeyExpand(GFSbox_192_kt);
235
                        encrypt192.LoadPt(GFSbox_192_pt[k]);
236
                        encrypt192.run(0);
237
                        ct = encrypt192.GetState();
238
                        $display("kt=%h pt=%h ct=%h expected=%h",GFSbox_192_kt,GFSbox_192_pt[k],ct,GFSbox_192_ct[k]);
239
                        if (ct != GFSbox_192_ct[k])
240
                        begin
241
                                $display("***Mismatch");
242
                                $stop;
243
                        end
244
                end
245
                $display("192-bit GFSbox Known Answer Test finished");
246
 
247
                // 192-bit KeySbox Known Answer Test. AESAVS appendix C.2.
248
                $display("\n192-bit KeySbox Known Answer Test");
249
                for (int k=0; k<`KEYSBOX_192_VEC_SIZE; k++)
250
                begin
251
                        encrypt192.KeyExpand(KeySbox_192_kt[k]);
252
                        encrypt192.LoadPt(KeySbox_192_pt);
253
                        encrypt192.run(0);
254
                        ct = encrypt192.GetState();
255
                        $display("kt=%h pt=%h ct=%h expected=%h",KeySbox_192_kt[k],KeySbox_192_pt,ct,KeySbox_192_ct[k]);
256
                        if (ct != KeySbox_192_ct[k])
257
                        begin
258
                                $display("***Mismatch");
259
                                $stop;
260
                        end
261
                end
262
                $display("192-bit KeySbox Known Answer Test finished");
263
 
264
                // 192-bit VarTxt Known Answer Test. AESAVS appendix D.2.
265
                $display("\n192-bit VarTxt Known Answer Test");
266
                for (int k=0; k<`VARTXT_192_VEC_SIZE; k++)
267
                begin
268
                        encrypt192.KeyExpand(VarTxt_192_kt);
269
                        encrypt192.LoadPt(VarTxt_192_pt[k]);
270
                        encrypt192.run(0);
271
                        ct = encrypt192.GetState();
272
                        $display("kt=%h pt=%h ct=%h expected=%h",VarTxt_192_kt,VarTxt_192_pt[k],ct,VarTxt_192_ct[k]);
273
                        if (ct != VarTxt_192_ct[k])
274
                        begin
275
                                $display("***Mismatch");
276
                                $stop;
277
                        end
278
                end
279
                $display("192-bit VarTxt Known Answer Test finished");
280
 
281
                // 192-bit VarKey Known Answer Test. AESAVS appendix E.2.
282
                $display("\n192-bit VarKey Known Answer Test");
283
                for (int k=0; k<`VARKEY_192_VEC_SIZE; k++)
284
                begin
285
                        encrypt192.KeyExpand(VarKey_192_kt[k]);
286
                        encrypt192.LoadPt(VarKey_192_pt);
287
                        encrypt192.run(0);
288
                        ct = encrypt192.GetState();
289
                        $display("kt=%h pt=%h ct=%h expected=%h",VarKey_192_kt[k],VarKey_192_pt,ct,VarKey_192_ct[k]);
290
                        if (ct != VarKey_192_ct[k])
291
                        begin
292
                                $display("***Mismatch");
293
                                $stop;
294
                        end
295
                end
296
                $display("192-bit VarKey Known Answer Test finished");
297
 
298
                //--------------------------------------------------------------------------------
299
                //
300
                // Start of 256-bit model test
301
                //
302
                //--------------------------------------------------------------------------------
303
 
304
                encrypt256 = new;
305
 
306
                // FIPS-197 256-bit Sample Vector Test
307
                // FIPS-197 appendix C.3
308
                $display("\nFIPS-197 256-bit Sample Vector Test");
309
                for (int k=0; k<`FIPS197_256_VEC_SIZE; k++)
310
                begin
311
                        encrypt256.KeyExpand(FIPS197_256_kt[k]);
312
                        encrypt256.LoadPt(FIPS197_256_pt[k]);
313
                        encrypt256.run(0);
314
                        ct = encrypt256.GetState();
315
                        $display("kt=%h pt=%h ct=%h expected=%h",FIPS197_256_kt[k],FIPS197_256_pt[k],ct,FIPS197_256_ct[k]);
316
                        if (ct != FIPS197_256_ct[k])
317
                        begin
318
                                $display("***Mismatch");
319
                                $stop;
320
                        end
321
                end
322
                $display("FIPS-197 256-bit Sample Vector Test finished");
323
 
324
                // ECB-AES256.Encrypt sample vector test
325
                // SP800-38a appendix F.1.5
326
                $display("\nECB-AES256.Encrypt sample vector test");
327
                for (int k=0; k<`ECB_ENCRYPT_256_VEC_SIZE; k++)
328
                begin
329
                        encrypt256.KeyExpand(ECBEncrypt_256_kt);
330
                        encrypt256.LoadPt(ECBEncrypt_256_pt[k]);
331
                        encrypt256.run(0);
332
                        ct  = encrypt256.GetState();
333
                        $display("kt=%h pt=%h ct=%h expected=%h",ECBDecrypt_256_kt,ECBDecrypt_256_pt[k],ct,ECBDecrypt_256_ct[k]);
334
                        if (ct != ECBDecrypt_256_ct[k])
335
                        begin
336
                                $display("***Mismatch");
337
                                $stop;
338
                        end
339
                end
340
                $display("ECB-AES256.Encrypt sample vector test finished");
341
 
342
                // 256-bit GFSbox Known Answer Test vectors.
343
                // AESAVS appendix B.2
344
                $display("\n256-bit GFSbox Known Answer Test");
345
                for (int k=0; k<`GFSbox_256_VEC_SIZE; k++)
346
                begin
347
                        encrypt256.KeyExpand(GFSbox_256_kt);
348
                        encrypt256.LoadPt(GFSbox_256_pt[k]);
349
                        encrypt256.run(0);
350
                        ct  = encrypt256.GetState();
351
                        $display("kt=%h pt=%h ct=%h expected=%h",GFSbox_256_kt,GFSbox_256_pt[k],ct,GFSbox_256_ct[k]);
352
                        if (ct != GFSbox_256_ct[k])
353
                        begin
354
                                $display("***Mismatch");
355
                                $stop;
356
                        end
357
                end
358
                $display("256-bit GFSbox Known Answer Test finished");
359
 
360
                // 256-bit KeySbox Known Answer Test. AESAVS appendix C.2.
361
                $display("\n256-bit KeySbox Known Answer Test");
362
                for (int k=0; k<`KEYSBOX_256_VEC_SIZE; k++)
363
                begin
364
                        encrypt256.KeyExpand(KeySbox_256_kt[k]);
365
                        encrypt256.LoadPt(KeySbox_256_pt);
366
                        encrypt256.run(0);
367
                        ct  = encrypt256.GetState();
368
                        $display("kt=%h pt=%h ct=%h expected=%h",KeySbox_256_kt[k],KeySbox_256_pt,ct,KeySbox_256_ct[k]);
369
                        if (ct != KeySbox_256_ct[k])
370
                        begin
371
                                $display("***Mismatch");
372
                                $stop;
373
                        end
374
                end
375
                $display("256-bit KeySbox Known Answer Test finished");
376
 
377
                // 256-bit VarTxt Known Answer Test. AESAVS appendix D.2.
378
                $display("\n256-bit VarTxt Known Answer Test");
379
                for (int k=0; k<`VARTXT_256_VEC_SIZE; k++)
380
                begin
381
                        encrypt256.KeyExpand(VarTxt_256_kt);
382
                        encrypt256.LoadPt(VarTxt_256_pt[k]);
383
                        encrypt256.run(0);
384
                        ct  = encrypt256.GetState();
385
                        $display("kt=%h pt=%h ct=%h expected=%h",VarTxt_256_kt,VarTxt_256_pt[k],ct,VarTxt_256_ct[k]);
386
                        if (ct != VarTxt_256_ct[k])
387
                        begin
388
                                $display("***Mismatch");
389
                                $stop;
390
                        end
391
                end
392
                $display("256-bit VarTxt Known Answer Test finished");
393
 
394
                // 256-bit VarKey Known Answer Test. AESAVS appendix E.2.
395
                $display("\n256-bit VarKey Known Answer Test");
396
                for (int k=0; k<`VARKEY_256_VEC_SIZE; k++)
397
                begin
398
                        encrypt256.KeyExpand(VarKey_256_kt[k]);
399
                        encrypt256.LoadPt(VarKey_256_pt);
400
                        encrypt256.run(0);
401
                        ct  = encrypt256.GetState();
402
                        $display("kt=%h pt=%h ct=%h expected=%h",VarKey_256_kt[k],VarKey_256_pt,ct,VarKey_256_ct[k]);
403
                        if (ct != VarKey_256_ct[k])
404
                        begin
405
                                $display("***Mismatch");
406
                                $stop;
407
                        end
408
                end
409
                $display("256-bit VarKey Known Answer Test finished");
410
        end
411
endmodule

powered by: WebSVN 2.1.0

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