Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, 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" |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 35 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 37 | #include "polarssl/md4.h" |
| 38 | #include "polarssl/md5.h" |
| 39 | #include "polarssl/sha1.h" |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 40 | #include "polarssl/sha256.h" |
| 41 | #include "polarssl/sha512.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 42 | #include "polarssl/arc4.h" |
| 43 | #include "polarssl/des.h" |
| 44 | #include "polarssl/aes.h" |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 45 | #include "polarssl/blowfish.h" |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 46 | #include "polarssl/camellia.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 47 | #include "polarssl/gcm.h" |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 48 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 49 | #include "polarssl/rsa.h" |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 50 | #include "polarssl/dhm.h" |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 51 | #include "polarssl/havege.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 |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 54 | #define HEADER_FORMAT " %-16s : " |
| 55 | #define TITLE_LEN 17 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 57 | #if !defined(POLARSSL_TIMING_C) |
| 58 | int main( int argc, char *argv[] ) |
| 59 | { |
| 60 | ((void) argc); |
| 61 | ((void) argv); |
| 62 | |
| 63 | printf("POLARSSL_TIMING_C not defined.\n"); |
| 64 | return( 0 ); |
| 65 | } |
| 66 | #else |
| 67 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 68 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 70 | size_t use_len; |
| 71 | int rnd; |
| 72 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | if( rng_state != NULL ) |
| 74 | rng_state = NULL; |
| 75 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 76 | while( len > 0 ) |
| 77 | { |
| 78 | use_len = len; |
| 79 | if( use_len > sizeof(int) ) |
| 80 | use_len = sizeof(int); |
| 81 | |
| 82 | rnd = rand(); |
| 83 | memcpy( output, &rnd, use_len ); |
| 84 | output += use_len; |
| 85 | len -= use_len; |
| 86 | } |
| 87 | |
| 88 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 91 | #define TIME_AND_TSC( TITLE, CODE ) \ |
| 92 | do { \ |
| 93 | unsigned long i, j, tsc; \ |
| 94 | \ |
| 95 | printf( HEADER_FORMAT, TITLE ); \ |
| 96 | fflush( stdout ); \ |
| 97 | \ |
| 98 | set_alarm( 1 ); \ |
| 99 | for( i = 1; ! alarmed; i++ ) \ |
| 100 | { \ |
| 101 | CODE; \ |
| 102 | } \ |
| 103 | \ |
| 104 | tsc = hardclock(); \ |
| 105 | for( j = 0; j < 1024; j++ ) \ |
| 106 | { \ |
| 107 | CODE; \ |
| 108 | } \ |
| 109 | \ |
| 110 | printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \ |
| 111 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \ |
| 112 | } while( 0 ) |
| 113 | |
| 114 | #define TIME_PUBLIC( TITLE, TYPE, CODE ) \ |
| 115 | do { \ |
| 116 | unsigned long i; \ |
| 117 | int ret; \ |
| 118 | \ |
| 119 | printf( HEADER_FORMAT, TITLE ); \ |
| 120 | fflush( stdout ); \ |
| 121 | set_alarm( 3 ); \ |
| 122 | \ |
| 123 | ret = 0; \ |
| 124 | for( i = 1; ! alarmed && ! ret ; i++ ) \ |
| 125 | { \ |
| 126 | CODE; \ |
| 127 | } \ |
| 128 | \ |
| 129 | if( ret != 0 ) \ |
| 130 | printf( "FAILED\n" ); \ |
| 131 | else \ |
| 132 | printf( "%9lu " TYPE "/s\n", i / 3 ); \ |
| 133 | } while( 0 ) |
| 134 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | unsigned char buf[BUFSIZE]; |
| 136 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 137 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 138 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 139 | int keysize; |
Paul Bakker | 5a0aa77 | 2009-02-09 22:38:52 +0000 | [diff] [blame] | 140 | unsigned char tmp[64]; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 141 | char title[TITLE_LEN]; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 142 | ((void) argc); |
| 143 | ((void) argv); |
| 144 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | memset( buf, 0xAA, sizeof( buf ) ); |
| 146 | |
| 147 | printf( "\n" ); |
| 148 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 149 | #if defined(POLARSSL_MD4_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 150 | TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 153 | #if defined(POLARSSL_MD5_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 154 | TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | #endif |
| 156 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 157 | #if defined(POLARSSL_SHA1_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 158 | TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 161 | #if defined(POLARSSL_SHA256_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 162 | TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | #endif |
| 164 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 165 | #if defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 166 | TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 169 | #if defined(POLARSSL_ARC4_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 170 | { |
| 171 | arc4_context arc4; |
| 172 | arc4_setup( &arc4, tmp, 32 ); |
| 173 | TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); |
| 174 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 177 | #if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 178 | { |
| 179 | des3_context des3; |
| 180 | des3_set3key_enc( &des3, tmp ); |
| 181 | TIME_AND_TSC( "3DES", |
| 182 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 183 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 185 | { |
| 186 | des_context des; |
| 187 | des_setkey_enc( &des, tmp ); |
| 188 | TIME_AND_TSC( "DES", |
| 189 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 190 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 193 | #if defined(POLARSSL_AES_C) |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 194 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 196 | aes_context aes; |
| 197 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 198 | { |
| 199 | snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 201 | memset( buf, 0, sizeof( buf ) ); |
| 202 | memset( tmp, 0, sizeof( tmp ) ); |
| 203 | aes_setkey_enc( &aes, tmp, keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 205 | TIME_AND_TSC( title, |
| 206 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 207 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 209 | #endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 210 | #if defined(POLARSSL_GCM_C) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 211 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 212 | gcm_context gcm; |
| 213 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 214 | { |
| 215 | snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 216 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 217 | memset( buf, 0, sizeof( buf ) ); |
| 218 | memset( tmp, 0, sizeof( tmp ) ); |
| 219 | gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 220 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 221 | TIME_AND_TSC( title, |
| 222 | gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, |
| 223 | 12, NULL, 0, buf, buf, 16, tmp ) ); |
| 224 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 225 | } |
| 226 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | #endif |
| 228 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 229 | #if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 230 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 231 | camellia_context camellia; |
| 232 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 233 | { |
| 234 | snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 236 | memset( buf, 0, sizeof( buf ) ); |
| 237 | memset( tmp, 0, sizeof( tmp ) ); |
| 238 | camellia_setkey_enc( &camellia, tmp, keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 239 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 240 | TIME_AND_TSC( title, |
| 241 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, |
| 242 | BUFSIZE, tmp, buf, buf ) ); |
| 243 | } |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 244 | } |
| 245 | #endif |
| 246 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 247 | #if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 248 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 249 | blowfish_context blowfish; |
| 250 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 251 | { |
| 252 | snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 254 | memset( buf, 0, sizeof( buf ) ); |
| 255 | memset( tmp, 0, sizeof( tmp ) ); |
| 256 | blowfish_setkey( &blowfish, tmp, keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 258 | TIME_AND_TSC( title, |
| 259 | blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, |
| 260 | tmp, buf, buf ) ); |
| 261 | } |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 262 | } |
| 263 | #endif |
| 264 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 265 | #if defined(POLARSSL_HAVEGE_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 266 | { |
| 267 | havege_state hs; |
| 268 | havege_init( &hs ); |
| 269 | TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) ); |
| 270 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 271 | #endif |
| 272 | |
| 273 | #if defined(POLARSSL_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 274 | { |
| 275 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 276 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 277 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 278 | exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 279 | TIME_AND_TSC( "CTR_DRBG (NOPR)", |
| 280 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 281 | exit(1) ); |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 282 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 283 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 284 | exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 285 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); |
| 286 | TIME_AND_TSC( "CTR_DRBG (PR)", |
| 287 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 288 | exit(1) ); |
| 289 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 290 | #endif |
| 291 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 292 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 294 | rsa_context rsa; |
| 295 | for( keysize = 1024; keysize <= 4096; keysize *= 2 ) |
| 296 | { |
| 297 | snprintf( title, sizeof( title ), "RSA-%d", keysize ); |
| 298 | |
| 299 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 300 | rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); |
| 301 | |
| 302 | TIME_PUBLIC( title, " public", |
| 303 | buf[0] = 0; |
| 304 | ret = rsa_public( &rsa, buf, buf ) ); |
| 305 | |
| 306 | TIME_PUBLIC( title, "private", |
| 307 | buf[0] = 0; |
| 308 | ret = rsa_private( &rsa, myrand, NULL, buf, buf ) ); |
| 309 | |
| 310 | rsa_free( &rsa ); |
| 311 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 312 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 313 | #endif |
| 314 | |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 315 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C) |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 316 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 317 | #define DHM_SIZES 3 |
| 318 | int sizes[DHM_SIZES] = { 1024, 2048, 3072 }; |
| 319 | const char *dhm_P[DHM_SIZES] = { |
| 320 | POLARSSL_DHM_RFC5114_MODP_1024_P, |
| 321 | POLARSSL_DHM_RFC3526_MODP_2048_P, |
| 322 | POLARSSL_DHM_RFC3526_MODP_3072_P, |
| 323 | }; |
| 324 | const char *dhm_G[DHM_SIZES] = { |
| 325 | POLARSSL_DHM_RFC5114_MODP_1024_G, |
| 326 | POLARSSL_DHM_RFC3526_MODP_2048_G, |
| 327 | POLARSSL_DHM_RFC3526_MODP_3072_G, |
| 328 | }; |
| 329 | |
| 330 | dhm_context dhm; |
| 331 | size_t olen; |
| 332 | for( keysize = 0; keysize < DHM_SIZES; keysize++ ) |
| 333 | { |
| 334 | memset( &dhm, 0, sizeof( dhm_context ) ); |
| 335 | |
| 336 | mpi_read_string( &dhm.P, 16, dhm_P[keysize] ); |
| 337 | mpi_read_string( &dhm.G, 16, dhm_G[keysize] ); |
| 338 | dhm.len = mpi_size( &dhm.P ); |
| 339 | dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL ); |
| 340 | mpi_copy( &dhm.GY, &dhm.GX ); |
| 341 | |
| 342 | snprintf( title, sizeof( title ), "DHM-%d", sizes[keysize] ); |
| 343 | TIME_PUBLIC( title, "handshake", |
| 344 | olen = sizeof( buf ); |
| 345 | ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, |
| 346 | myrand, NULL ); |
| 347 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 348 | |
| 349 | snprintf( title, sizeof( title ), "DHM-%d-fixed", sizes[keysize] ); |
| 350 | TIME_PUBLIC( title, "handshake", |
| 351 | olen = sizeof( buf ); |
| 352 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 353 | |
| 354 | dhm_free( &dhm ); |
| 355 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 356 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 357 | #endif |
| 358 | |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 359 | printf( "\n" ); |
| 360 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 361 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | printf( " Press Enter to exit this program.\n" ); |
| 363 | fflush( stdout ); getchar(); |
| 364 | #endif |
| 365 | |
| 366 | return( 0 ); |
| 367 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame^] | 368 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 369 | #endif /* POLARSSL_TIMING_C */ |