Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / programs / test / selftest.c
1 /*
2  *  Self-test demonstration program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  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 #include "mbedtls/hmac_drbg.h"
31 #include "mbedtls/ctr_drbg.h"
32 #include "mbedtls/dhm.h"
33 #include "mbedtls/gcm.h"
34 #include "mbedtls/ccm.h"
35 #include "mbedtls/cmac.h"
36 #include "mbedtls/md2.h"
37 #include "mbedtls/md4.h"
38 #include "mbedtls/md5.h"
39 #include "mbedtls/ripemd160.h"
40 #include "mbedtls/sha1.h"
41 #include "mbedtls/sha256.h"
42 #include "mbedtls/sha512.h"
43 #include "mbedtls/arc4.h"
44 #include "mbedtls/des.h"
45 #include "mbedtls/aes.h"
46 #include "mbedtls/camellia.h"
47 #include "mbedtls/aria.h"
48 #include "mbedtls/chacha20.h"
49 #include "mbedtls/poly1305.h"
50 #include "mbedtls/chachapoly.h"
51 #include "mbedtls/base64.h"
52 #include "mbedtls/bignum.h"
53 #include "mbedtls/rsa.h"
54 #include "mbedtls/x509.h"
55 #include "mbedtls/xtea.h"
56 #include "mbedtls/pkcs5.h"
57 #include "mbedtls/ecp.h"
58 #include "mbedtls/ecjpake.h"
59 #include "mbedtls/timing.h"
60 #include "mbedtls/nist_kw.h"
61
62 #include <string.h>
63
64 #if defined(MBEDTLS_PLATFORM_C)
65 #include "mbedtls/platform.h"
66 #else
67 #include <stdio.h>
68 #include <stdlib.h>
69 #define mbedtls_printf     printf
70 #define mbedtls_snprintf   snprintf
71 #define mbedtls_exit       exit
72 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
73 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
74 #endif
75
76 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
77 #include "mbedtls/memory_buffer_alloc.h"
78 #endif
79
80 #if defined(MBEDTLS_CHECK_PARAMS)
81 #include "mbedtls/platform_util.h"
82 void mbedtls_param_failed( const char *failure_condition,
83                            const char *file,
84                            int line )
85 {
86     mbedtls_printf( "%s:%i: Input param failed - %s\n",
87                     file, line, failure_condition );
88     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
89 }
90 #endif
91
92 static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
93 {
94     int ret;
95     char buf[10] = "xxxxxxxxx";
96     const char ref[10] = "xxxxxxxxx";
97
98     ret = mbedtls_snprintf( buf, n, "%s", "123" );
99     if( ret < 0 || (size_t) ret >= n )
100         ret = -1;
101
102     if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
103         ref_ret != ret ||
104         memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
105     {
106         return( 1 );
107     }
108
109     return( 0 );
110 }
111
112 static int run_test_snprintf( void )
113 {
114     return( test_snprintf( 0, "xxxxxxxxx",  -1 ) != 0 ||
115             test_snprintf( 1, "",           -1 ) != 0 ||
116             test_snprintf( 2, "1",          -1 ) != 0 ||
117             test_snprintf( 3, "12",         -1 ) != 0 ||
118             test_snprintf( 4, "123",         3 ) != 0 ||
119             test_snprintf( 5, "123",         3 ) != 0 );
120 }
121
122 /*
123  * Check if a seed file is present, and if not create one for the entropy
124  * self-test. If this fails, we attempt the test anyway, so no error is passed
125  * back.
126  */
127 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
128 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
129 static void create_entropy_seed_file( void )
130 {
131     int result;
132     size_t output_len = 0;
133     unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
134
135     /* Attempt to read the entropy seed file. If this fails - attempt to write
136      * to the file to ensure one is present. */
137     result = mbedtls_platform_std_nv_seed_read( seed_value,
138                                                     MBEDTLS_ENTROPY_BLOCK_SIZE );
139     if( 0 == result )
140         return;
141
142     result = mbedtls_platform_entropy_poll( NULL,
143                                             seed_value,
144                                             MBEDTLS_ENTROPY_BLOCK_SIZE,
145                                             &output_len );
146     if( 0 != result )
147         return;
148
149     if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
150         return;
151
152     mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
153 }
154 #endif
155
156 int mbedtls_entropy_self_test_wrapper( int verbose )
157 {
158 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
159     create_entropy_seed_file( );
160 #endif
161     return( mbedtls_entropy_self_test( verbose ) );
162 }
163 #endif
164
165 #if defined(MBEDTLS_SELF_TEST)
166 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
167 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
168 {
169     if( verbose != 0 )
170     {
171 #if defined(MBEDTLS_MEMORY_DEBUG)
172         mbedtls_memory_buffer_alloc_status( );
173 #endif
174     }
175     mbedtls_memory_buffer_alloc_free( );
176     return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
177 }
178 #endif
179
180 typedef struct
181 {
182     const char *name;
183     int ( *function )( int );
184 } selftest_t;
185
186 const selftest_t selftests[] =
187 {
188 #if defined(MBEDTLS_MD2_C)
189     {"md2", mbedtls_md2_self_test},
190 #endif
191 #if defined(MBEDTLS_MD4_C)
192     {"md4", mbedtls_md4_self_test},
193 #endif
194 #if defined(MBEDTLS_MD5_C)
195     {"md5", mbedtls_md5_self_test},
196 #endif
197 #if defined(MBEDTLS_RIPEMD160_C)
198     {"ripemd160", mbedtls_ripemd160_self_test},
199 #endif
200 #if defined(MBEDTLS_SHA1_C)
201     {"sha1", mbedtls_sha1_self_test},
202 #endif
203 #if defined(MBEDTLS_SHA256_C)
204     {"sha256", mbedtls_sha256_self_test},
205 #endif
206 #if defined(MBEDTLS_SHA512_C)
207     {"sha512", mbedtls_sha512_self_test},
208 #endif
209 #if defined(MBEDTLS_ARC4_C)
210     {"arc4", mbedtls_arc4_self_test},
211 #endif
212 #if defined(MBEDTLS_DES_C)
213     {"des", mbedtls_des_self_test},
214 #endif
215 #if defined(MBEDTLS_AES_C)
216     {"aes", mbedtls_aes_self_test},
217 #endif
218 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
219     {"gcm", mbedtls_gcm_self_test},
220 #endif
221 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
222     {"ccm", mbedtls_ccm_self_test},
223 #endif
224 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
225     {"nist_kw", mbedtls_nist_kw_self_test},
226 #endif
227 #if defined(MBEDTLS_CMAC_C)
228     {"cmac", mbedtls_cmac_self_test},
229 #endif
230 #if defined(MBEDTLS_CHACHA20_C)
231     {"chacha20", mbedtls_chacha20_self_test},
232 #endif
233 #if defined(MBEDTLS_POLY1305_C)
234     {"poly1305", mbedtls_poly1305_self_test},
235 #endif
236 #if defined(MBEDTLS_CHACHAPOLY_C)
237     {"chacha20-poly1305", mbedtls_chachapoly_self_test},
238 #endif
239 #if defined(MBEDTLS_BASE64_C)
240     {"base64", mbedtls_base64_self_test},
241 #endif
242 #if defined(MBEDTLS_BIGNUM_C)
243     {"mpi", mbedtls_mpi_self_test},
244 #endif
245 #if defined(MBEDTLS_RSA_C)
246     {"rsa", mbedtls_rsa_self_test},
247 #endif
248 #if defined(MBEDTLS_X509_USE_C)
249     {"x509", mbedtls_x509_self_test},
250 #endif
251 #if defined(MBEDTLS_XTEA_C)
252     {"xtea", mbedtls_xtea_self_test},
253 #endif
254 #if defined(MBEDTLS_CAMELLIA_C)
255     {"camellia", mbedtls_camellia_self_test},
256 #endif
257 #if defined(MBEDTLS_ARIA_C)
258     {"aria", mbedtls_aria_self_test},
259 #endif
260 #if defined(MBEDTLS_CTR_DRBG_C)
261     {"ctr_drbg", mbedtls_ctr_drbg_self_test},
262 #endif
263 #if defined(MBEDTLS_HMAC_DRBG_C)
264     {"hmac_drbg", mbedtls_hmac_drbg_self_test},
265 #endif
266 #if defined(MBEDTLS_ECP_C)
267     {"ecp", mbedtls_ecp_self_test},
268 #endif
269 #if defined(MBEDTLS_ECJPAKE_C)
270     {"ecjpake", mbedtls_ecjpake_self_test},
271 #endif
272 #if defined(MBEDTLS_DHM_C)
273     {"dhm", mbedtls_dhm_self_test},
274 #endif
275 #if defined(MBEDTLS_ENTROPY_C)
276     {"entropy", mbedtls_entropy_self_test_wrapper},
277 #endif
278 #if defined(MBEDTLS_PKCS5_C)
279     {"pkcs5", mbedtls_pkcs5_self_test},
280 #endif
281 /* Slower test after the faster ones */
282 #if defined(MBEDTLS_TIMING_C)
283     {"timing", mbedtls_timing_self_test},
284 #endif
285 /* Heap test comes last */
286 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
287     {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
288 #endif
289     {NULL, NULL}
290 };
291 #endif /* MBEDTLS_SELF_TEST */
292
293 int main( int argc, char *argv[] )
294 {
295 #if defined(MBEDTLS_SELF_TEST)
296     const selftest_t *test;
297 #endif /* MBEDTLS_SELF_TEST */
298     char **argp;
299     int v = 1; /* v=1 for verbose mode */
300     int exclude_mode = 0;
301     int suites_tested = 0, suites_failed = 0;
302 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
303     unsigned char buf[1000000];
304 #endif
305     void *pointer;
306
307     /*
308      * The C standard doesn't guarantee that all-bits-0 is the representation
309      * of a NULL pointer. We do however use that in our code for initializing
310      * structures, which should work on every modern platform. Let's be sure.
311      */
312     memset( &pointer, 0, sizeof( void * ) );
313     if( pointer != NULL )
314     {
315         mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
316         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
317     }
318
319     /*
320      * Make sure we have a snprintf that correctly zero-terminates
321      */
322     if( run_test_snprintf() != 0 )
323     {
324         mbedtls_printf( "the snprintf implementation is broken\n" );
325         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
326     }
327
328     for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
329     {
330         if( strcmp( *argp, "--quiet" ) == 0 ||
331             strcmp( *argp, "-q" ) == 0 )
332         {
333             v = 0;
334         }
335         else if( strcmp( *argp, "--exclude" ) == 0 ||
336                  strcmp( *argp, "-x" ) == 0 )
337         {
338             exclude_mode = 1;
339         }
340         else
341             break;
342     }
343
344     if( v != 0 )
345         mbedtls_printf( "\n" );
346
347 #if defined(MBEDTLS_SELF_TEST)
348
349 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
350     mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
351 #endif
352
353     if( *argp != NULL && exclude_mode == 0 )
354     {
355         /* Run the specified tests */
356         for( ; *argp != NULL; argp++ )
357         {
358             for( test = selftests; test->name != NULL; test++ )
359             {
360                 if( !strcmp( *argp, test->name ) )
361                 {
362                     if( test->function( v )  != 0 )
363                     {
364                         suites_failed++;
365                     }
366                     suites_tested++;
367                     break;
368                 }
369             }
370             if( test->name == NULL )
371             {
372                 mbedtls_printf( "  Test suite %s not available -> failed\n\n", *argp );
373                 suites_failed++;
374             }
375         }
376     }
377     else
378     {
379         /* Run all the tests except excluded ones */
380         for( test = selftests; test->name != NULL; test++ )
381         {
382             if( exclude_mode )
383             {
384                 char **excluded;
385                 for( excluded = argp; *excluded != NULL; ++excluded )
386                 {
387                     if( !strcmp( *excluded, test->name ) )
388                         break;
389                 }
390                 if( *excluded )
391                 {
392                     if( v )
393                         mbedtls_printf( "  Skip: %s\n", test->name );
394                     continue;
395                 }
396             }
397             if( test->function( v )  != 0 )
398             {
399                 suites_failed++;
400             }
401             suites_tested++;
402         }
403     }
404
405 #else
406     (void) exclude_mode;
407     mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
408 #endif
409
410     if( v != 0 )
411     {
412         mbedtls_printf( "  Executed %d test suites\n\n", suites_tested );
413
414         if( suites_failed > 0)
415         {
416             mbedtls_printf( "  [ %d tests FAIL ]\n\n", suites_failed );
417         }
418         else
419         {
420             mbedtls_printf( "  [ All tests PASS ]\n\n" );
421         }
422 #if defined(_WIN32)
423         mbedtls_printf( "  Press Enter to exit this program.\n" );
424         fflush( stdout ); getchar();
425 #endif
426     }
427
428     if( suites_failed > 0)
429         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
430
431     /* return() is here to prevent compiler warnings */
432     return( MBEDTLS_EXIT_SUCCESS );
433 }
434