blob: 3e70c2c87c2228289bd06a28610a352666d37764 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/md4.h"
34#include "polarssl/md5.h"
35#include "polarssl/sha1.h"
36#include "polarssl/sha2.h"
37#include "polarssl/arc4.h"
38#include "polarssl/des.h"
39#include "polarssl/aes.h"
40#include "polarssl/rsa.h"
41#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
43#define BUFSIZE 1024
44
45static int myrand( void *rng_state )
46{
47 if( rng_state != NULL )
48 rng_state = NULL;
49
50 return( rand() );
51}
52
53unsigned char buf[BUFSIZE];
54
55int main( void )
56{
57 int keysize;
58 unsigned long i, j, tsc;
59 unsigned char tmp[32];
Paul Bakker40e46942009-01-03 21:51:57 +000060#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000061 arc4_context arc4;
62#endif
Paul Bakker40e46942009-01-03 21:51:57 +000063#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000064 des3_context des3;
65 des_context des;
66#endif
Paul Bakker40e46942009-01-03 21:51:57 +000067#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000068 aes_context aes;
69#endif
Paul Bakker40e46942009-01-03 21:51:57 +000070#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000071 rsa_context rsa;
72#endif
73
74 memset( buf, 0xAA, sizeof( buf ) );
75
76 printf( "\n" );
77
Paul Bakker40e46942009-01-03 21:51:57 +000078#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000079 printf( " MD4 : " );
80 fflush( stdout );
81
82 set_alarm( 1 );
83 for( i = 1; ! alarmed; i++ )
84 md4( buf, BUFSIZE, tmp );
85
86 tsc = hardclock();
87 for( j = 0; j < 1024; j++ )
88 md4( buf, BUFSIZE, tmp );
89
90 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
91 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
92#endif
93
Paul Bakker40e46942009-01-03 21:51:57 +000094#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000095 printf( " MD5 : " );
96 fflush( stdout );
97
98 set_alarm( 1 );
99 for( i = 1; ! alarmed; i++ )
100 md5( buf, BUFSIZE, tmp );
101
102 tsc = hardclock();
103 for( j = 0; j < 1024; j++ )
104 md5( buf, BUFSIZE, tmp );
105
106 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
107 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
108#endif
109
Paul Bakker40e46942009-01-03 21:51:57 +0000110#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 printf( " SHA-1 : " );
112 fflush( stdout );
113
114 set_alarm( 1 );
115 for( i = 1; ! alarmed; i++ )
116 sha1( buf, BUFSIZE, tmp );
117
118 tsc = hardclock();
119 for( j = 0; j < 1024; j++ )
120 sha1( buf, BUFSIZE, tmp );
121
122 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
123 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
124#endif
125
Paul Bakker40e46942009-01-03 21:51:57 +0000126#if defined(POLARSSL_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 printf( " SHA-256 : " );
128 fflush( stdout );
129
130 set_alarm( 1 );
131 for( i = 1; ! alarmed; i++ )
132 sha2( buf, BUFSIZE, tmp, 0 );
133
134 tsc = hardclock();
135 for( j = 0; j < 1024; j++ )
136 sha2( buf, BUFSIZE, tmp, 0 );
137
138 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
139 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
140#endif
141
Paul Bakker40e46942009-01-03 21:51:57 +0000142#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 printf( " ARC4 : " );
144 fflush( stdout );
145
146 arc4_setup( &arc4, tmp, 32 );
147
148 set_alarm( 1 );
149 for( i = 1; ! alarmed; i++ )
150 arc4_crypt( &arc4, buf, BUFSIZE );
151
152 tsc = hardclock();
153 for( j = 0; j < 1024; j++ )
154 arc4_crypt( &arc4, buf, BUFSIZE );
155
156 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
157 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
158#endif
159
Paul Bakker40e46942009-01-03 21:51:57 +0000160#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 printf( " 3DES : " );
162 fflush( stdout );
163
164 des3_set3key_enc( &des3, tmp );
165
166 set_alarm( 1 );
167 for( i = 1; ! alarmed; i++ )
168 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
169
170 tsc = hardclock();
171 for( j = 0; j < 1024; j++ )
172 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
173
174 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
175 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
176
177 printf( " DES : " );
178 fflush( stdout );
179
180 des_setkey_enc( &des, tmp );
181
182 set_alarm( 1 );
183 for( i = 1; ! alarmed; i++ )
184 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
185
186 tsc = hardclock();
187 for( j = 0; j < 1024; j++ )
188 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
189
190 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
191 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
192#endif
193
Paul Bakker40e46942009-01-03 21:51:57 +0000194#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 for( keysize = 128; keysize <= 256; keysize += 64 )
196 {
197 printf( " AES-%d : ", keysize );
198 fflush( stdout );
199
200 memset( buf, 0, sizeof( buf ) );
201 memset( tmp, 0, sizeof( tmp ) );
202 aes_setkey_enc( &aes, tmp, keysize );
203
204 set_alarm( 1 );
205
206 for( i = 1; ! alarmed; i++ )
207 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
208
209 tsc = hardclock();
210 for( j = 0; j < 4096; j++ )
211 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
212
213 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
214 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
215 }
216#endif
217
Paul Bakker40e46942009-01-03 21:51:57 +0000218#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
220 rsa_gen_key( &rsa, 1024, 65537 );
221
222 printf( " RSA-1024 : " );
223 fflush( stdout );
224 set_alarm( 3 );
225
226 for( i = 1; ! alarmed; i++ )
227 {
228 buf[0] = 0;
229 rsa_public( &rsa, buf, buf );
230 }
231
232 printf( "%9lu public/s\n", i / 3 );
233
234 printf( " RSA-1024 : " );
235 fflush( stdout );
236 set_alarm( 3 );
237
238 for( i = 1; ! alarmed; i++ )
239 {
240 buf[0] = 0;
241 rsa_private( &rsa, buf, buf );
242 }
243
244 printf( "%9lu private/s\n\n", i / 3 );
245
246 rsa_free( &rsa );
247#endif
248
249#ifdef WIN32
250 printf( " Press Enter to exit this program.\n" );
251 fflush( stdout ); getchar();
252#endif
253
254 return( 0 );
255}