-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsiphash_core.v
499 lines (428 loc) · 14.7 KB
/
siphash_core.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//======================================================================
//
// siphash_core.v
// --------------
// Verilog 2001 implementation of SipHash.
// This is the internal core with wide interface.
// The core implements the round function as a one cycle, full parallel
// operation. This means chained 64 bit adders.
//
// Note that the module name is siphash_core to allow usage of the
// same TB and wrapper as the default and tiny versions of the core.
//
//
// Copyright (c) 2012, Secworks Sweden AB
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================
`default_nettype none
module siphash_core(
// Clock and reset.
input wire clk,
input wire reset_n,
input wire initalize,
input wire compress,
input wire finalize,
input wire long,
input wire [3 : 0] compression_rounds,
input wire [3 : 0] final_rounds,
input wire [127 : 0] key,
input wire [63 : 0] mi,
output wire ready,
output wire [127 : 0] siphash_word,
output wire siphash_word_valid
);
//----------------------------------------------------------------
// Internal constant and parameter definitions.
//----------------------------------------------------------------
localparam DP_INIT = 3'h0;
localparam DP_COMP_START = 3'h1;
localparam DP_COMP_END = 3'h2;
localparam DP_FINAL0_START = 3'h3;
localparam DP_FINAL1_START = 3'h4;
localparam DP_SIPROUND = 3'h5;
localparam CTRL_IDLE = 3'h0;
localparam CTRL_COMP_LOOP = 3'h1;
localparam CTRL_COMP_END = 3'h2;
localparam CTRL_FINAL0_LOOP = 3'h3;
localparam CTRL_FINAL0_END = 3'h4;
localparam CTRL_FINAL1_START = 3'h5;
localparam CTRL_FINAL1_LOOP = 3'h6;
localparam CTRL_FINAL1_END = 3'h7;
//----------------------------------------------------------------
// Registers including update variables and write enable.
//----------------------------------------------------------------
reg [63 : 0] v0_reg;
reg [63 : 0] v0_new;
reg v0_we;
reg [63 : 0] v1_reg;
reg [63 : 0] v1_new;
reg v1_we;
reg [63 : 0] v2_reg;
reg [63 : 0] v2_new;
reg v2_we;
reg [63 : 0] v3_reg;
reg [63 : 0] v3_new;
reg v3_we;
reg [63 : 0] mi_reg;
reg mi_we;
reg [3 : 0] loop_ctr_reg;
reg [3 : 0] loop_ctr_new;
reg loop_ctr_we;
reg loop_ctr_inc;
reg loop_ctr_rst;
reg ready_reg;
reg ready_new;
reg ready_we;
reg [63 : 0] siphash_word0_reg;
reg [63 : 0] siphash_word1_reg;
reg [63 : 0] siphash_word_new;
reg siphash_word0_we;
reg siphash_word1_we;
reg siphash_valid_reg;
reg siphash_valid_new;
reg siphash_valid_we;
reg [2 : 0] siphash_ctrl_reg;
reg [2 : 0] siphash_ctrl_new;
reg siphash_ctrl_we;
//----------------------------------------------------------------
// Wires.
//----------------------------------------------------------------
reg dp_update;
reg [2 : 0] dp_mode;
//----------------------------------------------------------------
// Concurrent connectivity for ports etc.
//----------------------------------------------------------------
assign ready = ready_reg;
assign siphash_word = {siphash_word1_reg, siphash_word0_reg};
assign siphash_word_valid = siphash_valid_reg;
//----------------------------------------------------------------
// reg_update
// Update functionality for all registers in the core.
// All registers are positive edge triggered with
// synchronous active low reset.
//----------------------------------------------------------------
always @ (posedge clk)
begin
if (!reset_n)
begin
// Reset all registers to defined values.
v0_reg <= 64'h0;
v1_reg <= 64'h0;
v2_reg <= 64'h0;
v3_reg <= 64'h0;
siphash_word0_reg <= 64'h0;
siphash_word1_reg <= 64'h0;
mi_reg <= 64'h0;
ready_reg <= 1;
siphash_valid_reg <= 0;
loop_ctr_reg <= 4'h0;
siphash_ctrl_reg <= CTRL_IDLE;
end
else
begin
if (siphash_word0_we)
siphash_word0_reg <= siphash_word_new;
if (siphash_word1_we)
siphash_word1_reg <= siphash_word_new;
if (v0_we)
v0_reg <= v0_new;
if (v1_we)
v1_reg <= v1_new;
if (v2_we)
v2_reg <= v2_new;
if (v3_we)
v3_reg <= v3_new;
if (mi_we)
mi_reg <= mi;
if (ready_we)
ready_reg <= ready_new;
if (siphash_valid_we)
siphash_valid_reg <= siphash_valid_new;
if (loop_ctr_we)
loop_ctr_reg <= loop_ctr_new;
if (siphash_ctrl_we)
siphash_ctrl_reg <= siphash_ctrl_new;
end
end // reg_update
//----------------------------------------------------------------
// datapath_update
// update_logic for the internal datapath with the internal state
// stored in the v0, v1, v2 and v3 registers.
//
// The datapath contains two parallel 64-bit adders with
// operand MUXes to support reuse during processing.
//----------------------------------------------------------------
always @*
begin : datapath_update
reg [63 : 0] add_0_res;
reg [63 : 0] add_1_res;
reg [63 : 0] add_2_res;
reg [63 : 0] add_3_res;
reg [63 : 0] v0_tmp;
reg [63 : 0] v1_tmp;
reg [63 : 0] v2_tmp;
reg [63 : 0] v3_tmp;
add_0_res = 64'h0;
add_1_res = 64'h0;
add_2_res = 64'h0;
add_3_res = 64'h0;
v0_tmp = 64'h0;
v1_tmp = 64'h0;
v2_tmp = 64'h0;
v3_tmp = 64'h0;
v0_new = 64'h0;
v0_we = 0;
v1_new = 64'h0;
v1_we = 0;
v2_new = 64'h0;
v2_we = 0;
v3_new = 64'h0;
v3_we = 0;
siphash_word_new = v0_reg ^ v1_reg ^ v2_reg ^ v3_reg;
if (dp_update)
begin
case (dp_mode)
DP_INIT:
begin
v0_new = key[063 : 000] ^ 64'h736f6d6570736575;
v2_new = key[063 : 000] ^ 64'h6c7967656e657261;
v3_new = key[127 : 064] ^ 64'h7465646279746573;
v0_we = 1;
v1_we = 1;
v2_we = 1;
v3_we = 1;
if (long)
v1_new = key[127 : 064] ^ 64'h646f72616e646f6d ^ 64'hee;
else
v1_new = key[127 : 064] ^ 64'h646f72616e646f6d;
end
DP_COMP_START:
begin
v3_new = v3_reg ^ mi;
v3_we = 1;
end
DP_COMP_END:
begin
v0_new = v0_reg ^ mi_reg;
v0_we = 1;
end
DP_FINAL0_START:
begin
v2_we = 1;
if (long)
v2_new = v2_reg ^ 64'hee;
else
v2_new = v2_reg ^ 64'hff;
end
DP_FINAL1_START:
begin
v1_new = v1_reg ^ 64'hdd;
v1_we = 1;
end
DP_SIPROUND:
begin
add_0_res = v0_reg + v1_reg;
add_1_res = v2_reg + v3_reg;
v0_tmp = {add_0_res[31:0], add_0_res[63:32]};
v1_tmp = {v1_reg[50:0], v1_reg[63:51]} ^ add_0_res;
v2_tmp = add_1_res;
v3_tmp = {v3_reg[47:0], v3_reg[63:48]} ^ add_1_res;
add_2_res = v1_tmp + v2_tmp;
add_3_res = v0_tmp + v3_tmp;
v0_new = add_3_res;
v0_we = 1;
v1_new = {v1_tmp[46:0], v1_tmp[63:47]} ^ add_2_res;
v1_we = 1;
v2_new = {add_2_res[31:0], add_2_res[63:32]};
v2_we = 1;
v3_new = {v3_tmp[42:0], v3_tmp[63:43]} ^ add_3_res;
v3_we = 1;
end
default:
begin
end
endcase // case (dp_state_reg)
end // if (dp_update)
end // block: datapath_update
//----------------------------------------------------------------
// loop_ctr
// Update logic for the loop counter, a monotonically
// increasing counter with reset.
//----------------------------------------------------------------
always @*
begin : loop_ctr
loop_ctr_new = 0;
loop_ctr_we = 0;
if (loop_ctr_rst)
loop_ctr_we = 1;
if (loop_ctr_inc)
begin
loop_ctr_new = loop_ctr_reg + 4'h1;
loop_ctr_we = 1;
end
end // loop_ctr
//----------------------------------------------------------------
// siphash_ctrl_fsm
// Logic for the state machine controlling the core behaviour.
//----------------------------------------------------------------
always @*
begin : siphash_ctrl_fsm
loop_ctr_rst = 0;
loop_ctr_inc = 0;
dp_update = 0;
dp_mode = DP_INIT;
mi_we = 0;
ready_new = 0;
ready_we = 0;
siphash_word0_we = 0;
siphash_word1_we = 0;
siphash_valid_new = 0;
siphash_valid_we = 0;
siphash_ctrl_new = CTRL_IDLE;
siphash_ctrl_we = 0;
case (siphash_ctrl_reg)
CTRL_IDLE:
begin
if (initalize)
begin
dp_update = 1;
dp_mode = DP_INIT;
siphash_valid_new = 0;
siphash_valid_we = 1;
end
else if (compress)
begin
mi_we = 1;
loop_ctr_rst = 1;
ready_new = 0;
ready_we = 1;
dp_update = 1;
dp_mode = DP_COMP_START;
siphash_ctrl_new = CTRL_COMP_LOOP;
siphash_ctrl_we = 1;
end
else if (finalize)
begin
loop_ctr_rst = 1;
ready_new = 0;
ready_we = 1;
dp_update = 1;
dp_mode = DP_FINAL0_START;
siphash_ctrl_new = CTRL_FINAL0_LOOP;
siphash_ctrl_we = 1;
end
end
CTRL_COMP_LOOP:
begin
loop_ctr_inc = 1;
dp_update = 1;
dp_mode = DP_SIPROUND;
if (loop_ctr_reg == (compression_rounds - 1))
begin
siphash_ctrl_new = CTRL_COMP_END;
siphash_ctrl_we = 1;
end
end
CTRL_COMP_END:
begin
ready_new = 1;
ready_we = 1;
dp_update = 1;
dp_mode = DP_COMP_END;
siphash_ctrl_new = CTRL_IDLE;
siphash_ctrl_we = 1;
end
CTRL_FINAL0_LOOP:
begin
loop_ctr_inc = 1;
dp_update = 1;
dp_mode = DP_SIPROUND;
if (loop_ctr_reg == (final_rounds - 1))
begin
if (long)
begin
siphash_ctrl_new = CTRL_FINAL1_START;
siphash_ctrl_we = 1;
end
else
begin
siphash_ctrl_new = CTRL_FINAL0_END;
siphash_ctrl_we = 1;
end
end
end
CTRL_FINAL0_END:
begin
ready_new = 1;
ready_we = 1;
siphash_word0_we = 1;
siphash_valid_new = 1;
siphash_valid_we = 1;
siphash_ctrl_new = CTRL_IDLE;
siphash_ctrl_we = 1;
end
CTRL_FINAL1_START:
begin
siphash_word0_we = 1;
loop_ctr_rst = 1;
dp_update = 1;
dp_mode = DP_FINAL1_START;
siphash_ctrl_new = CTRL_FINAL1_LOOP;
siphash_ctrl_we = 1;
end
CTRL_FINAL1_LOOP:
begin
loop_ctr_inc = 1;
dp_update = 1;
dp_mode = DP_SIPROUND;
if (loop_ctr_reg == (final_rounds - 1))
begin
siphash_ctrl_new = CTRL_FINAL1_END;
siphash_ctrl_we = 1;
end
end
CTRL_FINAL1_END:
begin
ready_new = 1;
ready_we = 1;
siphash_word1_we = 1;
siphash_valid_new = 1;
siphash_valid_we = 1;
siphash_ctrl_new = CTRL_IDLE;
siphash_ctrl_we = 1;
end
default:
begin
end
endcase // case (siphash_ctrl_reg)
end // siphash_ctrl_fsm
endmodule // siphash_core
//======================================================================
// EOF siphash_core.v
//======================================================================