blob: 5c8f9bef15c011b4d517b17d26451dc2560fbb1a [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 *
Andres Amaya Garcia757cd722018-03-08 21:25:25 +000012 * Copyright (C) 2018, Arm Limited, All Rights Reserved
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 * **********
53 *
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010054 * This file is part of mbed TLS (https://tls.mbed.org)
55 */
56
57#if !defined(MBEDTLS_CONFIG_FILE)
58#include "mbedtls/config.h"
59#else
60#include MBEDTLS_CONFIG_FILE
61#endif
62
63#include <stdio.h>
64
65#if defined(MBEDTLS_PLATFORM_C)
66#include "mbedtls/platform.h"
67#else
68#include <stdlib.h>
69#define mbedtls_printf printf
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020070#define mbedtls_exit exit
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010071#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
72#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
73#endif
74
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050075#include "mbedtls/platform_util.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010076
77#define BUFFER_LEN 1024
78
79void usage( void )
80{
81 mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050082 mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010083 mbedtls_printf( "debugger. This program takes a file as input and\n" );
84 mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
85 mbedtls_printf( " zeroize <FILE>\n" );
86}
87
88int main( int argc, char** argv )
89{
90 int exit_code = MBEDTLS_EXIT_FAILURE;
Andres Amaya Garcia6e34e632017-11-01 10:03:09 +000091 FILE *fp;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010092 char buf[BUFFER_LEN];
93 char *p = buf;
94 char *end = p + BUFFER_LEN;
Azim Khan1a8ef072018-06-06 03:44:03 +010095 int c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010096
97 if( argc != 2 )
98 {
99 mbedtls_printf( "This program takes exactly 1 agument\n" );
100 usage();
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200101 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100102 }
103
104 fp = fopen( argv[1], "r" );
105 if( fp == NULL )
106 {
107 mbedtls_printf( "Could not open file '%s'\n", argv[1] );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200108 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100109 }
110
111 while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
Azim Khan1a8ef072018-06-06 03:44:03 +0100112 *p++ = (char)c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100113 *p = '\0';
114
115 if( p - buf != 0 )
116 {
117 mbedtls_printf( "%s\n", buf );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100118 exit_code = MBEDTLS_EXIT_SUCCESS;
119 }
120 else
121 mbedtls_printf( "The file is empty!\n" );
122
123 fclose( fp );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -0500124 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100125
Bence Szépkúticd6fd062020-06-09 12:52:04 +0200126 mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment!
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100127}