| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 1 | /* | 
| Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 2 | * Zeroize application for debugger-driven testing | 
|  | 3 | * | 
| Andres Amaya Garcia | ae8e306 | 2018-03-13 19:19:16 +0000 | [diff] [blame] | 4 | * This is a simple test application used for debugger-driven testing to check | 
| Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 5 | * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler | 
| Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 6 | * optimizations. This application is used by the GDB script at | 
| Bence Szépkúti | 5620d71 | 2020-06-09 12:52:04 +0200 | [diff] [blame] | 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. | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 11 | * | 
| Andres Amaya Garcia | 757cd72 | 2018-03-08 21:25:25 +0000 | [diff] [blame] | 12 | *  Copyright (C) 2018, Arm Limited, All Rights Reserved | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 13 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 14 | * | 
|  | 15 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 16 | *  not use this file except in compliance with the License. | 
|  | 17 | *  You may obtain a copy of the License at | 
|  | 18 | * | 
|  | 19 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 20 | * | 
|  | 21 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 22 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 23 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 24 | *  See the License for the specific language governing permissions and | 
|  | 25 | *  limitations under the License. | 
|  | 26 | * | 
|  | 27 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
|  | 28 | */ | 
|  | 29 |  | 
|  | 30 | #if !defined(MBEDTLS_CONFIG_FILE) | 
|  | 31 | #include "mbedtls/config.h" | 
|  | 32 | #else | 
|  | 33 | #include MBEDTLS_CONFIG_FILE | 
|  | 34 | #endif | 
|  | 35 |  | 
|  | 36 | #include <stdio.h> | 
|  | 37 |  | 
|  | 38 | #if defined(MBEDTLS_PLATFORM_C) | 
|  | 39 | #include "mbedtls/platform.h" | 
|  | 40 | #else | 
|  | 41 | #include <stdlib.h> | 
|  | 42 | #define mbedtls_printf     printf | 
| k-stachowiak | 776521a | 2019-05-23 09:46:47 +0200 | [diff] [blame] | 43 | #define mbedtls_exit       exit | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 44 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS | 
|  | 45 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE | 
|  | 46 | #endif | 
|  | 47 |  | 
| Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 48 | #include "mbedtls/platform_util.h" | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 49 |  | 
|  | 50 | #define BUFFER_LEN 1024 | 
|  | 51 |  | 
|  | 52 | void usage( void ) | 
|  | 53 | { | 
|  | 54 | mbedtls_printf( "Zeroize is a simple program to assist with testing\n" ); | 
| Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 55 | mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" ); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 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 |  | 
|  | 61 | int main( int argc, char** argv ) | 
|  | 62 | { | 
|  | 63 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Andres Amaya Garcia | 6e34e63 | 2017-11-01 10:03:09 +0000 | [diff] [blame] | 64 | FILE *fp; | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 65 | char buf[BUFFER_LEN]; | 
|  | 66 | char *p = buf; | 
|  | 67 | char *end = p + BUFFER_LEN; | 
| Azim Khan | 1a8ef07 | 2018-06-06 03:44:03 +0100 | [diff] [blame] | 68 | int c; | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 69 |  | 
|  | 70 | if( argc != 2 ) | 
|  | 71 | { | 
|  | 72 | mbedtls_printf( "This program takes exactly 1 agument\n" ); | 
|  | 73 | usage(); | 
| k-stachowiak | 776521a | 2019-05-23 09:46:47 +0200 | [diff] [blame] | 74 | mbedtls_exit( exit_code ); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | fp = fopen( argv[1], "r" ); | 
|  | 78 | if( fp == NULL ) | 
|  | 79 | { | 
|  | 80 | mbedtls_printf( "Could not open file '%s'\n", argv[1] ); | 
| k-stachowiak | 776521a | 2019-05-23 09:46:47 +0200 | [diff] [blame] | 81 | mbedtls_exit( exit_code ); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | while( ( c = fgetc( fp ) ) != EOF && p < end - 1 ) | 
| Azim Khan | 1a8ef07 | 2018-06-06 03:44:03 +0100 | [diff] [blame] | 85 | *p++ = (char)c; | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 86 | *p = '\0'; | 
|  | 87 |  | 
|  | 88 | if( p - buf != 0 ) | 
|  | 89 | { | 
|  | 90 | mbedtls_printf( "%s\n", buf ); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 91 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 92 | } | 
|  | 93 | else | 
|  | 94 | mbedtls_printf( "The file is empty!\n" ); | 
|  | 95 |  | 
|  | 96 | fclose( fp ); | 
| Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 97 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 98 |  | 
| Bence Szépkúti | 5620d71 | 2020-06-09 12:52:04 +0200 | [diff] [blame] | 99 | mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment! | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 100 | } |