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