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 11

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

powered by: WebSVN 2.1.0

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