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