selftest.c 8.55 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#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"
#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"
52
#include "mbedtls/ecjpake.h"
53
#include "mbedtls/timing.h"
54

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

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

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

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

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

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

    return( 0 );
}

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

103 104
int main( int argc, char *argv[] )
{
105
    int v, suites_tested = 0, suites_failed = 0;
106
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
107 108
    unsigned char buf[1000000];
#endif
109 110 111 112 113 114 115 116 117 118 119
    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" );
120
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
121
    }
122

123 124 125 126 127 128
    /*
     * Make sure we have a snprintf that correctly zero-terminates
     */
    if( run_test_snprintf() != 0 )
    {
        mbedtls_printf( "the snprintf implementation is broken\n" );
129
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
130 131
    }

132 133 134
    if( argc == 2 && ( strcmp( argv[1], "--quiet" ) == 0  ||
        strcmp( argv[1], "-q" ) == 0 ) )
    {
135
        v = 0;
136
    }
137 138 139
    else
    {
        v = 1;
140
        mbedtls_printf( "\n" );
141 142
    }

143
#if defined(MBEDTLS_SELF_TEST)
144

145 146
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
147 148
#endif

149
#if defined(MBEDTLS_MD2_C)
150
    if( mbedtls_md2_self_test( v )  != 0 )
151 152 153 154
    {
        suites_failed++;
    }
    suites_tested++;
155 156
#endif

157
#if defined(MBEDTLS_MD4_C)
158
    if( mbedtls_md4_self_test( v ) != 0 )
159 160 161 162
    {
        suites_failed++;
    }
    suites_tested++;
163 164
#endif

165
#if defined(MBEDTLS_MD5_C)
166
    if( mbedtls_md5_self_test( v ) != 0 )
167 168 169 170
    {
        suites_failed++;
    }
    suites_tested++;
171 172
#endif

173
#if defined(MBEDTLS_RIPEMD160_C)
174
    if( mbedtls_ripemd160_self_test( v ) != 0 )
175 176 177 178
    {
        suites_failed++;
    }
    suites_tested++;
179 180
#endif

181
#if defined(MBEDTLS_SHA1_C)
182
    if( mbedtls_sha1_self_test( v ) != 0 )
183 184 185 186
    {
        suites_failed++;
    }
    suites_tested++;
187 188
#endif

189
#if defined(MBEDTLS_SHA256_C)
190
    if( mbedtls_sha256_self_test( v ) != 0 )
191 192 193 194
    {
        suites_failed++;
    }
    suites_tested++;
195 196
#endif

197
#if defined(MBEDTLS_SHA512_C)
198
    if( mbedtls_sha512_self_test( v ) != 0 )
199 200 201 202
    {
        suites_failed++;
    }
    suites_tested++;
203 204
#endif

205
#if defined(MBEDTLS_ARC4_C)
206
    if( mbedtls_arc4_self_test( v ) != 0 )
207 208 209 210
    {
        suites_failed++;
    }
    suites_tested++;
211 212
#endif

213
#if defined(MBEDTLS_DES_C)
214
    if( mbedtls_des_self_test( v ) != 0 )
215 216 217 218
    {
        suites_failed++;
    }
    suites_tested++;
219 220
#endif

221
#if defined(MBEDTLS_AES_C)
222
    if( mbedtls_aes_self_test( v ) != 0 )
223 224 225 226
    {
        suites_failed++;
    }
    suites_tested++;
227 228
#endif

229
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
230
    if( mbedtls_gcm_self_test( v ) != 0 )
231 232 233 234
    {
        suites_failed++;
    }
    suites_tested++;
235 236
#endif

237
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
238
    if( mbedtls_ccm_self_test( v ) != 0 )
239 240 241 242
    {
        suites_failed++;
    }
    suites_tested++;
243 244
#endif

245
#if defined(MBEDTLS_BASE64_C)
246
    if( mbedtls_base64_self_test( v ) != 0 )
247 248 249 250
    {
        suites_failed++;
    }
    suites_tested++;
251 252
#endif

253
#if defined(MBEDTLS_BIGNUM_C)
254
    if( mbedtls_mpi_self_test( v ) != 0 )
