-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKschedule.v
302 lines (251 loc) · 7.54 KB
/
Kschedule.v
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
////////////////////////////////////////////////////////////////////////////////////////////////
// This source is dedicated to the research paper enttled //
// "An 8-bit Serialized Architecture of SEED Block Cipher for Constrained Devices" //
// on IET Circuits, Devices & Systems journal //
// Authors : Lampros Pyrgas, Filippos Pirpilidis and Paris Kitsos //
// Institute: University of the Peloponnese //
// Department: Electrical and Computer Engineering //
// //
// This source is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. //
////////////////////////////////////////////////////////////////////////////////////////////////
// 4 LSBs first
module Kschedule(
input clk,
input reset_n,
input [7:0] Key,
output[7:0] SK,
input [4:0] main_counter,
input [4:0] round_counter
);
wire [31:0] KC_0 = 32'h9E3779B9;
wire [31:0] KC_1 = 32'h3C6EF373;
wire [31:0] KC_2 = 32'h78DDE6E6;
wire [31:0] KC_3 = 32'hF1BBCDCC;
wire [31:0] KC_4 = 32'hE3779B99;
wire [31:0] KC_5 = 32'hC6EF3733;
wire [31:0] KC_6 = 32'h8DDE6E67;
wire [31:0] KC_7 = 32'h1BBCDCCF;
wire [31:0] KC_8 = 32'h3779B99E;
wire [31:0] KC_9 = 32'h6EF3733C;
wire [31:0] KC_A = 32'hDDE6E678;
wire [31:0] KC_B = 32'hBBCDCCF1;
wire [31:0] KC_C = 32'h779B99E3;
wire [31:0] KC_D = 32'hEF3733C6;
wire [31:0] KC_E = 32'hDE6E678D;
wire [31:0] KC_F = 32'hBCDCCF1B;
wire [31:0] KC_32 = (
round_counter[3:0] == 0) ? KC_0 :
(round_counter[3:0] == 1) ? KC_1 :
(round_counter[3:0] == 2) ? KC_2 :
(round_counter[3:0] == 3) ? KC_3 :
(round_counter[3:0] == 4) ? KC_4 :
(round_counter[3:0] == 5) ? KC_5 :
(round_counter[3:0] == 6) ? KC_6 :
(round_counter[3:0] == 7) ? KC_7 :
(round_counter[3:0] == 8) ? KC_8 :
(round_counter[3:0] == 9) ? KC_9 :
(round_counter[3:0] == 10) ? KC_A :
(round_counter[3:0] == 11) ? KC_B :
(round_counter[3:0] == 12) ? KC_C :
(round_counter[3:0] == 13) ? KC_D :
(round_counter[3:0] == 14) ? KC_E : KC_F;
wire [7:0] KC =
(main_counter == 16 ) ? KC_32[31:24] :
(main_counter == 15 ) ? KC_32[23:16] :
(main_counter == 14 ) ? KC_32[15:8] : KC_32[7:0];
reg [7:0] reg_ABCD0;
reg [7:0] reg_ABCD1;
reg [7:0] reg_ABCD2;
reg [7:0] reg_ABCD3;
reg [7:0] reg_ABCD4;
reg [7:0] reg_ABCD5;
reg [7:0] reg_ABCD6;
reg [7:0] reg_ABCD7;
reg [7:0] reg_ABCD8;
reg [7:0] reg_ABCD9;
reg [7:0] reg_ABCD10;
reg [7:0] reg_ABCD11;
reg [7:0] reg_ABCD12;
reg [7:0] reg_ABCD13;
reg [7:0] reg_ABCD14;
reg [7:0] reg_ABCD15;
reg [7:0] reg_ABCD16;
reg [7:0] reg_ABCD17;
reg [7:0] reg_ABCD18;
reg [7:0] reg_ABCD19;
reg [7:0] reg_ABCD20;
reg [7:0] reg_ABCD21;
reg [7:0] reg_ABCD22;
reg [7:0] reg_ABCD23;
////////////////////////////////////////////////////////////////////////////
/////////////////////////////CONTROL///////////////////////////////////////
wire round_mod2 = (round_counter[0]) ? 1'b1 : 1'b0; //'1' at odd rounds
wire [7:0] ABCD_AB_rot;
wire [7:0] ABCD_CD_rot;
wire [7:0] ABCD_IN = (round_mod2) ? ABCD_AB_rot : ABCD_CD_rot;
wire [7:0] ABCD = (round_counter == 0) ? Key : ABCD_IN;
wire enable_cb = (main_counter>13)? 1'b1 : 1'b0;
wire enable_output = (main_counter<4) ? 1'b1 : 1'b0;
wire load_store = (main_counter >12) ? 1'b1 : 1'b0;
wire g_enable_cb = ( main_counter == 13
|| main_counter == 14
|| main_counter == 15
|| main_counter == 16
|| main_counter == 0
|| main_counter == 1
|| main_counter == 2
|| main_counter == 3
|| main_counter == 4
|| main_counter == 5
|| main_counter == 6
|| main_counter == 7
) ? 1'b1 : 1'b0;
////////////////////////////////////////////////////////////////////////////
reg dff_carry_AC;
reg dff_borrow_ACK;
reg dff_borrow_BD;
reg dff_carry_BDK;
////////////////////////////////////////////////////////////////////////////
wire AC_carry_out;
wire AC_carry_in = (enable_cb) ? dff_carry_AC : 1'b0;
wire [7:0] AC_sum;
adder AC_adder(
.A (reg_ABCD0),
.B (reg_ABCD8),
.c_in (AC_carry_in),
.Sum (AC_sum),
.c_out (AC_carry_out)
);
wire AC_K_borrow_out;
wire AC_K_borrow_in = (enable_cb) ? dff_borrow_ACK : 1'b0;
wire [7:0] AC_K_out;
subtractor AC_K_subtractor(
.A (AC_sum),
.B (KC),
.d_in (AC_K_borrow_in),
.Diff (AC_K_out),
.d_out (AC_K_borrow_out)
);
reg [7:0] regF0;
reg [7:0] regF1;
reg [7:0] regF2;
reg [7:0] regF3;
always @ ( posedge clk or negedge reset_n ) begin
if(!reset_n) begin
regF0 <= 0;
regF1 <= 0;
regF2 <= 0;
regF3 <= 0;
end else begin
regF0 <= AC_K_out;
regF1 <= regF0;
regF2 <= regF1;
regF3 <= regF2;
end
end
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
wire BD_borrow_out;
wire BD_borrow_in = (enable_cb) ? dff_borrow_BD : 1'b0;
wire [7:0] BD_diff;
subtractor BD_subtractor(
.A (reg_ABCD4),
.B (reg_ABCD12),
.d_in (BD_borrow_in),
.Diff (BD_diff),
.d_out (BD_borrow_out)
);
wire BD_K_carry_out;
wire BD_K_carry_in = (enable_cb) ? dff_carry_BDK : 1'b0;
wire [7:0] BD_K_out;
adder BD_K_adder(
.A (BD_diff),
.B (KC),
.c_in (BD_K_carry_in),
.Sum (BD_K_out),
.c_out (BD_K_carry_out)
);
////////////////////////////////////////////////////////////////////////////
always @ ( posedge clk or negedge reset_n ) begin
if(!reset_n)begin
dff_carry_AC <= 0;
dff_borrow_ACK <= 0;
dff_borrow_BD <= 0;
dff_carry_BDK <= 0;
end else begin
dff_carry_AC <= AC_carry_out;
dff_borrow_ACK <= AC_K_borrow_out;
dff_borrow_BD <= BD_borrow_out;
dff_carry_BDK <= BD_K_carry_out;
end
end
wire [7:0] F_IN = (main_counter>3) ? BD_K_out : regF3;
G_function fnG0(
.clk(clk),
.reset_n(reset_n),
.inp(F_IN),
.outp(SK),
.enable(g_enable_cb)
);
always @ ( posedge clk or negedge reset_n ) begin
if(!reset_n) begin
reg_ABCD0 <= 0;
reg_ABCD1 <= 0;
reg_ABCD2 <= 0;
reg_ABCD3 <= 0;
reg_ABCD4 <= 0;
reg_ABCD5 <= 0;
reg_ABCD6 <= 0;
reg_ABCD7 <= 0;
reg_ABCD8 <= 0;
reg_ABCD9 <= 0;
reg_ABCD10 <= 0;
reg_ABCD11 <= 0;
reg_ABCD12 <= 0;
reg_ABCD13 <= 0;
reg_ABCD14 <= 0;
reg_ABCD15 <= 0;
reg_ABCD16 <= 0;
reg_ABCD17 <= 0;
reg_ABCD18 <= 0;
reg_ABCD19 <= 0;
reg_ABCD20 <= 0;
reg_ABCD21 <= 0;
reg_ABCD22 <= 0;
reg_ABCD23 <= 0;
end else begin
reg_ABCD0 <= ABCD;
reg_ABCD1 <= reg_ABCD0;
reg_ABCD2 <= reg_ABCD1;
reg_ABCD3 <= reg_ABCD2;
reg_ABCD4 <= reg_ABCD3;
reg_ABCD5 <= reg_ABCD4;
reg_ABCD6 <= reg_ABCD5;
reg_ABCD7 <= reg_ABCD6;
reg_ABCD8 <= reg_ABCD7;
reg_ABCD9 <= reg_ABCD8;
reg_ABCD10 <= reg_ABCD9;
reg_ABCD11 <= reg_ABCD10;
reg_ABCD12 <= reg_ABCD11;
reg_ABCD13 <= reg_ABCD12;
reg_ABCD14 <= reg_ABCD13;
reg_ABCD15 <= reg_ABCD14;
reg_ABCD16 <= reg_ABCD15;
reg_ABCD17 <= reg_ABCD16;
reg_ABCD18 <= reg_ABCD17;
reg_ABCD19 <= reg_ABCD18;
reg_ABCD20 <= reg_ABCD19;
reg_ABCD21 <= reg_ABCD20;
reg_ABCD22 <= reg_ABCD21;
reg_ABCD23 <= reg_ABCD22;
end
end
assign ABCD_AB_rot =
(main_counter < 8 ) ? reg_ABCD16 :
((main_counter > 7) & (main_counter < 15)) ? reg_ABCD15 :
(main_counter > 14 ) ? reg_ABCD23 : 0;
assign ABCD_CD_rot =
(main_counter < 1 ) ? reg_ABCD9 :
((main_counter > 0) & (main_counter < 8)) ? reg_ABCD17 :
(main_counter > 7 ) ? reg_ABCD16 : 0;
endmodule