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

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

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

Rich Evans's avatar
Rich Evans committed
55 56 57
#include <stdio.h>
#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
#define mbedtls_printf     printf
63
#define mbedtls_snprintf   snprintf
Rich Evans's avatar
Rich Evans committed
64 65
#endif

66
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
67
#include "mbedtls/memory_buffer_alloc.h"
68 69
#endif

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
{
    int ret;
    char buf[10] = "xxxxxxxxx";

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

    if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
        ref_ret != ret )
    {
        return( 1 );
    }

    return( 0 );
}

static int run_test_snprintf( void )
{
    return( test_snprintf( 0, "xxxxxxxxx",  -1 ) != 0 ||
            test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
            test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
            test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
            test_snprintf( 4, "123\0xxxxx",  3 ) != 0 ||
            test_snprintf( 5, "123\0xxxxx",  3 ) != 0 );
}

98 99
int main( int argc, char *argv[] )
{
100
    int ret = 0, v;
101
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
102 103
    unsigned char buf[1000000];
#endif
104 105 106 107 108 109 110 111 112 113 114 115 116
    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" );
        return( 1 );
    }
117

118 119 120 121 122 123 124 125 126
    /*
     * Make sure we have a snprintf that correctly zero-terminates
     */
    if( run_test_snprintf() != 0 )
    {
        mbedtls_printf( "the snprintf implementation is broken\n" );
        return( 0 );
    }

127 128 129 130 131
    if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
        v = 0;
    else
    {
        v = 1;
132
        mbedtls_printf( "\n" );
133 134
    }

135
#if defined(MBEDTLS_SELF_TEST)
136

137 138
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
139 140
#endif

141 142
#if defined(MBEDTLS_MD2_C)
    if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
143 144 145
        return( ret );
#endif

146 147
#if defined(MBEDTLS_MD4_C)
    if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
148 149 150
        return( ret );
#endif

151 152
#if defined(MBEDTLS_MD5_C)
    if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
153 154 155
        return( ret );
#endif

156 157
#if defined(MBEDTLS_RIPEMD160_C)
    if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
158 159 160
        return( ret );
#endif

161 162
#if defined(MBEDTLS_SHA1_C)
    if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
163 164 165
        return( ret );
#endif

166 167
#if defined(MBEDTLS_SHA256_C)
    if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
168 169 170
        return( ret );
#endif

171 172
#if defined(MBEDTLS_SHA512_C)
    if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
173 174 175
        return( ret );
#endif

176 177
#if defined(MBEDTLS_ARC4_C)
    if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
178 179 180
        return( ret );
#endif

181 182
#if defined(MBEDTLS_DES_C)
    if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
183 184 185
        return( ret );
#endif

186 187
#if defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
188 189 190
        return( ret );
#endif

191 192
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
193 194 195
        return( ret );
#endif

196 197
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
198 199 200
        return( ret );
#endif

201 202
#if defined(MBEDTLS_BASE64_C)
    if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
203 204 205
        return( ret );
#endif

206 207
#if defined(MBEDTLS_BIGNUM_C)
    if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
208 209 210
        return( ret );
#endif

211 212
#if defined(MBEDTLS_RSA_C)
    if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
213 214 215
        return( ret );
#endif

216 217
#if defined(MBEDTLS_X509_USE_C)
    if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
218 219 220
        return( ret );
#endif

221 222
#if defined(MBEDTLS_XTEA_C)
    if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
223 224 225
        return( ret );
#endif

226 227
#if defined(MBEDTLS_CAMELLIA_C)
    if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
228 229 230
        return( ret );
#endif

231 232
#if defined(MBEDTLS_CTR_DRBG_C)
    if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
233 234 235
        return( ret );
#endif

236 237
#if defined(MBEDTLS_HMAC_DRBG_C)
    if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
238 239 240
        return( ret );
#endif

241 242
#if defined(MBEDTLS_ECP_C)
    if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
243 244 245
        return( ret );
#endif

246 247
#if defined(MBEDTLS_DHM_C)
    if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
248 249 250
        return( ret );
#endif

251 252
#if defined(MBEDTLS_ENTROPY_C)
    if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
253 254 255
        return( ret );
#endif

256 257
#if defined(MBEDTLS_PKCS5_C)
    if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
258 259
        return( ret );
#endif
260 261

/* Slow tests last */
262

263
/* Not stable enough on Windows and FreeBSD yet */
264 265
#if __linux__ && defined(MBEDTLS_TIMING_C)
    if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
266 267 268
        return( ret );
#endif

269
#else
270
    mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
271 272
#endif

273 274
    if( v != 0 )
    {
275 276
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_memory_buffer_alloc_status();
277
#endif
278 279
    }

280 281
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_free();
282

283
    if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
284 285
        return( ret );
#endif
286

287 288
    if( v != 0 )
    {
289
        mbedtls_printf( "  [ All tests passed ]\n\n" );
290
#if defined(_WIN32)
291
        mbedtls_printf( "  Press Enter to exit this program.\n" );
292 293 294 295 296 297
        fflush( stdout ); getchar();
#endif
    }

    return( ret );
}