3 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
5 * Copyright (c) 2000-2001, Aaron D. Gifford
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: sha2speed.c,v 1.1 2001/11/08 00:02:23 adg Exp adg $
44 void usage(char *prog) {
45 fprintf(stderr, "Usage:\t%s [<num-of-bytes>] [<num-of-loops>] [<fill-byte>]\n", prog);
49 void printspeed(char *caption, unsigned long bytes, double time) {
50 if (bytes / 1073741824UL > 0) {
51 printf("%s %.4f sec (%.3f GBps)\n", caption, time, (double)bytes/1073741824UL/time);
52 } else if (bytes / 1048576 > 0) {
53 printf("%s %.4f (%.3f MBps)\n", caption, time, (double)bytes/1048576/time);
54 } else if (bytes / 1024 > 0) {
55 printf("%s %.4f (%.3f KBps)\n", caption, time, (double)bytes/1024/time);
57 printf("%s %.4f (%f Bps)\n", caption, time, (double)bytes/time);
62 int main(int argc, char **argv) {
67 char md[SHA512_DIGEST_STRING_LENGTH];
68 int bytes, blocks, rep, i, j;
69 struct timeval start, end;
70 double t, ave256, ave384, ave512;
71 double best256, best384, best512;
77 /* Default to 1024 16K blocks (16 MB) */
78 bytes = 1024 * 1024 * 16;
80 blocks = atoi(argv[1]);
82 blocks = bytes / BUFSIZE;
84 /* Default to 10 repetitions */
90 /* Set up the input data */
92 memset(buf, atoi(argv[2]), BUFSIZE);
94 memset(buf, 0xb7, BUFSIZE);
97 ave256 = ave384 = ave512 = 0;
98 best256 = best384 = best512 = 100000;
99 for (i = 0; i < rep; i++) {
104 gettimeofday(&start, (struct timezone*)0);
105 for (j = 0; j < blocks; j++) {
106 SHA256_Update(&c256, (unsigned char*)buf, BUFSIZE);
108 if (bytes % BUFSIZE) {
109 SHA256_Update(&c256, (unsigned char*)buf, bytes % BUFSIZE);
111 SHA256_End(&c256, md);
112 gettimeofday(&end, (struct timezone*)0);
113 t = ((end.tv_sec - start.tv_sec) * 1000000.0 + (end.tv_usec - start.tv_usec)) / 1000000.0;
118 printf("SHA-256[%d] (%.4f/%.4f/%.4f seconds) = 0x%s\n", i+1, t, ave256/(i+1), best256, md);
120 gettimeofday(&start, (struct timezone*)0);
121 for (j = 0; j < blocks; j++) {
122 SHA384_Update(&c384, (unsigned char*)buf, BUFSIZE);
124 if (bytes % BUFSIZE) {
125 SHA384_Update(&c384, (unsigned char*)buf, bytes % BUFSIZE);
127 SHA384_End(&c384, md);
128 gettimeofday(&end, (struct timezone*)0);
129 t = ((end.tv_sec - start.tv_sec) * 1000000.0 + (end.tv_usec - start.tv_usec)) / 1000000.0;
134 printf("SHA-384[%d] (%.4f/%.4f/%.4f seconds) = 0x%s\n", i+1, t, ave384/(i+1), best384, md);
136 gettimeofday(&start, (struct timezone*)0);
137 for (j = 0; j < blocks; j++) {
138 SHA512_Update(&c512, (unsigned char*)buf, BUFSIZE);
140 if (bytes % BUFSIZE) {
141 SHA512_Update(&c512, (unsigned char*)buf, bytes % BUFSIZE);
143 SHA512_End(&c512, md);
144 gettimeofday(&end, (struct timezone*)0);
145 t = ((end.tv_sec - start.tv_sec) * 1000000.0 + (end.tv_usec - start.tv_usec)) / 1000000.0;
150 printf("SHA-512[%d] (%.4f/%.4f/%.4f seconds) = 0x%s\n", i+1, t, ave512/(i+1), best512, md);
155 printf("\nTEST RESULTS SUMMARY:\nTEST REPETITIONS: %d\n", rep);
156 if (bytes / 1073741824UL > 0) {
157 printf("TEST SET SIZE: %.3f GB\n", (double)bytes/1073741824UL);
158 } else if (bytes / 1048576 > 0) {
159 printf("TEST SET SIZE: %.3f MB\n", (double)bytes/1048576);
160 } else if (bytes /1024 > 0) {
161 printf("TEST SET SIZE: %.3f KB\n", (double)bytes/1024);
163 printf("TEST SET SIZE: %d B\n", bytes);
165 printspeed("SHA-256 average:", bytes, ave256);
166 printspeed("SHA-256 best: ", bytes, best256);
167 printspeed("SHA-384 average:", bytes, ave384);
168 printspeed("SHA-384 best: ", bytes, best384);
169 printspeed("SHA-512 average:", bytes, ave512);
170 printspeed("SHA-512 best: ", bytes, best512);