blob: 6f5726066690dc30af49ed0a53d35be4f6bb37e9 [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
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -05007 * tests/scripts/test_zeroize.gdb under the assumption that the code does not
Andres Amaya Garcia42defd12018-03-08 21:21:40 +00008 * 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
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050011 * memory and verify that the call to mbedtls_platform_zeroize() was not
12 * eliminated.
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010013 *
Andres Amaya Garcia757cd722018-03-08 21:25:25 +000014 * Copyright (C) 2018, Arm Limited, All Rights Reserved
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010015 * 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
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020043#include <stdio.h>
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010044#include <stdlib.h>
45#define mbedtls_printf printf
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020046#define mbedtls_exit exit
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010047#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
48#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
49#endif
50
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050051#include "mbedtls/platform_util.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010052
53#define BUFFER_LEN 1024
54
55void usage( void )
56{
57 mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050058 mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010059 mbedtls_printf( "debugger. This program takes a file as input and\n" );
60 mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
61 mbedtls_printf( " zeroize <FILE>\n" );
62}
63
64int main( int argc, char** argv )
65{
66 int exit_code = MBEDTLS_EXIT_FAILURE;
Andres Amaya Garcia6e34e632017-11-01 10:03:09 +000067 FILE *fp;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010068 char buf[BUFFER_LEN];
69 char *p = buf;
70 char *end = p + BUFFER_LEN;
Azim Khan1a8ef072018-06-06 03:44:03 +010071 int c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010072
73 if( argc != 2 )
74 {
75 mbedtls_printf( "This program takes exactly 1 agument\n" );
76 usage();
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020077 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010078 }
79
80 fp = fopen( argv[1], "r" );
81 if( fp == NULL )
82 {
83 mbedtls_printf( "Could not open file '%s'\n", argv[1] );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020084 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010085 }
86
87 while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
Azim Khan1a8ef072018-06-06 03:44:03 +010088 *p++ = (char)c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010089 *p = '\0';
90
91 if( p - buf != 0 )
92 {
93 mbedtls_printf( "%s\n", buf );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010094 exit_code = MBEDTLS_EXIT_SUCCESS;
95 }
96 else
97 mbedtls_printf( "The file is empty!\n" );
98
99 fclose( fp );
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -0500100 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100101
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200102 mbedtls_exit( exit_code );
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +0100103}