selftest.c 8.57 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

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

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

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

144
#if defined(MBEDTLS_SELF_TEST)
145

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/* Slow tests last */
351

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

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

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

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

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

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

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

401
    mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
402
}
403