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

Subversion Repositories aes_highthroughput_lowarea

[/] [aes_highthroughput_lowarea/] [trunk/] [verilog/] [bench/] [tb_kat.v] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 motilito
//---------------------------------------------------------------------------------------
2
//      Project:                        High Throughput & Low Area AES Core 
3
//
4
//      File name:                      tb_kat.v                        (Jan 1, 2011)
5
//
6
//      Writer:                         Moti Litochevski 
7
//
8
//      Description:
9
//              This file contains the core test bench to check compatibility with the Known 
10
//              Answer Test (KAT) vectors. The test bench runs all files listed in the 
11
//              "KAT_files.txt" file. 
12
//              Note that only ECB mode test vectors should be included since other modes require 
13
//              additional logic to be implemented around the AES algorithm core. 
14
//
15
//      Revision History:
16
//
17
//      Rev <revnumber>                 <Date>                  <owner> 
18
//              <comment>
19
// 
20
//---------------------------------------------------------------------------------------
21
 
22 7 motilito
`timescale 1ns / 10ps
23 8 motilito
 
24 7 motilito
module test ();
25
 
26
// define input list file name 
27
`define IN_FILE         "KAT_files.txt"
28
 
29
// global definitions 
30
`define EOF                             -1
31
`define CHAR_CR                 8'h0d
32
`define CHAR_LF                 8'h0a
33
`define CHAR_0                  8'h30
34
`define CHAR_9                  8'h39
35
`define CHAR_Eq                 8'h3D
36
`define CHAR_A                  8'h41
37
`define CHAR_F                  8'h46
38
`define CHAR_Z                  8'h5A
39
`define CHAR_a                  8'h61
40
`define CHAR_f                  8'h66
41
`define CHAR_z                  8'h7A
42
 
43 8 motilito
//---------------------------------------------------------------------------------------
44 7 motilito
// test bench signals 
45
reg clk;
46
reg reset;
47
reg [255:0] in_file_name;
48
reg key_start, enc_dec, data_in_valid, enable, test_start;
49
reg [255:0] key_in;
50
reg [1:0] key_mode;
51
reg [127:0] data_in, exp_data_out, init_vec, tmp_val;
52
wire [127:0] data_out;
53
wire key_ready, ready_out;
54
reg [15:0] param_name;
55
integer listfid, infid, intmp, tst_count, key_len;
56
 
57
// function to read a line from an input file 
58
function [255:0] fgetl;
59
input [31:0] fileid;
60
integer intint;
61
reg [255:0] outline;
62
begin
63
        // init output line 
64
        outline = 0;
65
 
66
        // pass over any CR and LF characters 
67
        intint = $fgetc(fileid);
68
        while (((intint == `CHAR_CR) || (intint == `CHAR_LF)) && (intint != `EOF))
69
                intint = $fgetc(fileid);
70
 
71
        // update output line 
72
        while ((intint != `CHAR_CR) && (intint != `CHAR_LF) && (intint != `EOF))
73
        begin
74
                outline[255:8] = outline[247:0];
75
                outline[7:0] = intint;
76
                intint = $fgetc(fileid);
77
        end
78
 
79
        // return the line read 
80
        fgetl = outline;
81
end
82
endfunction
83
 
84
// function to read the next field name from the input file 
85
function [15:0] fgetfield;
86
input [31:0] fileid;
87
integer intint;
88
reg [15:0] outfield;
89
begin
90
        // search for the start of the new line 
91
        intint = $fgetc(fileid);
92
        while ((intint != `CHAR_CR) && (intint != `CHAR_LF) && (intint != `EOF))
93
                intint = $fgetc(fileid);
94
        while (((intint < `CHAR_A) || (intint > `CHAR_Z)) && (intint != `EOF))
95
                intint = $fgetc(fileid);
96
 
97
        // the first two characters of the new line are the next field name 
98
        outfield[15:8] = intint;
99
        intint = $fgetc(fileid);
100
        outfield[7:0] = intint;
101
 
102
        // return the result 
103
        fgetfield = outfield;
104
end
105
endfunction
106
 
107
// function to read the file to the start of the parameter value 
108
function [7:0] fgetparam;
109
input [31:0] fileid;
110
integer intint;
111
begin
112
        // search for the equal sign 
