-
Notifications
You must be signed in to change notification settings - Fork 7
/
md5_fsm.v
692 lines (681 loc) · 11.4 KB
/
md5_fsm.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*-
* Copyright (c) 2009 Stanislav Sedov <[email protected]>.
* 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 AUTHOR ``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 AUTHOR 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.
*
* MD5 main FSM.
*/
`include "defines.h"
module md5_fsm(clk_i, rdy_i, msg_i, rst_i, hash_o);
input clk_i;
input rdy_i;
input [0:31] msg_i;
input rst_i;
output [0:127] hash_o;
wire clk_i;
wire rdy_i, rst_i;
wire [0:31] msg_i;
wire [0:127] hash_o;
reg [0:31] core_a, core_b, core_c, core_d;
reg [0:1] core_round;
wire [0:31] core_m, core_t;
wire [0:4] core_s;
wire [0:31] core_out;
reg [0:5] round;
wire [0:4] s_o;
wire [0:31] t_o;
reg [0:31] h0, h1, h2, h3;
reg [0:31] a;
reg [0:31] b;
reg [0:31] c;
reg [0:31] d;
assign core_m = {msg_i[24:31], msg_i[16:23], msg_i[8:15], msg_i[0:7]};
assign core_s = s_o;
assign core_t = t_o;
assign hash_o = {h0[24:31], h0[16:23], h0[8:15], h0[0:7],
h1[24:31], h1[16:23], h1[8:15], h1[0:7],
h2[24:31], h2[16:23], h2[8:15], h2[0:7],
h3[24:31], h3[16:23], h3[8:15], h3[0:7]}; /* Damn LE stuff. */
always @ (posedge clk_i)
begin
if (rst_i == 1) begin
round <= #`REGDELAY 0;
h0 <= #`REGDELAY 32'h67452301;
h1 <= #`REGDELAY 32'hefcdab89;
h2 <= #`REGDELAY 32'h98badcfe;
h3 <= #`REGDELAY 32'h10325476;
a <= #`REGDELAY 32'h67452301;
b <= #`REGDELAY 32'hefcdab89;
c <= #`REGDELAY 32'h98badcfe;
d <= #`REGDELAY 32'h10325476;
end else begin
if (rdy_i == 1) begin
round <= #`REGDELAY round + 1;
case (round)
0: begin
a <= #`REGDELAY core_out;
end
1: begin
d <= #`REGDELAY core_out;
end
2: begin
c <= #`REGDELAY core_out;
end
3: begin
b <= #`REGDELAY core_out;
end
4: begin
a <= #`REGDELAY core_out;
end
5: begin
d <= #`REGDELAY core_out;
end
6: begin
c <= #`REGDELAY core_out;
end
7: begin
b <= #`REGDELAY core_out;
end
8: begin
a <= #`REGDELAY core_out;
end
9: begin
d <= #`REGDELAY core_out;
end
10: begin
c <= #`REGDELAY core_out;
end
11: begin
b <= #`REGDELAY core_out;
end
12: begin
a <= #`REGDELAY core_out;
end
13: begin
d <= #`REGDELAY core_out;
end
14: begin
c <= #`REGDELAY core_out;
end
15: begin
b <= #`REGDELAY core_out;
end
16: begin
a <= #`REGDELAY core_out;
end
17: begin
d <= #`REGDELAY core_out;
end
18: begin
c <= #`REGDELAY core_out;
end
19: begin
b <= #`REGDELAY core_out;
end
20: begin
a <= #`REGDELAY core_out;
end
21: begin
d <= #`REGDELAY core_out;
end
22: begin
c <= #`REGDELAY core_out;
end
23: begin
b <= #`REGDELAY core_out;
end
24: begin
a <= #`REGDELAY core_out;
end
25: begin
d <= #`REGDELAY core_out;
end
26: begin
c <= #`REGDELAY core_out;
end
27: begin
b <= #`REGDELAY core_out;
end
28: begin
a <= #`REGDELAY core_out;
end
29: begin
d <= #`REGDELAY core_out;
end
30: begin
c <= #`REGDELAY core_out;
end
31: begin
b <= #`REGDELAY core_out;
end
32: begin
a <= #`REGDELAY core_out;
end
33: begin
d <= #`REGDELAY core_out;
end
34: begin
c <= #`REGDELAY core_out;
end
35: begin
b <= #`REGDELAY core_out;
end
36: begin
a <= #`REGDELAY core_out;
end
37: begin
d <= #`REGDELAY core_out;
end
38: begin
c <= #`REGDELAY core_out;
end
39: begin
b <= #`REGDELAY core_out;
end
40: begin
a <= #`REGDELAY core_out;
end
41: begin
d <= #`REGDELAY core_out;
end
42: begin
c <= #`REGDELAY core_out;
end
43: begin
b <= #`REGDELAY core_out;
end
44: begin
a <= #`REGDELAY core_out;
end
45: begin
d <= #`REGDELAY core_out;
end
46: begin
c <= #`REGDELAY core_out;
end
47: begin
b <= #`REGDELAY core_out;
end
48: begin
a <= #`REGDELAY core_out;
end
49: begin
d <= #`REGDELAY core_out;
end
50: begin
c <= #`REGDELAY core_out;
end
51: begin
b <= #`REGDELAY core_out;
end
52: begin
a <= #`REGDELAY core_out;
end
53: begin
d <= #`REGDELAY core_out;
end
54: begin
c <= #`REGDELAY core_out;
end
55: begin
b <= #`REGDELAY core_out;
end
56: begin
a <= #`REGDELAY core_out;
end
57: begin
d <= #`REGDELAY core_out;
end
58: begin
c <= #`REGDELAY core_out;
end
59: begin
b <= #`REGDELAY core_out;
end
60: begin
a <= #`REGDELAY core_out;
end
61: begin
d <= #`REGDELAY core_out;
end
62: begin
c <= #`REGDELAY core_out;
end
63: begin
h0 <= #`REGDELAY h0 + a;
h1 <= #`REGDELAY h1 + core_out;
h2 <= #`REGDELAY h2 + c;
h3 <= #`REGDELAY h3 + d;
a <= #`REGDELAY h0 + a;
b <= #`REGDELAY h1 + core_out;
c <= #`REGDELAY h2 + c;
d <= #`REGDELAY h3 + d;
end
endcase
end
end
end
always @*
begin
case (round)
0: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
1: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
2: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
3: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
4: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
5: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
6: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
7: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
8: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
9: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
10: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
11: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
12: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
13: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
14: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
15: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
16: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
17: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
18: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
19: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
20: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
21: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
22: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
23: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
24: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
25: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
26: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
27: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
28: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
29: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
30: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
31: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
32: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
33: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
34: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
35: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
36: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
37: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
38: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
39: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
40: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
41: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
42: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
43: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
44: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
45: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
46: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
47: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
48: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
49: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
50: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
51: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
52: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
53: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
54: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
55: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
56: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
57: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
58: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
59: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
60: begin
core_a = a;
core_b = b;
core_c = c;
core_d = d;
end
61: begin
core_a = d;
core_b = a;
core_c = b;
core_d = c;
end
62: begin
core_a = c;
core_b = d;
core_c = a;
core_d = b;
end
63: begin
core_a = b;
core_b = c;
core_c = d;
core_d = a;
end
endcase
end
always @* begin
if (round < 16) begin
core_round = 0;
end else if (round < 32) begin
core_round = 1;
end else if (round < 48) begin
core_round = 2;
end else begin
core_round = 3;
end
end
md5_core md5_core0(core_a, core_b, core_c, core_d, core_round, core_m,
core_s, core_t, core_out);
slut slut0(round, s_o);
tlut tlut0(round, t_o);
endmodule