Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 6 | #include <stdint.h> |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 10 | #include "cryptolib.h" |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 11 | #include "host_common.h" |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 12 | #include "timer_utils.h" |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 13 | |
| 14 | #define NUM_HASH_ALGORITHMS 3 |
| 15 | #define TEST_BUFFER_SIZE 4000000 |
| 16 | |
| 17 | /* Table of hash function pointers and their description. */ |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 18 | typedef uint8_t* (*Hashptr) (const uint8_t*, uint64_t, uint8_t*); |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 19 | typedef struct HashFxTable { |
| 20 | Hashptr hash; |
| 21 | char* description; |
| 22 | } HashFxTable; |
| 23 | |
| 24 | HashFxTable hash_functions[NUM_HASH_ALGORITHMS] = { |
Kees Cook | 201fe0b | 2012-05-22 18:05:53 -0700 | [diff] [blame] | 25 | {internal_SHA1, "sha1"}, |
| 26 | {internal_SHA256, "sha256"}, |
| 27 | {internal_SHA512, "sha512"} |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | int main(int argc, char* argv[]) { |
| 31 | int i; |
| 32 | double speed; |
| 33 | uint32_t msecs; |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 34 | uint8_t* buffer = (uint8_t*) malloc(TEST_BUFFER_SIZE); |
| 35 | uint8_t* digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE); /* Maximum size of |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 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 | |
Gaurav Shah | 8b95c70 | 2010-02-28 12:50:29 -0800 | [diff] [blame] | 49 | fprintf(stderr, "# %s Time taken = %u ms, Speed = %f Mbytes/sec\n", |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 50 | hash_functions[i].description, msecs, speed); |
Gaurav Shah | 444e1e1 | 2010-03-01 20:25:03 -0800 | [diff] [blame] | 51 | fprintf(stdout, "mbytes_per_sec_%s:%f\n", |
Gaurav Shah | 8b95c70 | 2010-02-28 12:50:29 -0800 | [diff] [blame] | 52 | hash_functions[i].description, speed); |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 55 | free(digest); |
| 56 | free(buffer); |
Gaurav Shah | cc1dd99 | 2010-02-12 11:21:18 -0800 | [diff] [blame] | 57 | return 0; |
| 58 | } |