selftest.c 9.96 KB
Newer Older
1 2 3
/*
 *  Self-test demonstration program
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7 8 9
 *  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
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13 14 15 16 17
 *  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.
18
 *
19
 *  This file is part of mbed TLS (https://tls.mbed.org)
20 21
 */

22
#if !defined(MBEDTLS_CONFIG_FILE)
23
#include "mbedtls/config.h"
24
#else
25
#include MBEDTLS_CONFIG_FILE
26
#endif
27

28
#include "mbedtls/entropy.h"
29
#include "mbedtls/entropy_poll.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "mbedtls/hmac_drbg.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/dhm.h"
#include "mbedtls/gcm.h"
#include "mbedtls/ccm.h"
#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"
#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"
53
#include "mbedtls/ecjpake.h"
54
#include "mbedtls/timing.h"
55

Rich Evans's avatar
Rich Evans committed
56 57
#include <string.h>

58
#if defined(MBEDTLS_PLATFORM_C)
59
#include "mbedtls/platform.h"
Rich Evans's avatar
Rich Evans committed
60 61
#else
#include <stdio.h>
62
#include <stdlib.h>
63
#define mbedtls_printf     printf
64
#define mbedtls_snprintf   snprintf
65 66 67
#define mbedtls_exit       exit
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans's avatar
Rich Evans committed
68 69
#endif

70
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
71
#include "mbedtls/memory_buffer_alloc.h"
72 73
#endif

74 75 76 77
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
{
    int ret;
    char buf[10] = "xxxxxxxxx";
78
    const char ref[10] = "xxxxxxxxx";
79 80 81 82 83

    ret = mbedtls_snprintf( buf, n, "%s", "123" );
    if( ret < 0 || (size_t) ret >= n )
        ret = -1;

84 85 86
    if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
        ref_ret != ret ||
        memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
87 88 89 90 91 92 93 94 95 96
    {
        return( 1 );
    }

    return( 0 );
}

static int run_test_snprintf( void )
{
    return( test_snprintf( 0, "xxxxxxxxx",  -1 ) != 0 ||
97 98 99 100 101
            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 );
102 103
}

104 105 106 107 108
/*
 * 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.
 */
109 110
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C) && \
    defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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

