Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / programs / test / zeroize.c
1 /*
2  * Zeroize application for debugger-driven testing
3  *
4  * This is a simple test application used for debugger-driven testing to check
5  * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler
6  * optimizations. This application is used by the GDB script at
7  * tests/scripts/test_zeroize.gdb under the assumption that the code does not
8  * change often (as opposed to the library code) because the script sets a
9  * breakpoint at the last return statement in the main() function of this
10  * program. The debugger facilities are then used to manually inspect the
11  * memory and verify that the call to mbedtls_platform_zeroize() was not
12  * eliminated.
13  *
14  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
15  *  SPDX-License-Identifier: Apache-2.0
16  *
17  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
18  *  not use this file except in compliance with the License.
19  *  You may obtain a copy of the License at
20  *
21  *  http://www.apache.org/licenses/LICENSE-2.0
22  *
23  *  Unless required by applicable law or agreed to in writing, software
24  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  *  See the License for the specific language governing permissions and
27  *  limitations under the License.
28  *
29  *  This file is part of mbed TLS (https://tls.mbed.org)
30  */
31
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
34 #else
35 #include MBEDTLS_CONFIG_FILE
36 #endif
37
38 #include <stdio.h>
39
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
42 #else
43 #include <stdlib.h>
44 #define mbedtls_printf     printf
45 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
46 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
47 #endif
48
49 #include "mbedtls/platform_util.h"
50
51 #define BUFFER_LEN 1024
52
53 void usage( void )
54 {
55     mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
56     mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
57     mbedtls_printf( "debugger. This program takes a file as input and\n" );
58     mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
59     mbedtls_printf( "       zeroize <FILE>\n" );
60 }
61
62 int main( int argc, char** argv )
63 {
64     int exit_code = MBEDTLS_EXIT_FAILURE;
65     FILE *fp;
66     char buf[BUFFER_LEN];
67     char *p = buf;
68     char *end = p + BUFFER_LEN;
69     int c;
70
71     if( argc != 2 )
72     {
73         mbedtls_printf( "This program takes exactly 1 agument\n" );
74         usage();
75         return( exit_code );
76     }
77
78     fp = fopen( argv[1], "r" );
79     if( fp == NULL )
80     {
81         mbedtls_printf( "Could not open file '%s'\n", argv[1] );
82         return( exit_code );
83     }
84
85     while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
86         *p++ = (char)c;
87     *p = '\0';
88
89     if( p - buf != 0 )
90     {
91         mbedtls_printf( "%s\n", buf );
92         exit_code = MBEDTLS_EXIT_SUCCESS;
93     }
94     else
95         mbedtls_printf( "The file is empty!\n" );
96
97     fclose( fp );
98     mbedtls_platform_zeroize( buf, sizeof( buf ) );
99
100     return( exit_code );
101 }