tb.v
4.04 KB
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
// This confidential and propriety software may be used
// only as authorized by a licensing agreement from
// Ocean Logic Pty Ltd http:/www.ocean-logic.com
//
// In the event of publication, the following notice is
// applicable
//
// (C) COPYRIGHT 1996-2002 Ocean Logic Pty Ltd
// ALL RIGHTS RESERVED
//
// the entire notice must be reproduced on all
// authorized copies
//
// File : tb.v
// Author : Vincenzo Liguori
// Date : 02-07-18
// Version 1.0
// Abstract :
// Testbench for the AES decryption
// key expander core.
// Keys taken from the NIST special
// publication 800-38A, AES CBC test
//
// Modification history :
// Date by Version Change description
// -------------------------------------------------
// 02-07-18 VL 1.0 Original
`timescale 1ns/1ns
module tb;
`define tclk 10
reg clk, rstn;
reg go, en;
reg [1:0] ksize;
reg [31:0] kin;
wire [31:0] key;
wire [5:0] addr;
wire key_req;
wire key_last;
// Key data storage
reg [0:31] memk [0:59];
reg [0:31] memk2 [0:59];
integer i;
// Main test bench
initial begin
clk = 0;
rstn = 1;
en = 1;
go = 0;
ksize = 0; // Set 128 bit key size
kin = 0;
#(`tclk/2+1);
#`tclk rstn=0; // Reset pulse
#`tclk rstn=1;
#(2*`tclk);
go = 1; // Start the core
// Expand 128 bit key
kin = 32'h2b7e1516;
#`tclk;
kin = 32'h28aed2a6;
#`tclk;
kin = 32'habf71588;
#`tclk;
kin = 32'h09cf4f3c;
#(41*`tclk);
// Expand 192 bit key
ksize = 1; // Set 192 bit key size
kin = 32'h8e73b0f7;
#`tclk;
kin = 32'hda0e6452;
#`tclk;
kin = 32'hc810f32b;
#`tclk;
kin = 32'h809079e5;
#`tclk;
kin = 32'h62f8ead2;
#`tclk;
kin = 32'h522c6b7b;
#(47*`tclk);
// Expand 256 bit key
ksize = 2; // Set 256 bit key size
kin = 32'h603deb10;
#`tclk;
kin = 32'h15ca71be;
#`tclk;
kin = 32'h2b73aef0;
#`tclk;
kin = 32'h857d7781;
#`tclk;
kin = 32'h1f352c07;
#`tclk;
kin = 32'h3b6108d7;
#`tclk;
kin = 32'h2d9810a3;
#`tclk;
kin = 32'h0914dff4;
#`tclk;
end
// Checking process
initial @(posedge go) begin
// Check 128 bit key expansion
// Store the expanding key
// according to the addr index
for(i=0;i<44;i=i+1) begin
@(posedge clk);
memk[addr]= key;
end
// Load the reference file
$readmemh("dk128.dat",memk2);
// memk[] is read backward and
// checked versus the reference
for(i=43;i>=0;i=i-1) begin
// $display("%x %x",memk[i],memk2[43-i]);
if(memk[i] != memk2[43-i]) begin
// $display("Key expansion failed");
$finish;
end
end
$display("128 bit key expanded correctly");
// Check 192 bit key expansion
// Store the expanding key
// according to the addr index
for(i=0;i<52;i=i+1) begin
@(posedge clk);
memk[addr]= key;
end
// Load the reference file
$readmemh("dk192.dat",memk2);
// memk[] is read backward and
// checked versus the reference
for(i=51;i>=0;i=i-1) begin
// $display("%x %x",memk[i],memk2[51-i]);
if(memk[i] != memk2[51-i]) begin
$display("Key expansion failed");
$finish;
end
end
$display("192 bit key expanded correctly");
// Check 256 bit key expansion
// Store the expanding key
// according to the addr index
for(i=0;i<60;i=i+1) begin
@(posedge clk);
memk[addr]= key;
end
// Load the reference file
$readmemh("dk256.dat",memk2);
// memk[] is read backward and
// checked versus the reference
for(i=59;i>=0;i=i-1) begin
// $display("%x %x",memk[i],memk2[59-i]);
if(memk[i] != memk2[59-i]) begin
$display("Key expansion failed");
$finish;
end
end
$display("256 bit key expanded correctly");
$display("Test successful");
$finish;
end
// System clock
always #(`tclk/2) clk=~clk;
// Key expander module instantiation
kexp_d u_kexp_d(
.clk(clk),
.rstn(rstn),
.en(en),
.go(go),
.ksize(ksize),
.kin(kin),
.key(key),
.addr(addr),
.key_req(key_req),
.key_last(key_last)
);
endmodule