blob: d7f2337d30ef86404384ed237734b13f7c0e5bb5 [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 *
4 * This is a simple test application used for debbuger-driven testing to check
5 * whether calls to mbedtls_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 line numbers do 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_zeroize() was not eliminated.
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010012 *
13 * Copyright (C) 2017, ARM Limited, All Rights Reserved
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *
28 * This file is part of mbed TLS (https://tls.mbed.org)
29 */
30
31#if !defined(MBEDTLS_CONFIG_FILE)
32#include "mbedtls/config.h"
33#else
34#include MBEDTLS_CONFIG_FILE
35#endif
36
37#include <stdio.h>
38
39#if defined(MBEDTLS_PLATFORM_C)
40#include "mbedtls/platform.h"
41#else
42#include <stdlib.h>
43#define mbedtls_printf printf
44#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
45#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
46#endif
47
48#include "mbedtls/utils.h"
49
50#define BUFFER_LEN 1024
51
52void usage( void )
53{
54 mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
55 mbedtls_printf( "the mbedtls_zeroize() function by using the\n" );
56 mbedtls_printf( "debugger. This program takes a file as input and\n" );
57 mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
58 mbedtls_printf( " zeroize <FILE>\n" );
59}
60
61int main( int argc, char** argv )
62{
63 int exit_code = MBEDTLS_EXIT_FAILURE;
Andres Amaya Garcia6e34e632017-11-01 10:03:09 +000064 FILE *fp;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010065 char buf[BUFFER_LEN];
66 char *p = buf;
67 char *end = p + BUFFER_LEN;
68 char c;
69
70 if( argc != 2 )
71 {
72 mbedtls_printf( "This program takes exactly 1 agument\n" );
73 usage();
74 return( exit_code );
75 }
76
77 fp = fopen( argv[1], "r" );
78 if( fp == NULL )
79 {
80 mbedtls_printf( "Could not open file '%s'\n", argv[1] );
81 return( exit_code );
82 }
83
84 while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
85 *p++ = c;
86 *p = '\0';
87
88 if( p - buf != 0 )
89 {
90 mbedtls_printf( "%s\n", buf );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010091 exit_code = MBEDTLS_EXIT_SUCCESS;
92 }
93 else
94 mbedtls_printf( "The file is empty!\n" );
95
96 fclose( fp );
Andres Amaya Garcia88f8f412017-10-31 21:27:59 +000097 mbedtls_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010098
99 return( exit_code );
100}