blob: dbc394e35bea80788138f02c456ccdac1fac03a2 [file] [log] [blame]
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +01001/*
Andres Amaya Garcia42defd12018-03-08 21:21:40 +00002 * Zeroize application for debugger-driven testing
3 *
Andres Amaya Garciaae8e3062018-03-13 19:19:16 +00004 * This is a simple test application used for debugger-driven testing to check
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -05005 * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler
Andres Amaya Garcia42defd12018-03-08 21:21:40 +00006 * optimizations. This application is used by the GDB script at
Bence Szépkúticd6fd062020-06-09 12:52:04 +02007 * 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.
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010011 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +020012 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020013 * 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:
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010020 *
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 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020033 * **********
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 * **********
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010053 */
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
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020068#define mbedtls_exit exit
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010069#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
70#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
71#endif
72
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050073#include "mbedtls/platform_util.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010074
75#define BUFFER_LEN 1024
76
77void usage( void )
78{
79 mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050080 mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010081 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
86int main( int argc, char** argv )
87{
88 int exit_code = MBEDTLS_EXIT_FAILURE;
Andres Amaya Garcia6e34e632017-11-01 10:03:09 +000089 FILE *fp;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010090 char buf[BUFFER_LEN];
91 char *p = buf;
92 char *end = p + BUFFER_LEN;
Azim Khan1a8ef072018-06-06 03:44:03 +010093 int c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010094
95 if( argc != 2 )
96 {
97 mbedtls_printf( "This program takes exactly 1 agument\n" );
98 usage();
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020099 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100100 }
101
102 fp = fopen( argv[1], "r" );
103 if( fp == NULL )
104 {
105 mbedtls_printf( "Could not open file '%s'\n", argv[1] );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200106 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100107 }
108
109 while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
Azim Khan1a8ef072018-06-06 03:44:03 +0100110 *p++ = (char)c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100111 *p = '\0';
112
113 if( p - buf != 0 )
114 {
115 mbedtls_printf( "%s\n", buf );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100116 exit_code = MBEDTLS_EXIT_SUCCESS;
117 }
118 else
119 mbedtls_printf( "The file is empty!\n" );
120
121 fclose( fp );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -0500122 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100123
Bence Szépkúticd6fd062020-06-09 12:52:04 +0200124 mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment!
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100125}