1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
//======================================================================
//
// tb_sha3.v
// -----------------------------
// Testbench for the SHA-3 core.
//
// Authors: Pavel Shatov, Joachim Strombergson, Pernd Paysan
// Copyright (c) 2015, 2017 NORDUnet A/S
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================
module tb_sha3();
//----------------------------------------------------------------
// Hashes of empty messages
//----------------------------------------------------------------
localparam [511:0] SHA3_224_EMPTY_MSG =
{ 224'h6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7,
{512-224{1'bX}}};
localparam [511:0] SHA3_256_EMPTY_MSG =
{ 256'ha7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a,
{512-256{1'bX}}};
localparam [511:0] SHA3_384_EMPTY_MSG =
{ 384'h0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004,
{512-384{1'bX}}};
localparam [511:0] SHA3_512_EMPTY_MSG =
{ 512'ha69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26};
/*
* The "short" message is the "abc" string (0x616263):
*
* https://www.di-mgt.com.au/sha_testvectors.html
*
*/
localparam [511:0] SHA3_224_SHORT_MSG =
{ 224'he642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf,
{512-224{1'bX}}};
localparam [511:0] SHA3_256_SHORT_MSG =
{ 256'h3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532,
{512-256{1'bX}}};
localparam [511:0] SHA3_384_SHORT_MSG =
{ 384'hec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25,
{512-384{1'bX}}};
localparam [511:0] SHA3_512_SHORT_MSG =
{ 512'hb751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0};
/*
* The "long" message is the 1600-bit string 0xA3...A3 taken from:
* https://csrc.nist.gov/Projects/Cryptographic-Standards-and-Guidelines/example-values
*
*/
localparam [511:0] SHA3_224_LONG_MSG =
{ 224'h9376816ABA503F72F96CE7EB65AC095DEEE3BE4BF9BBC2A1CB7E11E0,
{512-224{1'bX}}};
localparam [511:0] SHA3_256_LONG_MSG =
{ 256'h79F38ADEC5C20307A98EF76E8324AFBFD46CFD81B22E3973C65FA1BD9DE31787,
{512-256{1'bX}}};
localparam [511:0] SHA3_384_LONG_MSG =
{ 384'h1881DE2CA7E41EF95DC4732B8F5F002B189CC1E42B74168ED1732649CE1DBCDD76197A31FD55EE989F2D7050DD473E8F,
{512-384{1'bX}}};
localparam [511:0] SHA3_512_LONG_MSG =
{ 512'hE76DFAD22084A8B1467FCF2FFA58361BEC7628EDF5F3FDC0E4805DC48CAEECA81B7C13C30ADF52A3659584739A2DF46BE589C51CA1A4A8416DF6545A1CE8BA00};
/*
* Sponge Parameters
*
*/
`define SHA3_224_BLOCK_BITS 1152
`define SHA3_256_BLOCK_BITS 1088
`define SHA3_384_BLOCK_BITS 832
`define SHA3_512_BLOCK_BITS 576
`define SHA3_224_OUTPUT_BITS 224
`define SHA3_256_OUTPUT_BITS 256
`define SHA3_384_OUTPUT_BITS 384
`define SHA3_512_OUTPUT_BITS 512
//----------------------------------------------------------------
// Internal constant and parameter definitions.
//----------------------------------------------------------------
parameter CLK_HALF_PERIOD = 2;
parameter CLK_PERIOD = 2 * CLK_HALF_PERIOD;
//----------------------------------------------------------------
// Register and Wire declarations.
//----------------------------------------------------------------
reg tb_clk;
reg tb_rst_n;
reg tb_we;
reg [ 6:0] tb_addr;
reg [31:0] tb_wr_data;
reg tb_dut_init;
reg tb_dut_next;
wire [31:0] tb_rd_data;
wire tb_rdy;
integer i, j;
integer num_err;
integer total_bits;
integer block_words, output_words;
reg [511:0] hash_shreg; // output
reg [31: 0] hash_word; // current output word
reg mismatch; // error flag
//----------------------------------------------------------------
// Device Under Test.
//----------------------------------------------------------------
sha3 dut
(
.clk (tb_clk),
.nreset (tb_rst_n),
.w (tb_we),
.addr (tb_addr),
.din (tb_wr_data),
.dout (tb_rd_data),
.init (tb_dut_init),
.next (tb_dut_next),
.ready (tb_rdy)
);
//----------------------------------------------------------------
// clk_gen
//
// Clock generator process.
//----------------------------------------------------------------
always
begin : clk_gen
#CLK_HALF_PERIOD tb_clk = ~tb_clk;
end // clk_gen
//----------------------------------------------------------------
// reset_dut()
//
// Toggles reset to force the DUT into a well defined state.
//----------------------------------------------------------------
task reset_dut;
begin
$display("*** Toggling reset...");
tb_rst_n = 0;
#(4 * CLK_HALF_PERIOD);
tb_rst_n = 1;
end
endtask // reset_dut()
//----------------------------------------------------------------
// init_sim()
//
// Initialize all counters and testbed functionality as well
// as setting the DUT inputs to defined values.
//----------------------------------------------------------------
task init_sim;
begin
tb_clk = 0;
tb_rst_n = 0;
tb_we = 0;
tb_addr = 7'd0;
tb_wr_data = 32'h0;
tb_dut_init = 0;
tb_dut_next = 0;
end
endtask // init_sim()
//----------------------------------------------------------------
// test_empty_message()
//
// The first pritimive test that verifies what happens with an
// empty input message.
//----------------------------------------------------------------
task test_empty_message;
input [511:0] correct_hash; // known-good hash
input integer block_size; // block length (bits)
input integer output_size; // output length (bits)
begin
$display("*** Testing %0d-bit hash (%0d-bit block)...", output_size, block_size);
// number of words in block and output
block_words = block_size >> 5; // block_words = block_length / 32
output_words = output_size >> 5; // output_words = output_length / 32
// wait for some time
#(4*CLK_PERIOD);
/*
* Note, that we must wipe entire block buffer, because
* it may contain leftovers from previous calculations!
* We wipe the registers and apply padding at the same time.
*
*/
for (i=0; i<50; i=i+1) begin
case (i)
0: tb_wr_data = 32'h00000006; // ...0001 | 10
block_words-1: tb_wr_data = 32'h80000000; // 1000...
default: tb_wr_data = 32'h00000000;
endcase
tb_addr = {1'b0, i[5:0]}; // increment address
tb_we = 1; // enable writing
#(CLK_PERIOD);
end
tb_addr = 'bX;
tb_we = 0;
/* run one absord-squeeze cycle */
sha3_sponge_pump_init();
// reset error counter
mismatch = 0;
// save the expected result
hash_shreg = correct_hash;
// now read and compare
for (i=0; i<output_words; i=i+1) begin
tb_addr = {1'b1, i[5:0]};
#(CLK_PERIOD);
// swap bytes in the read value
for (j=31; j>0; j=j-8)
hash_word[j-:8] = tb_rd_data[31-j+:8];
$display(" *** hash_word[%0d]: reference = %08x, calculated = %08x",
i, hash_shreg[511-:32], hash_word);
// compare
if (hash_shreg[511-:32] !== hash_word) begin
mismatch = 1;
num_err = num_err + 1;
end
hash_shreg = {hash_shreg[511-32:0], {32{1'bX}}};
end
if (mismatch) $display(" *** Test failed.");
else $display(" *** Test passed.");
end
endtask // test_empty_message
//----------------------------------------------------------------
// test_short_message()
//
// The second simple test that verifies what happens with an
// input message, that fits into just one block.
//----------------------------------------------------------------
task test_short_message;
input [511:0] correct_hash; // known-good hash
input integer block_size; // block length (bits)
input integer output_size; // output length (bits)
begin
$display("*** Testing %0d-bit hash (%0d-bit block)...", output_size, block_size);
// number of words in block and output
block_words = block_size >> 5; // block_words = block_length / 32
output_words = output_size >> 5; // output_words = output_length / 32
// wait for some time
#(4*CLK_PERIOD);
/*
* Note, that we must wipe entire internal state, because
* it may contain leftovers from previous calculations!
* We wipe the internal state, fill in the message (0x616263)
* and apply padding all at once.
*
*/
for (i=0; i<50; i=i+1) begin
case (i)
0: tb_wr_data = 32'h06636261; // ...0001 | 10 | 'cba'
block_words-1: tb_wr_data = 32'h80000000; // 1000...
default: tb_wr_data = 32'h00000000;
endcase
tb_addr = {1'b0, i[5:0]}; // increment address
tb_we = 1; // enable writing
#(CLK_PERIOD);
end
tb_addr = 'bX;
tb_we = 0;
/* run one absord-squeeze cycle */
sha3_sponge_pump_init();
// reset error counter
mismatch = 0;
// save the expected result
hash_shreg = correct_hash;
// now read and compare
for (i=0; i<output_words; i=i+1) begin
tb_addr = {1'b1, i[5:0]};
#(CLK_PERIOD);
// swap bytes in the read value
for (j=31; j>0; j=j-8)
hash_word[j-:8] = tb_rd_data[31-j+:8];
$display(" *** hash_word[%0d]: reference = %08x, calculated = %08x",
i, hash_shreg[511-:32], hash_word);
// compare
if (hash_shreg[511-:32] !== hash_word) begin
mismatch = 1;
num_err = num_err + 1;
end
hash_shreg = {hash_shreg[511-32:0], {32{1'bX}}};
end
if (mismatch) $display(" *** Test failed.");
else $display(" *** Test passed.");
end
endtask // test_short_message
//----------------------------------------------------------------
// test_long_message()
//
// The second not so pritimive test, that verifies what happens
// when an input message spans more than one block.
//----------------------------------------------------------------
task test_long_message;
input [511:0] correct_hash;
input integer block_size;
input integer output_size;
begin
$display("*** Testing %0d-bit hash (%0d-bit block)...", output_size, block_size);
// number of words in block and output
block_words = block_size >> 5; // block_words = block_length / 32
output_words = output_size >> 5; // output_words = output_length / 32
// wait for some time
#(4*CLK_PERIOD);
/* start filling the first block */
total_bits = 1600;
/*
* note, that we must wipe entire internal state, because
* it may contain leftovers from previous calculations!
*/
for (i=0; i<50; i=i+1) begin
tb_wr_data = (i < block_words) ? 32'hA3A3A3A3 : 32'h00000000;
tb_addr = {1'b0, i[5:0]};
tb_we = 1;
#(CLK_PERIOD);
if (i < block_words)
total_bits = total_bits - 32;
end
tb_addr = 'bX;
tb_we = 0;
/* run one absord-squeeze cycle */
sha3_sponge_pump_init();
// process remaining bits
while (total_bits > -64) begin
/*
* note, that there's no need to fill entire bank now,
* because x ^ 0 == x
*/
for (i=0; i<block_words; i=i+1) begin
if (total_bits > 0) begin
// message
tb_wr_data = 32'hA3A3A3A3;
tb_addr = {1'b0, i[5:0]};
tb_we = 1;
#(CLK_PERIOD);
total_bits = total_bits - 32;
end else if (total_bits == 0) begin
// padding
tb_wr_data = 32'h00000006;
tb_addr = {1'b0, i[5:0]};
tb_we = 1;
#(CLK_PERIOD);
total_bits = total_bits - 32;
end else if (i == (block_words-1)) begin
// more padding
tb_wr_data = 32'h80000000;
tb_addr = {1'b0, i[5:0]};
tb_we = 1;
#(CLK_PERIOD);
total_bits = total_bits - 32;
end else begin
// intermediate bytes
tb_wr_data = 32'h00000000;
tb_addr = {1'b0, i[5:0]};
tb_we = 1;
#(CLK_PERIOD);
end
end
tb_addr = 'bX;
tb_we = 0;
/* run one absord-squeeze cycle */
sha3_sponge_pump_next();
end
// reset error counter
mismatch = 0;
// save the expected result
hash_shreg = correct_hash;
// now read and compare
for (i=0; i<output_words; i=i+1) begin
tb_addr = {1'b1, i[5:0]};
#(CLK_PERIOD);
// swap bytes in the read value
for (j=31; j>0; j=j-8)
hash_word[j-:8] = tb_rd_data[31-j+:8];
$display(" *** hash_word[%0d]: reference = %08x, calculated = %08x",
i, hash_shreg[511-:32], hash_word);
// compare
if (hash_shreg[511-:32] !== hash_word) begin
mismatch = 1;
num_err = num_err + 1;
end
hash_shreg = {hash_shreg[511-32:0], {32{1'bX}}};
end
if (mismatch) $display(" *** Test failed.");
else $display(" *** Test passed.");
end
endtask // test_long_message
//----------------------------------------------------------------
// sha3_sponge_pump_init;
//
// Make the "sponge" absord input data and then wait for output
// data to be "squeezed out".
//----------------------------------------------------------------
task sha3_sponge_pump_init;
begin : sha3_sponge_pump_init_task
reg poll_rdy; // flag to keep waiting while the DUT is busy
// start operation
tb_dut_init = 1;
#(CLK_PERIOD);
tb_dut_init = 0;
// poll ready output until the operation is finished
poll_rdy = 1;
while (poll_rdy) begin
#(CLK_PERIOD);
poll_rdy = !tb_rdy;
end
end
endtask // sha3_sponge_pump
//----------------------------------------------------------------
// sha3_sponge_pump_next;
//
// Make the "sponge" absord input data and then wait for output
// data to be "squeezed out".
//----------------------------------------------------------------
task sha3_sponge_pump_next;
begin : sha3_sponge_pump_next_task
reg poll_rdy; // flag to keep waiting while the DUT is busy
// start operation
tb_dut_next = 1;
#(CLK_PERIOD);
tb_dut_next = 0;
// poll ready output until the operation is finished
poll_rdy = 1;
while (poll_rdy) begin
#(CLK_PERIOD);
poll_rdy = !tb_rdy;
end
end
endtask // sha3_sponge_pump
//----------------------------------------------------------------
// sha3_test
// The main test functionality.
//----------------------------------------------------------------
initial
begin : sha3_test
$display("*** Testbench for sha3.v started.");
num_err = 0;
init_sim();
reset_dut();
test_empty_message(SHA3_224_EMPTY_MSG, `SHA3_224_BLOCK_BITS, `SHA3_224_OUTPUT_BITS);
test_empty_message(SHA3_256_EMPTY_MSG, `SHA3_256_BLOCK_BITS, `SHA3_256_OUTPUT_BITS);
test_empty_message(SHA3_384_EMPTY_MSG, `SHA3_384_BLOCK_BITS, `SHA3_384_OUTPUT_BITS);
test_empty_message(SHA3_512_EMPTY_MSG, `SHA3_512_BLOCK_BITS, `SHA3_512_OUTPUT_BITS);
test_short_message(SHA3_224_SHORT_MSG, `SHA3_224_BLOCK_BITS, `SHA3_224_OUTPUT_BITS);
test_short_message(SHA3_256_SHORT_MSG, `SHA3_256_BLOCK_BITS, `SHA3_256_OUTPUT_BITS);
test_short_message(SHA3_384_SHORT_MSG, `SHA3_384_BLOCK_BITS, `SHA3_384_OUTPUT_BITS);
test_short_message(SHA3_512_SHORT_MSG, `SHA3_512_BLOCK_BITS, `SHA3_512_OUTPUT_BITS);
test_long_message(SHA3_224_LONG_MSG, `SHA3_224_BLOCK_BITS, `SHA3_224_OUTPUT_BITS);
test_long_message(SHA3_256_LONG_MSG, `SHA3_256_BLOCK_BITS, `SHA3_256_OUTPUT_BITS);
test_long_message(SHA3_384_LONG_MSG, `SHA3_384_BLOCK_BITS, `SHA3_384_OUTPUT_BITS);
test_long_message(SHA3_512_LONG_MSG, `SHA3_512_BLOCK_BITS, `SHA3_512_OUTPUT_BITS);
$display("*** Testbench for sha3.v done.");
if (num_err == 0)
$display(" *** All tests passed.");
else
$display(" *** %0d tests not passed.", num_err);
$finish;
end // sha3_test
endmodule // tb_sha3
//======================================================================
// EOF tb_sha3.v
//======================================================================
|