bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / programs / test / benchmark.c
blob8542c0615c558a1662983ba54d317735b1d428fd
1 /*
2 * Benchmark demonstration program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdio.h>
44 #include "tropicssl/config.h"
46 #include "tropicssl/md4.h"
47 #include "tropicssl/md5.h"
48 #include "tropicssl/sha1.h"
49 #include "tropicssl/sha2.h"
50 #include "tropicssl/arc4.h"
51 #include "tropicssl/des.h"
52 #include "tropicssl/aes.h"
53 #include "tropicssl/camellia.h"
54 #include "tropicssl/rsa.h"
55 #include "tropicssl/timing.h"
57 #define BUFSIZE 1024
59 static int myrand(void *rng_state)
61 if (rng_state != NULL)
62 rng_state = NULL;
64 return (rand());
67 unsigned char buf[BUFSIZE];
69 int main(void)
71 int keysize;
72 unsigned long i, j, tsc;
73 unsigned char tmp[32];
74 #if defined(TROPICSSL_ARC4_C)
75 arc4_context arc4;
76 #endif
77 #if defined(TROPICSSL_DES_C)
78 des3_context des3;
79 des_context des;
80 #endif
81 #if defined(TROPICSSL_AES_C)
82 aes_context aes;
83 #endif
84 #if defined(TROPICSSL_CAMELLIA_C)
85 camellia_context camellia;
86 #endif
87 #if defined(TROPICSSL_RSA_C)
88 rsa_context rsa;
89 #endif
91 memset(buf, 0xAA, sizeof(buf));
93 printf("\n");
95 #if defined(TROPICSSL_MD4_C)
96 printf(" MD4 : ");
97 fflush(stdout);
99 set_alarm(1);
100 for (i = 1; !alarmed; i++)
101 md4(buf, BUFSIZE, tmp);
103 tsc = hardclock();
104 for (j = 0; j < 1024; j++)
105 md4(buf, BUFSIZE, tmp);
107 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
108 (hardclock() - tsc) / (j * BUFSIZE));
109 #endif
111 #if defined(TROPICSSL_MD5_C)
112 printf(" MD5 : ");
113 fflush(stdout);
115 set_alarm(1);
116 for (i = 1; !alarmed; i++)
117 md5(buf, BUFSIZE, tmp);
119 tsc = hardclock();
120 for (j = 0; j < 1024; j++)
121 md5(buf, BUFSIZE, tmp);
123 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
124 (hardclock() - tsc) / (j * BUFSIZE));
125 #endif
127 #if defined(TROPICSSL_SHA1_C)
128 printf(" SHA-1 : ");
129 fflush(stdout);
131 set_alarm(1);
132 for (i = 1; !alarmed; i++)
133 sha1(buf, BUFSIZE, tmp);
135 tsc = hardclock();
136 for (j = 0; j < 1024; j++)
137 sha1(buf, BUFSIZE, tmp);
139 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
140 (hardclock() - tsc) / (j * BUFSIZE));
141 #endif
143 #if defined(TROPICSSL_SHA2_C)
144 printf(" SHA-256 : ");
145 fflush(stdout);
147 set_alarm(1);
148 for (i = 1; !alarmed; i++)
149 sha2(buf, BUFSIZE, tmp, 0);
151 tsc = hardclock();
152 for (j = 0; j < 1024; j++)
153 sha2(buf, BUFSIZE, tmp, 0);
155 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
156 (hardclock() - tsc) / (j * BUFSIZE));
157 #endif
159 #if defined(TROPICSSL_ARC4_C)
160 printf(" ARC4 : ");
161 fflush(stdout);
163 arc4_setup(&arc4, tmp, 32);
165 set_alarm(1);
166 for (i = 1; !alarmed; i++)
167 arc4_crypt(&arc4, BUFSIZE, buf, buf);
169 tsc = hardclock();
170 for (j = 0; j < 1024; j++)
171 arc4_crypt(&arc4, BUFSIZE, buf, buf);
173 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
174 (hardclock() - tsc) / (j * BUFSIZE));
175 #endif
177 #if defined(TROPICSSL_DES_C)
178 printf(" 3DES : ");
179 fflush(stdout);
181 des3_set3key_enc(&des3, tmp);
183 set_alarm(1);
184 for (i = 1; !alarmed; i++)
185 des3_crypt_cbc(&des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf);
187 tsc = hardclock();
188 for (j = 0; j < 1024; j++)
189 des3_crypt_cbc(&des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf);
191 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
192 (hardclock() - tsc) / (j * BUFSIZE));
194 printf(" DES : ");
195 fflush(stdout);
197 des_setkey_enc(&des, tmp);
199 set_alarm(1);
200 for (i = 1; !alarmed; i++)
201 des_crypt_cbc(&des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf);
203 tsc = hardclock();
204 for (j = 0; j < 1024; j++)
205 des_crypt_cbc(&des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf);
207 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
208 (hardclock() - tsc) / (j * BUFSIZE));
209 #endif
211 #if defined(TROPICSSL_AES_C)
212 for (keysize = 128; keysize <= 256; keysize += 64) {
213 printf(" AES-%d : ", keysize);
214 fflush(stdout);
216 memset(buf, 0, sizeof(buf));
217 memset(tmp, 0, sizeof(tmp));
218 aes_setkey_enc(&aes, tmp, keysize);
220 set_alarm(1);
222 for (i = 1; !alarmed; i++)
223 aes_crypt_cbc(&aes, AES_ENCRYPT, BUFSIZE, tmp, buf,
224 buf);
226 tsc = hardclock();
227 for (j = 0; j < 4096; j++)
228 aes_crypt_cbc(&aes, AES_ENCRYPT, BUFSIZE, tmp, buf,
229 buf);
231 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
232 (hardclock() - tsc) / (j * BUFSIZE));
234 #endif
236 #if defined(TROPICSSL_CAMELLIA_C)
237 for (keysize = 128; keysize <= 256; keysize += 64) {
238 printf(" CAMELLIA-%d : ", keysize);
239 fflush(stdout);
241 memset(buf, 0, sizeof(buf));
242 memset(tmp, 0, sizeof(tmp));
243 camellia_setkey_enc(&camellia, tmp, keysize);
245 set_alarm(1);
247 for (i = 1; !alarmed; i++)
248 camellia_crypt_cbc(&camellia, CAMELLIA_ENCRYPT, BUFSIZE,
249 tmp, buf, buf);
251 tsc = hardclock();
252 for (j = 0; j < 4096; j++)
253 camellia_crypt_cbc(&camellia, CAMELLIA_ENCRYPT, BUFSIZE,
254 tmp, buf, buf);
256 printf("%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
257 (hardclock() - tsc) / (j * BUFSIZE));
259 #endif
261 #if defined(TROPICSSL_RSA_C)
262 rsa_init(&rsa, RSA_PKCS_V15, 0, myrand, NULL);
263 rsa_gen_key(&rsa, 1024, 65537);
265 printf(" RSA-1024 : ");
266 fflush(stdout);
267 set_alarm(3);
269 for (i = 1; !alarmed; i++) {
270 buf[0] = 0;
271 rsa_public(&rsa, buf, buf);
274 printf("%9lu public/s\n", i / 3);
276 printf(" RSA-1024 : ");
277 fflush(stdout);
278 set_alarm(3);
280 for (i = 1; !alarmed; i++) {
281 buf[0] = 0;
282 rsa_private(&rsa, buf, buf);
285 printf("%9lu private/s\n\n", i / 3);
287 rsa_free(&rsa);
288 #endif
290 #ifdef WIN32
291 printf(" Press Enter to exit this program.\n");
292 fflush(stdout);
293 getchar();
294 #endif
296 return (0);