1 |
3 |
shadow7853 |
library sha2;
|
2 |
|
|
use sha2.shaPkg.all;
|
3 |
|
|
library ieee;
|
4 |
|
|
use ieee.std_logic_1164.all;
|
5 |
|
|
use ieee.numeric_std.all;
|
6 |
|
|
use work.std_logic_1164_additions.all;
|
7 |
|
|
|
8 |
|
|
-- Add your library and packages declaration here ...
|
9 |
|
|
|
10 |
|
|
entity sha2_tb is
|
11 |
|
|
end sha2_tb;
|
12 |
|
|
|
13 |
|
|
architecture TB_ARCHITECTURE of sha2_tb is
|
14 |
|
|
-- Component declaration of the tested unit
|
15 |
|
|
component sha2
|
16 |
|
|
port (
|
17 |
|
|
clk : in std_logic;
|
18 |
|
|
rst : in std_logic;
|
19 |
|
|
chunk : in std_logic_vector(0 to CW-1);
|
20 |
|
|
len : in std_logic_vector(0 to CLENBIT-1);
|
21 |
|
|
load : in std_logic;
|
22 |
|
|
hash : out std_logic_vector(0 to WW-1);
|
23 |
|
|
valid : out std_logic
|
24 |
|
|
);
|
25 |
|
|
end component;
|
26 |
|
|
|
27 |
|
|
-- Stimulus signals - signals mapped to the input and inout ports of tested entity
|
28 |
|
|
signal clk : std_logic := '0';
|
29 |
|
|
signal rst : std_logic;
|
30 |
|
|
signal chunk : std_logic_vector(0 to CW-1);
|
31 |
|
|
signal len : std_logic_vector(0 to CLENBIT-1);
|
32 |
|
|
signal load : std_logic;
|
33 |
|
|
|
34 |
|
|
-- Observed signals - signals mapped to the output ports of tested entity
|
35 |
|
|
signal hash : std_logic_vector(0 to WW-1);
|
36 |
|
|
signal valid : std_logic;
|
37 |
|
|
signal stop : std_logic;
|
38 |
|
|
signal finish : std_logic;
|
39 |
|
|
signal output_hash : std_logic_vector(0 to OS-1);
|
40 |
|
|
signal hash_read : integer;
|
41 |
|
|
|
42 |
|
|
-- Add your code here ...
|
43 |
|
|
|
44 |
|
|
constant clk_period : time := 50 ns;
|
45 |
|
|
begin
|
46 |
|
|
clk <= not clk after clk_period / 2;
|
47 |
|
|
|
48 |
|
|
-- Unit Under Test port map
|
49 |
|
|
UUT : sha2
|
50 |
|
|
port map (
|
51 |
|
|
clk => clk,
|
52 |
|
|
rst => rst,
|
53 |
|
|
chunk => chunk,
|
54 |
|
|
len => len,
|
55 |
|
|
load => load,
|
56 |
|
|
hash => hash,
|
57 |
|
|
valid => valid
|
58 |
|
|
);
|
59 |
|
|
|
60 |
|
|
-- Add your stimulus here ...
|
61 |
|
|
test : process (clk)
|
62 |
|
|
|
63 |
|
|
-- Choose a Benchmark !!!
|
64 |
|
|
-- for test http://www.fileformat.info/tool/hash.htm SHA-256 from ASCII and HEX
|
65 |
|
|
-- http://www.miniwebtool.com/sha224-hash-generator SHA-224 only ASCII
|
66 |
|
|
|
67 |
|
|
-- TEST VECTOR SHA-224 (includere sha224Pkg ed escludere sha256Pkg)
|
68 |
|
|
-- EMPTY STRING
|
69 |
|
|
--variable text_len : integer := 0; variable text : std_logic_vector(0 to 3) := x"0"; variable expected_hash : std_logic_vector(0 to OS-1) := x"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
|
70 |
|
|
-- abc
|
71 |
|
|
--variable text_len : integer := 24; variable text : std_logic_vector(0 to text_len-1) := x"616263"; variable expected_hash : std_logic_vector(0 to OS-1) := x"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
|
72 |
|
|
-- The quick brown fox jumps over the lazy dog
|
73 |
|
|
--variable text_len : integer := 344; variable text : std_logic_vector(0 to text_len-1) := x"54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67"; variable expected_hash : std_logic_vector(0 to OS-1) := x"730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525";
|
74 |
|
|
-- The quick brown fox jumps over the lazy dog.
|
75 |
|
|
--variable text_len : integer := 352; variable text : std_logic_vector(0 to text_len-1) := x"54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e"; variable expected_hash : std_logic_vector(0 to OS-1) := x"619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c";
|
76 |
|
|
-- 60 caratteri 'a' (60*8+1 % 512 > 448, richiede blocco extra)
|
77 |
|
|
--variable text_len : integer := 480; variable text : std_logic_vector(0 to text_len-1) := x"616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"efda4316fe2d457d622cf1fc42993d41566f77449b7494b38e250c41";
|
78 |
|
|
-- 115 caratteri 'a' (125*8 % 512 < 448, NON richiede blocco extra)
|
79 |
|
|
--variable text_len : integer := 920; variable text : std_logic_vector(0 to text_len-1) := x"61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"3705538e8bcc6a326824e0aa1cb57a5eea41bd332f39eb296b78a06c";
|
80 |
|
|
-- 125 caratteri 'a' (125*8 % 512 > 448, richiede blocco extra)
|
81 |
|
|
--variable text_len : integer := 1000; variable text : std_logic_vector(0 to text_len-1) := x"6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"0087b823c4dfaefcca81f8ff6c5d1a3ca0104d466e51fc6b450b1494";
|
82 |
|
|
-- 126 caratteri 'abc' (126*8+1 % 512 > 448, richiede blocco extra)
|
83 |
|
|
--variable text_len : integer := 1008; variable text : std_logic_vector(0 to text_len-1) := x"616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263"; variable expected_hash : std_logic_vector(0 to OS-1) := x"c50b5fd769d58c627b773ec065ced52eb5b461fe90a444c2ea498661";
|
84 |
|
|
|
85 |
|
|
-- TEST VECTOR SHA-256 (includere sha256Pkg ed escludere sha224Pkg)
|
86 |
|
|
-- EMPTY STRING
|
87 |
|
|
--variable text_len : integer := 0; variable text : std_logic_vector(0 to 3) := x"0"; variable expected_hash : std_logic_vector(0 to OS-1) := x"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
88 |
|
|
-- abc
|
89 |
|
|
--variable text_len : integer := 24; variable text : std_logic_vector(0 to text_len-1) := x"616263"; variable expected_hash : std_logic_vector(0 to OS-1) := x"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
90 |
|
|
-- The quick brown fox jumps over the lazy dog
|
91 |
|
|
--variable text_len : integer := 344; variable text : std_logic_vector(0 to text_len-1) := x"54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67"; variable expected_hash : std_logic_vector(0 to OS-1) := x"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
|
92 |
|
|
-- The quick brown fox jumps over the lazy dog.
|
93 |
|
|
--variable text_len : integer := 352; variable text : std_logic_vector(0 to text_len-1) := x"54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e"; variable expected_hash : std_logic_vector(0 to OS-1) := x"ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c";
|
94 |
|
|
-- 60 caratteri 'a' (60*8+1 % 512 > 448, richiede blocco extra)
|
95 |
|
|
--variable text_len : integer := 480; variable text : std_logic_vector(0 to text_len-1) := x"616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"11ee391211c6256460b6ed375957fadd8061cafbb31daf967db875aebd5aaad4";
|
96 |
|
|
-- 115 caratteri 'a' (125*8+1 % 512 < 448, NON richiede blocco extra)
|
97 |
|
|
--variable text_len : integer := 920; variable text : std_logic_vector(0 to text_len-1) := x"61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"64410e651b346524cfe56e68c237ea76c0377921697027eb794a067501fb2910";
|
98 |
|
|
-- 125 caratteri 'a' (125*8+1 % 512 > 448, richiede blocco extra)
|
99 |
|
|
--variable text_len : integer := 1000; variable text : std_logic_vector(0 to text_len-1) := x"6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161"; variable expected_hash : std_logic_vector(0 to OS-1) := x"a8e1a0f35c15c01a458bcb345528b751556c14850f6f9bdcc9933b8003d29b43";
|
100 |
|
|
-- 126 caratteri 'abc' (126*8+1 % 512 > 448, richiede blocco extra)
|
101 |
|
|
variable text_len : integer := 1008; variable text : std_logic_vector(0 to text_len-1) := x"616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263616263"; variable expected_hash : std_logic_vector(0 to OS-1) := x"bfd187c5d9f06e35d0794555203b0d043cb6e6b4a5176c763a7892891bd3a9e2";
|
102 |
|
|
|
103 |
|
|
-- CUSTOM
|
104 |
|
|
--variable text_len : integer := ;
|
105 |
|
|
--variable text : std_logic_vector(0 to text_len-1) := x"";
|
106 |
|
|
--variable expected_hash : std_logic_vector(0 to OS-1) := x"";
|
107 |
|
|
|
108 |
|
|
variable doloop : bit := '1'; -- conversioni cicliche
|
109 |
|
|
variable num_a : integer := 0; -- numero di caratteri 'a' == "aaa...aa"
|
110 |
|
|
|
111 |
|
|
variable counter : integer := -2;
|
112 |
|
|
variable last_blk : std_logic_vector(0 to WW-1);
|
113 |
|
|
variable c : unsigned(0 to CW-1) := "00000001";
|
114 |
|
|
variable index : integer := 0;
|
115 |
|
|
begin
|
116 |
|
|
|
117 |
|
|
if (clk'event and clk = '0') then
|
118 |
|
|
c := c rol 1;
|
119 |
|
|
|
120 |
|
|
if counter < 0 then
|
121 |
|
|
stop <= '0';
|
122 |
|
|
finish <= '0';
|
123 |
|
|
rst <= '1';
|
124 |
|
|
load <= '0';
|
125 |
|
|
chunk <= (others => '0');
|
126 |
|
|
len <= (others => '0');
|
127 |
|
|
output_hash <= (others => '0');
|
128 |
|
|
hash_read <= 0;
|
129 |
|
|
else
|
130 |
|
|
if valid = '1' then
|
131 |
|
|
output_hash(hash_read * WW to (hash_read + 1) * WW - 1) <= hash;
|
132 |
|
|
hash_read <= hash_read + 1;
|
133 |
|
|
elsif hash_read = WOUT then
|
134 |
|
|
hash_read <= 0;
|
135 |
|
|
if output_hash = expected_hash then
|
136 |
|
|
report "--------- COMPUTE HASH OK --------" severity NOTE;
|
137 |
|
|
report to_hstring(output_hash) severity NOTE;
|
138 |
|
|
else
|
139 |
|
|
report "--------- COMPUTED: --------" severity NOTE;
|
140 |
|
|
report to_hstring(output_hash) severity NOTE;
|
141 |
|
|
report "--------- EXPECTED: --------" severity NOTE;
|
142 |
|
|
report to_hstring(expected_hash) severity NOTE;
|
143 |
|
|
report "--------- COMPUTE HASH FAILED --------" severity FAILURE;
|
144 |
|
|
end if;
|
145 |
|
|
finish <= '1';
|
146 |
|
|
end if;
|
147 |
|
|
|
148 |
|
|
if doloop = '1' and finish = '1' and index = 30 then
|
149 |
|
|
counter := 0;
|
150 |
|
|
index := 0;
|
151 |
|
|
stop <= '0';
|
152 |
|
|
finish <= '0';
|
153 |
|
|
output_hash <= (others => '0');
|
154 |
|
|
elsif doloop = '1' and finish = '1' then
|
155 |
|
|
index := index + 1;
|
156 |
|
|
elsif stop = '1' then
|
157 |
|
|
load <= '0';
|
158 |
|
|
chunk <= (others => '0');
|
159 |
|
|
elsif counter = 0 then
|
160 |
|
|
rst <= '0';
|
161 |
|
|
elsif text_len > 0 then
|
162 |
|
|
load <= '1';
|
163 |
|
|
if (index+1) * CW < text_len then
|
164 |
|
|
chunk <= text(index * CW to (index+1) * CW - 1);
|
165 |
|
|
len <= "1000";
|
166 |
|
|
else
|
167 |
|
|
chunk <= text(index * CW to text_len - 1);
|
168 |
|
|
len <= std_logic_vector(to_unsigned(integer(integer(text_len - integer(index) * integer(CW))), 4));
|
169 |
|
|
counter := 1000000;
|
170 |
|
|
index := 0;
|
171 |
|
|
stop <= '1';
|
172 |
|
|
end if;
|
173 |
|
|
index := index + 1;
|
174 |
|
|
elsif num_a = 0 and counter = 1 then -- caso empty chunk e len sono a 0
|
175 |
|
|
load <= '1';
|
176 |
|
|
chunk <= (others => '0');
|
177 |
|
|
len <= (others => '0');
|
178 |
|
|
elsif counter <= num_a then
|
179 |
|
|
load <= '1';
|
180 |
|
|
chunk <= x"61"; -- a
|
181 |
|
|
len <= "1000";
|
182 |
|
|
else
|
183 |
|
|
load <= '0';
|
184 |
|
|
chunk <= (others => '0');
|
185 |
|
|
end if;
|
186 |
|
|
end if;
|
187 |
|
|
counter := counter + 1;
|
188 |
|
|
end if;
|
189 |
|
|
end process;
|
190 |
|
|
|
191 |
|
|
end TB_ARCHITECTURE;
|
192 |
|
|
|
193 |
|
|
configuration TESTBENCH_FOR_sha2 of sha2_tb is
|
194 |
|
|
for TB_ARCHITECTURE
|
195 |
|
|
for UUT : sha2
|
196 |
|
|
use entity work.sha2(hash);
|
197 |
|
|
end for;
|
198 |
|
|
end for;
|
199 |
|
|
end TESTBENCH_FOR_sha2;
|