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