113
        intint = $fgetc(fileid);
114
        while (intint != `CHAR_Eq)
115
                intint = $fgetc(fileid);
116
        // read first char of parameter 
117
        while ((intint < `CHAR_0) || ((intint > `CHAR_9) && (intint < `CHAR_A)) ||
118
                        ((intint > `CHAR_Z) && (intint < `CHAR_a)) || (intint > `CHAR_z))
119
                intint = $fgetc(fileid);
120
 
121
        // return the read character 
122
        fgetparam = intint;
123
end
124
endfunction
125
 
126
// function to convert a character to its HEX value 
127
function [7:0] char2val;
128
input [7:0] char_val;
129
integer out_val;
130
begin
131
        if ((char_val >= `CHAR_0) && (char_val <= `CHAR_9))
132
                out_val = char_val - `CHAR_0;
133
        else if ((char_val >= `CHAR_A) && (char_val <= `CHAR_F))
134
                out_val = char_val - `CHAR_A + 'd10;
135
        else if ((char_val >= `CHAR_a) && (char_val <= `CHAR_f))
136
                out_val = char_val - `CHAR_a + 'd10;
137
        else
138
                out_val = 0;
139
 
140
        // return the resulting value 
141
        char2val = out_val;
142
end
143
endfunction
144
 
145 8 motilito
//---------------------------------------------------------------------------------------
146
// test bench implementation 
147 7 motilito
// global clock generator 
148
initial         clk = 1'b1;
149
always          #10 clk = ~clk;
150
 
151
// gloabl reset generator 
152
initial
153
begin
154
        reset = 1'b1;
155
        #100;
156
        reset = 1'b0;
157
end
158
 
159
// cosmetics 
160
initial
161
begin
162
        // announce start of simulation 
163
        $display("");
164
        $display("-------------------------------------");
165
        $display("        AES_HT_LA Simulation");
166
        $display("-------------------------------------");
167
        $display("");
168
 
169
        // VCD dump 
170
        $dumpfile("test.vcd");
171
        $dumpvars(0, test);
172
        $display("");
173
end
174
 
175
// main test bench contorl module 
176
initial
177
begin
178
        // signals reset values 
179
        enc_dec = 1'b0;                 // 0: encryption; 1: decryption
180
        key_mode = 'b0;                 // 0: 128; 1: 192; 2: 256 
181
        key_in = 'b0;
182
        key_start = 1'b0;
183
        data_in_valid = 1'b0;
184
        data_in = 'b0;
185
        enable = 1;
186
        test_start = 0;
187
        @(posedge clk);
188
 
189
        // wait for global reset 
190
        wait (reset == 1'b0);
191
        repeat (10) @(posedge clk);
192
 
193
        // open input list file 
194
        listfid = $fopen(`IN_FILE, "rb");
195
 
196
        // read first input filename 
197
        in_file_name = fgetl(listfid);
198
 
199
        // loop through input files 
200
        while (in_file_name != 0)
201
        begin
202
                // announce start of simulation for the current file 
203
                $display("Starting simulation for input file: %0s", in_file_name);
204
                $display("--------------------------------------------------------------------------");
205
                $display("");
206
 
207
                // open current simulation input file 
208
                infid = $fopen(in_file_name, "rb");
209
 
210
                // read core mode for the current file 
211
                intmp = $fgetc(infid);  // first char is "[" 
212
                intmp = $fgetc(infid);  // second char should be either "E" or "D" 
213
                // check read character for mode of operation 
214
                if (intmp == "E")
215
                        // set flag accordingly 
216
                        enc_dec = 1'b0;
217
                else if (intmp == "D")
218
                        // set flag accordingly 
219
                        enc_dec = 1'b1;
220
                else
221
                begin
222
                        // no valid mode was found, announce error and quit simulation 
223
                        $display("ERROR: Simulation mode could not be determined!");
224
                        $finish;
225
                end
226
 
227
                // repeat reading the file till end of file 
228
                param_name = fgetfield(infid);
229
                while (param_name != `EOF)
230
                begin
231
                        // clear test start flag 
232
                        test_start = 0;
233
 
234
                        // check read parameter name  
235
                        if (param_name == "CO")
236
                        begin
237
                                // init test count 
238
                                tst_count = 0;
239
                                // get file pointer to the start of value 
240
                                intmp = fgetparam(infid);
241
                                // update test count value 
242
                                while ((intmp >= `CHAR_0) && (intmp <= `CHAR_9))
243
                                begin
244
                                        tst_count = (tst_count * 10) + (intmp - `CHAR_0);
245
                                        intmp = $fgetc(infid);
246
                                end
247
                        end
248
                        else if (param_name == "KE")
249
                        begin
250
                                // init key value and length 
251
                                key_in = 0;
252
                                key_len = 0;
253
 
254
                                // get file pointer to the start of value 
255
                                intmp = fgetparam(infid);
256
 
257
                                // update key value & length 
258
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
259
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
260
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
261
                                begin
262
                                        key_in[255:4] = key_in[251:0];
263
                                        key_in[3:0] = char2val(intmp);
264
                                        key_len = key_len + 4;
265
                                        intmp = $fgetc(infid);
266
                                end
267
 
268
                                // check key length to see if it is legal and if the key needs zero padding 
269
                                if (key_len == 'd128)
270
                                begin
271
                                        // update key value and mode 
272
                                        key_in = {key_in[127:0], 128'b0};
273
                                        key_mode = 2'd0;
274
                                end
275
                                else if (key_len == 'd192)
276
                                begin
277
                                        // update key value and mode 
278
                                        key_in = {key_in[191:0], 64'b0};
279
                                        key_mode = 2'd1;
280
                                end
281
                                else if (key_len == 'd256)
282
                                begin
283
                                        // update key mode 
284
                                        key_mode = 2'd2;
285
                                end
286
                                else
287
                                begin
288
                                        // illegal key length error 
289
                                        $display("ERROR: Illegal key length at test %0d (%0d)", tst_count, key_len);
290
                                        $finish;
291
                                end
292
                        end
293
                        else if (param_name == "IV")
294
                        begin
295
                                // init init vector value 
296
                                init_vec = 0;
297
 
298
                                // get file pointer to the start of value 
299
                                intmp = fgetparam(infid);
300
                                // update init vector value 
301
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
302
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
303
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
304
                                begin
305
                                        init_vec[127:4] = init_vec[123:0];
306
                                        init_vec[3:0] = char2val(intmp);
307
                                        intmp = $fgetc(infid);
308
                                end
309
                        end
310
                        else if (param_name == "CI")
311
                        begin
312
                                // init temp value 
313
                                tmp_val = 0;
314
 
315
                                // get file pointer to the start of value 
316
                                intmp = fgetparam(infid);
317
                                // update temp value 
318
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
319
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
320
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
321
                                begin
322
                                        tmp_val[127:4] = tmp_val[123:0];
323
                                        tmp_val[3:0] = char2val(intmp);
324
                                        intmp = $fgetc(infid);
325
                                end
326
 
327
                                // check simulation mode to determine if this is the last value and if 
328
                                // it is the data input or the expected data 
329
                                if (enc_dec == 1'b0)
330
                                begin
331
                                        // for encryption the CIPHERTEXT is the expected result and the 
332
                                        // simulation should start 
333
                                        exp_data_out = tmp_val;
334
                                        test_start = 1'b1;
335
                                end
336
                                else
337
                                        // for decryption the CIPHERTEXT is the input data 
338
                                        data_in = tmp_val;
339
                        end
340
                        else if (param_name == "PL")
341
                        begin
342
                                // init temp value 
343
                                tmp_val = 0;
344
 
345
                                // get file pointer to the start of value 
346
                                intmp = fgetparam(infid);
347
                                // update temp value 
348
                                while (((intmp >= `CHAR_0) && (intmp <= `CHAR_9)) ||
349
                                       ((intmp >= `CHAR_A) && (intmp <= `CHAR_F)) ||
350
                                       ((intmp >= `CHAR_a) && (intmp <= `CHAR_f)))
351
                                begin
352
                                        tmp_val[127:4] = tmp_val[123:0];
353
                                        tmp_val[3:0] = char2val(intmp);
354
                                        intmp = $fgetc(infid);
355
                                end
356
 
357
                                // check simulation mode to determine if this is the last value and if 
358
                                // it is the data input or the expected data 
359
                                if (enc_dec == 1'b0)
360
                                        // for encryption the PLAINTEXT is the input data 
361
                                        data_in = tmp_val;
362
                                else
363
                                begin
364
                                        // for decryption the PLAINTEXT is the expected result and the 
365
                                        // simulation should start 
366
                                        exp_data_out = tmp_val;
367
                                        test_start = 1'b1;
368
                                end
369
                        end
370
                        else
371
                        begin
372
                                // no matching parameter was found 
373
                                $display("ERROR: Could not find a matching parameter after test %0d", tst_count);
374
                                $finish;
375
                        end
376
 
377
                        // check if simulation should start 
378
                        if (test_start)
379
                        begin
380
                                // run core simulation 
381
                                repeat (10) @(posedge clk);
382
                                // update input key 
383
                                key_start <= 1'b1;
384
                                @ (posedge clk);
385
                                key_start <= 1'b0;
386
                                @ (posedge clk);
387
                                // wait for key to be ready 
388
                                while (!key_ready)
389
                                        @(posedge clk);
390
                                // sign input data is valid 
391
                                data_in_valid <= 1'b1;
392
                                @(posedge clk);
393
                            data_in_valid <= 1'b0;
394
                                repeat (3) @ (posedge clk);
395
                                // wait for result to be ready 
396
                                while (!data_out_valid)
397
                                        @ (posedge clk);
398
                                @ (posedge clk);
399
                                // check expected result 
400
                                if (exp_data_out != data_out)
401
                                begin
402
                                        // data output error 
403
                                        $display("ERROR: Expected data output error at test %0d", tst_count);
404
                                        repeat (10) @(posedge clk);
405
                                        $finish;
406
                                end
407
                                else
408
                                begin
409
                                        $display("Test finished OK!");
410
                                        $display("");
411
                                end
412
                        end
413
 
414
                        // read next parameter name 
415
                        param_name = fgetfield(infid);
416
                end
417
 
418
                // close input file 
419
                $fclose(infid);
420
 
421
                // read next input filename 
422
                in_file_name = fgetl(listfid);
423
        end
424
 
425
        // close input list file 
426
        $fclose(listfid);
427
        // finish simulation 
428
        $finish;
429
end
430
 
431
aes dut(
432
   .clk(clk),
433
   .reset(reset),
434
   .i_start(key_start),
435
   .i_enable(enable), //TBD
436
   .i_ende(enc_dec),
437
   .i_key(key_in),
438
   .i_key_mode(key_mode),
439
   .i_data(data_in),
440
   .i_data_valid(data_in_valid),
441
   .o_ready(ready_out),
442
   .o_data(data_out),
443
   .o_data_valid(data_out_valid),
444
   .o_key_ready(key_ready)
445
);
446
 
447
// display mode of operation, input key length and value 
448
always @ (posedge clk)
449
        if (key_start)
450
        begin
451
                // display mode of operation 
452
                if (enc_dec)
453
                        $display("Decryption test, count %0d, in file %0s", tst_count, in_file_name);
454
                else
455
                        $display("Encryption test, count %0d, in file %0s", tst_count, in_file_name);
456
 
457
                // display key size 
458
                case (key_mode)
459
                        2'b00:  $display("Key size is 128 bits");
460
                        2'b01:  $display("Key size is 192 bits");
461
                        2'b10:  $display("Key size is 256 bits");
462
                        2'b11:  $display("ERROR: Illegal key size");
463
                endcase
464
                // display key value 
465
                $display("Key In:      %16h",key_in);
466
        end
467
 
468
// display input data 
469
always @ (posedge clk)
470
        if (data_in_valid)
471
                $display("Data In:     %16h",data_in);
472
 
473
// display output data 
474
always @ (posedge clk)
475
        if (data_out_valid)
476
                $display("Data Out:    %16h",data_out);
477
 
478
endmodule
479 8 motilito
//---------------------------------------------------------------------------------------
480
//                                              Th.. Th.. Th.. Thats all folks !!!
481
//---------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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