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