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