138 139
int main( int argc, char *argv[] )
{
140
    int v, suites_tested = 0, suites_failed = 0;
141
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
142 143
    unsigned char buf[1000000];
#endif
144 145 146 147 148 149 150 151 152 153 154
    void *pointer;

    /*
     * 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 )
    {
        mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
155
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
156
    }
157

158 159 160 161 162 163
    /*
     * Make sure we have a snprintf that correctly zero-terminates
     */
    if( run_test_snprintf() != 0 )
    {
        mbedtls_printf( "the snprintf implementation is broken\n" );
164
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
165 166
    }

167 168 169
    if( argc == 2 && ( strcmp( argv[1], "--quiet" ) == 0  ||
        strcmp( argv[1], "-q" ) == 0 ) )
    {
170
        v = 0;
171
    }
172 173 174
    else
    {
        v = 1;
175
        mbedtls_printf( "\n" );
176 177
    }

178
#if defined(MBEDTLS_SELF_TEST)
179

180 181
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
182 183
#endif

184
#if defined(MBEDTLS_MD2_C)
185
    if( mbedtls_md2_self_test( v )  != 0 )
186 187 188 189
    {
        suites_failed++;
    }
    suites_tested++;
190 191
#endif

192
#if defined(MBEDTLS_MD4_C)
193
    if( mbedtls_md4_self_test( v ) != 0 )
194 195 196 197
    {
        suites_failed++;
    }
    suites_tested++;
198 199
#endif

200
#if defined(MBEDTLS_MD5_C)
201
    if( mbedtls_md5_self_test( v ) != 0 )
202 203 204 205
    {
        suites_failed++;
    }
    suites_tested++;
206 207
#endif

208
#if defined(MBEDTLS_RIPEMD160_C)
209
    if( mbedtls_ripemd160_self_test( v ) != 0 )
210 211 212 213
    {
        suites_failed++;
    }
    suites_tested++;
214 215
#endif

216
#if defined(MBEDTLS_SHA1_C)
217
    if( mbedtls_sha1_self_test( v ) != 0 )
218 219 220 221
    {
        suites_failed++;
    }
    suites_tested++;
222 223
#endif

224
#if defined(MBEDTLS_SHA256_C)
225
    if( mbedtls_sha256_self_test( v ) != 0 )
226 227 228 229
    {
        suites_failed++;
    }
    suites_tested++;
230 231
#endif

232
#if defined(MBEDTLS_SHA512_C)
233
    if( mbedtls_sha512_self_test( v ) != 0 )
234 235 236 237
    {
        suites_failed++;
    }
    suites_tested++;
238 239
#endif

240
#if defined(MBEDTLS_ARC4_C)
241
    if( mbedtls_arc4_self_test( v ) != 0 )
242 243 244 245
    {
        suites_failed++;
    }
    suites_tested++;
246 247
#endif

248
#if defined(MBEDTLS_DES_C)
249
    if( mbedtls_des_self_test( v ) != 0 )
250 251 252 253
    {
        suites_failed++;
    }
    suites_tested++;
254 255
#endif

256
#if defined(MBEDTLS_AES_C)
257
    if( mbedtls_aes_self_test( v ) != 0 )
258 259 260 261
    {
        suites_failed++;
    }
    suites_tested++;
262 263
#endif

264
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
265
    if( mbedtls_gcm_self_test( v ) != 0 )
266 267 268 269
    {
        suites_failed++;
    }
    suites_tested++;
270 271
#endif

272
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
273
    if( mbedtls_ccm_self_test( v ) != 0 )
274 275 276 277
    {
        suites_failed++;
    }
    suites_tested++;
278 279
#endif

280
#if defined(MBEDTLS_BASE64_C)
281
    if( mbedtls_base64_self_test( v ) != 0 )
282 283 284 285
    {
        suites_failed++;
    }
    suites_tested++;
286 287
#endif

288
#if defined(MBEDTLS_BIGNUM_C)
289
    if( mbedtls_mpi_self_test( v ) != 0 )
290 291 292 293
    {
        suites_failed++;
    }
    suites_tested++;
294 295
#endif

296
#if defined(MBEDTLS_RSA_C)
297
    if( mbedtls_rsa_self_test( v ) != 0 )
298 299 300 301
    {
        suites_failed++;
    }
    suites_tested++;
302 303
#endif

304
#if defined(MBEDTLS_X509_USE_C)
305
    if( mbedtls_x509_self_test( v ) != 0 )
306 307 308 309
    {
        suites_failed++;
    }
    suites_tested++;
310 311
#endif

312
#if defined(MBEDTLS_XTEA_C)
313
    if( mbedtls_xtea_self_test( v ) != 0 )
314 315 316 317
    {
        suites_failed++;
    }
    suites_tested++;
318 319
#endif

320
#if defined(MBEDTLS_CAMELLIA_C)
321
    if( mbedtls_camellia_self_test( v ) != 0 )
322 323 324 325
    {
        suites_failed++;
    }
    suites_tested++;
326 327
#endif

328
#if defined(MBEDTLS_CTR_DRBG_C)
329
    if( mbedtls_ctr_drbg_self_test( v ) != 0 )
330 331 332 333
    {
        suites_failed++;
    }
    suites_tested++;
334 335
#endif

336
#if defined(MBEDTLS_HMAC_DRBG_C)
337
    if( mbedtls_hmac_drbg_self_test( v ) != 0 )
338 339 340 341
    {
        suites_failed++;
    }
    suites_tested++;
342 343
#endif

344
#if defined(MBEDTLS_ECP_C)
345
    if( mbedtls_ecp_self_test( v ) != 0 )
346 347 348 349
    {
        suites_failed++;
    }
    suites_tested++;
350 351
#endif

352
#if defined(MBEDTLS_ECJPAKE_C)
353
    if( mbedtls_ecjpake_self_test( v ) != 0 )
354 355 356 357
    {
        suites_failed++;
    }
    suites_tested++;
358 359
#endif

360
#if defined(MBEDTLS_DHM_C)
361
    if( mbedtls_dhm_self_test( v ) != 0 )
362 363 364 365
    {
        suites_failed++;
    }
    suites_tested++;
366 367
#endif

368
#if defined(MBEDTLS_ENTROPY_C)
369 370 371 372 373

#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
    create_entropy_seed_file();
#endif

374
    if( mbedtls_entropy_self_test( v ) != 0 )
375 376 377 378
    {
        suites_failed++;
    }
    suites_tested++;
379 380
#endif

381
#if defined(MBEDTLS_PKCS5_C)
382
    if( mbedtls_pkcs5_self_test( v ) != 0 )
383 384 385 386
    {
        suites_failed++;
    }
    suites_tested++;
387
#endif
388 389

/* Slow tests last */
390

391
#if defined(MBEDTLS_TIMING_C)
392
    if( mbedtls_timing_self_test( v ) != 0 )
393 394 395 396
    {
        suites_failed++;
    }
    suites_tested++;
397 398
#endif

399 400
    if( v != 0 )
    {
401 402
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_memory_buffer_alloc_status();
403
#endif
404 405
    }

406 407
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_free();
408
    if( mbedtls_memory_buffer_alloc_self_test( v ) != 0 )
409 410 411 412
    {
        suites_failed++;
    }
    suites_tested++;
413
#endif
414

415 416 417 418
#else
    mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
#endif

419 420
    if( v != 0 )
    {
421
        mbedtls_printf( "  Executed %d test suites\n\n", suites_tested );
422 423 424 425 426 427 428 429 430

        if( suites_failed > 0)
        {
            mbedtls_printf( "  [ %d tests FAIL ]\n\n", suites_failed );
        }
        else
        {
            mbedtls_printf( "  [ All tests PASS ]\n\n" );
        }
431
#if defined(_WIN32)
432
        mbedtls_printf( "  Press Enter to exit this program.\n" );
433 434 435 436
        fflush( stdout ); getchar();
#endif
    }

437
    if( suites_failed > 0)
438
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
439

440
    /* return() is here to prevent compiler warnings */
441
    return( MBEDTLS_EXIT_SUCCESS );
442
}
443