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