File: md5_testharness.c

package info (click to toggle)
dietlibc 0.34~cvs20160606-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,336 kB
  • sloc: ansic: 71,631; asm: 13,006; cpp: 1,860; makefile: 799; sh: 292; perl: 62
file content (158 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download | duplicates (7)
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

#include <stdio.h>
#include <string.h>

#ifndef __dietlibc__
#warning "You are not using diet libc, md5 test disbled"
int main(void) { return 0; }
#else
#include <md5.h>


#if defined (__i386__) || defined (__x86_64__)
 #define RDTSC(dst) { asm volatile ("rdtsc" : "=a" (dst) : : "edx"); }
 #define ITERATIONS 10
#else
 #define RDTSC(dst) { dst = 0; }
 #define ITERATIONS 1
#endif


static const char rawdata[] = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "bcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "cdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "defghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "efghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "fghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "ghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "hijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "ijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "jklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "klmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "lmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "mnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "nopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "opqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "pqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "qrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "rstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "stuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "tuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "uvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "vwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "wxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
                              "xyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";


/*
   You will just have to trust me that this is correct... :-)
*/

static const char hash_expected[32+1] = "45392e1d034f05172294e7714c555fac";


static int IsLittleEndian (void)
{
   static const unsigned long ul = 0x00000001;

   return ((int) (*((unsigned char *) &ul)));
}

static int IsBigEndian (void)
{
   static const unsigned long ul = 0x01000000;

   return ((int) (*((unsigned char *) &ul)));
}


int main (int argc, char *argv[])
{
   int i, j;
   int errorcode = 0;

   unsigned int start;
   unsigned int stop;
   unsigned int fastest;
   
   unsigned int total_hashed_byte_count;
   unsigned int total_md5update_calls;

   MD5_CTX Context;

   unsigned char hash[16];

   char hash_as_ascii[32+1];

   char *result;

   if (IsLittleEndian() && !IsBigEndian())
   {
      printf ("\n\nThis platform is Little-Endian\n");
   }
	else if (!IsLittleEndian() && IsBigEndian())
   {
      printf ("\n\nThis platform is Big-Endian\n");
   }
	else
   {
      printf ("\n\nThis platform is Broken.\n");
   }

   fastest = 0xffffffff;

   for (i = 0; i < ITERATIONS; i++)
   {
      total_hashed_byte_count = 0;
      total_md5update_calls = 0;

      RDTSC(start);

      MD5Init (&Context);

      for (j = 0; j < sizeof(rawdata); j++)
      {
         unsigned int hashed_byte_count = (sizeof(rawdata) - j);

         MD5Update (&Context, (unsigned char *) &rawdata[j], hashed_byte_count);

         total_hashed_byte_count += hashed_byte_count;
         total_md5update_calls++;
      }

      MD5Final (hash, &Context);
   
      RDTSC(stop);
      
      fastest = ((stop - start) < fastest) ? (stop - start) : fastest;
   }      

   sprintf (hash_as_ascii, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            hash[ 0], hash[ 1], hash[ 2], hash[ 3], hash[ 4], hash[ 5], hash[ 6], hash[ 7],
            hash[ 8], hash[ 9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]);

   if (strcmp (hash_expected, hash_as_ascii) == 0)
   {
      result = " (Passed)";
   }
   else
   {
      result = " (Failed !!)";
      errorcode++;
   }
   
   printf ("\n");
   printf ("MD5 Hashing test (aligned + unaligned data)\n");
   printf ("-------------------------------------------\n");
   printf ("Expected Hash      = %s\n",   hash_expected);
   printf ("Actual Hash        = %s%s\n", hash_as_ascii, result);
   printf ("Execution time     = %d\n",   fastest);
   printf ("Hashed bytes       = %d\n",   total_hashed_byte_count);     /* NB: total is per iteration */
   printf ("Calls to MD5Update = %d\n",   total_md5update_calls);       /* NB: total is per iteration */
   
   printf ("\n");

   return (errorcode);
}
#endif