-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtb_des.vhd
428 lines (385 loc) · 14.8 KB
/
tb_des.vhd
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
-- ======================================================================
-- DES encryption/decryption testbench
-- tests according to NIST 800-17 special publication
-- Copyright (C) 2011 Torsten Meissner
-------------------------------------------------------------------------
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-- ======================================================================
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library osvvm;
use osvvm.RandomPkg.all;
use std.env.all;
use work.des_pkg.all;
use work.tb_des_pkg.all;
entity tb_des is
end entity tb_des;
architecture rtl of tb_des is
signal s_reset : std_logic := '0';
signal s_clk : std_logic := '0';
signal s_mode : std_logic := '0';
signal s_key : std_logic_vector(0 to 63) := (others => '0');
signal s_datain : std_logic_vector(0 to 63) := (others => '0');
signal s_validin : std_logic := '0';
signal s_acceptout : std_logic;
signal s_dataout : std_logic_vector(0 to 63);
signal s_validout : std_logic;
signal s_acceptin : std_logic;
procedure cryptData(datain : in std_logic_vector(0 to 63);
key : in std_logic_vector(0 to 63);
mode : in boolean;
dataout : out std_logic_vector(0 to 63);
bytelen : in integer) is
begin
report "VHPIDIRECT cryptData" severity failure;
end procedure;
attribute foreign of cryptData: procedure is "VHPIDIRECT cryptData";
function swap (datain : std_logic_vector(0 to 63)) return std_logic_vector is
variable v_data : std_logic_vector(0 to 63);
begin
for i in 0 to 7 loop
for y in 0 to 7 loop
v_data((i*8)+y) := datain((i*8)+7-y);
end loop;
end loop;
return v_data;
end function;
begin
s_clk <= not(s_clk) after 10 ns;
s_reset <= '1' after 100 ns;
testP : process is
variable v_key : std_logic_vector(0 to 63);
variable v_datain : std_logic_vector(0 to 63);
variable v_dataout : std_logic_vector(0 to 63);
variable v_plaintext : std_logic_vector(0 to 63) := x"8000000000000000";
variable v_random : RandomPType;
begin
-- ENCRYPTION TESTS
s_validin <= '0';
s_acceptin <= '0';
s_mode <= '0';
s_datain <= (others => '0');
s_key <= (others => '0');
wait until s_reset = '1';
report "# ENCRYPTION TESTS";
-- Variable plaintext known answer test
report "# Variable plaintext known answer test";
v_key := x"0101010101010101";
v_datain := x"8000000000000000";
for index in c_variable_plaintext_known_answers'range loop
wait until rising_edge(s_clk);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_variable_plaintext_known_answers(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
v_datain := '0' & v_datain(0 to 62);
end loop;
-- Inverse permutation known answer test
report "# Inverse permutation known answer test";
v_key := x"0101010101010101";
for index in c_variable_plaintext_known_answers'range loop
v_datain := c_variable_plaintext_known_answers(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = v_plaintext
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
v_plaintext := '0' & v_plaintext(0 to 62);
end loop;
-- Variable key known answer test
report "# Variable key known answer test";
v_key := x"8000000000000000";
v_datain := (others => '0');
for index in c_variable_key_known_answers'range loop
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_variable_key_known_answers(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
if (index = 6 or index = 13 or index = 20 or index = 27 or index = 34 or
index = 41 or index = 48) then
v_key := "00" & v_key(0 to 61);
else
v_key := '0' & v_key(0 to 62);
end if;
end loop;
-- Permutation operation known answer test
report "# Permutation operation known answer test";
v_datain := (others => '0');
for index in c_permutation_operation_known_answers_keys'range loop
wait until rising_edge(s_clk);
v_key := c_permutation_operation_known_answers_keys(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_permutation_operation_known_answers_cipher(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
-- Substitution table known answer test
report "# Substitution table known answer test";
for index in c_substitution_table_test_keys'range loop
wait until rising_edge(s_clk);
v_key := c_substitution_table_test_keys(index);
v_datain := c_substitution_table_test_plain(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_substitution_table_test_cipher(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
-- Random key & data openSSL reference test
report "# Random key & data openSSL reference test";
for index in 0 to 63 loop
wait until rising_edge(s_clk);
v_key := v_random.RandSlv(64);
v_datain := v_random.RandSlv(64);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), true, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
-- DECRYPTION TESTS
report "# DECRYPTION TESTS";
s_mode <= '1';
-- Variable ciphertext known answer test
report "# Variable ciphertext known answer test";
v_key := x"0101010101010101";
v_plaintext := x"8000000000000000";
for index in c_variable_plaintext_known_answers'range loop
wait until rising_edge(s_clk);
v_datain := c_variable_plaintext_known_answers(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = v_plaintext
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
v_plaintext := '0' & v_plaintext(0 to 62);
end loop;
-- Initial permutation known answer test
report "# Initial permutation known answer test";
v_key := x"0101010101010101";
v_datain := x"8000000000000000";
for index in c_variable_plaintext_known_answers'range loop
wait until rising_edge(s_clk);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_variable_plaintext_known_answers(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
v_datain := '0' & v_datain(0 to 62);
end loop;
-- Variable key known answer test
report "# Variable key known answer test";
v_key := x"8000000000000000";
for index in c_variable_key_known_answers'range loop
v_datain := c_variable_key_known_answers(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = 64x"0"
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
if (index = 6 or index = 13 or index = 20 or index = 27 or index = 34 or
index = 41 or index = 48) then
v_key := "00" & v_key(0 to 61);
else
v_key := '0' & v_key(0 to 62);
end if;
end loop;
-- Permutation operation known answer test
report "# Permutation operation known answer test";
v_datain := (others => '0');
for index in c_permutation_operation_known_answers_keys'range loop
wait until rising_edge(s_clk);
v_key := c_permutation_operation_known_answers_keys(index);
v_datain := c_permutation_operation_known_answers_cipher(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = 64x"0"
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
-- Substitution table known answer test
report "# Substitution table known answer test";
for index in c_substitution_table_test_keys'range loop
wait until rising_edge(s_clk);
v_key := c_substitution_table_test_keys(index);
v_datain := c_substitution_table_test_cipher(index);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = c_substitution_table_test_plain(index)
report "Encryption error"
severity failure;
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
-- Random key & data openSSL reference test
report "# Random key & data openSSL reference test";
for index in 0 to 63 loop
wait until rising_edge(s_clk);
v_key := v_random.RandSlv(64);
v_datain := v_random.RandSlv(64);
s_validin <= '1';
s_key <= v_key;
s_datain <= v_datain;
cryptData(swap(v_datain), swap(v_key), false, v_dataout, v_datain'length/8);
wait until rising_edge(s_clk) and s_acceptout = '1';
s_validin <= '0';
wait until rising_edge(s_clk) and s_validout = '1';
s_acceptin <= '1';
assert s_dataout = swap(v_dataout)
report "Encryption openSSL reference error"
severity failure;
wait until rising_edge(s_clk);
s_acceptin <= '0';
end loop;
wait for 100 ns;
report "# Successfully passed all tests";
finish(0);
wait;
end process testP;
i_des : des
generic map (
design_type => "ITER"
)
port map (
reset_i => s_reset,
clk_i => s_clk,
mode_i => s_mode,
key_i => s_key,
data_i => s_datain,
valid_i => s_validin,
accept_o => s_acceptout,
data_o => s_dataout,
valid_o => s_validout,
accept_i => s_acceptin
);
end architecture rtl;