Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org> |
| 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 7 | * Joined copyright on original XySSL code with: Christophe Devine |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| 24 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 25 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 26 | #endif |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 32 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 33 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 34 | #include "polarssl/md4.h" |
| 35 | #include "polarssl/md5.h" |
| 36 | #include "polarssl/sha1.h" |
| 37 | #include "polarssl/sha2.h" |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 38 | #include "polarssl/sha4.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 39 | #include "polarssl/arc4.h" |
| 40 | #include "polarssl/des.h" |
| 41 | #include "polarssl/aes.h" |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 42 | #include "polarssl/camellia.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 43 | #include "polarssl/rsa.h" |
| 44 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | |
| 46 | #define BUFSIZE 1024 |
| 47 | |
| 48 | static int myrand( void *rng_state ) |
| 49 | { |
| 50 | if( rng_state != NULL ) |
| 51 | rng_state = NULL; |
| 52 | |
| 53 | return( rand() ); |
| 54 | } |
| 55 | |
| 56 | unsigned char buf[BUFSIZE]; |
| 57 | |
| 58 | int main( void ) |
| 59 | { |
| 60 | int keysize; |
| 61 | unsigned long i, j, tsc; |
Paul Bakker | 5a0aa77 | 2009-02-09 22:38:52 +0000 | [diff] [blame] | 62 | unsigned char tmp[64]; |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 63 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | arc4_context arc4; |
| 65 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 66 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | des3_context des3; |
| 68 | des_context des; |
| 69 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 70 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | aes_context aes; |
| 72 | #endif |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 73 | #if defined(POLARSSL_CAMELLIA_C) |
| 74 | camellia_context camellia; |
| 75 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 76 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | rsa_context rsa; |
| 78 | #endif |
| 79 | |
| 80 | memset( buf, 0xAA, sizeof( buf ) ); |
| 81 | |
| 82 | printf( "\n" ); |
| 83 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 84 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | printf( " MD4 : " ); |
| 86 | fflush( stdout ); |
| 87 | |
| 88 | set_alarm( 1 ); |
| 89 | for( i = 1; ! alarmed; i++ ) |
| 90 | md4( buf, BUFSIZE, tmp ); |
| 91 | |
| 92 | tsc = hardclock(); |
| 93 | for( j = 0; j < 1024; j++ ) |
| 94 | md4( buf, BUFSIZE, tmp ); |
| 95 | |
| 96 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 97 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 98 | #endif |
| 99 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 100 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | printf( " MD5 : " ); |
| 102 | fflush( stdout ); |
| 103 | |
| 104 | set_alarm( 1 ); |
| 105 | for( i = 1; ! alarmed; i++ ) |
| 106 | md5( buf, BUFSIZE, tmp ); |
| 107 | |
| 108 | tsc = hardclock(); |
| 109 | for( j = 0; j < 1024; j++ ) |
| 110 | md5( buf, BUFSIZE, tmp ); |
| 111 | |
| 112 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 113 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 114 | #endif |
| 115 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 116 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | printf( " SHA-1 : " ); |
| 118 | fflush( stdout ); |
| 119 | |
| 120 | set_alarm( 1 ); |
| 121 | for( i = 1; ! alarmed; i++ ) |
| 122 | sha1( buf, BUFSIZE, tmp ); |
| 123 | |
| 124 | tsc = hardclock(); |
| 125 | for( j = 0; j < 1024; j++ ) |
| 126 | sha1( buf, BUFSIZE, tmp ); |
| 127 | |
| 128 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 129 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 130 | #endif |
| 131 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 132 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | printf( " SHA-256 : " ); |
| 134 | fflush( stdout ); |
| 135 | |
| 136 | set_alarm( 1 ); |
| 137 | for( i = 1; ! alarmed; i++ ) |
| 138 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 139 | |
| 140 | tsc = hardclock(); |
| 141 | for( j = 0; j < 1024; j++ ) |
| 142 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 143 | |
| 144 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 145 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 146 | #endif |
| 147 | |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 148 | #if defined(POLARSSL_SHA4_C) |
| 149 | printf( " SHA-512 : " ); |
| 150 | fflush( stdout ); |
| 151 | |
| 152 | set_alarm( 1 ); |
| 153 | for( i = 1; ! alarmed; i++ ) |
| 154 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 155 | |
| 156 | tsc = hardclock(); |
| 157 | for( j = 0; j < 1024; j++ ) |
| 158 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 159 | |
| 160 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 161 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 162 | #endif |
| 163 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 164 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | printf( " ARC4 : " ); |
| 166 | fflush( stdout ); |
| 167 | |
| 168 | arc4_setup( &arc4, tmp, 32 ); |
| 169 | |
| 170 | set_alarm( 1 ); |
| 171 | for( i = 1; ! alarmed; i++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame^] | 172 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | |
| 174 | tsc = hardclock(); |
| 175 | for( j = 0; j < 1024; j++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame^] | 176 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | |
| 178 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 179 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 180 | #endif |
| 181 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 182 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | printf( " 3DES : " ); |
| 184 | fflush( stdout ); |
| 185 | |
| 186 | des3_set3key_enc( &des3, tmp ); |
| 187 | |
| 188 | set_alarm( 1 ); |
| 189 | for( i = 1; ! alarmed; i++ ) |
| 190 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 191 | |
| 192 | tsc = hardclock(); |
| 193 | for( j = 0; j < 1024; j++ ) |
| 194 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 195 | |
| 196 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 197 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 198 | |
| 199 | printf( " DES : " ); |
| 200 | fflush( stdout ); |
| 201 | |
| 202 | des_setkey_enc( &des, tmp ); |
| 203 | |
| 204 | set_alarm( 1 ); |
| 205 | for( i = 1; ! alarmed; i++ ) |
| 206 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 207 | |
| 208 | tsc = hardclock(); |
| 209 | for( j = 0; j < 1024; j++ ) |
| 210 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 211 | |
| 212 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 213 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 214 | #endif |
| 215 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 216 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 217 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 218 | { |
| 219 | printf( " AES-%d : ", keysize ); |
| 220 | fflush( stdout ); |
| 221 | |
| 222 | memset( buf, 0, sizeof( buf ) ); |
| 223 | memset( tmp, 0, sizeof( tmp ) ); |
| 224 | aes_setkey_enc( &aes, tmp, keysize ); |
| 225 | |
| 226 | set_alarm( 1 ); |
| 227 | |
| 228 | for( i = 1; ! alarmed; i++ ) |
| 229 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 230 | |
| 231 | tsc = hardclock(); |
| 232 | for( j = 0; j < 4096; j++ ) |
| 233 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 234 | |
| 235 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 236 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 237 | } |
| 238 | #endif |
| 239 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 240 | #if defined(POLARSSL_CAMELLIA_C) |
| 241 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 242 | { |
| 243 | printf( " CAMELLIA-%d : ", keysize ); |
| 244 | fflush( stdout ); |
| 245 | |
| 246 | memset( buf, 0, sizeof( buf ) ); |
| 247 | memset( tmp, 0, sizeof( tmp ) ); |
| 248 | camellia_setkey_enc( &camellia, tmp, keysize ); |
| 249 | |
| 250 | set_alarm( 1 ); |
| 251 | |
| 252 | for( i = 1; ! alarmed; i++ ) |
| 253 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 254 | |
| 255 | tsc = hardclock(); |
| 256 | for( j = 0; j < 4096; j++ ) |
| 257 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 258 | |
| 259 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 260 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 261 | } |
| 262 | #endif |
| 263 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 264 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL ); |
| 266 | rsa_gen_key( &rsa, 1024, 65537 ); |
| 267 | |
| 268 | printf( " RSA-1024 : " ); |
| 269 | fflush( stdout ); |
| 270 | set_alarm( 3 ); |
| 271 | |
| 272 | for( i = 1; ! alarmed; i++ ) |
| 273 | { |
| 274 | buf[0] = 0; |
| 275 | rsa_public( &rsa, buf, buf ); |
| 276 | } |
| 277 | |
| 278 | printf( "%9lu public/s\n", i / 3 ); |
| 279 | |
| 280 | printf( " RSA-1024 : " ); |
| 281 | fflush( stdout ); |
| 282 | set_alarm( 3 ); |
| 283 | |
| 284 | for( i = 1; ! alarmed; i++ ) |
| 285 | { |
| 286 | buf[0] = 0; |
| 287 | rsa_private( &rsa, buf, buf ); |
| 288 | } |
| 289 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 290 | printf( "%9lu private/s\n", i / 3 ); |
| 291 | |
| 292 | rsa_free( &rsa ); |
| 293 | |
| 294 | rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL ); |
| 295 | rsa_gen_key( &rsa, 2048, 65537 ); |
| 296 | |
| 297 | printf( " RSA-2048 : " ); |
| 298 | fflush( stdout ); |
| 299 | set_alarm( 3 ); |
| 300 | |
| 301 | for( i = 1; ! alarmed; i++ ) |
| 302 | { |
| 303 | buf[0] = 0; |
| 304 | rsa_public( &rsa, buf, buf ); |
| 305 | } |
| 306 | |
| 307 | printf( "%9lu public/s\n", i / 3 ); |
| 308 | |
| 309 | printf( " RSA-2048 : " ); |
| 310 | fflush( stdout ); |
| 311 | set_alarm( 3 ); |
| 312 | |
| 313 | for( i = 1; ! alarmed; i++ ) |
| 314 | { |
| 315 | buf[0] = 0; |
| 316 | rsa_private( &rsa, buf, buf ); |
| 317 | } |
| 318 | |
| 319 | printf( "%9lu private/s\n", i / 3 ); |
| 320 | |
| 321 | rsa_free( &rsa ); |
| 322 | |
| 323 | rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL ); |
| 324 | rsa_gen_key( &rsa, 4096, 65537 ); |
| 325 | |
| 326 | printf( " RSA-4096 : " ); |
| 327 | fflush( stdout ); |
| 328 | set_alarm( 3 ); |
| 329 | |
| 330 | for( i = 1; ! alarmed; i++ ) |
| 331 | { |
| 332 | buf[0] = 0; |
| 333 | rsa_public( &rsa, buf, buf ); |
| 334 | } |
| 335 | |
| 336 | printf( "%9lu public/s\n", i / 3 ); |
| 337 | |
| 338 | printf( " RSA-4096 : " ); |
| 339 | fflush( stdout ); |
| 340 | set_alarm( 3 ); |
| 341 | |
| 342 | for( i = 1; ! alarmed; i++ ) |
| 343 | { |
| 344 | buf[0] = 0; |
| 345 | rsa_private( &rsa, buf, buf ); |
| 346 | } |
| 347 | |
| 348 | printf( "%9lu private/s\n", i / 3 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 349 | |
| 350 | rsa_free( &rsa ); |
| 351 | #endif |
| 352 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 353 | printf( "\n" ); |
| 354 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 355 | #ifdef WIN32 |
| 356 | printf( " Press Enter to exit this program.\n" ); |
| 357 | fflush( stdout ); getchar(); |
| 358 | #endif |
| 359 | |
| 360 | return( 0 ); |
| 361 | } |