blob: 2193b78197f6224068119a1f7c1ad89bc9d29b66 [file] [log] [blame]
Randall Spangler32a65262011-06-27 10:49:11 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shahcc1dd992010-02-12 11:21:18 -08002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
Bill Richardson0c3ba242013-03-29 11:09:30 -07006#include <stdint.h>
Gaurav Shahcc1dd992010-02-12 11:21:18 -08007#include <stdio.h>
8#include <stdlib.h>
9
Gaurav Shah5411c7a2010-03-31 10:56:49 -070010#include "cryptolib.h"
Randall Spangler32a65262011-06-27 10:49:11 -070011#include "host_common.h"
Gaurav Shahcc1dd992010-02-12 11:21:18 -080012#include "timer_utils.h"
Gaurav Shahcc1dd992010-02-12 11:21:18 -080013
14#define NUM_HASH_ALGORITHMS 3
15#define TEST_BUFFER_SIZE 4000000
16
17/* Table of hash function pointers and their description. */
Gaurav Shah456678b2010-03-10 18:38:45 -080018typedef uint8_t* (*Hashptr) (const uint8_t*, uint64_t, uint8_t*);
Gaurav Shahcc1dd992010-02-12 11:21:18 -080019typedef struct HashFxTable {
20 Hashptr hash;
21 char* description;
22} HashFxTable;
23
24HashFxTable hash_functions[NUM_HASH_ALGORITHMS] = {
Kees Cook201fe0b2012-05-22 18:05:53 -070025 {internal_SHA1, "sha1"},
26 {internal_SHA256, "sha256"},
27 {internal_SHA512, "sha512"}
Gaurav Shahcc1dd992010-02-12 11:21:18 -080028};
29
30int main(int argc, char* argv[]) {
31 int i;
32 double speed;
33 uint32_t msecs;
Randall Spangler32a65262011-06-27 10:49:11 -070034 uint8_t* buffer = (uint8_t*) malloc(TEST_BUFFER_SIZE);
35 uint8_t* digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE); /* Maximum size of
Gaurav Shahcc1dd992010-02-12 11:21:18 -080036 * 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 Shah8b95c702010-02-28 12:50:29 -080049 fprintf(stderr, "# %s Time taken = %u ms, Speed = %f Mbytes/sec\n",
Gaurav Shahcc1dd992010-02-12 11:21:18 -080050 hash_functions[i].description, msecs, speed);
Gaurav Shah444e1e12010-03-01 20:25:03 -080051 fprintf(stdout, "mbytes_per_sec_%s:%f\n",
Gaurav Shah8b95c702010-02-28 12:50:29 -080052 hash_functions[i].description, speed);
Gaurav Shahcc1dd992010-02-12 11:21:18 -080053 }
54
Randall Spangler32a65262011-06-27 10:49:11 -070055 free(digest);
56 free(buffer);
Gaurav Shahcc1dd992010-02-12 11:21:18 -080057 return 0;
58}