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 | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame^] | 45 | #include "polarssl/gcm.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 46 | #include "polarssl/rsa.h" |
| 47 | #include "polarssl/timing.h" |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 48 | #include "polarssl/havege.h" |
| 49 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 51 | #define BUFSIZE 1024 |
| 52 | #define HEADER_FORMAT " %-15s : " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 54 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 56 | size_t use_len; |
| 57 | int rnd; |
| 58 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | if( rng_state != NULL ) |
| 60 | rng_state = NULL; |
| 61 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 62 | while( len > 0 ) |
| 63 | { |
| 64 | use_len = len; |
| 65 | if( use_len > sizeof(int) ) |
| 66 | use_len = sizeof(int); |
| 67 | |
| 68 | rnd = rand(); |
| 69 | memcpy( output, &rnd, use_len ); |
| 70 | output += use_len; |
| 71 | len -= use_len; |
| 72 | } |
| 73 | |
| 74 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | unsigned char buf[BUFSIZE]; |
| 78 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 79 | #if !defined(POLARSSL_TIMING_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 80 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 81 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 82 | ((void) argc); |
| 83 | ((void) argv); |
| 84 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 85 | printf("POLARSSL_TIMING_C not defined.\n"); |
| 86 | return( 0 ); |
| 87 | } |
| 88 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 89 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | { |
| 91 | int keysize; |
| 92 | unsigned long i, j, tsc; |
Paul Bakker | 5a0aa77 | 2009-02-09 22:38:52 +0000 | [diff] [blame] | 93 | unsigned char tmp[64]; |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 94 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | arc4_context arc4; |
| 96 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 97 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | des3_context des3; |
| 99 | des_context des; |
| 100 | #endif |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 101 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | aes_context aes; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame^] | 103 | #if defined(POLARSSL_GCM_C) |
| 104 | gcm_context gcm; |
| 105 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | #endif |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 107 | #if defined(POLARSSL_CAMELLIA_C) |
| 108 | camellia_context camellia; |
| 109 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 110 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ |
| 111 | defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | rsa_context rsa; |
| 113 | #endif |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 114 | #if defined(POLARSSL_HAVEGE_C) |
| 115 | havege_state hs; |
| 116 | #endif |
| 117 | #if defined(POLARSSL_CTR_DRBG_C) |
| 118 | ctr_drbg_context ctr_drbg; |
| 119 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 120 | ((void) argc); |
| 121 | ((void) argv); |
| 122 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | memset( buf, 0xAA, sizeof( buf ) ); |
| 124 | |
| 125 | printf( "\n" ); |
| 126 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 127 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 128 | printf( HEADER_FORMAT, "MD4" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | fflush( stdout ); |
| 130 | |
| 131 | set_alarm( 1 ); |
| 132 | for( i = 1; ! alarmed; i++ ) |
| 133 | md4( buf, BUFSIZE, tmp ); |
| 134 | |
| 135 | tsc = hardclock(); |
| 136 | for( j = 0; j < 1024; j++ ) |
| 137 | md4( buf, BUFSIZE, tmp ); |
| 138 | |
| 139 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 140 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 141 | #endif |
| 142 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 143 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 144 | printf( HEADER_FORMAT, "MD5" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | fflush( stdout ); |
| 146 | |
| 147 | set_alarm( 1 ); |
| 148 | for( i = 1; ! alarmed; i++ ) |
| 149 | md5( buf, BUFSIZE, tmp ); |
| 150 | |
| 151 | tsc = hardclock(); |
| 152 | for( j = 0; j < 1024; j++ ) |
| 153 | md5( buf, BUFSIZE, tmp ); |
| 154 | |
| 155 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 156 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 157 | #endif |
| 158 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 159 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 160 | printf( HEADER_FORMAT, "SHA-1" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | fflush( stdout ); |
| 162 | |
| 163 | set_alarm( 1 ); |
| 164 | for( i = 1; ! alarmed; i++ ) |
| 165 | sha1( buf, BUFSIZE, tmp ); |
| 166 | |
| 167 | tsc = hardclock(); |
| 168 | for( j = 0; j < 1024; j++ ) |
| 169 | sha1( buf, BUFSIZE, tmp ); |
| 170 | |
| 171 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 172 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 173 | #endif |
| 174 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 175 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 176 | printf( HEADER_FORMAT, "SHA-256" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | fflush( stdout ); |
| 178 | |
| 179 | set_alarm( 1 ); |
| 180 | for( i = 1; ! alarmed; i++ ) |
| 181 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 182 | |
| 183 | tsc = hardclock(); |
| 184 | for( j = 0; j < 1024; j++ ) |
| 185 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 186 | |
| 187 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 188 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 189 | #endif |
| 190 | |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 191 | #if defined(POLARSSL_SHA4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 192 | printf( HEADER_FORMAT, "SHA-512" ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 193 | fflush( stdout ); |
| 194 | |
| 195 | set_alarm( 1 ); |
| 196 | for( i = 1; ! alarmed; i++ ) |
| 197 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 198 | |
| 199 | tsc = hardclock(); |
| 200 | for( j = 0; j < 1024; j++ ) |
| 201 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 202 | |
| 203 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 204 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 205 | #endif |
| 206 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 207 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 208 | printf( HEADER_FORMAT, "ARC4" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | fflush( stdout ); |
| 210 | |
| 211 | arc4_setup( &arc4, tmp, 32 ); |
| 212 | |
| 213 | set_alarm( 1 ); |
| 214 | for( i = 1; ! alarmed; i++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 215 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | |
| 217 | tsc = hardclock(); |
| 218 | for( j = 0; j < 1024; j++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 219 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | |
| 221 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 222 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 223 | #endif |
| 224 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 225 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 226 | printf( HEADER_FORMAT, "3DES" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | fflush( stdout ); |
| 228 | |
| 229 | des3_set3key_enc( &des3, tmp ); |
| 230 | |
| 231 | set_alarm( 1 ); |
| 232 | for( i = 1; ! alarmed; i++ ) |
| 233 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 234 | |
| 235 | tsc = hardclock(); |
| 236 | for( j = 0; j < 1024; j++ ) |
| 237 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 238 | |
| 239 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 240 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 241 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 242 | printf( HEADER_FORMAT, "DES" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | fflush( stdout ); |
| 244 | |
| 245 | des_setkey_enc( &des, tmp ); |
| 246 | |
| 247 | set_alarm( 1 ); |
| 248 | for( i = 1; ! alarmed; i++ ) |
| 249 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 250 | |
| 251 | tsc = hardclock(); |
| 252 | for( j = 0; j < 1024; j++ ) |
| 253 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 254 | |
| 255 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 256 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 257 | #endif |
| 258 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 259 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 261 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame^] | 262 | printf( " AES-CBC-%d : ", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 263 | fflush( stdout ); |
| 264 | |
| 265 | memset( buf, 0, sizeof( buf ) ); |
| 266 | memset( tmp, 0, sizeof( tmp ) ); |
| 267 | aes_setkey_enc( &aes, tmp, keysize ); |
| 268 | |
| 269 | set_alarm( 1 ); |
| 270 | |
| 271 | for( i = 1; ! alarmed; i++ ) |
| 272 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 273 | |
| 274 | tsc = hardclock(); |
| 275 | for( j = 0; j < 4096; j++ ) |
| 276 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 277 | |
| 278 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 279 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 280 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame^] | 281 | #if defined(POLARSSL_GCM_C) |
| 282 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 283 | { |
| 284 | printf( " AES-GCM-%d : ", keysize ); |
| 285 | fflush( stdout ); |
| 286 | |
| 287 | memset( buf, 0, sizeof( buf ) ); |
| 288 | memset( tmp, 0, sizeof( tmp ) ); |
| 289 | gcm_init( &gcm, tmp, keysize ); |
| 290 | |
| 291 | set_alarm( 1 ); |
| 292 | |
| 293 | for( i = 1; ! alarmed; i++ ) |
| 294 | gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 16, NULL, 0, buf, buf, 16, tmp ); |
| 295 | |
| 296 | tsc = hardclock(); |
| 297 | for( j = 0; j < 4096; j++ ) |
| 298 | gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 16, NULL, 0, buf, buf, 16, tmp ); |
| 299 | |
| 300 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 301 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 302 | } |
| 303 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 304 | #endif |
| 305 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 306 | #if defined(POLARSSL_CAMELLIA_C) |
| 307 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 308 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame^] | 309 | printf( " CAMELLIA-CBC-%d: ", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 310 | fflush( stdout ); |
| 311 | |
| 312 | memset( buf, 0, sizeof( buf ) ); |
| 313 | memset( tmp, 0, sizeof( tmp ) ); |
| 314 | camellia_setkey_enc( &camellia, tmp, keysize ); |
| 315 | |
| 316 | set_alarm( 1 ); |
| 317 | |
| 318 | for( i = 1; ! alarmed; i++ ) |
| 319 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 320 | |
| 321 | tsc = hardclock(); |
| 322 | for( j = 0; j < 4096; j++ ) |
| 323 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 324 | |
| 325 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 326 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 327 | } |
| 328 | #endif |
| 329 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 330 | #if defined(POLARSSL_HAVEGE_C) |
| 331 | printf( HEADER_FORMAT, "HAVEGE" ); |
| 332 | fflush( stdout ); |
| 333 | |
| 334 | havege_init( &hs ); |
| 335 | |
| 336 | set_alarm( 1 ); |
| 337 | for( i = 1; ! alarmed; i++ ) |
| 338 | havege_random( &hs, buf, BUFSIZE ); |
| 339 | |
| 340 | tsc = hardclock(); |
| 341 | for( j = 1; j < 1024; j++ ) |
| 342 | havege_random( &hs, buf, BUFSIZE ); |
| 343 | |
| 344 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 345 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 346 | #endif |
| 347 | |
| 348 | #if defined(POLARSSL_CTR_DRBG_C) |
| 349 | printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" ); |
| 350 | fflush( stdout ); |
| 351 | |
| 352 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 353 | exit(1); |
| 354 | |
| 355 | set_alarm( 1 ); |
| 356 | for( i = 1; ! alarmed; i++ ) |
| 357 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 358 | exit(1); |
| 359 | |
| 360 | tsc = hardclock(); |
| 361 | for( j = 1; j < 1024; j++ ) |
| 362 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 363 | exit(1); |
| 364 | |
| 365 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 366 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 367 | |
| 368 | printf( HEADER_FORMAT, "CTR_DRBG (PR)" ); |
| 369 | fflush( stdout ); |
| 370 | |
| 371 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 372 | exit(1); |
| 373 | |
| 374 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); |
| 375 | |
| 376 | set_alarm( 1 ); |
| 377 | for( i = 1; ! alarmed; i++ ) |
| 378 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 379 | exit(1); |
| 380 | |
| 381 | tsc = hardclock(); |
| 382 | for( j = 1; j < 1024; j++ ) |
| 383 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 384 | exit(1); |
| 385 | |
| 386 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 387 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 388 | #endif |
| 389 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 390 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ |
| 391 | defined(POLARSSL_GENPRIME) |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 392 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 393 | rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 395 | printf( HEADER_FORMAT, "RSA-1024" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 396 | fflush( stdout ); |
| 397 | set_alarm( 3 ); |
| 398 | |
| 399 | for( i = 1; ! alarmed; i++ ) |
| 400 | { |
| 401 | buf[0] = 0; |
| 402 | rsa_public( &rsa, buf, buf ); |
| 403 | } |
| 404 | |
| 405 | printf( "%9lu public/s\n", i / 3 ); |
| 406 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 407 | printf( HEADER_FORMAT, "RSA-1024" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | fflush( stdout ); |
| 409 | set_alarm( 3 ); |
| 410 | |
| 411 | for( i = 1; ! alarmed; i++ ) |
| 412 | { |
| 413 | buf[0] = 0; |
| 414 | rsa_private( &rsa, buf, buf ); |
| 415 | } |
| 416 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 417 | printf( "%9lu private/s\n", i / 3 ); |
| 418 | |
| 419 | rsa_free( &rsa ); |
| 420 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 421 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 422 | rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 423 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 424 | printf( HEADER_FORMAT, "RSA-2048" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 425 | fflush( stdout ); |
| 426 | set_alarm( 3 ); |
| 427 | |
| 428 | for( i = 1; ! alarmed; i++ ) |
| 429 | { |
| 430 | buf[0] = 0; |
| 431 | rsa_public( &rsa, buf, buf ); |
| 432 | } |
| 433 | |
| 434 | printf( "%9lu public/s\n", i / 3 ); |
| 435 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 436 | printf( HEADER_FORMAT, "RSA-2048" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 437 | fflush( stdout ); |
| 438 | set_alarm( 3 ); |
| 439 | |
| 440 | for( i = 1; ! alarmed; i++ ) |
| 441 | { |
| 442 | buf[0] = 0; |
| 443 | rsa_private( &rsa, buf, buf ); |
| 444 | } |
| 445 | |
| 446 | printf( "%9lu private/s\n", i / 3 ); |
| 447 | |
| 448 | rsa_free( &rsa ); |
| 449 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 450 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 451 | rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 452 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 453 | printf( HEADER_FORMAT, "RSA-4096" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 454 | fflush( stdout ); |
| 455 | set_alarm( 3 ); |
| 456 | |
| 457 | for( i = 1; ! alarmed; i++ ) |
| 458 | { |
| 459 | buf[0] = 0; |
| 460 | rsa_public( &rsa, buf, buf ); |
| 461 | } |
| 462 | |
| 463 | printf( "%9lu public/s\n", i / 3 ); |
| 464 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 465 | printf( HEADER_FORMAT, "RSA-4096" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 466 | fflush( stdout ); |
| 467 | set_alarm( 3 ); |
| 468 | |
| 469 | for( i = 1; ! alarmed; i++ ) |
| 470 | { |
| 471 | buf[0] = 0; |
| 472 | rsa_private( &rsa, buf, buf ); |
| 473 | } |
| 474 | |
| 475 | printf( "%9lu private/s\n", i / 3 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 476 | |
| 477 | rsa_free( &rsa ); |
| 478 | #endif |
| 479 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 480 | printf( "\n" ); |
| 481 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 482 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 483 | printf( " Press Enter to exit this program.\n" ); |
| 484 | fflush( stdout ); getchar(); |
| 485 | #endif |
| 486 | |
| 487 | return( 0 ); |
| 488 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 489 | #endif /* POLARSSL_TIMING_C */ |