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