255 256 257 258
    {
        suites_failed++;
    }
    suites_tested++;
259 260
#endif

261
#if defined(MBEDTLS_RSA_C)
262
    if( mbedtls_rsa_self_test( v ) != 0 )
263 264 265 266
    {
        suites_failed++;
    }
    suites_tested++;
267 268
#endif

269
#if defined(MBEDTLS_X509_USE_C)
270
    if( mbedtls_x509_self_test( v ) != 0 )
271 272 273 274
    {
        suites_failed++;
    }
    suites_tested++;
275 276
#endif

277
#if defined(MBEDTLS_XTEA_C)
278
    if( mbedtls_xtea_self_test( v ) != 0 )
279 280 281 282
    {
        suites_failed++;
    }
    suites_tested++;
283 284
#endif

285
#if defined(MBEDTLS_CAMELLIA_C)
286
    if( mbedtls_camellia_self_test( v ) != 0 )
287 288 289 290
    {
        suites_failed++;
    }
    suites_tested++;
291 292
#endif

293
#if defined(MBEDTLS_CTR_DRBG_C)
294
    if( mbedtls_ctr_drbg_self_test( v ) != 0 )
295 296 297 298
    {
        suites_failed++;
    }
    suites_tested++;
299 300
#endif

301
#if defined(MBEDTLS_HMAC_DRBG_C)
302
    if( mbedtls_hmac_drbg_self_test( v ) != 0 )
303 304 305 306
    {
        suites_failed++;
    }
    suites_tested++;
307 308
#endif

309
#if defined(MBEDTLS_ECP_C)
310
    if( mbedtls_ecp_self_test( v ) != 0 )
311 312 313 314
    {
        suites_failed++;
    }
    suites_tested++;
315 316
#endif

317
#if defined(MBEDTLS_ECJPAKE_C)
318
    if( mbedtls_ecjpake_self_test( v ) != 0 )
319 320 321 322
    {
        suites_failed++;
    }
    suites_tested++;
323 324
#endif

325
#if defined(MBEDTLS_DHM_C)
326
    if( mbedtls_dhm_self_test( v ) != 0 )
327 328 329 330
    {
        suites_failed++;
    }
    suites_tested++;
331 332
#endif

333
#if defined(MBEDTLS_ENTROPY_C)
334
    if( mbedtls_entropy_self_test( v ) != 0 )
335 336 337 338
    {
        suites_failed++;
    }
    suites_tested++;
339 340
#endif

341
#if defined(MBEDTLS_PKCS5_C)
342
    if( mbedtls_pkcs5_self_test( v ) != 0 )
343 344 345 346
    {
        suites_failed++;
    }
    suites_tested++;
347
#endif
348 349

/* Slow tests last */
350

351
#if defined(MBEDTLS_TIMING_C)
352
    if( mbedtls_timing_self_test( v ) != 0 )
353 354 355 356
    {
        suites_failed++;
    }
    suites_tested++;
357 358
#endif

359
#else
360
    mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
361 362
#endif

363 364
    if( v != 0 )
    {
365 366
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_memory_buffer_alloc_status();
367
#endif
368 369
    }

370 371
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_free();
372
    if( mbedtls_memory_buffer_alloc_self_test( v ) != 0 )
373 374 375 376
    {
        suites_failed++;
    }
    suites_tested++;
377
#endif
378

379 380
    if( v != 0 )
    {
381
        mbedtls_printf( "  Executed %d test suites\n\n", suites_tested );
382 383 384 385 386 387 388 389 390

        if( suites_failed > 0)
        {
            mbedtls_printf( "  [ %d tests FAIL ]\n\n", suites_failed );
        }
        else
        {
            mbedtls_printf( "  [ All tests PASS ]\n\n" );
        }
391
#if defined(_WIN32)
392
        mbedtls_printf( "  Press Enter to exit this program.\n" );
393 394 395 396
        fflush( stdout ); getchar();
#endif
    }

397
    if( suites_failed > 0)
398
        mbedtls_exit( MBEDTLS_EXIT_FAILURE );
399

400
    return( MBEDTLS_EXIT_SUCCESS );
401
}
402