2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Self-test demonstration program
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-19 17:54:54 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#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 16:49:45 +01:00
|
|
|
#include "mbedtls/cmac.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/md5.h"
|
|
|
|
#include "mbedtls/ripemd160.h"
|
|
|
|
#include "mbedtls/sha1.h"
|
|
|
|
#include "mbedtls/sha256.h"
|
|
|
|
#include "mbedtls/sha512.h"
|
2022-05-20 20:42:33 +02:00
|
|
|
#include "mbedtls/sha3.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/des.h"
|
|
|
|
#include "mbedtls/aes.h"
|
|
|
|
#include "mbedtls/camellia.h"
|
2017-11-30 17:00:34 +01:00
|
|
|
#include "mbedtls/aria.h"
|
2016-05-18 15:09:28 +02:00
|
|
|
#include "mbedtls/chacha20.h"
|
|
|
|
#include "mbedtls/poly1305.h"
|
2018-05-07 10:43:27 +02:00
|
|
|
#include "mbedtls/chachapoly.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/base64.h"
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/rsa.h"
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/pkcs5.h"
|
|
|
|
#include "mbedtls/ecp.h"
|
2015-08-05 15:44:42 +02:00
|
|
|
#include "mbedtls/ecjpake.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/timing.h"
|
2018-07-15 08:33:07 +02:00
|
|
|
#include "mbedtls/nist_kw.h"
|
2022-04-11 13:59:45 +02:00
|
|
|
#include "mbedtls/debug.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-10-16 14:22:44 +02:00
|
|
|
#include <limits.h>
|
2015-02-11 15:06:19 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-02-11 15:06:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/memory_buffer_alloc.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-06 18:43:31 +01:00
|
|
|
|
2019-09-17 18:18:58 +02: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). */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int calloc_self_test(int verbose)
|
2019-09-17 18:18:58 +02:00
|
|
|
{
|
|
|
|
int failures = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
void *empty1 = mbedtls_calloc(0, 1);
|
|
|
|
void *empty2 = mbedtls_calloc(0, 1);
|
|
|
|
void *buffer1 = mbedtls_calloc(1, 1);
|
|
|
|
void *buffer2 = mbedtls_calloc(1, 1);
|
2023-05-04 23:29:55 +02:00
|
|
|
unsigned int buffer_3_size = 256;
|
|
|
|
unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
|
|
|
|
unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
|
|
|
|
unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
|
2023-05-09 22:38:04 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (empty1 == NULL && empty2 == NULL) {
|
|
|
|
if (verbose) {
|
2023-04-27 15:30:18 +02:00
|
|
|
mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else if (empty1 == NULL || empty2 == NULL) {
|
|
|
|
if (verbose) {
|
2023-04-27 15:30:18 +02:00
|
|
|
mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
++failures;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (empty1 == empty2) {
|
|
|
|
if (verbose) {
|
2023-04-27 15:30:18 +02:00
|
|
|
mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-12-07 15:14:21 +01:00
|
|
|
empty2 = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (verbose) {
|
2023-04-27 15:30:18 +02:00
|
|
|
mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_free(empty1);
|
|
|
|
mbedtls_free(empty2);
|
|
|
|
|
|
|
|
empty1 = mbedtls_calloc(1, 0);
|
|
|
|
empty2 = mbedtls_calloc(1, 0);
|
|
|
|
if (empty1 == NULL && empty2 == NULL) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
|
|
|
|
}
|
|
|
|
} else if (empty1 == NULL || empty2 == NULL) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
|
|
|
|
}
|
|
|
|
++failures;
|
|
|
|
} else if (empty1 == empty2) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
|
|
|
|
}
|
2023-12-07 15:14:21 +01:00
|
|
|
empty2 = NULL;
|
2023-04-27 15:30:18 +02:00
|
|
|
} else {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buffer1 == NULL || buffer2 == NULL) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1): failed (NULL)\n");
|
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
++failures;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (buffer1 == buffer2) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
|
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
++failures;
|
2023-12-07 15:14:21 +01:00
|
|
|
buffer2 = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1): passed\n");
|
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(buffer1);
|
|
|
|
buffer1 = mbedtls_calloc(1, 1);
|
|
|
|
if (buffer1 == NULL) {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
|
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
++failures;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(1 again): passed\n");
|
|
|
|
}
|
2019-09-17 18:18:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 23:29:55 +02:00
|
|
|
for (unsigned int i = 0; i < buffer_3_size; i++) {
|
2023-04-27 15:30:18 +02:00
|
|
|
if (buffer3[i] != 0) {
|
|
|
|
++failures;
|
|
|
|
if (verbose) {
|
2023-05-04 23:29:55 +02:00
|
|
|
mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
|
|
|
|
buffer_3_size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < buffer_4_size; i++) {
|
|
|
|
if (buffer4[i] != 0) {
|
|
|
|
++failures;
|
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
|
|
|
|
buffer_4_size);
|
2023-04-27 15:30:18 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose) {
|
|
|
|
mbedtls_printf("\n");
|
|
|
|
}
|
|
|
|
mbedtls_free(empty1);
|
|
|
|
mbedtls_free(empty2);
|
|
|
|
mbedtls_free(buffer1);
|
|
|
|
mbedtls_free(buffer2);
|
2023-04-27 15:30:18 +02:00
|
|
|
mbedtls_free(buffer3);
|
2023-05-04 23:29:55 +02:00
|
|
|
mbedtls_free(buffer4);
|
2023-01-11 14:50:10 +01:00
|
|
|
return failures;
|
2019-09-17 18:18:58 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
|
2015-06-22 10:48:01 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[10] = "xxxxxxxxx";
|
2015-06-26 11:24:32 +02:00
|
|
|
const char ref[10] = "xxxxxxxxx";
|
2015-06-22 10:48:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(buf, n, "%s", "123");
|
|
|
|
if (ret < 0 || (size_t) ret >= n) {
|
2015-06-22 10:48:01 +02:00
|
|
|
ret = -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-06-22 10:48:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
|
2015-06-26 11:24:32 +02:00
|
|
|
ref_ret != ret ||
|
2023-01-11 14:50:10 +01:00
|
|
|
memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
|
|
|
|
return 1;
|
2015-06-22 10:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-06-22 10:48:01 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int run_test_snprintf(void)
|
2015-06-22 10:48:01 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
|
|
|
|
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 10:48:01 +02:00
|
|
|
}
|
|
|
|
|
2016-06-18 23:45:37 +02: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 14:57:18 +01:00
|
|
|
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
2023-01-11 14:50:10 +01:00
|
|
|
static void create_entropy_seed_file(void)
|
2016-06-18 23:45:37 +02:00
|
|
|
{
|
|
|
|
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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
result = mbedtls_platform_std_nv_seed_read(seed_value,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE);
|
|
|
|
if (0 == result) {
|
2016-06-18 23:45:37 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2016-06-18 23:45:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
result = mbedtls_platform_entropy_poll(NULL,
|
|
|
|
seed_value,
|
|
|
|
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
|
|
|
&output_len);
|
|
|
|
if (0 != result) {
|
2016-06-18 23:45:37 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2016-06-18 23:45:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
|
2016-06-18 23:45:37 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2016-06-18 23:45:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
|
2016-06-18 23:45:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_entropy_self_test_wrapper(int verbose)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2017-12-15 14:57:18 +01:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
2023-01-11 14:50:10 +01:00
|
|
|
create_entropy_seed_file();
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_entropy_self_test(verbose);
|
2017-12-15 14:57:18 +01:00
|
|
|
}
|
2013-07-03 13:37:05 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
|
2017-12-15 14:57:18 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verbose != 0) {
|
2017-12-15 14:57:18 +01:00
|
|
|
#if defined(MBEDTLS_MEMORY_DEBUG)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_status();
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_free();
|
|
|
|
return mbedtls_memory_buffer_alloc_self_test(verbose);
|
2017-12-15 14:57:18 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2017-12-15 14:57:18 +01:00
|
|
|
const char *name;
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*function)(int);
|
2017-12-15 14:57:18 +01:00
|
|
|
} selftest_t;
|
|
|
|
|
|
|
|
const selftest_t selftests[] =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "calloc", calloc_self_test },
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "md5", mbedtls_md5_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RIPEMD160_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "ripemd160", mbedtls_ripemd160_self_test },
|
2014-01-17 14:44:38 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "sha1", mbedtls_sha1_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2022-12-14 10:58:02 +01:00
|
|
|
#if defined(MBEDTLS_SHA224_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "sha224", mbedtls_sha224_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "sha256", mbedtls_sha256_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2022-12-14 08:55:53 +01:00
|
|
|
#if defined(MBEDTLS_SHA384_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "sha384", mbedtls_sha384_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "sha512", mbedtls_sha512_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2022-05-20 20:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SHA3_C)
|
2023-02-08 12:52:18 +01:00
|
|
|
{ "sha3", mbedtls_sha3_self_test },
|
2022-05-20 20:42:33 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "des", mbedtls_des_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "aes", mbedtls_aes_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "gcm", mbedtls_gcm_self_test },
|
2012-03-20 14:50:09 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "ccm", mbedtls_ccm_self_test },
|
2014-05-02 15:17:29 +02:00
|
|
|
#endif
|
2018-07-15 08:33:07 +02:00
|
|
|
#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "nist_kw", mbedtls_nist_kw_self_test },
|
2018-07-15 08:33:07 +02:00
|
|
|
#endif
|
2016-10-06 11:27:22 +02:00
|
|
|
#if defined(MBEDTLS_CMAC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "cmac", mbedtls_cmac_self_test },
|
2015-12-11 16:49:45 +01:00
|
|
|
#endif
|
2016-05-18 15:09:28 +02:00
|
|
|
#if defined(MBEDTLS_CHACHA20_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "chacha20", mbedtls_chacha20_self_test },
|
2016-05-18 15:09:28 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_POLY1305_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "poly1305", mbedtls_poly1305_self_test },
|
2016-05-18 15:09:28 +02:00
|
|
|
#endif
|
2018-05-07 10:43:27 +02:00
|
|
|
#if defined(MBEDTLS_CHACHAPOLY_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "chacha20-poly1305", mbedtls_chachapoly_self_test },
|
2016-05-18 15:09:28 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_BASE64_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "base64", mbedtls_base64_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "mpi", mbedtls_mpi_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "rsa", mbedtls_rsa_self_test },
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CAMELLIA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "camellia", mbedtls_camellia_self_test },
|
2009-01-11 00:31:23 +01:00
|
|
|
#endif
|
2017-11-30 17:00:34 +01:00
|
|
|
#if defined(MBEDTLS_ARIA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "aria", mbedtls_aria_self_test },
|
2017-11-30 17:00:34 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "ctr_drbg", mbedtls_ctr_drbg_self_test },
|
2011-11-27 15:46:59 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "hmac_drbg", mbedtls_hmac_drbg_self_test },
|
2014-01-31 11:12:09 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "ecp", mbedtls_ecp_self_test },
|
2012-11-13 20:57:00 +01:00
|
|
|
#endif
|
2015-08-05 15:44:42 +02:00
|
|
|
#if defined(MBEDTLS_ECJPAKE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "ecjpake", mbedtls_ecjpake_self_test },
|
2015-08-05 15:44:42 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "dhm", mbedtls_dhm_self_test },
|
2013-09-15 17:43:54 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ENTROPY_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "entropy", mbedtls_entropy_self_test_wrapper },
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PKCS5_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "pkcs5", mbedtls_pkcs5_self_test },
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
|
|
|
/* Heap test comes last */
|
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
{ "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
{ NULL, NULL }
|
2017-12-15 14:57:18 +01:00
|
|
|
};
|
2017-12-15 15:01:27 +01:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2016-06-18 23:45:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2017-12-15 14:57:18 +01:00
|
|
|
{
|
2017-12-15 15:01:27 +01:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2017-12-15 14:57:18 +01:00
|
|
|
const selftest_t *test;
|
2017-12-15 15:01:27 +01:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2017-12-20 18:09:27 +01: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 14:57:18 +01:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
|
|
|
|
unsigned char buf[1000000];
|
2016-06-18 23:45:37 +02:00
|
|
|
#endif
|
2017-12-15 14:57:18 +01:00
|
|
|
void *pointer;
|
2016-06-18 23:45:37 +02:00
|
|
|
|
2023-02-10 17:16:17 +01:00
|
|
|
/*
|
|
|
|
* Check some basic platform requirements as specified in README.md
|
|
|
|
*/
|
|
|
|
if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
|
|
|
|
mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(int) < 4) {
|
|
|
|
mbedtls_printf("int must be at least 32 bits\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(size_t) < 4) {
|
|
|
|
mbedtls_printf("size_t must be at least 32 bits\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t endian_test = 0x12345678;
|
|
|
|
char *p = (char *) &endian_test;
|
|
|
|
if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
|
|
|
|
!(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
|
|
|
|
mbedtls_printf("Mixed-endian platforms are not supported\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:57:18 +01: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.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&pointer, 0, sizeof(void *));
|
|
|
|
if (pointer != NULL) {
|
|
|
|
mbedtls_printf("all-bits-zero is not a NULL pointer\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2014-05-30 10:34:15 +02:00
|
|
|
|
2018-10-16 14:22:44 +02:00
|
|
|
/*
|
|
|
|
* The C standard allows padding bits in the representation
|
|
|
|
* of standard integer types, but our code does currently not
|
|
|
|
* support them.
|
|
|
|
*
|
|
|
|
* Here we check that the underlying C implementation doesn't
|
|
|
|
* use padding bits, and fail cleanly if it does.
|
|
|
|
*
|
|
|
|
* The check works by casting the maximum value representable
|
|
|
|
* by a given integer type into the unpadded integer type of the
|
|
|
|
* same bit-width and checking that it agrees with the maximum value
|
|
|
|
* of that unpadded type. For example, for a 4-byte int,
|
|
|
|
* MAX_INT should be 0x7fffffff in int32_t. This assumes that
|
|
|
|
* CHAR_BIT == 8, which is checked in check_config.h.
|
2018-10-19 18:32:45 +02:00
|
|
|
*
|
|
|
|
* We assume that [u]intxx_t exist and that they don't
|
|
|
|
* have padding bits, as the standard requires.
|
2018-10-16 14:22:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define CHECK_PADDING_SIGNED(TYPE, NAME) \
|
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
|
|
|
|
sizeof(TYPE) == 8) { \
|
|
|
|
if ((sizeof(TYPE) == 2 && \
|
|
|
|
(int16_t) NAME ## _MAX != 0x7FFF) || \
|
|
|
|
(sizeof(TYPE) == 4 && \
|
|
|
|
(int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
|
|
|
|
(sizeof(TYPE) == 8 && \
|
|
|
|
(int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
|
2022-04-08 13:41:23 +02:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
|
2022-04-08 13:41:23 +02:00
|
|
|
} \
|
|
|
|
} else { \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
|
|
|
|
" - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
|
|
|
|
sizeof(TYPE)); \
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
|
2018-10-16 14:22:44 +02:00
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2018-10-16 14:22:44 +02:00
|
|
|
|
|
|
|
#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
|
|
|
|
do \
|
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((sizeof(TYPE) == 2 && \
|
|
|
|
(uint16_t) NAME ## _MAX != 0xFFFF) || \
|
|
|
|
(sizeof(TYPE) == 4 && \
|
|
|
|
(uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
|
|
|
|
(sizeof(TYPE) == 8 && \
|
|
|
|
(uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
|
2018-10-16 14:22:44 +02:00
|
|
|
{ \
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
|
2018-10-16 14:22:44 +02:00
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2018-10-16 14:22:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_PADDING_SIGNED(short, SHRT);
|
|
|
|
CHECK_PADDING_SIGNED(int, INT);
|
|
|
|
CHECK_PADDING_SIGNED(long, LONG);
|
|
|
|
CHECK_PADDING_SIGNED(long long, LLONG);
|
|
|
|
CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
|
2018-10-16 14:22:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
|
|
|
|
CHECK_PADDING_UNSIGNED(unsigned, UINT);
|
|
|
|
CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
|
|
|
|
CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
|
|
|
|
CHECK_PADDING_UNSIGNED(size_t, SIZE);
|
2018-10-16 14:22:44 +02:00
|
|
|
|
|
|
|
#undef CHECK_PADDING_SIGNED
|
|
|
|
#undef CHECK_PADDING_UNSIGNED
|
|
|
|
|
2017-12-15 14:57:18 +01:00
|
|
|
/*
|
|
|
|
* Make sure we have a snprintf that correctly zero-terminates
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (run_test_snprintf() != 0) {
|
|
|
|
mbedtls_printf("the snprintf implementation is broken\n");
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2014-03-27 20:12:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
|
|
|
|
if (strcmp(*argp, "--quiet") == 0 ||
|
|
|
|
strcmp(*argp, "-q") == 0) {
|
2017-12-20 18:09:27 +01:00
|
|
|
v = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(*argp, "--exclude") == 0 ||
|
|
|
|
strcmp(*argp, "-x") == 0) {
|
2017-12-20 18:09:27 +01:00
|
|
|
exclude_mode = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2017-12-20 18:09:27 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2017-12-20 18:09:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (v != 0) {
|
|
|
|
mbedtls_printf("\n");
|
|
|
|
}
|
2014-11-27 11:33:55 +01:00
|
|
|
|
2017-12-15 14:57:18 +01:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
|
2017-12-15 14:57:18 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*argp != NULL && exclude_mode == 0) {
|
2017-12-15 15:01:27 +01:00
|
|
|
/* Run the specified tests */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *argp != NULL; argp++) {
|
|
|
|
for (test = selftests; test->name != NULL; test++) {
|
|
|
|
if (!strcmp(*argp, test->name)) {
|
|
|
|
if (test->function(v) != 0) {
|
2017-12-15 15:01:27 +01:00
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
suites_tested++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (test->name == NULL) {
|
|
|
|
mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
|
2017-12-15 15:01:27 +01:00
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2017-12-20 18:09:27 +01:00
|
|
|
/* Run all the tests except excluded ones */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (test = selftests; test->name != NULL; test++) {
|
|
|
|
if (exclude_mode) {
|
2017-12-20 18:09:27 +01:00
|
|
|
char **excluded;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (excluded = argp; *excluded != NULL; ++excluded) {
|
|
|
|
if (!strcmp(*excluded, test->name)) {
|
2017-12-20 18:09:27 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-12-20 18:09:27 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*excluded) {
|
|
|
|
if (v) {
|
|
|
|
mbedtls_printf(" Skip: %s\n", test->name);
|
|
|
|
}
|
2017-12-20 18:09:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (test->function(v) != 0) {
|
2017-12-15 15:01:27 +01:00
|
|
|
suites_failed++;
|
|
|
|
}
|
|
|
|
suites_tested++;
|
2017-12-15 14:57:18 +01:00
|
|
|
}
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2013-07-03 13:37:05 +02:00
|
|
|
|
2016-07-14 15:30:45 +02:00
|
|
|
#else
|
2017-12-20 18:09:27 +01:00
|
|
|
(void) exclude_mode;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
|
2016-07-14 15:30:45 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (v != 0) {
|
|
|
|
mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
|
2016-03-11 01:40:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (suites_failed > 0) {
|
|
|
|
mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
|
|
|
|
} else {
|
|
|
|
mbedtls_printf(" [ All tests PASS ]\n\n");
|
2016-03-11 01:40:54 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (suites_failed > 0) {
|
|
|
|
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
|
|
|
|
}
|
2016-03-11 01:40:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|