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