OpenCores
URL https://opencores.org/ocsvn/sc2v/sc2v/trunk

Subversion Repositories sc2v

[/] [sc2v/] [trunk/] [examples/] [md5.cpp] - Blame information for rev 36

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jcastillo
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  MD5 main implementation file                                ////
4
////                                                              ////
5
////  This file is part of the SystemC MD5                        ////
6
////                                                              ////
7
////  Description:                                                ////
8
////  MD5 main implementation file                                ////
9
////                                                              ////
10
////  To Do:                                                      ////
11
////   - done                                                     ////
12
////                                                              ////
13
////  Author(s):                                                  ////
14 4 jcastillo
////      - Javier Castillo, jcastillo@opencores.org              ////
15 2 jcastillo
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2000 Authors and OPENCORES.ORG                 ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
//
43
// CVS Revision History
44
//
45
// $Log: not supported by cvs2svn $
46 4 jcastillo
// Revision 1.1.1.1  2004/10/08 14:04:10  jcastillo
47
// First import
48
//
49 2 jcastillo
// Revision 1.1.1.1  2004/09/08 16:24:49  jcastillo
50
// Initial release
51
//
52
 
53
#include "md5.h"
54
 
55
 
56
void
57
md5::md5_rom ()
58
{
59
  switch (round64.read ())
60
    {
61
    case 0:
62
      t.write (0xD76AA478070);
63
      break;
64
    case 1:
65
      t.write (0xE8C7B7560C1);
66
      break;
67
    case 2:
68
      t.write (0x242070DB112);
69
      break;
70
    case 3:
71
      t.write (0xC1BDCEEE163);
72
      break;
73
    case 4:
74
      t.write (0xF57C0FAF074);
75
      break;
76
    case 5:
77
      t.write (0x4787C62A0C5);
78
      break;
79
    case 6:
80
      t.write (0xA8304613116);
81
      break;
82
    case 7:
83
      t.write (0xFD469501167);
84
      break;
85
    case 8:
86
      t.write (0x698098D8078);
87
      break;
88
    case 9:
89
      t.write (0x8B44F7AF0C9);
90
      break;
91
    case 10:
92
      t.write (0xFFFF5BB111A);
93
      break;
94
    case 11:
95
      t.write (0x895CD7BE16B);
96
      break;
97
    case 12:
98
      t.write (0x6B90112207C);
99
      break;
100
    case 13:
101
      t.write (0xFD9871930CD);
102
      break;
103
    case 14:
104
      t.write (0xA679438E11E);
105
      break;
106
    case 15:
107
      t.write (0x49B4082116F);
108
      break;
109
 
110
    case 16:
111
      t.write (0xf61e2562051);
112
      break;
113
    case 17:
114
      t.write (0xc040b340096);
115
      break;
116
    case 18:
117
      t.write (0x265e5a510EB);
118
      break;
119
    case 19:
120
      t.write (0xe9b6c7aa140);
121
      break;
122
    case 20:
123
      t.write (0xd62f105d055);
124
      break;
125
    case 21:
126
      t.write (0x0244145309A);
127
      break;
128
    case 22:
129
      t.write (0xd8a1e6810EF);
130
      break;
131
    case 23:
132
      t.write (0xe7d3fbc8144);
133
      break;
134
    case 24:
135
      t.write (0x21e1cde6059);
136
      break;
137
    case 25:
138
      t.write (0xc33707d609E);
139
      break;
140
    case 26:
141
      t.write (0xf4d50d870E3);
142
      break;
143
    case 27:
144
      t.write (0x455a14ed148);
145
      break;
146
    case 28:
147
      t.write (0xa9e3e90505D);
148
      break;
149
    case 29:
150
      t.write (0xfcefa3f8092);
151
      break;
152
    case 30:
153
      t.write (0x676f02d90E7);
154
      break;
155
    case 31:
156
      t.write (0x8d2a4c8a14C);
157
      break;
158
 
159
    case 32:
160
      t.write (0xfffa3942045);
161
      break;
162
    case 33:
163
      t.write (0x8771f6810B8);
164
      break;
165
    case 34:
166
      t.write (0x6d9d612210B);
167
      break;
168
    case 35:
169
      t.write (0xfde5380c17E);
170
      break;
171
    case 36:
172
      t.write (0xa4beea44041);
173
      break;
174
    case 37:
175
      t.write (0x4bdecfa90B4);
176
      break;
177
    case 38:
178
      t.write (0xf6bb4b60107);
179
      break;
180
    case 39:
181
      t.write (0xbebfbc7017A);
182
      break;
183
    case 40:
184
      t.write (0x289b7ec604D);
185
      break;
186
    case 41:
187
      t.write (0xeaa127fa0B0);
188
      break;
189
    case 42:
190
      t.write (0xd4ef3085103);
191
      break;
192
    case 43:
193
      t.write (0x04881d05176);
194
      break;
195
    case 44:
196
      t.write (0xd9d4d039049);
197
      break;
198
    case 45:
199
      t.write (0xe6db99e50BC);
200
      break;
201
    case 46:
202
      t.write (0x1fa27cf810F);
203
      break;
204
    case 47:
205
      t.write (0xc4ac5665172);
206
      break;
207
 
208
    case 48:
209
      t.write (0xf4292244060);
210
      break;
211
    case 49:
212
      t.write (0x432aff970A7);
213
      break;
214
    case 50:
215
      t.write (0xab9423a70FE);
216
      break;
217
    case 51:
218
      t.write (0xfc93a039155);
219
      break;
220
    case 52:
221
      t.write (0x655b59c306C);
222
      break;
223
    case 53:
224
      t.write (0x8f0ccc920A3);
225
      break;
226
    case 54:
227
      t.write (0xffeff47d0FA);
228
      break;
229
    case 55:
230
      t.write (0x85845dd1151);
231
      break;
232
    case 56:
233
      t.write (0x6fa87e4f068);
234
      break;
235
    case 57:
236
      t.write (0xfe2ce6e00AF);
237
      break;
238
    case 58:
239
      t.write (0xa30143140F6);
240
      break;
241
    case 59:
242
      t.write (0x4e0811a115D);
243
      break;
244
    case 60:
245
      t.write (0xf7537e82064);
246
      break;
247
    case 61:
248
      t.write (0xbd3af2350AB);
249
      break;
250
    case 62:
251
      t.write (0x2ad7d2bb0F2);
252
      break;
253
    case 63:
254
      t.write (0xeb86d391159);
255
      break;
256
 
257
    }
258
}
259
 
