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