• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: the script sets a breakpoint at the last
8  * return statement in the main() function of this program. The debugger
9  * facilities are then used to manually inspect the memory and verify that the
10  * call to mbedtls_platform_zeroize() was not eliminated.
11  *
12  *  Copyright The Mbed TLS Contributors
13  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
14  *
15  *  This file is provided under the Apache License 2.0, or the
16  *  GNU General Public License v2.0 or later.
17  *
18  *  **********
19  *  Apache License 2.0:
20  *
21  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
22  *  not use this file except in compliance with the License.
23  *  You may obtain a copy of the License at
24  *
25  *  http://www.apache.org/licenses/LICENSE-2.0
26  *
27  *  Unless required by applicable law or agreed to in writing, software
28  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  *  See the License for the specific language governing permissions and
31  *  limitations under the License.
32  *
33  *  **********
34  *
35  *  **********
36  *  GNU General Public License v2.0 or later:
37  *
38  *  This program is free software; you can redistribute it and/or modify
39  *  it under the terms of the GNU General Public License as published by
40  *  the Free Software Foundation; either version 2 of the License, or
41  *  (at your option) any later version.
42  *
43  *  This program is distributed in the hope that it will be useful,
44  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
45  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46  *  GNU General Public License for more details.
47  *
48  *  You should have received a copy of the GNU General Public License along
49  *  with this program; if not, write to the Free Software Foundation, Inc.,
50  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
51  *
52  *  **********
53  */
54 
55 #if !defined(MBEDTLS_CONFIG_FILE)
56 #include "mbedtls/config.h"
57 #else
58 #include MBEDTLS_CONFIG_FILE
59 #endif
60 
61 #include <stdio.h>
62 
63 #if defined(MBEDTLS_PLATFORM_C)
64 #include "mbedtls/platform.h"
65 #else
66 #include <stdlib.h>
67 #define mbedtls_printf     printf
68 #define mbedtls_exit       exit
69 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
70 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
71 #endif
72 
73 #include "mbedtls/platform_util.h"
74 
75 #define BUFFER_LEN 1024
76 
usage(void)77 void usage( void )
78 {
79     mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
80     mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
81     mbedtls_printf( "debugger. This program takes a file as input and\n" );
82     mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
83     mbedtls_printf( "       zeroize <FILE>\n" );
84 }
85 
main(int argc,char ** argv)86 int main( int argc, char** argv )
87 {
88     int exit_code = MBEDTLS_EXIT_FAILURE;
89     FILE *fp;
90     char buf[BUFFER_LEN];
91     char *p = buf;
92     char *end = p + BUFFER_LEN;
93     int c;
94 
95     if( argc != 2 )
96     {
97         mbedtls_printf( "This program takes exactly 1 agument\n" );
98         usage();
99         mbedtls_exit( exit_code );
100     }
101 
102     fp = fopen( argv[1], "r" );
103     if( fp == NULL )
104     {
105         mbedtls_printf( "Could not open file '%s'\n", argv[1] );
106         mbedtls_exit( exit_code );
107     }
108 
109     while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
110         *p++ = (char)c;
111     *p = '\0';
112 
113     if( p - buf != 0 )
114     {
115         mbedtls_printf( "%s\n", buf );
116         exit_code = MBEDTLS_EXIT_SUCCESS;
117     }
118     else
119         mbedtls_printf( "The file is empty!\n" );
120 
121     fclose( fp );
122     mbedtls_platform_zeroize( buf, sizeof( buf ) );
123 
124     mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment!
125 }
126