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

Subversion Repositories bluespec_md6

[/] [bluespec_md6/] [trunk/] [C_implementation/] [md6sum.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kfleming
/* File:    md6sum.c
2
** Author:  Ronald L. Rivest
3
** Address: Room 32G-692 Stata Center
4
**          32 Vassar Street
5
**          Cambridge, MA 02139
6
** Email:   rivest@mit.edu
7
** Date:    9/25/2008
8
**
9
** (The following license is known as "The MIT License")
10
**
11
** Copyright (c) 2008 Ronald L. Rivest
12
**
13
** Permission is hereby granted, free of charge, to any person obtaining a copy
14
** of this software and associated documentation files (the "Software"), to deal
15
** in the Software without restriction, including without limitation the rights
16
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
** copies of the Software, and to permit persons to whom the Software is
18
** furnished to do so, subject to the following conditions:
19
**
20
** The above copyright notice and this permission notice shall be included in
21
** all copies or substantial portions of the Software.
22
**
23
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
** THE SOFTWARE.
30
**
31
** (end of license)
32
**
33
** This is an application illustrating the use of the MD6 hash function.
34
** The files defining the md6 hash function are:
35
**    md6.h
36
**    md6_compress.c
37
**    md6_mode.c
38
**
39
** The files defining the interface between MD6 and the NIST SHA-3
40
** API are:
41
**    md6_nist.h
42
**    md6_nist.c
43
** The NIST SHA-3 API is defined in:
44
**    http://www.csrc.nist.gov/groups/ST/hash/documents/SHA3-C-API.pdf
45
**
46
** See  http://groups.csail.mit.edu/cis/md6  for more information.
47
**
48
*/
49
 
50
#include <stdio.h>
51
#include <stdlib.h>
52
#include <string.h>
53
#include <ctype.h>
54
#include <time.h>
55
#include <assert.h>
56
 
57
#include "md6.h"
58
 
59
/* Cycle count routines */
60
 
61
#if defined _MSC_VER
62
 
63
/* Microsoft */
64
#include <intrin.h>
65
#pragma intrinsic(__rdtsc)
66
uint64_t ticks()
67
{
68
  return __rdtsc();
69
}
70
 
71
#else 
72
 
73
/* GCC */
74
#include <stdint.h>
75
inline uint64_t ticks() {
76
  /* read timestamp counter */
77
  uint32_t lo, hi;
78
  asm volatile (
79
                "xorl %%eax,%%eax \n        cpuid"
80
                ::: "%rax", "%rbx", "%rcx", "%rdx");
81
  asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
82
  return (uint64_t)hi << 32 | lo;
83
}
84
 
85
#endif
86
 
87
 /* Constants for MD6 */
88
 
89
#define w  md6_w
90
#define c  md6_c
91
#define n  md6_n
92
#define b  md6_b
93
#define u  md6_u
94
#define v  md6_v
95
#define k  md6_k
96
#define q  md6_q
97
 
98
/* Useful macros: min and max */
99
#ifndef min
100
#define min(a,b) ((a)<(b)? (a) : (b))
101
#endif
102
#ifndef max
103
#define max(a,b) ((a)>(b)? (a) : (b))
104
#endif
105
 
106
/* MD6 parameters */
107
int d;                    /* digest length */
108
int L;                    /* mode parameter */
109
int r;                    /* number of rounds */
110
unsigned char K[100];     /* key */
111
int keylen;               /* key length in bytes (at most 64) */
112
md6_state st;             /* md6 computation state */
113
 
114
char msg[5000];           /* message to be hashed (if given with -M) */
115
int msglenbytes;          /* message length in bytes */
116
 
117
char help_string[] =
118
"md6sum [OPTIONS] file1 file2 ...\n"
119
"Options:\n"
120
"\t(option letters are case-sensitive and processed in order):\n"
121
"\t-bnnnn\tHash a dummy file of length nnnn bits.\n"
122
"\t-Bnnnn\tHash a dummy file of length nnnn bytes.\n"
123
"\t-c xxxx\tCheck hash from file xxxx to see if they are still valid.\n"
124
"\t\tHere xxxx is saved output from previous run of md6sum.\n"
125
"\t\tNames of files whose hash value have changed are printed.\n"
126
"\t-dnnn\tSet digest length d to nnn bits, 1<=nnn<=512; default is 256.\n"
127
"\t-h\tPrint this help information.\n"
128
"\t-i\tPrint input/output data for each compression function call.\n"
129
"\t-I\tLike -i, but also prints out all intermediate values computed.\n"
130
"\t-Kxxxxx\tSet MD6 key to xxxxx (length 0 to 64 bytes).\n"
131
"\t-Lnn\tSet MD6 mode parameter L to nn (0<=L<=64; default 64).\n"
132
"\t-Mxxxxx\tCompute hash of message xxxxx.\n"
133
"\t-rnnn\tSet MD6 number r of rounds to nn (0<=r<=255; default is 40+(d/4).\n"
134
"\t-snnn\tMeasure time to perform nnn MD6 initializations (nnn optional).\n"
135
"\t-t\tTurn on printing of elapsed times and bytes/second.\n"
136
"For each file given, md6sum prints a line of the form: \n"
137
"\thashvalue filename\n"
138
"If file is `-', or if no files are given, standard input is used.\n"
139
"Integers nnnn may use scientific notation, e.g. -B1e9 .\n"
140
;
141
 
142
/* return integer starting at s (input presumed to end with '\n')
143
** (It may be expressed in exponential format e.g. 1e9.)
144
*/
145
 
146
uint64_t get_int(char *s)
147
{ long double g;
148
  sscanf(s,"%Lg",&g);
149
  return (uint64_t)g;
150
}
151
 
152
/* routines to escape/unescape filenames, in case they
153
** contain backslash or \n 's.
154
*/
155
 
156
void encode(char *s, char *t)
157
/* input t, output s -- recode t so it all newlines and
158
** backslashes are escaped as \n and \\ respectively.
159
** Also, a leading '-' is escaped to \-.
160
*/
161
{ if (*t && *t=='-')
162
    { *s++ = '\\'; *s++ = '-'; t++; }
163
  while (*t)
164
    { if (*t=='\\')      { *s++ = '\\'; *s++ = '\\'; }
165
      else if (*t=='\n') { *s++ = '\\'; *s++ = 'n';  }
166
      else               *s++ = *t;
167
      t++;
168
    }
169
  *s = 0;
170
  return;
171
}
172
 
173
void decode(char *s, char *t)
174
/* inverse of encode -- s is unescaped version of t. */
175
{ while (*t)
176
    { if (*t == '\\')
177
        { if (*(t+1)=='\\')     { *s++ = '\\'; t+=1; }
178
          else if (*(t+1)=='n') { *s++ = '\n'; t+=1; }
179
          else if (*(t+1)=='-') { *s++ = '-'; t+=1; }
180
          else if (*(t+1)==0)   { *s++ = '\\'; }
181
          else                  { *s++ = *t; }
182
        }
183
      else *s++ = *t;
184
      t++;
185
    }
186
  *s = 0;
187
  return;
188
}
189
 
190
/* timing variables and routines */
191
 
192
double start_time;
193
double end_time;
194
uint64_t start_ticks;
195
uint64_t end_ticks;
196
 
197
void start_timer()
198
{
199
  start_time = ((double)clock())/CLOCKS_PER_SEC;
200
  start_ticks = ticks();
201
}
202
 
203
void end_timer()
204
{
205
  end_time = ((double)clock())/CLOCKS_PER_SEC;
206
  end_ticks = ticks();
207
}
208
 
209
int print_times = 0;
210
 
211
void print_time()
212
{ double elapsed_time = end_time - start_time;
213
  uint64_t elapsed_ticks = end_ticks - start_ticks;
214
  uint64_t bytes = st.bits_processed/8;
215
  int bits = st.bits_processed % 8;
216
  if (!print_times) return;
217
  printf("-- Length = ");
218
  if (st.bits_processed==0) printf("0");
219
  if (bytes>0) printf("%g byte",(double)bytes);
220
  if (bytes>1) printf("s");
221
  if (bytes>0 && bits>0) printf(" + ");
222
  if (bits>0) printf("%d bit",bits);
223
  if (bits>1) printf("s");
224
  printf("\n");
225
  printf("-- Compression calls made = %g\n",(double)st.compression_calls);
226
  if (elapsed_time == 0.0)
227
    printf("-- Elapsed time too short to measure...\n");
228
  else
229
    { printf("-- Elapsed time = %.3f seconds.\n", elapsed_time);
230
      printf("-- Megabytes per second = %g.\n",
231
             (bytes/elapsed_time)/1000000.0);
232
      printf("-- Microseconds per compression function = %g.\n",
233
             (elapsed_time*1.0e6 / st.compression_calls ));
234
    }
235
  printf("-- Total clock ticks = %lld\n",
236
         (long long int)elapsed_ticks);
237
  if (bytes>0)
238
    printf("-- Clock ticks / byte = %lld\n",
239
           (long long int)(elapsed_ticks/bytes));
240
  printf("-- Clock ticks / compression function call = %lld\n",
241
         (long long int)(elapsed_ticks/st.compression_calls));
242
}
243
 
244
 /* testing and debugging */
245
 
246
/* Global variables used by compression_hook_1 */
247
FILE *outFile = NULL;
248
int  print_input_output = 0;
249
int  print_intermediate = 0;
250
 
251
void compression_hook_1(md6_word *C,
252
                        const md6_word *Q,
253
                        md6_word *K,
254
                        int ell,
255
                        int ii,
256
                        int r,
257
                        int L,
258
                        int z,
259
                        int p,
260
                        int keylen,
261
                        int d,
262
                        md6_word *B
263
)
264
{ int i;
265
  md6_word A[5000];
266
  time_t now;
267
 
268
  md6_pack(A,Q,K,ell,ii,r,L,z,p,keylen,d,B);
269
 
270
  md6_main_compression_loop( A, r);
271
 
272
  if (ell==1 && ii==0)
273
    { time(&now);
274
      fprintf(outFile,"-- d = %6d (digest length in bits)\n",d);
275
      fprintf(outFile,"-- L = %6d (number of parallel passes)\n",L);
276
      fprintf(outFile,"-- r = %6d (number of rounds)\n",r);
277
      /* print key out as chars, since for md6sum it is essentially
278
      ** impossible to enter non-char keys...
279
      */
280
      fprintf(outFile,"-- K = '");
281
      for (i=0;i<keylen;i++)
282
        fprintf(outFile,"%c",(int)(K[i/(w/8)]>>8*(7-(i%(w/8))))&0xff);
283
      fprintf(outFile,"' (key)\n");
284
      fprintf(outFile,"-- k = %6d (key length in bytes)\n",keylen);
285
      fprintf(outFile,"\n");
286
    }
287
 
288
  fprintf(outFile,"MD6 compression function computation ");
289
  fprintf(outFile,"(level %d, index %d):\n",ell,ii);
290
  fprintf(outFile,"Input (%d words):\n",n);
291
 
292
  for (i=0;i<r*c+n;i++)
293
    {
294
      if ((i<q))
295
        { fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
296
          fprintf(outFile," Q[%d]\n",i);
297
        }
298
      else if ((i>=q)&&(i<q+k))
299
        { fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
300
          fprintf(outFile," key K[%d]\n",i-q);
301
        }
302
      else if ((u>0)&&(i==q+k+u-1))
303
        { fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
304
          fprintf(outFile," nodeID U = (ell,i) = (%d,%d)\n",ell,ii);
305
        }
306
      else if ((v>0)&&(i==q+k+u+v-1))
307
        { fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
308
          fprintf(outFile," control word V = "
309
                          "(r,L,z,p,keylen,d) = "
310
                  "(%d,%d,%d,%d,%d,%d)\n",r,L,z,p,keylen,d);
311
        }
312
      else if ((i>=q+k+u+v)&&(i<n))
313
        { fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
314
          fprintf(outFile," data B[%2d] ",i-q-k-u-v);
315
          if (ell < L+1) /* PAR node */
316
            { if (ell == 1)
317
                { if ( (i+(p/w))<n )
318
                    fprintf(outFile,"input message word %4d",
319
                            ii*b+(i-(q+k+u+v)));
320
                  else
321
                    fprintf(outFile,"padding");
322
                }
323
              else
324
                if ( (i+(p/w))< n )
325
                  fprintf(outFile,
326
                          "chaining from (%d,%d)",
327
                          ell-1,
328
                          4*ii+(i-(q+k+u+v))/c);
329
                else
330
                  fprintf(outFile,"padding");
331
            }
332
          else /* SEQ node: ell == L+1 */
333
            { if (i-(q+k+u+v)<c) /* initial portion: IV or chaining */
334
                { if (ii == 0)
335
                    fprintf(outFile,"IV");
336
                  else
337
                    fprintf(outFile,"chaining from (%d,%d)",ell,ii-1);
338
                }
339
              else /* data, chaining from below, or padding */
340
                { if (i+(p/w)>=n)
341
                    fprintf(outFile,"padding");
342
                  else if (ell == 1)
343
                    fprintf(outFile,"input message word %4d",
344
                            ii*(b-c)+(i-(q+k+u+v+c)));
345
                  else
346
                    fprintf(outFile,
347
                            "chaining from (%d,%d)",
348
                            ell-1,
349
                            3*ii+(i-(q+k+u+v+c))/c);
350
                }
351
            }
352
          fprintf(outFile,"\n");
353
        }
354
      else if ((i>=r*c+n-c))
355
        { if ((i==r*c+n-c))
356
            fprintf(outFile,"Output (%d words of chaining values):\n",c);
357
          fprintf(outFile,"A[%4d] = " PR_MD6_WORD,i,A[i]);
358
          fprintf(outFile," output chaining value C[%d]\n",i-(r*c+n-c));
359
        }
360
      else
361
        { if (i==n)
362
            { if (print_intermediate)
363
                fprintf(outFile,"Intermediate values:\n");
364
              else
365
                fprintf(outFile,
366
                        "Intermediate values A[%d..%d] omitted... "
367
                        "\n",n,r*c+n-c-1);
368
            }
369
          if (print_intermediate)
370
            fprintf(outFile,"A[%4d] = " PR_MD6_WORD "\n",i,A[i]);
371
        }
372
    }
373
  fprintf(outFile,"\n");
374
}
375
 
376
/* interface to hash routines
377
*/
378
 
379
void hash_init()
380
{ int err;
381
  start_timer();
382
  if ((err=md6_full_init(&st,d,K,keylen,L,r)))
383
    { printf("Bad MD6 parameters; can't initialize md6. "
384
             "errcode = %d\n",err);
385
      return;
386
    }
387
  if (print_input_output)
388
    compression_hook = compression_hook_1;
389
}
390
 
391
void hash_update(char* data,
392
                 uint64_t databitlen)
393
{ int err;
394
  if ((err=md6_update(&st,
395
                      (unsigned char *)data,
396
                      databitlen)))
397
    { printf("MD6 update error. error code: %d\n",err);
398
      return;
399
    }
400
}
401
 
402
void hash_final()
403
{ int err;
404
  if ((err=md6_final(&st,NULL)))
405
    { printf("MD6 finalization error. error code: %d\n",err);
406
      return;
407
    }
408
  end_timer();
409
}
410
 
411
void hash_filep(FILE *inFile)
412
{ uint64_t bytes;
413
  char data[1024];
414
  if (inFile==NULL)
415
    { printf("hash_filep has NULL input file pointer.\n");
416
      return;
417
    }
418
  hash_init();
419
  while ((bytes = fread (data, 1, 1024, inFile)) != 0)
420
    hash_update(data,bytes*8);
421
  hash_final();
422
}
423
 
424
void hash_stdin()
425
{ hash_filep(stdin);
426
}
427
 
428
void hash_file( char *filename )
429
{ FILE *inFile = fopen (filename, "rb");
430
  if ( inFile == NULL )
431
    { printf("%s can't be opened.\n", filename);
432
      return;
433
    }
434
  hash_filep(inFile);
435
  fclose(inFile);
436
}
437
 
438
void hash_b(uint64_t bitlen)
439
/* Hash dummy input file of length bitlen bits.
440
** File (hex) repeats with period 7:
441
**   11 22 33 44 55 66 77 11 22 33 44 55 66 77 11 22 33 ...
442
*/
443
{ int i;
444
  char data[700];  /* nice if length is multiple of 7 for periodicity */
445
  for (i=0;i<700;i++)
446
    data[i] =  0x11 + (char)((i % 7)*(0x11));
447
  hash_init();
448
  while (bitlen>0)
449
    { uint64_t part_len = min(700*8,bitlen);
450
      hash_update(data,part_len);
451
      bitlen = bitlen - part_len;
452
    }
453
  hash_final();
454
}
455
 
456
void hash_B(uint64_t B)
457
/* Hash dummy input file of length B bytes.
458
*/
459
{ hash_b(B*8);
460
}
461
 
462
/* Routines to handle command-line options
463
*/
464
 
465
void optd(char *optstr)
466
{ /* set MD6 digest length
467
  ** -dnn sets digest length to nn, 1 <= nn <= 512
468
  */
469
  int dopt = get_int(optstr+2);
470
  if (dopt<1 || dopt>512)
471
    printf("Illegal digest size option %s ignored.\n",optstr);
472
  else
473
    d = dopt;
474
  r = md6_default_r(d);
475
}
476
 
477
void opth()
478
{ /* print md6sum help string */
479
  printf(help_string);
480
}
481
 
482
void optK(char *optstr)
483
{ /* set MD6 key */
484
  optstr += 2;
485
  keylen = 0;
486
  while (*optstr && keylen<64) K[keylen++] = *optstr++;
487
  K[keylen] = 0;
488
}
489
 
490
void optL(char *optstr)
491
{ /* set MD6 mode parameter
492
  ** -Lnn where 0<=n<=64
493
  ** nn = 0 means fully sequential
494
  ** nn = 64 means fully hierarchical
495
  ** intermediate values give a blended approach
496
  */
497
  int Lopt = get_int(optstr+2);
498
  if (Lopt<0 || Lopt>64)
499
    printf("Illegal L options %s ignored.\n",optstr);
500
  else
501
    L = Lopt;
502
}
503
 
504
void optr(char *optstr)
505
{ /* set MD6 number of rounds
506
  ** -rnn where 0<=r<=255
507
  */
508
  int ropt = get_int(optstr+2);
509
  if (r<0 || r>255)
510
    printf("Illegal r options %s ignored.\n",optstr);
511
  else
512
    r = ropt;
513
 
514
}
515
 
516
void optM(char *optstr)
517
{ /* hash a message given as a command-line argument */
518
  char *p = optstr + 2;
519
  msglenbytes = 0;
520
  while (*p && msglenbytes<4990) msg[msglenbytes++] = *p++;
521
  msg[msglenbytes] = 0;
522
  hash_init();
523
  hash_update(msg,msglenbytes*8);
524
  hash_final();
525
}
526
 
527
void optb(char *optstr)
528
/* -bnnnn hash dummy file of length nnnn bits */
529
{
530
  uint64_t bitlen = get_int(optstr+2);
531
  hash_b(bitlen);
532
}
533
 
534
void optB(char *optstr)
535
/* -Bnnnn hash dummy file of length nnnn bytes */
536
{ uint64_t B = get_int(optstr+2);
537
  hash_b(B*8);
538
}
539
 
540
void check_line(char *line)
541
/* print filename if its hash doesn't agree with what's given in line
542
*/
543
{ char *x;
544
  char hexhashval[1000];
545
  int hexhashlen;
546
  char filename[1000];
547
  char decfilename[1000];
548
  int filenamelen;
549
  /* collect hash value */
550
  x = line;
551
  hexhashlen = 0;
552
  while (*x && *x!=' ' && hexhashlen<900)
553
    hexhashval[hexhashlen++] = *x++;
554
  hexhashval[hexhashlen] = 0;
555
  if (*x != ' ')
556
    { printf("Badly formed hash check file line: %s\n",line);
557
      return;
558
    }
559
  x++;
560
  /* collect filename and decode it */
561
  filenamelen = 0;
562
  while (*x && *x != '\n' && filenamelen<900)
563
    filename[filenamelen++] = *x++;
564
  filename[filenamelen] = 0;
565
  decode(decfilename,filename);
566
  if (filename[0]=='-')
567
    { /* handle "filenames" starting with '-' specially,
568
      ** even though legitimate filenames may start with '-'.
569
      */
570
      if (filenamelen==1)
571
        return;         /* skip standard input */
572
      switch( filename[1] )
573
        {
574
        case 'M': optM(decfilename); break;
575
        case 'b': optb(decfilename); break;
576
        case 'B': optB(decfilename); break;
577
        default: hash_file(decfilename); break;
578
        }
579
    }
580
  else
581
    { /* now compute hash of file */
582
      hash_file(decfilename);
583
    }
584
  if (strcmp(hexhashval,(char *)st.hexhashval)!=0)
585
    printf("%s\n",decfilename);
586
}
587
 
588
void optc(int argc, char **argv, int i)
589
/* Recompute hashes, and check them for current validity.
590
** -c file     causes hashes from given file to be checked.
591
**             (This file is e.g. output from previous run of md6sum.)
592
**             Names of files that's don't hash the same are printed.
593
*/
594
{
595
  FILE *checkfilep;
596
  char line[1000];
597
 
598
  if (i == argc-1)
599
    { printf("md6sum error: no file given for -c option.\n");
600
      return;
601
    }
602
  checkfilep = fopen(argv[i+1],"r");
603
  if (checkfilep==NULL)
604
    { printf("Hash check file %s can't be opened.\n",argv[i+1]);
605
      return;
606
    }
607
  while (fgets( line, 990, checkfilep))
608
    { if (strlen(line)==0) continue;
609
      line[strlen(line)-1] = 0; /* kill '\n' */
610
      if (line[0]=='-') /* handle md6sum option */
611
        { if (strlen(line)==1)
612
            { printf("Hash check file contains illegal line with single '-'; ignored.\n");
613
            }
614
          switch ( line[1] )
615
            {
616
            case 'd': optd(line); break;
617
            case 'K': optK(line); break;
618
            case 'L': optL(line); break;
619
            case 'r': optr(line); break;
620
            case ' ': break; /* ignore lines starting with '- ' or '--' */
621
            case '-': break;
622
            default: printf("Unrecognized md6sum option in check file: %s\n",argv[i]);
623
                     break;
624
            };
625
          continue;
626
        }
627
      /* now handle typical line with hash value */
628
      check_line(line);
629
    }
630
  fclose(checkfilep);
631
}
632
 
633
void optt()
634
/* turn on timing printout */
635
{
636
  print_times = 1;
637
}
638
 
639
/* Routine to print hashvalue filename line.
640
** Prints time_of_day first if it hasn't been printed already.
641
*/
642
 
643
int tod_printed = 0;
644
 
645
void print_tod()
646
{ /* print time-of-day if it hasn't been printed yet. */
647
  time_t now;
648
  if (!tod_printed)
649
    { time(&now);
650
      printf("-- %s",ctime(&now));
651
      tod_printed = 1;
652
    }
653
}
654
 
655
void opti()
656
/* turn on printing of input/output values for compression function calls */
657
{ print_tod();
658
  print_input_output = 1;
659
  outFile = stdout;
660
}
661
 
662
void optI()
663
/* turn on printing of input/output values AND intermediate values */
664
{ print_tod();
665
  print_input_output = 1;
666
  print_intermediate = 1;
667
  outFile = stdout;
668
}
669
 
670
void opts(char *optstr)
671
{ uint64_t trials = get_int(optstr+2);
672
  uint64_t i;
673
  int err;
674
  double elapsed_time;
675
  uint64_t elapsed_ticks;
676
  if (trials == 0) trials = 1;
677
  start_timer();
678
  for (i=0;i<trials;i++)
679
    { st.initialized = 0;
680
      if ((err = md6_full_init(&st,d,K,keylen,L,r)))
681
        printf("MD6 initialization error %d for option -s.\n",err);
682
    }
683
  end_timer();
684
  elapsed_time = end_time - start_time;
685
  printf("-- Setup trials = %lld\n",(long long int)trials);
686
  printf("-- Elapsed time = %.3f seconds\n",elapsed_time);
687
  elapsed_ticks = end_ticks - start_ticks;
688
  printf("-- Total clock ticks = %lld\n",(long long int)elapsed_ticks);
689
  printf("-- Clock ticks / setup = %lld\n",(long long int)(elapsed_ticks/trials));
690
}
691
 
692
void print_hash(char *filename)
693
{ print_tod();
694
  if (print_input_output == 0)
695
    printf("%s %s\n",st.hexhashval,filename);
696
  else
697
    printf("Final hash value = %s\n",st.hexhashval);
698
  print_time(); /* running time */
699
}
700
 
701
 
702
 
703
/* main
704
*/
705
int main(int argc, char **argv)
706
{ int i;
707
  char encfilename[1000];
708
 
709
  /* set default md6 parameter settings */
710
  d = 256;
711
  keylen = 0;
712
  L = 64;
713
  r = md6_default_r(d);
714
 
715
  /* Process command line options */
716
  if ( argc == 1 )
717
    { hash_stdin();
718
      print_hash("-");
719
    }
720
  for (i=1;i<argc;i++)
721
    {
722
      if (strlen(argv[i])==0) continue;
723
      if (argv[i][0]!='-')
724
        { /* argument is filename */
725
          hash_file(argv[i]);
726
          encode(encfilename,argv[i]);
727
          print_hash(encfilename);
728
          print_time();
729
        }
730
      else
731
        {
732
          if (strlen(argv[i])==1)
733
            { hash_stdin();
734
              print_hash("-");
735
              continue;
736
            }
737
          switch ( argv[i][1] )
738
            {
739
            case 'b': optb(argv[i]); print_hash(argv[i]); break;
740
            case 'B': optB(argv[i]); print_hash(argv[i]); break;
741
            case 'c': optc(argc,argv,i); i+=1; break;
742
            case 'd': optd(argv[i]); printf("-d%d\n",d); break;
743
            case 'h': opth(); break;
744
            case 'i': opti(); break;
745
            case 'I': optI(); break;
746
            case 'K': optK(argv[i]); printf("-K%s\n",K); break;
747
            case 'L': optL(argv[i]); printf("-L%d\n",L); break;
748
            case 'M': optM(argv[i]); print_hash(argv[i]); break;
749
            case 'r': optr(argv[i]); printf("-r%d\n",r); break;
750
            case 's': opts(argv[i]); break;
751
            case 't': optt(); break;
752
            default:  hash_file(argv[i]);
753
                      encode(encfilename,argv[i]);
754
                      print_hash(encfilename);
755
                      print_time();
756
                      break;
757
            }
758
        }
759
    }
760
  return 0;
761
}

powered by: WebSVN 2.1.0

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