260
 
261
void
262
md5::funcs ()
263
{
264
  sc_uint < 32 > aux, fr_var, tr_var, rotate1, rotate2;
265
  sc_uint < 8 > s_var;
266
  sc_uint < 4 > nblock;
267
  sc_uint < 32 > message_var[16];
268
 
269
  message_var[0]=message.read().range(511,480);
270
  message_var[1]=message.read().range(479,448);
271
  message_var[2]=message.read().range(447,416);
272
  message_var[3]=message.read().range(415,384);
273
  message_var[4]=message.read().range(383,352);
274
  message_var[5]=message.read().range(351,320);
275
  message_var[6]=message.read().range(319,288);
276
  message_var[7]=message.read().range(287,256);
277
  message_var[8]=message.read().range(255,224);
278
  message_var[9]=message.read().range(223,192);
279
  message_var[10]=message.read().range(191,160);
280
  message_var[11]=message.read().range(159,128);
281
  message_var[12]=message.read().range(127,96);
282
  message_var[13]=message.read().range(95,64);
283
  message_var[14]=message.read().range(63,32);
284
  message_var[15]=message.read().range(31,0);
285
 
286
  fr_var = 0;
287
 
288
  switch (round.read ())
289
    {
290
    case 0:
291
      fr_var = ((br.read () & cr.read ()) | (~br.read () & dr.read ()));
292
      break;
293
    case 1:
294
      fr_var = ((br.read () & dr.read ()) | (cr.read () & (~dr.read ())));
295
      break;
296
    case 2:
297
      fr_var = (br.read () ^ cr.read () ^ dr.read ());
298
      break;
299
    case 3:
300
      fr_var = (cr.read () ^ (br.read () | ~dr.read ()));
301
      break;
302
    default:
303
      break;
304
    }
305
 
306
  tr_var = t.read ().range (43, 12);
307
  s_var = t.read ().range (11, 4);
308
  nblock = t.read ().range (3, 0);
309
 
310
  aux = (ar.read () + fr_var + message_var[(int) nblock] + tr_var);
311
 
312
  //cout << (int)round64.read() << " " << (int)fr_var << " " << (int)aux << " " << (int)nblock << " " << (int)message_var[(int)nblock] << endl;
313
 
314
  rotate1 = aux << (int) s_var;
315
  rotate2 = aux >> (int) (32 - s_var);
316
  func_out.write (br.read () + (rotate1 | rotate2));
317
 
318
}
319
 
320
void
321
md5::round64FSM ()
322
{
323
 
324
  next_ar.write (ar.read ());
325
  next_br.write (br.read ());
326
  next_cr.write (cr.read ());
327
  next_dr.write (dr.read ());
328
  next_round64.write (round64.read ());
329
  next_round.write (round.read ());
330
  hash_generated.write (0);
331
 
332
  if (generate_hash.read () != 0)
333
    {
334
      next_ar.write (dr.read ());
335
      next_br.write (func_out.read ());
336
      next_cr.write (br.read ());
337
      next_dr.write (cr.read ());
338
    }
339
 
340
  switch (round64.read ())
341
    {
342
 
343
    case 0:
344
      next_round.write (0);
345
      if (generate_hash.read ())
346
        {
347
          next_round64.write (1);
348
        }
349
      break;
350
    case 15:
351
    case 31:
352
    case 47:
353
      next_round.write (round.read () + 1);
354
      next_round64.write (round64.read () + 1);
355
      break;
356
    case 63:
357
      next_round.write (0);
358
      next_round64.write (0);
359
      hash_generated.write (1);
360
      break;
361
    default:
362
      next_round64.write (round64.read () + 1);
363
      break;
364
    }
365
 
366
  if (newtext_i.read ())
367
    {
368
      next_ar.write (0x67452301);
369
      next_br.write (0xEFCDAB89);
370
      next_cr.write (0x98BADCFE);
371
      next_dr.write (0x10325476);
372
      next_round.write (0);
373
      next_round64.write (0);
374
    }
375
 
376
  if (getdata_state.read () == 0)
377
    {
378
      next_ar.write (A.read ());
379
      next_br.write (B.read ());
380
      next_cr.write (C.read ());
381
      next_dr.write (D.read ());
382
    }
383
}
384
 
