selftest.c 6.29 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
Rich Evans's avatar
Rich Evans committed
63 64
#endif

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

69 70
int main( int argc, char *argv[] )
{
71
    int ret = 0, v;
72
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
73 74
    unsigned char buf[1000000];
#endif
75 76 77 78 79 80 81 82 83 84 85 86 87
    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 );
    }
88 89 90 91 92 93

    if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
        v = 0;
    else
    {
        v = 1;
94
        mbedtls_printf( "\n" );
95 96
    }

97
#if defined(MBEDTLS_SELF_TEST)
98

99 100
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
101 102
#endif

103 104
#if defined(MBEDTLS_MD2_C)
    if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
105 106 107
        return( ret );
#endif

108 109
#if defined(MBEDTLS_MD4_C)
    if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
110 111 112
        return( ret );
#endif

113 114
#if defined(MBEDTLS_MD5_C)
    if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
115 116 117
        return( ret );
#endif

118 119
#if defined(MBEDTLS_RIPEMD160_C)
    if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
120 121 122
        return( ret );
#endif

123 124
#if defined(MBEDTLS_SHA1_C)
    if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
125 126 127
        return( ret );
#endif

128 129
#if defined(MBEDTLS_SHA256_C)
    if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
130 131 132
        return( ret );
#endif

133 134
#if defined(MBEDTLS_SHA512_C)
    if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
135 136 137
        return( ret );
#endif

138 139
#if defined(MBEDTLS_ARC4_C)
    if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
140 141 142
        return( ret );
#endif

143 144
#if defined(MBEDTLS_DES_C)
    if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
145 146 147
        return( ret );
#endif

148 149
#if defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
150 151 152
        return( ret );
#endif

153 154
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
155 156 157
        return( ret );
#endif

158 159
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
    if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
160 161 162
        return( ret );
#endif

163 164
#if defined(MBEDTLS_BASE64_C)
    if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
165 166 167
        return( ret );
#endif

168 169
#if defined(MBEDTLS_BIGNUM_C)
    if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
170 171 172
        return( ret );
#endif

173 174
#if defined(MBEDTLS_RSA_C)
    if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
175 176 177
        return( ret );
#endif

178 179
#if defined(MBEDTLS_X509_USE_C)
    if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
180 181 182
        return( ret );
#endif

183 184
#if defined(MBEDTLS_XTEA_C)
    if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
185 186 187
        return( ret );
#endif

188 189
#if defined(MBEDTLS_CAMELLIA_C)
    if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
190 191 192
        return( ret );
#endif

193 194
#if defined(MBEDTLS_CTR_DRBG_C)
    if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
195 196 197
        return( ret );
#endif

198 199
#if defined(MBEDTLS_HMAC_DRBG_C)
    if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
200 201 202
        return( ret );
#endif

203 204
#if defined(MBEDTLS_ECP_C)
    if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
205 206 207
        return( ret );
#endif

208 209
#if defined(MBEDTLS_DHM_C)
    if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
210 211 212
        return( ret );
#endif

213 214
#if defined(MBEDTLS_ENTROPY_C)
    if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
215 216 217
        return( ret );
#endif

218 219
#if defined(MBEDTLS_PKCS5_C)
    if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
220 221
        return( ret );
#endif
222 223

/* Slow tests last */
224

225
/* Not stable enough on Windows and FreeBSD yet */
226 227
#if __linux__ && defined(MBEDTLS_TIMING_C)
    if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
228 229 230
        return( ret );
#endif

231
#else
232
    mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
233 234
#endif

235 236
    if( v != 0 )
    {
237 238
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
        mbedtls_memory_buffer_alloc_status();
239
#endif
240 241
    }

242 243
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
    mbedtls_memory_buffer_alloc_free();
244

245
    if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
246 247
        return( ret );
#endif
248

249 250
    if( v != 0 )
    {
251
        mbedtls_printf( "  [ All tests passed ]\n\n" );
252
#if defined(_WIN32)
253
        mbedtls_printf( "  Press Enter to exit this program.\n" );
254 255 256 257 258 259
        fflush( stdout ); getchar();
#endif
    }

    return( ret );
}