2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Self-test demonstration program
|
|
|
|
*
|
2020-08-19 14:37:36 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* 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
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* 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.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/entropy.h"
|
2016-06-18 21:45:37 +00:00
|
|
|
#include "mbedtls/entropy_poll.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/hmac_drbg.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/dhm.h"
|
|
|
|
#include "mbedtls/gcm.h"
|
|
|
|
#include "mbedtls/ccm.h"
|
2015-12-11 15:49:45 +00:00
|
|
|
#include "mbedtls/cmac.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/md2.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/camellia.h"
|
2017-11-30 16:00:34 +00:00
|
|
|
#include "mbedtls/aria.h"
|
2016-05-18 13:09:28 +00:00
|
|
|
#include "mbedtls/chacha20.h"
|
|
|
|
#include "mbedtls/poly1305.h"
|
2018-05-07 08:43:27 +00:00
|
|
|
#include "mbedtls/chachapoly.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/base64.h"
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/xtea.h"
|
|
|
|
#include "mbedtls/pkcs5.h"
|
|
|
|
#include "mbedtls/ecp.h"
|
2015-08-05 13:44:42 +00:00
|
|
|
#include "mbedtls/ecjpake.h"
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/timing.h"
|
2018-07-15 06:33:07 +00:00
|
|
|
#include "mbedtls/nist_kw.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-02-11 14:06:19 +00:00
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
2016-03-18 16:25:52 +00:00
|
|
|
#include <stdlib.h>
|
2019-09-17 16:18:58 +00:00
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_printf printf
|
2015-06-22 08:48:01 +00:00
|
|
|
#define mbedtls_snprintf snprintf
|
2016-03-18 16:25:52 +00:00
|
|
|
#define mbedtls_exit exit
|
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
2015-02-11 14:06:19 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
2013-07-03 11:37:05 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-06 17:43:31 +00:00
|
|
|
|
2019-09-17 16:18:58 +00:00
|
|
|
#if defined MBEDTLS_SELF_TEST
|
|
|
|
/* Sanity check for malloc. This is not expected to fail, and is rather
|
|
|
|
* intended to display potentially useful information about the platform,
|
|
|
|
* in particular the behavior of malloc(0). */
|
|
|
|
static int calloc_self_test( int verbose )
|
|
|
|
{
|
|
|
|
int failures = 0;
|
|
|
|
void *empty1 = mbedtls_calloc( 0, 1 );
|
|
|
|
void *empty2 = mbedtls_calloc( 0, 1 );
|
|
|
|
void *buffer1 = mbedtls_calloc( 1, 1 );
|
|
|
|
void *buffer2 = mbedtls_calloc( 1, 1 );
|
|
|
|
uintptr_t old_buffer1;
|
|
|
|
|
|
|
|
if( empty1 == NULL && empty2 == NULL )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
|
|
|
|
}
|
|
|
|
else if( empty1 == NULL || empty2 == NULL )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
|
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
else if( empty1 == empty2 )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( buffer1 == NULL || buffer2 == NULL )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
|
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
else if( buffer1 == buffer2 )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
|
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(1): passed\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
old_buffer1 = (uintptr_t) buffer1;
|
|
|
|
mbedtls_free( buffer1 );
|
|
|
|
buffer1 = mbedtls_calloc( 1, 1 );
|
|
|
|
if( buffer1 == NULL )
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
|
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
|
|
|
|
(uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
|
|
|
|
"same" : "different" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose )
|
|
|
|
mbedtls_printf( "\n" );
|
|
|
|
mbedtls_free( empty1 );
|
|
|
|
mbedtls_free( empty2 );
|
|
|
|
mbedtls_free( buffer1 );
|
|
|
|
mbedtls_free( buffer2 );
|
|
|
|
return( failures );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
2020-11-10 05:28:50 +00:00
|
|
|
static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
|
2015-06-22 08:48:01 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[10] = "xxxxxxxxx";
|
2015-06-26 09:24:32 +00:00
|
|
|
const char ref[10] = "xxxxxxxxx";
|
2015-06-22 08:48:01 +00:00
|
|
|
|
|
|
|
ret = mbedtls_snprintf( buf, n, "%s", "123" );
|
|
|
|
if( ret < 0 || (size_t) ret >= n )
|
|
|
|
ret = -1;
|
|
|
|
|
2015-06-26 09:24:32 +00:00
|
|
|
if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
|
|
|
|
ref_ret != ret ||
|
|
|
|
memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
|
2015-06-22 08:48:01 +00:00
|
|
|
{
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_test_snprintf( void )
|
|
|
|
{
|
|
|
|
return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
|
2015-06-26 09:24:32 +00:00
|
|
|
test_snprintf( 1, "", -1 ) != 0 ||
|
|
|
|
test_snprintf( 2, "1", -1 ) != 0 ||
|
|
|
|
test_snprintf( 3, "12", -1 ) != 0 ||
|
|
|
|
test_snprintf( 4, "123", 3 ) != 0 ||
|
|
|
|
test_snprintf( 5, "123", 3 ) != 0 );
|
2015-06-22 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 21:45:37 +00:00
|
|
|
/*
|
|
|
|
* Check if a seed file is present, and if not create one for the entropy
|
|
|
|
* self-test. If this fails, we attempt the test anyway, so no error is passed
|
|
|
|
* back.
|
|
|
|
*/
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
2016-06-18 21:45:37 +00:00
|
|
|
static void create_entropy_seed_file( void )
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
size_t output_len = 0;
|
|
|
|
unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
|
|
|
|
|
|
|
/* Attempt to read the entropy seed file. If this fails - attempt to write
|
|
|
|
* to the file to ensure one is present. */
|
|
|
|
result = mbedtls_platform_std_nv_seed_read( seed_value,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE );
|
|
|
|
if( 0 == result )
|
|
|
|
return;
|
|
|
|
|
|
|
|
result = mbedtls_platform_entropy_poll( NULL,
|
|
|
|
seed_value,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
&output_len );
|
|
|
|
if( 0 != result )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
|
|
|
|
return;
|
|
|
|
|
|
|
|
mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
int mbedtls_entropy_self_test_wrapper( int verbose )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
|
|
|
create_entropy_seed_file( );
|
|
|
|
#endif
|
|
|
|
return( mbedtls_entropy_self_test( verbose ) );
|
|
|
|
}
|
2013-07-03 11:37:05 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2016-03-11 00:40:54 +00:00
|
|
|
{
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
|
|
|
mbedtls_memory_buffer_alloc_status( );
|
|
|
|
#endif
|
2016-03-11 00:40:54 +00:00
|
|
|
}
|
2017-12-15 13:57:18 +00:00
|
|
|
mbedtls_memory_buffer_alloc_free( );
|
|
|
|
return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int ( *function )( int );
|
|
|
|
} selftest_t;
|
|
|
|
|
|
|
|
const selftest_t selftests[] =
|
|
|
|
{
|
2019-09-17 16:18:58 +00:00
|
|
|
{"calloc", calloc_self_test},
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_MD2_C)
|
|
|
|
{"md2", mbedtls_md2_self_test},
|
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD4_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"md4", mbedtls_md4_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"md5", mbedtls_md5_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"ripemd160", mbedtls_ripemd160_self_test},
|
2014-01-17 13:44:38 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"sha1", mbedtls_sha1_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"sha256", mbedtls_sha256_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"sha512", mbedtls_sha512_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"arc4", mbedtls_arc4_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"des", mbedtls_des_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"aes", mbedtls_aes_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"gcm", mbedtls_gcm_self_test},
|
2012-03-20 13:50:09 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"ccm", mbedtls_ccm_self_test},
|
2014-05-02 13:17:29 +00:00
|
|
|
#endif
|
2018-07-15 06:33:07 +00:00
|
|
|
#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
|
|
|
|
{"nist_kw", mbedtls_nist_kw_self_test},
|
|
|
|
#endif
|
2016-10-06 09:27:22 +00:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"cmac", mbedtls_cmac_self_test},
|
2015-12-11 15:49:45 +00:00
|
|
|
#endif
|
2016-05-18 13:09:28 +00:00
|
|
|
#if defined(MBEDTLS_CHACHA20_C)
|
|
|
|
{"chacha20", mbedtls_chacha20_self_test},
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_POLY1305_C)
|
|
|
|
{"poly1305", mbedtls_poly1305_self_test},
|
|
|
|
#endif
|
2018-05-07 08:43:27 +00:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
|
|
|
{"chacha20-poly1305", mbedtls_chachapoly_self_test},
|
2016-05-18 13:09:28 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_BASE64_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"base64", mbedtls_base64_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"mpi", mbedtls_mpi_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"rsa", mbedtls_rsa_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_USE_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"x509", mbedtls_x509_self_test},
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_XTEA_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"xtea", mbedtls_xtea_self_test},
|
2009-01-04 18:15:48 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CAMELLIA_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"camellia", mbedtls_camellia_self_test},
|
2009-01-10 23:31:23 +00:00
|
|
|
#endif
|
2017-11-30 16:00:34 +00:00
|
|
|
#if defined(MBEDTLS_ARIA_C)
|
|
|
|
{"aria", mbedtls_aria_self_test},
|
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"ctr_drbg", mbedtls_ctr_drbg_self_test},
|
2011-11-27 14:46:59 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"hmac_drbg", mbedtls_hmac_drbg_self_test},
|
2014-01-31 10:12:09 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"ecp", mbedtls_ecp_self_test},
|
2012-11-13 19:57:00 +00:00
|
|
|
#endif
|
2015-08-05 13:44:42 +00:00
|
|
|
#if defined(MBEDTLS_ECJPAKE_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"ecjpake", mbedtls_ecjpake_self_test},
|
2015-08-05 13:44:42 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_DHM_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"dhm", mbedtls_dhm_self_test},
|
2013-09-15 15:43:54 +00:00
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
{"entropy", mbedtls_entropy_self_test_wrapper},
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PKCS5_C)
|
|
|
|
{"pkcs5", mbedtls_pkcs5_self_test},
|
|
|
|
#endif
|
|
|
|
/* Slower test after the faster ones */
|
|
|
|
#if defined(MBEDTLS_TIMING_C)
|
|
|
|
{"timing", mbedtls_timing_self_test},
|
|
|
|
#endif
|
|
|
|
/* Heap test comes last */
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
|
|
|
{"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
|
|
|
|
#endif
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2017-12-15 14:01:27 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2016-06-18 21:45:37 +00:00
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2017-12-15 14:01:27 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2017-12-15 13:57:18 +00:00
|
|
|
const selftest_t *test;
|
2017-12-15 14:01:27 +00:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2017-12-20 17:09:27 +00:00
|
|
|
char **argp;
|
|
|
|
int v = 1; /* v=1 for verbose mode */
|
|
|
|
int exclude_mode = 0;
|
|
|
|
int suites_tested = 0, suites_failed = 0;
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
|
|
|
|
unsigned char buf[1000000];
|
2016-06-18 21:45:37 +00:00
|
|
|
#endif
|
2017-12-15 13:57:18 +00:00
|
|
|
void *pointer;
|
2016-06-18 21:45:37 +00:00
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
/*
|
|
|
|
* The C standard doesn't guarantee that all-bits-0 is the representation
|
|
|
|
* of a NULL pointer. We do however use that in our code for initializing
|
|
|
|
* structures, which should work on every modern platform. Let's be sure.
|
|
|
|
*/
|
|
|
|
memset( &pointer, 0, sizeof( void * ) );
|
|
|
|
if( pointer != NULL )
|
2016-03-11 00:40:54 +00:00
|
|
|
{
|
2017-12-15 13:57:18 +00:00
|
|
|
mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
|
|
|
|
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
2016-03-11 00:40:54 +00:00
|
|
|
}
|
2014-05-30 08:34:15 +00:00
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
/*
|
|
|
|
* Make sure we have a snprintf that correctly zero-terminates
|
|
|
|
*/
|
|
|
|
if( run_test_snprintf() != 0 )
|
2016-03-11 00:40:54 +00:00
|
|
|
{
|
2017-12-15 13:57:18 +00:00
|
|
|
mbedtls_printf( "the snprintf implementation is broken\n" );
|
|
|
|
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
2016-03-11 00:40:54 +00:00
|
|
|
}
|
2014-03-27 19:12:44 +00:00
|
|
|
|
2017-12-20 17:09:27 +00:00
|
|
|
for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
|
2016-03-11 00:40:54 +00:00
|
|
|
{
|
2017-12-20 17:09:27 +00:00
|
|
|
if( strcmp( *argp, "--quiet" ) == 0 ||
|
|
|
|
strcmp( *argp, "-q" ) == 0 )
|
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
else if( strcmp( *argp, "--exclude" ) == 0 ||
|
|
|
|
strcmp( *argp, "-x" ) == 0 )
|
|
|
|
{
|
|
|
|
exclude_mode = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2016-03-11 00:40:54 +00:00
|
|
|
}
|
2017-12-20 17:09:27 +00:00
|
|
|
|
|
|
|
if( v != 0 )
|
2017-12-15 13:57:18 +00:00
|
|
|
mbedtls_printf( "\n" );
|
2014-11-27 10:33:55 +00:00
|
|
|
|
2017-12-15 13:57:18 +00:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2017-12-15 13:57:18 +00:00
|
|
|
mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
|
|
|
|
#endif
|
|
|
|
|
2017-12-20 17:09:27 +00:00
|
|
|
if( *argp != NULL && exclude_mode == 0 )
|
2017-12-15 14:01:27 +00:00
|
|
|
{
|
|
|
|
/* Run the specified tests */
|
|
|
|
for( ; *argp != NULL; argp++ )
|
|
|
|
{
|
|
|
|
for( test = selftests; test->name != NULL; test++ )
|
|
|
|
{
|
|
|
|
if( !strcmp( *argp, test->name ) )
|
|
|
|
{
|
|
|
|
if( test->function( v ) != 0 )
|
|
|
|
{
|
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
suites_tested++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( test->name == NULL )
|
|
|
|
{
|
|
|
|
mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
|
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-03-11 00:40:54 +00:00
|
|
|
{
|
2017-12-20 17:09:27 +00:00
|
|
|
/* Run all the tests except excluded ones */
|
2017-12-15 14:01:27 +00:00
|
|
|
for( test = selftests; test->name != NULL; test++ )
|
2017-12-15 13:57:18 +00:00
|
|
|
{
|
2017-12-20 17:09:27 +00:00
|
|
|
if( exclude_mode )
|
|
|
|
{
|
|
|
|
char **excluded;
|
|
|
|
for( excluded = argp; *excluded != NULL; ++excluded )
|
|
|
|
{
|
|
|
|
if( !strcmp( *excluded, test->name ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( *excluded )
|
|
|
|
{
|
|
|
|
if( v )
|
|
|
|
mbedtls_printf( " Skip: %s\n", test->name );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 14:01:27 +00:00
|
|
|
if( test->function( v ) != 0 )
|
|
|
|
{
|
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
suites_tested++;
|
2017-12-15 13:57:18 +00:00
|
|
|
}
|
2016-03-11 00:40:54 +00:00
|
|
|
}
|
2013-07-03 11:37:05 +00:00
|
|
|
|
2016-07-14 13:30:45 +00:00
|
|
|
#else
|
2017-12-20 17:09:27 +00:00
|
|
|
(void) exclude_mode;
|
2016-07-14 13:30:45 +00:00
|
|
|
mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
|
|
|
|
#endif
|
|
|
|
|
2014-11-27 10:33:55 +00:00
|
|
|
if( v != 0 )
|
|
|
|
{
|
2016-03-14 23:09:39 +00:00
|
|
|
mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
|
2016-03-11 00:40:54 +00:00
|
|
|
|
|
|
|
if( suites_failed > 0)
|
|
|
|
{
|
|
|
|
mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mbedtls_printf( " [ All tests PASS ]\n\n" );
|
|
|
|
}
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf( " Press Enter to exit this program.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-11 00:40:54 +00:00
|
|
|
if( suites_failed > 0)
|
2016-03-18 16:25:52 +00:00
|
|
|
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
2016-03-11 00:40:54 +00:00
|
|
|
|
2019-08-16 06:15:04 +00:00
|
|
|
mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|