385
void
386
md5::reg_signal ()
387
{
388
  if (!reset)
389
    {
390
      ready_o.write (0);
391
      data_o.write (0);
392
      message.write (0);
393
 
394
      ar.write (0x67452301);
395
      br.write (0xEFCDAB89);
396
      cr.write (0x98BADCFE);
397
      dr.write (0x10325476);
398
 
399
      getdata_state.write (0);
400
      generate_hash.write (0);
401
 
402
      round.write (0);
403
      round64.write (0);
404
 
405
      A.write (0x67452301);
406
      B.write (0xEFCDAB89);
407
      C.write (0x98BADCFE);
408
      D.write (0x10325476);
409
 
410
    }
411
  else
412
    {
413
      ready_o.write (next_ready_o.read ());
414
      data_o.write (next_data_o.read ());
415
      message.write (next_message.read ());
416
 
417
      ar.write (next_ar.read ());
418
      br.write (next_br.read ());
419
      cr.write (next_cr.read ());
420
      dr.write (next_dr.read ());
421
 
422
      A.write (next_A.read ());
423
      B.write (next_B.read ());
424
      C.write (next_C.read ());
425
      D.write (next_D.read ());
426
 
427
      generate_hash.write (next_generate_hash.read ());
428
      getdata_state.write (next_getdata_state.read ());
429
 
430
      round.write (next_round.read ());
431
      round64.write (next_round64.read ());
432
 
433
    }
434
 
435
}
436
 
437
 
438
void
439
md5::md5_getdata ()
440
{
441
 
442
  sc_biguint < 128 > data_o_var;
443
  sc_biguint < 512 > aux;
444
 
445
  sc_uint < 32 > A_t, B_t, C_t, D_t;
446
 
447
  next_A.write (A.read ());
448
  next_B.write (B.read ());
449
  next_C.write (C.read ());
450
  next_D.write (D.read ());
451
 
452
  next_generate_hash.write (0);
453
  next_ready_o.write (0);
454
  next_data_o.write (0);
455
 
456
  aux = message.read ();
457
  next_message.write (message.read ());
458
  next_getdata_state.write (getdata_state.read ());
459
 
460
  if (newtext_i.read ())
461
    {
462
      next_A.write (0x67452301);
463
      next_B.write (0xEFCDAB89);
464
      next_C.write (0x98BADCFE);
465
      next_D.write (0x10325476);
466
      next_getdata_state.write (0);
467
    }
468
 
469
  switch (getdata_state.read ())
470
    {
471
 
472
    case 0:
473
      if (load_i.read ())
474
        {
475
          aux.range (511, 384) = data_i.read ();
476
          next_message.write (aux);
477
          next_getdata_state.write (1);
478
        }
479
      break;
480
    case 1:
481
      if (load_i.read ())
482
        {
483
          aux.range (383, 256) = data_i.read ();
484
          next_message.write (aux);
485
          next_getdata_state.write (2);
486
        }
487
      break;
488
    case 2:
489
      if (load_i.read ())
490
        {
491
          aux.range (255, 128) = data_i.read ();
492
          next_message.write (aux);
493
          next_getdata_state.write (3);
494
        }
495
      break;
496
    case 3:
497
      if (load_i.read ())
498
        {
499
          aux.range (127, 0) = data_i.read ();
500
          next_message.write (aux);
501
          next_getdata_state.write (4);
502
          next_generate_hash.write (1);
503
        }
504
      break;
505
    case 4:
506
      next_generate_hash.write (1);
507
 
508
      A_t = dr.read () + A.read ();
509
      B_t = func_out.read () + B.read ();
510
      C_t = br.read () + C.read ();
511
      D_t = cr.read () + D.read ();
512
 
513
      data_o_var.range (127, 96) = A_t;
514
      data_o_var.range (95, 64) = B_t;
515
      data_o_var.range (63, 32) = C_t;
516
      data_o_var.range (31, 0) = D_t;
517
      next_data_o.write (data_o_var);
518
 
519
 
520
      if (hash_generated.read ())
521
        {
522
          next_A.write (A_t);
523
          next_B.write (B_t);
524
          next_C.write (C_t);
525
          next_D.write (D_t);
526
          next_getdata_state.write (0);
527
          next_ready_o.write (1);
528
          next_generate_hash.write (0);
529
        }
530
      break;
531
    }
532
 
533
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.