diff options
Diffstat (limited to 'src/tb')
-rw-r--r-- | src/tb/tb_sha256.v | 142 | ||||
-rw-r--r-- | src/tb/tb_sha256_core.v | 3 |
2 files changed, 115 insertions, 30 deletions
diff --git a/src/tb/tb_sha256.v b/src/tb/tb_sha256.v index d881dae..6377688 100644 --- a/src/tb/tb_sha256.v +++ b/src/tb/tb_sha256.v @@ -60,6 +60,7 @@ module tb_sha256(); parameter ADDR_NAME0 = 8'h00; parameter ADDR_NAME1 = 8'h01; parameter ADDR_VERSION = 8'h02; + parameter CTRL_MODE_VALUE = 8'h04; parameter ADDR_CTRL = 8'h08; parameter CTRL_INIT_VALUE = 8'h01; @@ -95,6 +96,9 @@ module tb_sha256(); parameter ADDR_DIGEST6 = 8'h26; parameter ADDR_DIGEST7 = 8'h27; + parameter SHA224_MODE = 0; + parameter SHA256_MODE = 1; + //---------------------------------------------------------------- // Register and Wire declarations. @@ -435,17 +439,27 @@ module tb_sha256(); // // Perform test of a single block digest. //---------------------------------------------------------------- - task single_block_test(input [511 : 0] block, + task single_block_test(input mode, + input [511 : 0] block, input [255 : 0] expected); begin $display("*** TC%01d - Single block test started.", tc_ctr); write_block(block); - write_word(ADDR_CTRL, CTRL_INIT_VALUE); + + if (mode) + write_word(ADDR_CTRL, (CTRL_MODE_VALUE + CTRL_INIT_VALUE)); + else + write_word(ADDR_CTRL, CTRL_INIT_VALUE); + #(CLK_PERIOD); wait_ready(); read_digest(); + // We need to ignore the LSW in SHA224 mode. + if (mode == SHA224_MODE) + digest_data[31 : 0] = 32'h0; + if (digest_data == expected) begin $display("TC%01d: OK.", tc_ctr); @@ -470,7 +484,8 @@ module tb_sha256(); // Perform test of a double block digest. Note that we check // the digests for both the first and final block. //---------------------------------------------------------------- - task double_block_test(input [511 : 0] block0, + task double_block_test(input mode, + input [511 : 0] block0, input [255 : 0] expected0, input [511 : 0] block1, input [255 : 0] expected1 @@ -480,11 +495,20 @@ module tb_sha256(); // First block write_block(block0); - write_word(ADDR_CTRL, CTRL_INIT_VALUE); + + if (mode) + write_word(ADDR_CTRL, (CTRL_MODE_VALUE + CTRL_INIT_VALUE)); + else + write_word(ADDR_CTRL, CTRL_INIT_VALUE); + #(CLK_PERIOD); wait_ready(); read_digest(); + // We need to ignore the LSW in SHA224 mode. + if (mode == SHA224_MODE) + digest_data[31 : 0] = 32'h0; + if (digest_data == expected0) begin $display("TC%01d first block: OK.", tc_ctr); @@ -499,11 +523,20 @@ module tb_sha256(); // Final block write_block(block1); - write_word(ADDR_CTRL, CTRL_NEXT_VALUE); + + if (mode) + write_word(ADDR_CTRL, (CTRL_MODE_VALUE + CTRL_NEXT_VALUE)); + else + write_word(ADDR_CTRL, CTRL_NEXT_VALUE); + #(CLK_PERIOD); wait_ready(); read_digest(); + // We need to ignore the LSW in SHA224 mode. + if (mode == SHA224_MODE) + digest_data[31 : 0] = 32'h0; + if (digest_data == expected1) begin $display("TC%01d final block: OK.", tc_ctr); @@ -525,15 +558,19 @@ module tb_sha256(); //---------------------------------------------------------------- // restore_state_test() // - // // Perform test of a double block digest. Note that we check // the digests for both the first and final block. //---------------------------------------------------------------- - task restore_state_test(input [255 : 0] state, - input [511 : 0] block, - input [255 : 0] expected - ); - begin + task restore_state_test; + begin : restore_state + reg [255 : 0] state; + reg [511 : 0] block; + reg [255 : 0] expected; + + state = 256'h85E655D6417A17953363376A624CDE5C76E09589CAC5F811CC4B32C1F20E533A; + block = 512'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001C0; + expected = 256'h248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1; + $display("*** TC%01d - Restore state test started.", tc_ctr); // Write state. @@ -548,7 +585,7 @@ module tb_sha256(); // Process block. write_block(block); - write_word(ADDR_CTRL, CTRL_NEXT_VALUE); + write_word(ADDR_CTRL, (CTRL_MODE_VALUE + CTRL_NEXT_VALUE)); #(CLK_PERIOD); wait_ready(); read_digest(); @@ -572,14 +609,14 @@ module tb_sha256(); //---------------------------------------------------------------- - // sha256_test - // The main test functionality. + // sha224_tests() // + // Run test cases for sha224. // Test cases taken from: - // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf + // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA224.pdf //---------------------------------------------------------------- - initial - begin : sha256_test + task sha224_tests; + begin : sha224_tests_block reg [511 : 0] tc0; reg [255 : 0] res0; @@ -588,35 +625,80 @@ module tb_sha256(); reg [511 : 0] tc1_1; reg [255 : 0] res1_1; - $display(" -- Testbench for sha256 started --"); + $display("*** Testcases for sha224 functionality started."); - init_sim(); - reset_dut(); - check_name_version(); + tc0 = 512'h61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018; - // dump_dut_state(); - // write_word(ADDR_BLOCK0, 32'hdeadbeef); - // dump_dut_state(); - // read_word(ADDR_BLOCK0); - // dump_dut_state(); + res0 = 256'h23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA700000000; + single_block_test(SHA224_MODE, tc0, res0); + + tc1_0 = 512'h6162636462636465636465666465666765666768666768696768696A68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F70718000000000000000; + res1_0 = 256'h8250e65dbcf62f8466659c3333e5e91a10c8b7b0953927691f1419c200000000; + tc1_1 = 512'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001C0; + res1_1 = 256'h75388b16512776cc5dba5da1fd890150b0c6455cb4f58b195252252500000000; + double_block_test(SHA224_MODE, tc1_0, res1_0, tc1_1, res1_1); + + $display("*** Testcases for sha224 functionality completed."); + end + endtask // sha224_tests + + + //---------------------------------------------------------------- + // sha256_tests() + // + // Run test cases for sha256. + // Test cases taken from: + // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf + //---------------------------------------------------------------- + task sha256_tests; + begin : sha256_tests_block + reg [511 : 0] tc0; + reg [255 : 0] res0; + + reg [511 : 0] tc1_0; + reg [255 : 0] res1_0; + reg [511 : 0] tc1_1; + reg [255 : 0] res1_1; + + $display("*** Testcases for sha256 functionality started."); tc0 = 512'h61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018; res0 = 256'hBA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD; - single_block_test(tc0, res0); + single_block_test(SHA256_MODE, tc0, res0); tc1_0 = 512'h6162636462636465636465666465666765666768666768696768696A68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F70718000000000000000; res1_0 = 256'h85E655D6417A17953363376A624CDE5C76E09589CAC5F811CC4B32C1F20E533A; tc1_1 = 512'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001C0; res1_1 = 256'h248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1; - double_block_test(tc1_0, res1_0, tc1_1, res1_1); + double_block_test(SHA256_MODE, tc1_0, res1_0, tc1_1, res1_1); - restore_state_test(res1_0, tc1_1, res1_1); + $display("*** Testcases for sha256 functionality completed."); + end + endtask // sha256_tests + + + //---------------------------------------------------------------- + // sha256_top_test + // The main test functionality. + //---------------------------------------------------------------- + initial + begin : sha256_top_test + $display(" -- Testbench for sha256 started --"); + + init_sim(); + reset_dut(); + + check_name_version(); + sha224_tests(); + sha256_tests(); + restore_state_test(); display_test_result(); $display(" -- Testbench for sha256 done. --"); $finish; - end // sha256_test + end // sha256_top_test + endmodule // tb_sha256 //====================================================================== diff --git a/src/tb/tb_sha256_core.v b/src/tb/tb_sha256_core.v index 07e2334..981351c 100644 --- a/src/tb/tb_sha256_core.v +++ b/src/tb/tb_sha256_core.v @@ -68,6 +68,7 @@ module tb_sha256_core(); reg tb_reset_n; reg tb_init; reg tb_next; + reg tb_mode; reg [511 : 0] tb_block; reg [31 : 0] tb_state_wr_data; reg tb_state0_we; @@ -92,6 +93,7 @@ module tb_sha256_core(); .init(tb_init), .next(tb_next), + .mode(tb_mode), .block(tb_block), @@ -244,6 +246,7 @@ module tb_sha256_core(); tb_init = 0; tb_next = 0; + tb_mode = 1; tb_block = 512'h0; tb_state_wr_data = 32'h0; tb_state0_we = 0; |