Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
| 6 | * Copyright (C) 2009 Paul Bakker |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 33 | #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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
| 43 | #define BUFSIZE 1024 |
| 44 | |
| 45 | static int myrand( void *rng_state ) |
| 46 | { |
| 47 | if( rng_state != NULL ) |
| 48 | rng_state = NULL; |
| 49 | |
| 50 | return( rand() ); |
| 51 | } |
| 52 | |
| 53 | unsigned char buf[BUFSIZE]; |
| 54 | |
| 55 | int main( void ) |
| 56 | { |
| 57 | int keysize; |
| 58 | unsigned long i, j, tsc; |
| 59 | unsigned char tmp[32]; |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 60 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | arc4_context arc4; |
| 62 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 63 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | des3_context des3; |
| 65 | des_context des; |
| 66 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 67 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | aes_context aes; |
| 69 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 70 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | rsa_context rsa; |
| 72 | #endif |
| 73 | |
| 74 | memset( buf, 0xAA, sizeof( buf ) ); |
| 75 | |
| 76 | printf( "\n" ); |
| 77 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 78 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 94 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 110 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 126 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 142 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 160 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 194 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 218 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | 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 | } |