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