Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <stdio.h> |
| 41 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 42 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 44 | #include "polarssl/md4.h" |
| 45 | #include "polarssl/md5.h" |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 46 | #include "polarssl/ripemd160.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 47 | #include "polarssl/sha1.h" |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 48 | #include "polarssl/sha256.h" |
| 49 | #include "polarssl/sha512.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 50 | #include "polarssl/arc4.h" |
| 51 | #include "polarssl/des.h" |
| 52 | #include "polarssl/aes.h" |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 53 | #include "polarssl/blowfish.h" |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 54 | #include "polarssl/camellia.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 55 | #include "polarssl/gcm.h" |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 56 | #include "polarssl/ccm.h" |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 57 | #include "polarssl/havege.h" |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 58 | #include "polarssl/ctr_drbg.h" |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 59 | #include "polarssl/hmac_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 60 | #include "polarssl/rsa.h" |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 61 | #include "polarssl/dhm.h" |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 62 | #include "polarssl/ecdsa.h" |
| 63 | #include "polarssl/ecdh.h" |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 64 | #include "polarssl/error.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2f77ce3 | 2013-10-03 11:59:57 +0200 | [diff] [blame] | 66 | #if defined _MSC_VER && !defined snprintf |
| 67 | #define snprintf _snprintf |
| 68 | #endif |
| 69 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 70 | #define BUFSIZE 1024 |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 71 | #define HEADER_FORMAT " %-24s : " |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 72 | #define TITLE_LEN 25 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 74 | #if !defined(POLARSSL_TIMING_C) |
| 75 | int main( int argc, char *argv[] ) |
| 76 | { |
| 77 | ((void) argc); |
| 78 | ((void) argv); |
| 79 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 80 | polarssl_printf("POLARSSL_TIMING_C not defined.\n"); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 81 | return( 0 ); |
| 82 | } |
| 83 | #else |
| 84 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 85 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 87 | size_t use_len; |
| 88 | int rnd; |
| 89 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | if( rng_state != NULL ) |
| 91 | rng_state = NULL; |
| 92 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 93 | while( len > 0 ) |
| 94 | { |
| 95 | use_len = len; |
| 96 | if( use_len > sizeof(int) ) |
| 97 | use_len = sizeof(int); |
| 98 | |
| 99 | rnd = rand(); |
| 100 | memcpy( output, &rnd, use_len ); |
| 101 | output += use_len; |
| 102 | len -= use_len; |
| 103 | } |
| 104 | |
| 105 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 108 | #define TIME_AND_TSC( TITLE, CODE ) \ |
| 109 | do { \ |
| 110 | unsigned long i, j, tsc; \ |
| 111 | \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 112 | polarssl_printf( HEADER_FORMAT, TITLE ); \ |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 113 | fflush( stdout ); \ |
| 114 | \ |
| 115 | set_alarm( 1 ); \ |
| 116 | for( i = 1; ! alarmed; i++ ) \ |
| 117 | { \ |
| 118 | CODE; \ |
| 119 | } \ |
| 120 | \ |
| 121 | tsc = hardclock(); \ |
| 122 | for( j = 0; j < 1024; j++ ) \ |
| 123 | { \ |
| 124 | CODE; \ |
| 125 | } \ |
| 126 | \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 127 | polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \ |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 128 | ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \ |
| 129 | } while( 0 ) |
| 130 | |
Paul Bakker | 0c5e429 | 2014-05-22 14:11:13 +0200 | [diff] [blame] | 131 | #if defined(POLARSSL_ERROR_C) |
| 132 | #define PRINT_ERROR \ |
| 133 | polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 134 | polarssl_printf( "FAILED: %s\n", tmp ); |
Paul Bakker | 0c5e429 | 2014-05-22 14:11:13 +0200 | [diff] [blame] | 135 | #else |
| 136 | #define PRINT_ERROR \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 137 | polarssl_printf( "FAILED: -0x%04x\n", -ret ); |
Paul Bakker | 0c5e429 | 2014-05-22 14:11:13 +0200 | [diff] [blame] | 138 | #endif |
| 139 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 140 | #define TIME_PUBLIC( TITLE, TYPE, CODE ) \ |
| 141 | do { \ |
| 142 | unsigned long i; \ |
| 143 | int ret; \ |
| 144 | \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 145 | polarssl_printf( HEADER_FORMAT, TITLE ); \ |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 146 | fflush( stdout ); \ |
| 147 | set_alarm( 3 ); \ |
| 148 | \ |
| 149 | ret = 0; \ |
| 150 | for( i = 1; ! alarmed && ! ret ; i++ ) \ |
| 151 | { \ |
| 152 | CODE; \ |
| 153 | } \ |
| 154 | \ |
| 155 | if( ret != 0 ) \ |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 156 | { \ |
Paul Bakker | 0c5e429 | 2014-05-22 14:11:13 +0200 | [diff] [blame] | 157 | PRINT_ERROR; \ |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 158 | } \ |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 159 | else \ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 160 | polarssl_printf( "%9lu " TYPE "/s\n", i / 3 ); \ |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 161 | } while( 0 ) |
| 162 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | unsigned char buf[BUFSIZE]; |
| 164 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 165 | typedef struct { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 166 | char md4, md5, ripemd160, sha1, sha256, sha512, |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 167 | arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish, |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 168 | havege, ctr_drbg, hmac_drbg, |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 169 | rsa, dhm, ecdsa, ecdh; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 170 | } todo_list; |
| 171 | |
| 172 | #define OPTIONS \ |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 173 | "md4, md5, ripemd160, sha1, sha256, sha512,\n" \ |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 174 | "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \ |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 175 | "havege, ctr_drbg, hmac_drbg\n" \ |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 176 | "rsa, dhm, ecdsa, ecdh.\n" |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 177 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 178 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 179 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 180 | int keysize, i; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 181 | unsigned char tmp[200]; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 182 | char title[TITLE_LEN]; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 183 | todo_list todo; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 184 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 185 | if( argc == 1 ) |
| 186 | memset( &todo, 1, sizeof( todo ) ); |
| 187 | else |
| 188 | { |
| 189 | memset( &todo, 0, sizeof( todo ) ); |
| 190 | |
| 191 | for( i = 1; i < argc; i++ ) |
| 192 | { |
| 193 | if( strcmp( argv[i], "md4" ) == 0 ) |
| 194 | todo.md4 = 1; |
| 195 | else if( strcmp( argv[i], "md5" ) == 0 ) |
| 196 | todo.md5 = 1; |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 197 | else if( strcmp( argv[i], "ripemd160" ) == 0 ) |
| 198 | todo.ripemd160 = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 199 | else if( strcmp( argv[i], "sha1" ) == 0 ) |
| 200 | todo.sha1 = 1; |
| 201 | else if( strcmp( argv[i], "sha256" ) == 0 ) |
| 202 | todo.sha256 = 1; |
| 203 | else if( strcmp( argv[i], "sha512" ) == 0 ) |
| 204 | todo.sha512 = 1; |
| 205 | else if( strcmp( argv[i], "arc4" ) == 0 ) |
| 206 | todo.arc4 = 1; |
| 207 | else if( strcmp( argv[i], "des3" ) == 0 ) |
| 208 | todo.des3 = 1; |
| 209 | else if( strcmp( argv[i], "des" ) == 0 ) |
| 210 | todo.des = 1; |
| 211 | else if( strcmp( argv[i], "aes_cbc" ) == 0 ) |
| 212 | todo.aes_cbc = 1; |
| 213 | else if( strcmp( argv[i], "aes_gcm" ) == 0 ) |
| 214 | todo.aes_gcm = 1; |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 215 | else if( strcmp( argv[i], "aes_ccm" ) == 0 ) |
| 216 | todo.aes_ccm = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 217 | else if( strcmp( argv[i], "camellia" ) == 0 ) |
| 218 | todo.camellia = 1; |
| 219 | else if( strcmp( argv[i], "blowfish" ) == 0 ) |
| 220 | todo.blowfish = 1; |
| 221 | else if( strcmp( argv[i], "havege" ) == 0 ) |
| 222 | todo.havege = 1; |
| 223 | else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) |
| 224 | todo.ctr_drbg = 1; |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 225 | else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) |
| 226 | todo.hmac_drbg = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 227 | else if( strcmp( argv[i], "rsa" ) == 0 ) |
| 228 | todo.rsa = 1; |
| 229 | else if( strcmp( argv[i], "dhm" ) == 0 ) |
| 230 | todo.dhm = 1; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 231 | else if( strcmp( argv[i], "ecdsa" ) == 0 ) |
| 232 | todo.ecdsa = 1; |
| 233 | else if( strcmp( argv[i], "ecdh" ) == 0 ) |
| 234 | todo.ecdh = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 235 | else |
| 236 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 237 | polarssl_printf( "Unrecognized option: %s\n", argv[i] ); |
| 238 | polarssl_printf( "Available options: " OPTIONS ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 243 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 245 | memset( buf, 0xAA, sizeof( buf ) ); |
Paul Bakker | df71dd1 | 2014-04-17 16:03:48 +0200 | [diff] [blame] | 246 | memset( tmp, 0xBB, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 247 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 248 | #if defined(POLARSSL_MD4_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 249 | if( todo.md4 ) |
| 250 | TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | #endif |
| 252 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 253 | #if defined(POLARSSL_MD5_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 254 | if( todo.md5 ) |
| 255 | TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 258 | #if defined(POLARSSL_RIPEMD160_C) |
| 259 | if( todo.ripemd160 ) |
| 260 | TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) ); |
Manuel Pégourié-Gonnard | 01b0b38 | 2014-01-17 14:29:46 +0100 | [diff] [blame] | 261 | #endif |
| 262 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 263 | #if defined(POLARSSL_SHA1_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 264 | if( todo.sha1 ) |
| 265 | TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | #endif |
| 267 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 268 | #if defined(POLARSSL_SHA256_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 269 | if( todo.sha256 ) |
| 270 | TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | #endif |
| 272 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 273 | #if defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 274 | if( todo.sha512 ) |
| 275 | TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 276 | #endif |
| 277 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 278 | #if defined(POLARSSL_ARC4_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 279 | if( todo.arc4 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 280 | { |
| 281 | arc4_context arc4; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 282 | arc4_init( &arc4 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 283 | arc4_setup( &arc4, tmp, 32 ); |
| 284 | TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 285 | arc4_free( &arc4 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 286 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 289 | #if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 290 | if( todo.des3 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 291 | { |
| 292 | des3_context des3; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 293 | des3_init( &des3 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 294 | des3_set3key_enc( &des3, tmp ); |
| 295 | TIME_AND_TSC( "3DES", |
| 296 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 297 | des3_free( &des3 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 298 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 300 | if( todo.des ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 301 | { |
| 302 | des_context des; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 303 | des_init( &des ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 304 | des_setkey_enc( &des, tmp ); |
| 305 | TIME_AND_TSC( "DES", |
| 306 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 307 | des_free( &des ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 308 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 311 | #if defined(POLARSSL_AES_C) |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 312 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 313 | if( todo.aes_cbc ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 315 | aes_context aes; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 316 | aes_init( &aes ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 317 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 318 | { |
| 319 | snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 321 | memset( buf, 0, sizeof( buf ) ); |
| 322 | memset( tmp, 0, sizeof( tmp ) ); |
| 323 | aes_setkey_enc( &aes, tmp, keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 325 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 326 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 327 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 328 | aes_free( &aes ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 330 | #endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 331 | #if defined(POLARSSL_GCM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 332 | if( todo.aes_gcm ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 333 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 334 | gcm_context gcm; |
| 335 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 336 | { |
| 337 | snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 338 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 339 | memset( buf, 0, sizeof( buf ) ); |
| 340 | memset( tmp, 0, sizeof( tmp ) ); |
| 341 | gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 342 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 343 | TIME_AND_TSC( title, |
| 344 | gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, |
| 345 | 12, NULL, 0, buf, buf, 16, tmp ) ); |
Paul Bakker | f70fe81 | 2013-12-16 16:43:10 +0100 | [diff] [blame] | 346 | |
| 347 | gcm_free( &gcm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 348 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 349 | } |
| 350 | #endif |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 351 | #if defined(POLARSSL_CCM_C) |
| 352 | if( todo.aes_ccm ) |
| 353 | { |
| 354 | ccm_context ccm; |
| 355 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 356 | { |
| 357 | snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); |
| 358 | |
| 359 | memset( buf, 0, sizeof( buf ) ); |
| 360 | memset( tmp, 0, sizeof( tmp ) ); |
| 361 | ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); |
| 362 | |
| 363 | TIME_AND_TSC( title, |
| 364 | ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, |
| 365 | 12, NULL, 0, buf, buf, tmp, 16 ) ); |
| 366 | |
| 367 | ccm_free( &ccm ); |
| 368 | } |
| 369 | } |
| 370 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 371 | #endif |
| 372 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 373 | #if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 374 | if( todo.camellia ) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 375 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 376 | camellia_context camellia; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 377 | camellia_init( &camellia ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 378 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 379 | { |
| 380 | snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 381 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 382 | memset( buf, 0, sizeof( buf ) ); |
| 383 | memset( tmp, 0, sizeof( tmp ) ); |
| 384 | camellia_setkey_enc( &camellia, tmp, keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 385 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 386 | TIME_AND_TSC( title, |
| 387 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, |
| 388 | BUFSIZE, tmp, buf, buf ) ); |
| 389 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 390 | camellia_free( &camellia ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 391 | } |
| 392 | #endif |
| 393 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 394 | #if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 395 | if( todo.blowfish ) |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 396 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 397 | blowfish_context blowfish; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 398 | blowfish_init( &blowfish ); |
| 399 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 400 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 401 | { |
| 402 | snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 403 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 404 | memset( buf, 0, sizeof( buf ) ); |
| 405 | memset( tmp, 0, sizeof( tmp ) ); |
| 406 | blowfish_setkey( &blowfish, tmp, keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 407 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 408 | TIME_AND_TSC( title, |
| 409 | blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, |
| 410 | tmp, buf, buf ) ); |
| 411 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 412 | |
| 413 | blowfish_free( &blowfish ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 414 | } |
| 415 | #endif |
| 416 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 417 | #if defined(POLARSSL_HAVEGE_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 418 | if( todo.havege ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 419 | { |
| 420 | havege_state hs; |
| 421 | havege_init( &hs ); |
| 422 | TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 423 | havege_free( &hs ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 424 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 425 | #endif |
| 426 | |
| 427 | #if defined(POLARSSL_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 428 | if( todo.ctr_drbg ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 429 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 430 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 431 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 432 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 433 | exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 434 | TIME_AND_TSC( "CTR_DRBG (NOPR)", |
| 435 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 436 | exit(1) ); |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 437 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 438 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 439 | exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 440 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); |
| 441 | TIME_AND_TSC( "CTR_DRBG (PR)", |
| 442 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 443 | exit(1) ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 444 | ctr_drbg_free( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 445 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 446 | #endif |
| 447 | |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 448 | #if defined(POLARSSL_HMAC_DRBG_C) |
| 449 | if( todo.hmac_drbg ) |
| 450 | { |
| 451 | hmac_drbg_context hmac_drbg; |
| 452 | const md_info_t *md_info; |
| 453 | |
| 454 | #if defined(POLARSSL_SHA1_C) |
| 455 | if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL ) |
| 456 | exit(1); |
| 457 | |
| 458 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 459 | exit(1); |
| 460 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", |
| 461 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 462 | exit(1) ); |
| 463 | hmac_drbg_free( &hmac_drbg ); |
| 464 | |
| 465 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 466 | exit(1); |
| 467 | hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 468 | POLARSSL_HMAC_DRBG_PR_ON ); |
| 469 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", |
| 470 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 471 | exit(1) ); |
| 472 | hmac_drbg_free( &hmac_drbg ); |
| 473 | #endif |
| 474 | |
| 475 | #if defined(POLARSSL_SHA256_C) |
| 476 | if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL ) |
| 477 | exit(1); |
| 478 | |
| 479 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 480 | exit(1); |
| 481 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", |
| 482 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 483 | exit(1) ); |
| 484 | hmac_drbg_free( &hmac_drbg ); |
| 485 | |
| 486 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 487 | exit(1); |
| 488 | hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 489 | POLARSSL_HMAC_DRBG_PR_ON ); |
| 490 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", |
| 491 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 492 | exit(1) ); |
| 493 | hmac_drbg_free( &hmac_drbg ); |
| 494 | #endif |
| 495 | } |
| 496 | #endif |
| 497 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 498 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 499 | if( todo.rsa ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 500 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 501 | rsa_context rsa; |
| 502 | for( keysize = 1024; keysize <= 4096; keysize *= 2 ) |
| 503 | { |
| 504 | snprintf( title, sizeof( title ), "RSA-%d", keysize ); |
| 505 | |
| 506 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 507 | rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); |
| 508 | |
| 509 | TIME_PUBLIC( title, " public", |
| 510 | buf[0] = 0; |
| 511 | ret = rsa_public( &rsa, buf, buf ) ); |
| 512 | |
| 513 | TIME_PUBLIC( title, "private", |
| 514 | buf[0] = 0; |
| 515 | ret = rsa_private( &rsa, myrand, NULL, buf, buf ) ); |
| 516 | |
| 517 | rsa_free( &rsa ); |
| 518 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 520 | #endif |
| 521 | |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 522 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 523 | if( todo.dhm ) |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 524 | { |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 525 | #define DHM_SIZES 3 |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 526 | int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 }; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 527 | const char *dhm_P[DHM_SIZES] = { |
| 528 | POLARSSL_DHM_RFC5114_MODP_1024_P, |
| 529 | POLARSSL_DHM_RFC3526_MODP_2048_P, |
| 530 | POLARSSL_DHM_RFC3526_MODP_3072_P, |
| 531 | }; |
| 532 | const char *dhm_G[DHM_SIZES] = { |
| 533 | POLARSSL_DHM_RFC5114_MODP_1024_G, |
| 534 | POLARSSL_DHM_RFC3526_MODP_2048_G, |
| 535 | POLARSSL_DHM_RFC3526_MODP_3072_G, |
| 536 | }; |
| 537 | |
| 538 | dhm_context dhm; |
| 539 | size_t olen; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 540 | for( i = 0; i < DHM_SIZES; i++ ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 541 | { |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 542 | dhm_init( &dhm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 543 | |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 544 | if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || |
| 545 | mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) |
| 546 | { |
| 547 | exit( 1 ); |
| 548 | } |
| 549 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 550 | dhm.len = mpi_size( &dhm.P ); |
Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 551 | dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 552 | if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) |
| 553 | exit( 1 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 554 | |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 555 | snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 556 | TIME_PUBLIC( title, "handshake", |
| 557 | olen = sizeof( buf ); |
Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 558 | ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 559 | myrand, NULL ); |
| 560 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 561 | |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 562 | snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 563 | TIME_PUBLIC( title, "handshake", |
| 564 | olen = sizeof( buf ); |
| 565 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 566 | |
| 567 | dhm_free( &dhm ); |
| 568 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 569 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 570 | #endif |
| 571 | |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 572 | #if defined(POLARSSL_ECDSA_C) |
| 573 | if( todo.ecdsa ) |
| 574 | { |
| 575 | ecdsa_context ecdsa; |
| 576 | const ecp_curve_info *curve_info; |
| 577 | size_t sig_len; |
| 578 | |
| 579 | memset( buf, 0x2A, sizeof( buf ) ); |
| 580 | |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 581 | for( curve_info = ecp_curve_list(); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 582 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 583 | curve_info++ ) |
| 584 | { |
| 585 | ecdsa_init( &ecdsa ); |
| 586 | |
| 587 | if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) |
| 588 | exit( 1 ); |
| 589 | |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 590 | snprintf( title, sizeof( title ), "ECDSA-%s", |
| 591 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 592 | TIME_PUBLIC( title, "sign", |
| 593 | ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size, |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 594 | tmp, &sig_len, myrand, NULL ) ); |
| 595 | |
| 596 | TIME_PUBLIC( title, "verify", |
| 597 | ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size, |
| 598 | tmp, sig_len ) ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 599 | |
| 600 | ecdsa_free( &ecdsa ); |
| 601 | } |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | #if defined(POLARSSL_ECDH_C) |
| 606 | if( todo.ecdh ) |
| 607 | { |
| 608 | ecdh_context ecdh; |
| 609 | const ecp_curve_info *curve_info; |
| 610 | size_t olen; |
| 611 | |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 612 | for( curve_info = ecp_curve_list(); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 613 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 614 | curve_info++ ) |
| 615 | { |
| 616 | ecdh_init( &ecdh ); |
| 617 | |
| 618 | if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 || |
| 619 | ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 620 | myrand, NULL ) != 0 || |
| 621 | ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) |
| 622 | { |
| 623 | exit( 1 ); |
| 624 | } |
| 625 | |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 626 | snprintf( title, sizeof( title ), "ECDHE-%s", |
| 627 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 628 | TIME_PUBLIC( title, "handshake", |
| 629 | ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 630 | myrand, NULL ); |
| 631 | ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 632 | myrand, NULL ) ); |
| 633 | |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 634 | snprintf( title, sizeof( title ), "ECDH-%s", |
| 635 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 636 | TIME_PUBLIC( title, "handshake", |
| 637 | ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 638 | myrand, NULL ) ); |
| 639 | ecdh_free( &ecdh ); |
| 640 | } |
| 641 | } |
| 642 | #endif |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 643 | polarssl_printf( "\n" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 644 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 645 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 646 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 | fflush( stdout ); getchar(); |
| 648 | #endif |
| 649 | |
| 650 | return( 0 ); |
| 651 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 652 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 653 | #endif /* POLARSSL_TIMING_C */ |