Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2011, 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) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 61 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 62 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 63 | ((void) argc); |
| 64 | ((void) argv); |
| 65 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 66 | printf("POLARSSL_TIMING_C not defined.\n"); |
| 67 | return( 0 ); |
| 68 | } |
| 69 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 70 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | { |
| 72 | int keysize; |
| 73 | unsigned long i, j, tsc; |
Paul Bakker | 5a0aa77 | 2009-02-09 22:38:52 +0000 | [diff] [blame] | 74 | unsigned char tmp[64]; |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 75 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | arc4_context arc4; |
| 77 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 78 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | des3_context des3; |
| 80 | des_context des; |
| 81 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 82 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | aes_context aes; |
| 84 | #endif |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 85 | #if defined(POLARSSL_CAMELLIA_C) |
| 86 | camellia_context camellia; |
| 87 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 88 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ |
| 89 | defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | rsa_context rsa; |
| 91 | #endif |
| 92 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 93 | ((void) argc); |
| 94 | ((void) argv); |
| 95 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | memset( buf, 0xAA, sizeof( buf ) ); |
| 97 | |
| 98 | printf( "\n" ); |
| 99 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 100 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | printf( " MD4 : " ); |
| 102 | fflush( stdout ); |
| 103 | |
| 104 | set_alarm( 1 ); |
| 105 | for( i = 1; ! alarmed; i++ ) |
| 106 | md4( buf, BUFSIZE, tmp ); |
| 107 | |
| 108 | tsc = hardclock(); |
| 109 | for( j = 0; j < 1024; j++ ) |
| 110 | md4( 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_MD5_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | printf( " MD5 : " ); |
| 118 | fflush( stdout ); |
| 119 | |
| 120 | set_alarm( 1 ); |
| 121 | for( i = 1; ! alarmed; i++ ) |
| 122 | md5( buf, BUFSIZE, tmp ); |
| 123 | |
| 124 | tsc = hardclock(); |
| 125 | for( j = 0; j < 1024; j++ ) |
| 126 | md5( 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_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | printf( " SHA-1 : " ); |
| 134 | fflush( stdout ); |
| 135 | |
| 136 | set_alarm( 1 ); |
| 137 | for( i = 1; ! alarmed; i++ ) |
| 138 | sha1( buf, BUFSIZE, tmp ); |
| 139 | |
| 140 | tsc = hardclock(); |
| 141 | for( j = 0; j < 1024; j++ ) |
| 142 | sha1( buf, BUFSIZE, tmp ); |
| 143 | |
| 144 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 145 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 146 | #endif |
| 147 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 148 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | printf( " SHA-256 : " ); |
| 150 | fflush( stdout ); |
| 151 | |
| 152 | set_alarm( 1 ); |
| 153 | for( i = 1; ! alarmed; i++ ) |
| 154 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 155 | |
| 156 | tsc = hardclock(); |
| 157 | for( j = 0; j < 1024; j++ ) |
| 158 | sha2( 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 | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 164 | #if defined(POLARSSL_SHA4_C) |
| 165 | printf( " SHA-512 : " ); |
| 166 | fflush( stdout ); |
| 167 | |
| 168 | set_alarm( 1 ); |
| 169 | for( i = 1; ! alarmed; i++ ) |
| 170 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 171 | |
| 172 | tsc = hardclock(); |
| 173 | for( j = 0; j < 1024; j++ ) |
| 174 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 175 | |
| 176 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 177 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 178 | #endif |
| 179 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 180 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | printf( " ARC4 : " ); |
| 182 | fflush( stdout ); |
| 183 | |
| 184 | arc4_setup( &arc4, tmp, 32 ); |
| 185 | |
| 186 | set_alarm( 1 ); |
| 187 | for( i = 1; ! alarmed; i++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 188 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | |
| 190 | tsc = hardclock(); |
| 191 | for( j = 0; j < 1024; j++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 192 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | |
| 194 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 195 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 196 | #endif |
| 197 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 198 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 199 | printf( " 3DES : " ); |
| 200 | fflush( stdout ); |
| 201 | |
| 202 | des3_set3key_enc( &des3, tmp ); |
| 203 | |
| 204 | set_alarm( 1 ); |
| 205 | for( i = 1; ! alarmed; i++ ) |
| 206 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 207 | |
| 208 | tsc = hardclock(); |
| 209 | for( j = 0; j < 1024; j++ ) |
| 210 | des3_crypt_cbc( &des3, 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 | |
| 215 | printf( " DES : " ); |
| 216 | fflush( stdout ); |
| 217 | |
| 218 | des_setkey_enc( &des, tmp ); |
| 219 | |
| 220 | set_alarm( 1 ); |
| 221 | for( i = 1; ! alarmed; i++ ) |
| 222 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 223 | |
| 224 | tsc = hardclock(); |
| 225 | for( j = 0; j < 1024; j++ ) |
| 226 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 227 | |
| 228 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 229 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 230 | #endif |
| 231 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 232 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 234 | { |
| 235 | printf( " AES-%d : ", keysize ); |
| 236 | fflush( stdout ); |
| 237 | |
| 238 | memset( buf, 0, sizeof( buf ) ); |
| 239 | memset( tmp, 0, sizeof( tmp ) ); |
| 240 | aes_setkey_enc( &aes, tmp, keysize ); |
| 241 | |
| 242 | set_alarm( 1 ); |
| 243 | |
| 244 | for( i = 1; ! alarmed; i++ ) |
| 245 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 246 | |
| 247 | tsc = hardclock(); |
| 248 | for( j = 0; j < 4096; j++ ) |
| 249 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 250 | |
| 251 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 252 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 253 | } |
| 254 | #endif |
| 255 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 256 | #if defined(POLARSSL_CAMELLIA_C) |
| 257 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 258 | { |
| 259 | printf( " CAMELLIA-%d : ", keysize ); |
| 260 | fflush( stdout ); |
| 261 | |
| 262 | memset( buf, 0, sizeof( buf ) ); |
| 263 | memset( tmp, 0, sizeof( tmp ) ); |
| 264 | camellia_setkey_enc( &camellia, tmp, keysize ); |
| 265 | |
| 266 | set_alarm( 1 ); |
| 267 | |
| 268 | for( i = 1; ! alarmed; i++ ) |
| 269 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 270 | |
| 271 | tsc = hardclock(); |
| 272 | for( j = 0; j < 4096; j++ ) |
| 273 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 274 | |
| 275 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 276 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 277 | } |
| 278 | #endif |
| 279 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 280 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ |
| 281 | defined(POLARSSL_GENPRIME) |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 282 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 283 | rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | |
| 285 | printf( " RSA-1024 : " ); |
| 286 | fflush( stdout ); |
| 287 | set_alarm( 3 ); |
| 288 | |
| 289 | for( i = 1; ! alarmed; i++ ) |
| 290 | { |
| 291 | buf[0] = 0; |
| 292 | rsa_public( &rsa, buf, buf ); |
| 293 | } |
| 294 | |
| 295 | printf( "%9lu public/s\n", i / 3 ); |
| 296 | |
| 297 | printf( " RSA-1024 : " ); |
| 298 | fflush( stdout ); |
| 299 | set_alarm( 3 ); |
| 300 | |
| 301 | for( i = 1; ! alarmed; i++ ) |
| 302 | { |
| 303 | buf[0] = 0; |
| 304 | rsa_private( &rsa, buf, buf ); |
| 305 | } |
| 306 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 307 | printf( "%9lu private/s\n", i / 3 ); |
| 308 | |
| 309 | rsa_free( &rsa ); |
| 310 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 311 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 312 | rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 313 | |
| 314 | printf( " RSA-2048 : " ); |
| 315 | fflush( stdout ); |
| 316 | set_alarm( 3 ); |
| 317 | |
| 318 | for( i = 1; ! alarmed; i++ ) |
| 319 | { |
| 320 | buf[0] = 0; |
| 321 | rsa_public( &rsa, buf, buf ); |
| 322 | } |
| 323 | |
| 324 | printf( "%9lu public/s\n", i / 3 ); |
| 325 | |
| 326 | printf( " RSA-2048 : " ); |
| 327 | fflush( stdout ); |
| 328 | set_alarm( 3 ); |
| 329 | |
| 330 | for( i = 1; ! alarmed; i++ ) |
| 331 | { |
| 332 | buf[0] = 0; |
| 333 | rsa_private( &rsa, buf, buf ); |
| 334 | } |
| 335 | |
| 336 | printf( "%9lu private/s\n", i / 3 ); |
| 337 | |
| 338 | rsa_free( &rsa ); |
| 339 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 340 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 341 | rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 342 | |
| 343 | printf( " RSA-4096 : " ); |
| 344 | fflush( stdout ); |
| 345 | set_alarm( 3 ); |
| 346 | |
| 347 | for( i = 1; ! alarmed; i++ ) |
| 348 | { |
| 349 | buf[0] = 0; |
| 350 | rsa_public( &rsa, buf, buf ); |
| 351 | } |
| 352 | |
| 353 | printf( "%9lu public/s\n", i / 3 ); |
| 354 | |
| 355 | printf( " RSA-4096 : " ); |
| 356 | fflush( stdout ); |
| 357 | set_alarm( 3 ); |
| 358 | |
| 359 | for( i = 1; ! alarmed; i++ ) |
| 360 | { |
| 361 | buf[0] = 0; |
| 362 | rsa_private( &rsa, buf, buf ); |
| 363 | } |
| 364 | |
| 365 | printf( "%9lu private/s\n", i / 3 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 366 | |
| 367 | rsa_free( &rsa ); |
| 368 | #endif |
| 369 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 370 | printf( "\n" ); |
| 371 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame^] | 372 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 373 | printf( " Press Enter to exit this program.\n" ); |
| 374 | fflush( stdout ); getchar(); |
| 375 | #endif |
| 376 | |
| 377 | return( 0 ); |
| 378 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 379 | #endif /* POLARSSL_TIMING_C */ |