mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-05-13 00:04:14 +08:00
029ba813cd
d8180f8d84 Merge remote-tracking branch 'origin/mbedtls-2.7' into mbedtls-2.7-restricted db649896e6 Merge pull request #2895 from gilles-peskine-arm/drbg-set_entropy_len-2.7 373a7097eb Merge pull request #673 from gilles-peskine-arm/ctr_drbg-aes_fail-2.7 b2be1fca2c Catch AES failure in mbedtls_ctr_drbg_random df1b3e54c7 Merge pull request #2937 from gilles-peskine-arm/memory_buffer_alloc-fatal-pass-2.7 02fbc08d2e Enable more test cases without MBEDTLS_MEMORY_DEBUG 786f068ec0 More accurate test case description 04d45c98e8 Clarify that the "FATAL" message is expected c5a016dde1 Merge remote-tracking branch 'restricted/pr/666' into mbedtls-2.7-restricted e70059df85 Merge remote-tracking branch 'restricted/pr/668' into mbedtls-2.7-restricted 10fcdd25d4 Merge pull request #664 from ARMmbed/dev/yanesca/iotcrypt-958-ecdsa-side-channel-fix-2.7 07597365cd Zeroize local AES variables before exiting the function dfa4d71873 Add ChangeLog entry b4edac5616 mpi_lt_mpi_ct: fix condition handling f4482aaccc mpi_lt_mpi_ct: Add further tests a776aea91a mpi_lt_mpi_ct: Fix test numbering 1b86eeb06b mpi_lt_mpi_ct perform tests for both limb size 5823961558 ct_lt_mpi_uint: cast the return value explicitely 6adff06e50 mbedtls_mpi_lt_mpi_ct: add tests for 32 bit limbs cff9e6e03d mbedtls_mpi_lt_mpi_ct: simplify condition 8ec2a953af Rename variable for better readability a2b9a96fb8 mbedtls_mpi_lt_mpi_ct: Improve documentation 51ed14e20f Make mbedtls_mpi_lt_mpi_ct more portable 9741fa6e2b Bignum: Document assumptions about the sign field 9332ecefc8 Add more tests for mbedtls_mpi_lt_mpi_ct aaa3f22b76 mpi_lt_mpi_ct test: hardcode base 16 3173a53fe9 Document ct_lt_mpi_uint 782cbe592d mpi_lt_mpi_ct: make use of unsigned consistent db9f449409 ct_lt_mpi_uint: make use of biL c3b376e2f2 Change mbedtls_mpi_cmp_mpi_ct to check less than 8461c0e2a8 mbedtls_mpi_cmp_mpi_ct: remove multiplications 8de2d45cd7 Remove excess vertical space c587a32a9c Remove declaration after statement 5f3019b298 Fix side channel vulnerability in ECDSA 883801d3ec Add tests to constant time mpi comparison e0187b95f0 Add new, constant time mpi comparison 4c575c0270 Note that mbedtls_ctr_drbg_seed() must not be called twice eab4d701ca Fix CTR_DRBG benchmark 5cf41f80a4 Add ChangeLog entry 82debf8332 ECDSA: Fix side channel vulnerability 093aa517c4 Changelog entry for xxx_drbg_set_entropy_len before xxx_drbg_seed b729e1b9ba CTR_DRBG: support set_entropy_len() before seed() 845ac103a9 CTR_DRBG: Don't use functions before they're defined 9c742249cf HMAC_DRBG: support set_entropy_len() before seed() c87a54683b Merge pull request #2900 from gilles-peskine-arm/asan-test-fail-2.7 cc656ac96b Merge pull request #2872 from gilles-peskine-arm/test_malloc_0_null-2.7 5ee14d70d2 'make test' must fail if Asan fails 4c2697f43f Asan make builds: avoid sanitizer recovery 260921d3f2 Use UBsan in addition to Asan with 'make test' c20a4053c3 Unify ASan options in make builds 395d8c1222 Merge remote-tracking branch 'origin/pr/2878' into mbedtls-2.7 55e120b9b2 mbedtls_hmac_drbg_set_entropy_len() only matters when reseeding dff3682477 mbedtls_ctr_drbg_set_entropy_len() only matters when reseeding 2abefefec2 mbedtls_ctr_drbg_seed: correct maximum for len 406d25878c Add a note about CTR_DRBG security strength to config.h f0b3dcb14b CTR_DRBG: more consistent formatting and wording b9cfe58180 DRBG documentation: Relate f_entropy arguments to the entropy module 97edf5e1e2 Add ChangeLog entry for the DRBG documentation improvements 5cc748e58f Merge remote-tracking branch 'origin/pr/2866' into mbedtls-2.7 d89173066c HMAC_DRBG documentation improvements 2fc6cf5da7 Merge remote-tracking branch 'origin/pr/2704' into mbedtls-2.7 eb99c1028f CTR_DRBG: explain the security strength and the entropy input length 25e1945321 CTR_DRBG documentation improvements 0ab4092e2d Reduce stack usage of test_suite_pkcs1_v15 dd4277f70d Reduce stack usage of test_suite_pkcs1_v21 b3d3973264 Reduce stack usage of test_suite_rsa 6827d1c588 Reduce stack usage of test_suite_pk 0981a5d7ab Add a test component with malloc(0) returning NULL ea5d3571b0 Add a calloc self-test d28b9b3c5d Merge remote-tracking branch 'origin/pr/2828' into mbedtls-2.7 9b1c248209 Enable MBEDTLS_MEMORY_DEBUG in memory buffer alloc test in all.sh 7eb7f8db8b Remove unnecessary memory buffer alloc unsets 6addfdd190 Disable DTLS proxy tests for MEMORY_BUFFER_ALLOC test 9a461a1cd7 all.sh: restructure memory allocator tests 7aad93c9da Add missing dependency in memory buffer alloc set in all.sh 19aa89ad47 Don't set MBEDTLS_MEMORY_DEBUG through `scripts/config.pl full` 8561115cb8 Add cfg dep MBEDTLS_MEMORY_DEBUG->MBEDTLS_MEMORY_BUFFER_ALLOC_C 167ae43852 Add all.sh run with full config and ASan enabled f5baaaaf89 Add all.sh run with MBEDTLS_MEMORY_BUFFER_ALLOC_C enabled e1c62e6641 Update documentation of exceptions for `config.pl full` c7f97f1c8d Adapt all.sh to removal of buffer allocator from full config 26c333ac01 Disable memory buffer allocator in full config 76ef31116b Check dependencies of MBEDTLS_MEMORY_BACKTRACE in check_config.h 9bf1509ef3 Adapt auth_crypt_tv usage to 2.7 dd91b24764 Add missing dependencies in test_suite_cipher.gcm d62577fa74 Adapt ChangeLog 311276c871 Add NIST AES GCM test vectors to single-step cipher API test suite git-subtree-dir: Sources/mbedTLS git-subtree-split: f8199650a9d49b3982a7b7f3d448899b67b09571
882 lines
31 KiB
C
882 lines
31 KiB
C
/*
|
|
* Benchmark demonstration program
|
|
*
|
|
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
* not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
*/
|
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
#include "mbedtls/config.h"
|
|
#else
|
|
#include MBEDTLS_CONFIG_FILE
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
#include "mbedtls/platform.h"
|
|
#else
|
|
#include <stdio.h>
|
|
#define mbedtls_exit exit
|
|
#define mbedtls_printf printf
|
|
#define mbedtls_snprintf snprintf
|
|
#define mbedtls_free free
|
|
#endif
|
|
|
|
#if !defined(MBEDTLS_TIMING_C)
|
|
int main( void )
|
|
{
|
|
mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
|
|
return( 0 );
|
|
}
|
|
#else
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "mbedtls/timing.h"
|
|
|
|
#include "mbedtls/md4.h"
|
|
#include "mbedtls/md5.h"
|
|
#include "mbedtls/ripemd160.h"
|
|
#include "mbedtls/sha1.h"
|
|
#include "mbedtls/sha256.h"
|
|
#include "mbedtls/sha512.h"
|
|
#include "mbedtls/arc4.h"
|
|
#include "mbedtls/des.h"
|
|
#include "mbedtls/aes.h"
|
|
#include "mbedtls/blowfish.h"
|
|
#include "mbedtls/camellia.h"
|
|
#include "mbedtls/gcm.h"
|
|
#include "mbedtls/ccm.h"
|
|
#include "mbedtls/cmac.h"
|
|
#include "mbedtls/havege.h"
|
|
#include "mbedtls/ctr_drbg.h"
|
|
#include "mbedtls/hmac_drbg.h"
|
|
#include "mbedtls/rsa.h"
|
|
#include "mbedtls/dhm.h"
|
|
#include "mbedtls/ecdsa.h"
|
|
#include "mbedtls/ecdh.h"
|
|
#include "mbedtls/error.h"
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
|
#endif
|
|
|
|
/*
|
|
* For heap usage estimates, we need an estimate of the overhead per allocated
|
|
* block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
|
|
* so use that as our baseline.
|
|
*/
|
|
#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
|
|
|
|
/*
|
|
* Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
|
|
*/
|
|
#define HEAP_SIZE (1u << 16) // 64k
|
|
|
|
#define BUFSIZE 1024
|
|
#define HEADER_FORMAT " %-24s : "
|
|
#define TITLE_LEN 25
|
|
|
|
#define OPTIONS \
|
|
"md4, md5, ripemd160, sha1, sha256, sha512,\n" \
|
|
"arc4, des3, des, camellia, blowfish,\n" \
|
|
"aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,\n" \
|
|
"havege, ctr_drbg, hmac_drbg\n" \
|
|
"rsa, dhm, ecdsa, ecdh.\n"
|
|
|
|
#if defined(MBEDTLS_ERROR_C)
|
|
#define PRINT_ERROR \
|
|
mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
|
|
mbedtls_printf( "FAILED: %s\n", tmp );
|
|
#else
|
|
#define PRINT_ERROR \
|
|
mbedtls_printf( "FAILED: -0x%04x\n", -ret );
|
|
#endif
|
|
|
|
#define TIME_AND_TSC( TITLE, CODE ) \
|
|
do { \
|
|
unsigned long ii, jj, tsc; \
|
|
\
|
|
mbedtls_printf( HEADER_FORMAT, TITLE ); \
|
|
fflush( stdout ); \
|
|
\
|
|
mbedtls_set_alarm( 1 ); \
|
|
for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
|
|
{ \
|
|
CODE; \
|
|
} \
|
|
\
|
|
tsc = mbedtls_timing_hardclock(); \
|
|
for( jj = 0; jj < 1024; jj++ ) \
|
|
{ \
|
|
CODE; \
|
|
} \
|
|
\
|
|
mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
|
|
ii * BUFSIZE / 1024, \
|
|
( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
|
|
} while( 0 )
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
#define MEMORY_MEASURE_INIT \
|
|
size_t max_used, max_blocks, max_bytes; \
|
|
size_t prv_used, prv_blocks; \
|
|
mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
|
|
mbedtls_memory_buffer_alloc_max_reset( );
|
|
|
|
#define MEMORY_MEASURE_PRINT( title_len ) \
|
|
mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
|
|
for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
|
|
max_used -= prv_used; \
|
|
max_blocks -= prv_blocks; \
|
|
max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
|
|
mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
|
|
|
|
#else
|
|
#define MEMORY_MEASURE_INIT
|
|
#define MEMORY_MEASURE_PRINT( title_len )
|
|
#endif
|
|
|
|
#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
|
|
do { \
|
|
unsigned long ii; \
|
|
int ret; \
|
|
MEMORY_MEASURE_INIT; \
|
|
\
|
|
mbedtls_printf( HEADER_FORMAT, TITLE ); \
|
|
fflush( stdout ); \
|
|
mbedtls_set_alarm( 3 ); \
|
|
\
|
|
ret = 0; \
|
|
for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
|
|
{ \
|
|
CODE; \
|
|
} \
|
|
\
|
|
if( ret != 0 ) \
|
|
{ \
|
|
PRINT_ERROR; \
|
|
} \
|
|
else \
|
|
{ \
|
|
mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
|
|
MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
|
|
mbedtls_printf( "\n" ); \
|
|
} \
|
|
} while( 0 )
|
|
|
|
static int myrand( void *rng_state, unsigned char *output, size_t len )
|
|
{
|
|
size_t use_len;
|
|
int rnd;
|
|
|
|
if( rng_state != NULL )
|
|
rng_state = NULL;
|
|
|
|
while( len > 0 )
|
|
{
|
|
use_len = len;
|
|
if( use_len > sizeof(int) )
|
|
use_len = sizeof(int);
|
|
|
|
rnd = rand();
|
|
memcpy( output, &rnd, use_len );
|
|
output += use_len;
|
|
len -= use_len;
|
|
}
|
|
|
|
return( 0 );
|
|
}
|
|
|
|
/*
|
|
* Clear some memory that was used to prepare the context
|
|
*/
|
|
#if defined(MBEDTLS_ECP_C)
|
|
void ecp_clear_precomputed( mbedtls_ecp_group *grp )
|
|
{
|
|
if( grp->T != NULL )
|
|
{
|
|
size_t i;
|
|
for( i = 0; i < grp->T_size; i++ )
|
|
mbedtls_ecp_point_free( &grp->T[i] );
|
|
mbedtls_free( grp->T );
|
|
}
|
|
grp->T = NULL;
|
|
grp->T_size = 0;
|
|
}
|
|
#else
|
|
#define ecp_clear_precomputed( g )
|
|
#endif
|
|
|
|
unsigned char buf[BUFSIZE];
|
|
|
|
typedef struct {
|
|
char md4, md5, ripemd160, sha1, sha256, sha512,
|
|
arc4, des3, des,
|
|
aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,
|
|
camellia, blowfish,
|
|
havege, ctr_drbg, hmac_drbg,
|
|
rsa, dhm, ecdsa, ecdh;
|
|
} todo_list;
|
|
|
|
int main( int argc, char *argv[] )
|
|
{
|
|
int i;
|
|
unsigned char tmp[200];
|
|
char title[TITLE_LEN];
|
|
todo_list todo;
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
unsigned char alloc_buf[HEAP_SIZE] = { 0 };
|
|
#endif
|
|
|
|
if( argc <= 1 )
|
|
{
|
|
memset( &todo, 1, sizeof( todo ) );
|
|
}
|
|
else
|
|
{
|
|
memset( &todo, 0, sizeof( todo ) );
|
|
|
|
for( i = 1; i < argc; i++ )
|
|
{
|
|
if( strcmp( argv[i], "md4" ) == 0 )
|
|
todo.md4 = 1;
|
|
else if( strcmp( argv[i], "md5" ) == 0 )
|
|
todo.md5 = 1;
|
|
else if( strcmp( argv[i], "ripemd160" ) == 0 )
|
|
todo.ripemd160 = 1;
|
|
else if( strcmp( argv[i], "sha1" ) == 0 )
|
|
todo.sha1 = 1;
|
|
else if( strcmp( argv[i], "sha256" ) == 0 )
|
|
todo.sha256 = 1;
|
|
else if( strcmp( argv[i], "sha512" ) == 0 )
|
|
todo.sha512 = 1;
|
|
else if( strcmp( argv[i], "arc4" ) == 0 )
|
|
todo.arc4 = 1;
|
|
else if( strcmp( argv[i], "des3" ) == 0 )
|
|
todo.des3 = 1;
|
|
else if( strcmp( argv[i], "des" ) == 0 )
|
|
todo.des = 1;
|
|
else if( strcmp( argv[i], "aes_cbc" ) == 0 )
|
|
todo.aes_cbc = 1;
|
|
else if( strcmp( argv[i], "aes_gcm" ) == 0 )
|
|
todo.aes_gcm = 1;
|
|
else if( strcmp( argv[i], "aes_ccm" ) == 0 )
|
|
todo.aes_ccm = 1;
|
|
else if( strcmp( argv[i], "aes_cmac" ) == 0 )
|
|
todo.aes_cmac = 1;
|
|
else if( strcmp( argv[i], "des3_cmac" ) == 0 )
|
|
todo.des3_cmac = 1;
|
|
else if( strcmp( argv[i], "camellia" ) == 0 )
|
|
todo.camellia = 1;
|
|
else if( strcmp( argv[i], "blowfish" ) == 0 )
|
|
todo.blowfish = 1;
|
|
else if( strcmp( argv[i], "havege" ) == 0 )
|
|
todo.havege = 1;
|
|
else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
|
|
todo.ctr_drbg = 1;
|
|
else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
|
|
todo.hmac_drbg = 1;
|
|
else if( strcmp( argv[i], "rsa" ) == 0 )
|
|
todo.rsa = 1;
|
|
else if( strcmp( argv[i], "dhm" ) == 0 )
|
|
todo.dhm = 1;
|
|
else if( strcmp( argv[i], "ecdsa" ) == 0 )
|
|
todo.ecdsa = 1;
|
|
else if( strcmp( argv[i], "ecdh" ) == 0 )
|
|
todo.ecdh = 1;
|
|
else
|
|
{
|
|
mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
|
|
mbedtls_printf( "Available options: " OPTIONS );
|
|
}
|
|
}
|
|
}
|
|
|
|
mbedtls_printf( "\n" );
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
|
|
#endif
|
|
memset( buf, 0xAA, sizeof( buf ) );
|
|
memset( tmp, 0xBB, sizeof( tmp ) );
|
|
|
|
#if defined(MBEDTLS_MD4_C)
|
|
if( todo.md4 )
|
|
TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
if( todo.md5 )
|
|
TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
|
if( todo.ripemd160 )
|
|
TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
if( todo.sha1 )
|
|
TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
if( todo.sha256 )
|
|
TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
if( todo.sha512 )
|
|
TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
|
if( todo.arc4 )
|
|
{
|
|
mbedtls_arc4_context arc4;
|
|
mbedtls_arc4_init( &arc4 );
|
|
mbedtls_arc4_setup( &arc4, tmp, 32 );
|
|
TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
|
|
mbedtls_arc4_free( &arc4 );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_DES_C)
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
if( todo.des3 )
|
|
{
|
|
mbedtls_des3_context des3;
|
|
mbedtls_des3_init( &des3 );
|
|
mbedtls_des3_set3key_enc( &des3, tmp );
|
|
TIME_AND_TSC( "3DES",
|
|
mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
|
|
mbedtls_des3_free( &des3 );
|
|
}
|
|
|
|
if( todo.des )
|
|
{
|
|
mbedtls_des_context des;
|
|
mbedtls_des_init( &des );
|
|
mbedtls_des_setkey_enc( &des, tmp );
|
|
TIME_AND_TSC( "DES",
|
|
mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
|
|
mbedtls_des_free( &des );
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
if( todo.des3_cmac )
|
|
{
|
|
unsigned char output[8];
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
|
|
|
|
TIME_AND_TSC( "3DES-CMAC",
|
|
mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
|
|
BUFSIZE, output ) );
|
|
}
|
|
#endif /* MBEDTLS_CMAC_C */
|
|
#endif /* MBEDTLS_DES_C */
|
|
|
|
#if defined(MBEDTLS_AES_C)
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
if( todo.aes_cbc )
|
|
{
|
|
int keysize;
|
|
mbedtls_aes_context aes;
|
|
mbedtls_aes_init( &aes );
|
|
for( keysize = 128; keysize <= 256; keysize += 64 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
mbedtls_aes_setkey_enc( &aes, tmp, keysize );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
|
|
}
|
|
mbedtls_aes_free( &aes );
|
|
}
|
|
#endif
|
|
#if defined(MBEDTLS_GCM_C)
|
|
if( todo.aes_gcm )
|
|
{
|
|
int keysize;
|
|
mbedtls_gcm_context gcm;
|
|
|
|
mbedtls_gcm_init( &gcm );
|
|
for( keysize = 128; keysize <= 256; keysize += 64 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
|
|
12, NULL, 0, buf, buf, 16, tmp ) );
|
|
|
|
mbedtls_gcm_free( &gcm );
|
|
}
|
|
}
|
|
#endif
|
|
#if defined(MBEDTLS_CCM_C)
|
|
if( todo.aes_ccm )
|
|
{
|
|
int keysize;
|
|
mbedtls_ccm_context ccm;
|
|
|
|
mbedtls_ccm_init( &ccm );
|
|
for( keysize = 128; keysize <= 256; keysize += 64 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
|
|
12, NULL, 0, buf, buf, tmp, 16 ) );
|
|
|
|
mbedtls_ccm_free( &ccm );
|
|
}
|
|
}
|
|
#endif
|
|
#if defined(MBEDTLS_CMAC_C)
|
|
if( todo.aes_cmac )
|
|
{
|
|
unsigned char output[16];
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
mbedtls_cipher_type_t cipher_type;
|
|
int keysize;
|
|
|
|
for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
|
|
keysize <= 256;
|
|
keysize += 64, cipher_type++ )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_cipher_cmac( cipher_info, tmp, keysize,
|
|
buf, BUFSIZE, output ) );
|
|
}
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
TIME_AND_TSC( "AES-CMAC-PRF-128",
|
|
mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
|
|
output ) );
|
|
}
|
|
#endif /* MBEDTLS_CMAC_C */
|
|
#endif /* MBEDTLS_AES_C */
|
|
|
|
#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
if( todo.camellia )
|
|
{
|
|
int keysize;
|
|
mbedtls_camellia_context camellia;
|
|
mbedtls_camellia_init( &camellia );
|
|
for( keysize = 128; keysize <= 256; keysize += 64 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
|
|
BUFSIZE, tmp, buf, buf ) );
|
|
}
|
|
mbedtls_camellia_free( &camellia );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
if( todo.blowfish )
|
|
{
|
|
int keysize;
|
|
mbedtls_blowfish_context blowfish;
|
|
mbedtls_blowfish_init( &blowfish );
|
|
|
|
for( keysize = 128; keysize <= 256; keysize += 64 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
memset( tmp, 0, sizeof( tmp ) );
|
|
mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
|
|
|
|
TIME_AND_TSC( title,
|
|
mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
|
|
tmp, buf, buf ) );
|
|
}
|
|
|
|
mbedtls_blowfish_free( &blowfish );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_HAVEGE_C)
|
|
if( todo.havege )
|
|
{
|
|
mbedtls_havege_state hs;
|
|
mbedtls_havege_init( &hs );
|
|
TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
|
|
mbedtls_havege_free( &hs );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
|
if( todo.ctr_drbg )
|
|
{
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
TIME_AND_TSC( "CTR_DRBG (NOPR)",
|
|
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
|
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
|
|
TIME_AND_TSC( "CTR_DRBG (PR)",
|
|
if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C)
|
|
if( todo.hmac_drbg )
|
|
{
|
|
mbedtls_hmac_drbg_context hmac_drbg;
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_hmac_drbg_init( &hmac_drbg );
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
|
|
mbedtls_exit(1);
|
|
|
|
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
|
|
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
|
|
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
|
|
MBEDTLS_HMAC_DRBG_PR_ON );
|
|
TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
|
|
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
|
|
mbedtls_exit(1);
|
|
|
|
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
|
|
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
|
|
if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
|
|
mbedtls_exit(1);
|
|
mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
|
|
MBEDTLS_HMAC_DRBG_PR_ON );
|
|
TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
|
|
if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
|
|
mbedtls_exit(1) );
|
|
#endif
|
|
mbedtls_hmac_drbg_free( &hmac_drbg );
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
|
if( todo.rsa )
|
|
{
|
|
int keysize;
|
|
mbedtls_rsa_context rsa;
|
|
for( keysize = 2048; keysize <= 4096; keysize *= 2 )
|
|
{
|
|
mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
|
|
|
|
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
|
mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
|
|
|
|
TIME_PUBLIC( title, " public",
|
|
buf[0] = 0;
|
|
ret = mbedtls_rsa_public( &rsa, buf, buf ) );
|
|
|
|
TIME_PUBLIC( title, "private",
|
|
buf[0] = 0;
|
|
ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
|
|
|
|
mbedtls_rsa_free( &rsa );
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
|
|
if( todo.dhm )
|
|
{
|
|
int dhm_sizes[] = { 2048, 3072 };
|
|
static const unsigned char dhm_P_2048[] =
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
|
|
static const unsigned char dhm_P_3072[] =
|
|
MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
|
|
static const unsigned char dhm_G_2048[] =
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
|
|
static const unsigned char dhm_G_3072[] =
|
|
MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
|
|
|
|
const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
|
|
const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
|
|
sizeof( dhm_P_3072 ) };
|
|
|
|
const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
|
|
const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
|
|
sizeof( dhm_G_3072 ) };
|
|
|
|
mbedtls_dhm_context dhm;
|
|
size_t olen;
|
|
for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
|
|
{
|
|
mbedtls_dhm_init( &dhm );
|
|
|
|
if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
|
|
dhm_P_size[i] ) != 0 ||
|
|
mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
|
|
dhm_G_size[i] ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
|
|
dhm.len = mbedtls_mpi_size( &dhm.P );
|
|
mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
|
|
if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
|
|
mbedtls_exit( 1 );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
|
|
TIME_PUBLIC( title, "handshake",
|
|
ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
|
|
myrand, NULL );
|
|
ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
|
|
TIME_PUBLIC( title, "handshake",
|
|
ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
|
|
|
|
mbedtls_dhm_free( &dhm );
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
|
|
if( todo.ecdsa )
|
|
{
|
|
mbedtls_ecdsa_context ecdsa;
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
size_t sig_len;
|
|
|
|
memset( buf, 0x2A, sizeof( buf ) );
|
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
curve_info++ )
|
|
{
|
|
mbedtls_ecdsa_init( &ecdsa );
|
|
|
|
if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
|
|
mbedtls_exit( 1 );
|
|
ecp_clear_precomputed( &ecdsa.grp );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
|
|
curve_info->name );
|
|
TIME_PUBLIC( title, "sign",
|
|
ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
|
|
tmp, &sig_len, myrand, NULL ) );
|
|
|
|
mbedtls_ecdsa_free( &ecdsa );
|
|
}
|
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
curve_info++ )
|
|
{
|
|
mbedtls_ecdsa_init( &ecdsa );
|
|
|
|
if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
|
|
mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
|
|
tmp, &sig_len, myrand, NULL ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
ecp_clear_precomputed( &ecdsa.grp );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
|
|
curve_info->name );
|
|
TIME_PUBLIC( title, "verify",
|
|
ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
|
|
tmp, sig_len ) );
|
|
|
|
mbedtls_ecdsa_free( &ecdsa );
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
if( todo.ecdh )
|
|
{
|
|
mbedtls_ecdh_context ecdh;
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
|
mbedtls_mpi z;
|
|
#endif
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
size_t olen;
|
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
curve_info++ )
|
|
{
|
|
mbedtls_ecdh_init( &ecdh );
|
|
|
|
if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
|
|
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
|
|
myrand, NULL ) != 0 ||
|
|
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
ecp_clear_precomputed( &ecdh.grp );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
|
|
curve_info->name );
|
|
TIME_PUBLIC( title, "handshake",
|
|
ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
|
|
myrand, NULL );
|
|
ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
|
|
myrand, NULL ) );
|
|
mbedtls_ecdh_free( &ecdh );
|
|
}
|
|
|
|
/* Curve25519 needs to be handled separately */
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
|
mbedtls_ecdh_init( &ecdh );
|
|
mbedtls_mpi_init( &z );
|
|
|
|
if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
|
|
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
|
|
TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
|
|
ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
|
|
myrand, NULL );
|
|
ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
|
|
myrand, NULL ) );
|
|
|
|
mbedtls_ecdh_free( &ecdh );
|
|
mbedtls_mpi_free( &z );
|
|
#endif
|
|
|
|
for( curve_info = mbedtls_ecp_curve_list();
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
curve_info++ )
|
|
{
|
|
mbedtls_ecdh_init( &ecdh );
|
|
|
|
if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
|
|
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
|
|
myrand, NULL ) != 0 ||
|
|
mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
|
|
mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
|
|
myrand, NULL ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
ecp_clear_precomputed( &ecdh.grp );
|
|
|
|
mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
|
|
curve_info->name );
|
|
TIME_PUBLIC( title, "handshake",
|
|
ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
|
|
myrand, NULL ) );
|
|
mbedtls_ecdh_free( &ecdh );
|
|
}
|
|
|
|
/* Curve25519 needs to be handled separately */
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
|
mbedtls_ecdh_init( &ecdh );
|
|
mbedtls_mpi_init( &z );
|
|
|
|
if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
|
|
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
|
|
myrand, NULL ) != 0 ||
|
|
mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
|
|
{
|
|
mbedtls_exit( 1 );
|
|
}
|
|
|
|
TIME_PUBLIC( "ECDH-Curve25519", "handshake",
|
|
ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
|
|
myrand, NULL ) );
|
|
|
|
mbedtls_ecdh_free( &ecdh );
|
|
mbedtls_mpi_free( &z );
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
mbedtls_printf( "\n" );
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
mbedtls_memory_buffer_alloc_free();
|
|
#endif
|
|
|
|
#if defined(_WIN32)
|
|
mbedtls_printf( " Press Enter to exit this program.\n" );
|
|
fflush( stdout ); getchar();
|
|
#endif
|
|
|
|
return( 0 );
|
|
}
|
|
|
|
#endif /* MBEDTLS_TIMING_C */
|