1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include "cryptolib.h"
11 #include "host_common.h"
12 #include "timer_utils.h"
13 
14 #define NUM_HASH_ALGORITHMS 3
15 #define TEST_BUFFER_SIZE 4000000
16 
17 /* Table of hash function pointers and their description. */
18 typedef uint8_t* (*Hashptr) (const uint8_t*, uint64_t, uint8_t*);
19 typedef struct HashFxTable {
20   Hashptr hash;
21   char* description;
22 } HashFxTable;
23 
24 HashFxTable hash_functions[NUM_HASH_ALGORITHMS] = {
25   {internal_SHA1, "sha1"},
26   {internal_SHA256, "sha256"},
27   {internal_SHA512, "sha512"}
28 };
29 
main(int argc,char * argv[])30 int main(int argc, char* argv[]) {
31   int i;
32   double speed;
33   uint32_t msecs;
34   uint8_t* buffer = (uint8_t*) malloc(TEST_BUFFER_SIZE);
35   uint8_t* digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE); /* Maximum size of
36                                                             * the digest. */
37   ClockTimerState ct;
38 
39   /* Iterate through all the hash functions. */
40   for(i = 0; i < NUM_HASH_ALGORITHMS; i++) {
41     StartTimer(&ct);
42     hash_functions[i].hash(buffer, TEST_BUFFER_SIZE, digest);
43     StopTimer(&ct);
44 
45     msecs = GetDurationMsecs(&ct);
46     speed = ((TEST_BUFFER_SIZE / 10e6)
47              / (msecs / 10e3)); /* Mbytes/sec */
48 
49     fprintf(stderr, "# %s Time taken = %u ms, Speed = %f Mbytes/sec\n",
50             hash_functions[i].description, msecs, speed);
51     fprintf(stdout, "mbytes_per_sec_%s:%f\n",
52             hash_functions[i].description, speed);
53   }
54 
55   free(digest);
56   free(buffer);
57   return 0;
58 }
59