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 ) ); |
Paul Bakker | d6d1f41 | 2014-04-17 16:03:48 +0200 | [diff] [blame^] | 128 | memset( tmp, 0xBB, sizeof( tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | |
| 130 | printf( "\n" ); |
| 131 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 132 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 133 | printf( HEADER_FORMAT, "MD4" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | fflush( stdout ); |
| 135 | |
| 136 | set_alarm( 1 ); |
| 137 | for( i = 1; ! alarmed; i++ ) |
| 138 | md4( buf, BUFSIZE, tmp ); |
| 139 | |
| 140 | tsc = hardclock(); |
| 141 | for( j = 0; j < 1024; j++ ) |
| 142 | md4( buf, BUFSIZE, tmp ); |
| 143 | |
| 144 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 145 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 146 | #endif |
| 147 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 148 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 149 | printf( HEADER_FORMAT, "MD5" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | fflush( stdout ); |
| 151 | |
| 152 | set_alarm( 1 ); |
| 153 | for( i = 1; ! alarmed; i++ ) |
| 154 | md5( buf, BUFSIZE, tmp ); |
| 155 | |
| 156 | tsc = hardclock(); |
| 157 | for( j = 0; j < 1024; j++ ) |
| 158 | md5( buf, BUFSIZE, tmp ); |
| 159 | |
| 160 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 161 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 162 | #endif |
| 163 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 164 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 165 | printf( HEADER_FORMAT, "SHA-1" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | fflush( stdout ); |
| 167 | |
| 168 | set_alarm( 1 ); |
| 169 | for( i = 1; ! alarmed; i++ ) |
| 170 | sha1( buf, BUFSIZE, tmp ); |
| 171 | |
| 172 | tsc = hardclock(); |
| 173 | for( j = 0; j < 1024; j++ ) |
| 174 | sha1( buf, BUFSIZE, tmp ); |
| 175 | |
| 176 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 177 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 178 | #endif |
| 179 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 180 | #if defined(POLARSSL_SHA2_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 181 | printf( HEADER_FORMAT, "SHA-256" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | fflush( stdout ); |
| 183 | |
| 184 | set_alarm( 1 ); |
| 185 | for( i = 1; ! alarmed; i++ ) |
| 186 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 187 | |
| 188 | tsc = hardclock(); |
| 189 | for( j = 0; j < 1024; j++ ) |
| 190 | sha2( buf, BUFSIZE, tmp, 0 ); |
| 191 | |
| 192 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 193 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 194 | #endif |
| 195 | |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 196 | #if defined(POLARSSL_SHA4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 197 | printf( HEADER_FORMAT, "SHA-512" ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 198 | fflush( stdout ); |
| 199 | |
| 200 | set_alarm( 1 ); |
| 201 | for( i = 1; ! alarmed; i++ ) |
| 202 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 203 | |
| 204 | tsc = hardclock(); |
| 205 | for( j = 0; j < 1024; j++ ) |
| 206 | sha4( buf, BUFSIZE, tmp, 0 ); |
| 207 | |
| 208 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 209 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 210 | #endif |
| 211 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 212 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 213 | printf( HEADER_FORMAT, "ARC4" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | fflush( stdout ); |
| 215 | |
| 216 | arc4_setup( &arc4, tmp, 32 ); |
| 217 | |
| 218 | set_alarm( 1 ); |
| 219 | for( i = 1; ! alarmed; i++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 220 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | |
| 222 | tsc = hardclock(); |
| 223 | for( j = 0; j < 1024; j++ ) |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 224 | arc4_crypt( &arc4, BUFSIZE, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | |
| 226 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 227 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 228 | #endif |
| 229 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 230 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 231 | printf( HEADER_FORMAT, "3DES" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | fflush( stdout ); |
| 233 | |
| 234 | des3_set3key_enc( &des3, tmp ); |
| 235 | |
| 236 | set_alarm( 1 ); |
| 237 | for( i = 1; ! alarmed; i++ ) |
| 238 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 239 | |
| 240 | tsc = hardclock(); |
| 241 | for( j = 0; j < 1024; j++ ) |
| 242 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 243 | |
| 244 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 245 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 246 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 247 | printf( HEADER_FORMAT, "DES" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 248 | fflush( stdout ); |
| 249 | |
| 250 | des_setkey_enc( &des, tmp ); |
| 251 | |
| 252 | set_alarm( 1 ); |
| 253 | for( i = 1; ! alarmed; i++ ) |
| 254 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 255 | |
| 256 | tsc = hardclock(); |
| 257 | for( j = 0; j < 1024; j++ ) |
| 258 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 259 | |
| 260 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 261 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 262 | #endif |
| 263 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 264 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 266 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 267 | printf( " AES-CBC-%d : ", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | fflush( stdout ); |
| 269 | |
| 270 | memset( buf, 0, sizeof( buf ) ); |
| 271 | memset( tmp, 0, sizeof( tmp ) ); |
| 272 | aes_setkey_enc( &aes, tmp, keysize ); |
| 273 | |
| 274 | set_alarm( 1 ); |
| 275 | |
| 276 | for( i = 1; ! alarmed; i++ ) |
| 277 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 278 | |
| 279 | tsc = hardclock(); |
| 280 | for( j = 0; j < 4096; j++ ) |
| 281 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 282 | |
| 283 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 284 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 285 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 286 | #if defined(POLARSSL_GCM_C) |
| 287 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 288 | { |
| 289 | printf( " AES-GCM-%d : ", keysize ); |
| 290 | fflush( stdout ); |
| 291 | |
| 292 | memset( buf, 0, sizeof( buf ) ); |
| 293 | memset( tmp, 0, sizeof( tmp ) ); |
| 294 | gcm_init( &gcm, tmp, keysize ); |
| 295 | |
| 296 | set_alarm( 1 ); |
| 297 | |
| 298 | for( i = 1; ! alarmed; i++ ) |
Paul Bakker | b78c745 | 2012-03-20 15:05:59 +0000 | [diff] [blame] | 299 | 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] | 300 | |
| 301 | tsc = hardclock(); |
| 302 | for( j = 0; j < 4096; j++ ) |
Paul Bakker | b78c745 | 2012-03-20 15:05:59 +0000 | [diff] [blame] | 303 | 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] | 304 | |
| 305 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 306 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 307 | } |
| 308 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 311 | #if defined(POLARSSL_CAMELLIA_C) |
| 312 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 313 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 314 | printf( " CAMELLIA-CBC-%d: ", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 315 | fflush( stdout ); |
| 316 | |
| 317 | memset( buf, 0, sizeof( buf ) ); |
| 318 | memset( tmp, 0, sizeof( tmp ) ); |
| 319 | camellia_setkey_enc( &camellia, tmp, keysize ); |
| 320 | |
| 321 | set_alarm( 1 ); |
| 322 | |
| 323 | for( i = 1; ! alarmed; i++ ) |
| 324 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 325 | |
| 326 | tsc = hardclock(); |
| 327 | for( j = 0; j < 4096; j++ ) |
| 328 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 329 | |
| 330 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 331 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 332 | } |
| 333 | #endif |
| 334 | |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 335 | #if defined(POLARSSL_BLOWFISH_C) |
| 336 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 337 | { |
| 338 | printf( " BLOWFISH-CBC-%d: ", keysize ); |
| 339 | fflush( stdout ); |
| 340 | |
| 341 | memset( buf, 0, sizeof( buf ) ); |
| 342 | memset( tmp, 0, sizeof( tmp ) ); |
| 343 | blowfish_setkey( &blowfish, tmp, keysize ); |
| 344 | |
| 345 | set_alarm( 1 ); |
| 346 | |
| 347 | for( i = 1; ! alarmed; i++ ) |
| 348 | blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 349 | |
| 350 | tsc = hardclock(); |
| 351 | for( j = 0; j < 4096; j++ ) |
| 352 | blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf ); |
| 353 | |
| 354 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 355 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 356 | } |
| 357 | #endif |
| 358 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 359 | #if defined(POLARSSL_HAVEGE_C) |
| 360 | printf( HEADER_FORMAT, "HAVEGE" ); |
| 361 | fflush( stdout ); |
| 362 | |
| 363 | havege_init( &hs ); |
| 364 | |
| 365 | set_alarm( 1 ); |
| 366 | for( i = 1; ! alarmed; i++ ) |
| 367 | havege_random( &hs, buf, BUFSIZE ); |
| 368 | |
| 369 | tsc = hardclock(); |
| 370 | for( j = 1; j < 1024; j++ ) |
| 371 | havege_random( &hs, buf, BUFSIZE ); |
| 372 | |
| 373 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 374 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 375 | #endif |
| 376 | |
| 377 | #if defined(POLARSSL_CTR_DRBG_C) |
| 378 | printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" ); |
| 379 | fflush( stdout ); |
| 380 | |
| 381 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 382 | exit(1); |
| 383 | |
| 384 | set_alarm( 1 ); |
| 385 | for( i = 1; ! alarmed; i++ ) |
| 386 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 387 | exit(1); |
| 388 | |
| 389 | tsc = hardclock(); |
| 390 | for( j = 1; j < 1024; j++ ) |
| 391 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 392 | exit(1); |
| 393 | |
| 394 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 395 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 396 | |
| 397 | printf( HEADER_FORMAT, "CTR_DRBG (PR)" ); |
| 398 | fflush( stdout ); |
| 399 | |
| 400 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 401 | exit(1); |
| 402 | |
| 403 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); |
| 404 | |
| 405 | set_alarm( 1 ); |
| 406 | for( i = 1; ! alarmed; i++ ) |
| 407 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 408 | exit(1); |
| 409 | |
| 410 | tsc = hardclock(); |
| 411 | for( j = 1; j < 1024; j++ ) |
| 412 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 413 | exit(1); |
| 414 | |
| 415 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, |
| 416 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); |
| 417 | #endif |
| 418 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 419 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \ |
| 420 | defined(POLARSSL_GENPRIME) |
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, 1024, 65537 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 423 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 424 | printf( HEADER_FORMAT, "RSA-1024" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +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-1024" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | fflush( stdout ); |
| 438 | set_alarm( 3 ); |
| 439 | |
| 440 | for( i = 1; ! alarmed; i++ ) |
| 441 | { |
| 442 | buf[0] = 0; |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame] | 443 | rsa_private( &rsa, myrand, NULL, buf, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 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, 2048, 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-2048" ); |
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-2048" ); |
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; |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame] | 472 | rsa_private( &rsa, myrand, NULL, buf, buf ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | printf( "%9lu private/s\n", i / 3 ); |
| 476 | |
| 477 | rsa_free( &rsa ); |
| 478 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 479 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 480 | rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 481 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 482 | printf( HEADER_FORMAT, "RSA-4096" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 483 | fflush( stdout ); |
| 484 | set_alarm( 3 ); |
| 485 | |
| 486 | for( i = 1; ! alarmed; i++ ) |
| 487 | { |
| 488 | buf[0] = 0; |
| 489 | rsa_public( &rsa, buf, buf ); |
| 490 | } |
| 491 | |
| 492 | printf( "%9lu public/s\n", i / 3 ); |
| 493 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 494 | printf( HEADER_FORMAT, "RSA-4096" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 495 | fflush( stdout ); |
| 496 | set_alarm( 3 ); |
| 497 | |
| 498 | for( i = 1; ! alarmed; i++ ) |
| 499 | { |
| 500 | buf[0] = 0; |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame] | 501 | rsa_private( &rsa, myrand, NULL, buf, buf ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | printf( "%9lu private/s\n", i / 3 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | |
| 506 | rsa_free( &rsa ); |
| 507 | #endif |
| 508 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 509 | printf( "\n" ); |
| 510 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 511 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 512 | printf( " Press Enter to exit this program.\n" ); |
| 513 | fflush( stdout ); getchar(); |
| 514 | #endif |
| 515 | |
| 516 | return( 0 ); |
| 517 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 518 | #endif /* POLARSSL_TIMING_C */ |