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>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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"
59 static int myrand(void *rng_state
)
61 if (rng_state
!= NULL
)
67 unsigned char buf
[BUFSIZE
];
72 unsigned long i
, j
, tsc
;
73 unsigned char tmp
[32];
74 #if defined(TROPICSSL_ARC4_C)
77 #if defined(TROPICSSL_DES_C)
81 #if defined(TROPICSSL_AES_C)
84 #if defined(TROPICSSL_CAMELLIA_C)
85 camellia_context camellia
;
87 #if defined(TROPICSSL_RSA_C)
91 memset(buf
, 0xAA, sizeof(buf
));
95 #if defined(TROPICSSL_MD4_C)
100 for (i
= 1; !alarmed
; i
++)
101 md4(buf
, BUFSIZE
, tmp
);
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
));
111 #if defined(TROPICSSL_MD5_C)
116 for (i
= 1; !alarmed
; i
++)
117 md5(buf
, BUFSIZE
, tmp
);
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
));
127 #if defined(TROPICSSL_SHA1_C)
132 for (i
= 1; !alarmed
; i
++)
133 sha1(buf
, BUFSIZE
, tmp
);
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
));
143 #if defined(TROPICSSL_SHA2_C)
144 printf(" SHA-256 : ");
148 for (i
= 1; !alarmed
; i
++)
149 sha2(buf
, BUFSIZE
, tmp
, 0);
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
));
159 #if defined(TROPICSSL_ARC4_C)
163 arc4_setup(&arc4
, tmp
, 32);
166 for (i
= 1; !alarmed
; i
++)
167 arc4_crypt(&arc4
, BUFSIZE
, buf
, buf
);
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
));
177 #if defined(TROPICSSL_DES_C)
181 des3_set3key_enc(&des3
, tmp
);
184 for (i
= 1; !alarmed
; i
++)
185 des3_crypt_cbc(&des3
, DES_ENCRYPT
, BUFSIZE
, tmp
, buf
, buf
);
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
));
197 des_setkey_enc(&des
, tmp
);
200 for (i
= 1; !alarmed
; i
++)
201 des_crypt_cbc(&des
, DES_ENCRYPT
, BUFSIZE
, tmp
, buf
, buf
);
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
));
211 #if defined(TROPICSSL_AES_C)
212 for (keysize
= 128; keysize
<= 256; keysize
+= 64) {
213 printf(" AES-%d : ", keysize
);
216 memset(buf
, 0, sizeof(buf
));
217 memset(tmp
, 0, sizeof(tmp
));
218 aes_setkey_enc(&aes
, tmp
, keysize
);
222 for (i
= 1; !alarmed
; i
++)
223 aes_crypt_cbc(&aes
, AES_ENCRYPT
, BUFSIZE
, tmp
, buf
,
227 for (j
= 0; j
< 4096; j
++)
228 aes_crypt_cbc(&aes
, AES_ENCRYPT
, BUFSIZE
, tmp
, buf
,
231 printf("%9lu Kb/s, %9lu cycles/byte\n", i
* BUFSIZE
/ 1024,
232 (hardclock() - tsc
) / (j
* BUFSIZE
));
236 #if defined(TROPICSSL_CAMELLIA_C)
237 for (keysize
= 128; keysize
<= 256; keysize
+= 64) {
238 printf(" CAMELLIA-%d : ", keysize
);
241 memset(buf
, 0, sizeof(buf
));
242 memset(tmp
, 0, sizeof(tmp
));
243 camellia_setkey_enc(&camellia
, tmp
, keysize
);
247 for (i
= 1; !alarmed
; i
++)
248 camellia_crypt_cbc(&camellia
, CAMELLIA_ENCRYPT
, BUFSIZE
,
252 for (j
= 0; j
< 4096; j
++)
253 camellia_crypt_cbc(&camellia
, CAMELLIA_ENCRYPT
, BUFSIZE
,
256 printf("%9lu Kb/s, %9lu cycles/byte\n", i
* BUFSIZE
/ 1024,
257 (hardclock() - tsc
) / (j
* BUFSIZE
));
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 : ");
269 for (i
= 1; !alarmed
; i
++) {
271 rsa_public(&rsa
, buf
, buf
);
274 printf("%9lu public/s\n", i
/ 3);
276 printf(" RSA-1024 : ");
280 for (i
= 1; !alarmed
; i
++) {
282 rsa_private(&rsa
, buf
, buf
);
285 printf("%9lu private/s\n\n", i
/ 3);
291 printf(" Press Enter to exit this program.\n");