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 | cd6fd06 | 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 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame^] | 12 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 13 | * 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 Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 20 | * |
| 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úti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 33 | * ********** |
| 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 Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 53 | */ |
| 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 Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 68 | #define mbedtls_exit exit |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 69 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 70 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 71 | #endif |
| 72 | |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 73 | #include "mbedtls/platform_util.h" |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 74 | |
| 75 | #define BUFFER_LEN 1024 |
| 76 | |
| 77 | void usage( void ) |
| 78 | { |
| 79 | 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] | 80 | mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 81 | 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 | |
| 86 | int main( int argc, char** argv ) |
| 87 | { |
| 88 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Andres Amaya Garcia | 6e34e63 | 2017-11-01 10:03:09 +0000 | [diff] [blame] | 89 | FILE *fp; |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 90 | char buf[BUFFER_LEN]; |
| 91 | char *p = buf; |
| 92 | char *end = p + BUFFER_LEN; |
Azim Khan | 1a8ef07 | 2018-06-06 03:44:03 +0100 | [diff] [blame] | 93 | int c; |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 94 | |
| 95 | if( argc != 2 ) |
| 96 | { |
| 97 | mbedtls_printf( "This program takes exactly 1 agument\n" ); |
| 98 | usage(); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 99 | mbedtls_exit( exit_code ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | fp = fopen( argv[1], "r" ); |
| 103 | if( fp == NULL ) |
| 104 | { |
| 105 | mbedtls_printf( "Could not open file '%s'\n", argv[1] ); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 106 | mbedtls_exit( exit_code ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | while( ( c = fgetc( fp ) ) != EOF && p < end - 1 ) |
Azim Khan | 1a8ef07 | 2018-06-06 03:44:03 +0100 | [diff] [blame] | 110 | *p++ = (char)c; |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 111 | *p = '\0'; |
| 112 | |
| 113 | if( p - buf != 0 ) |
| 114 | { |
| 115 | mbedtls_printf( "%s\n", buf ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 116 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 117 | } |
| 118 | else |
| 119 | mbedtls_printf( "The file is empty!\n" ); |
| 120 | |
| 121 | fclose( fp ); |
Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 122 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 123 | |
Bence Szépkúti | cd6fd06 | 2020-06-09 12:52:04 +0200 | [diff] [blame] | 124 | 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] | 125 | } |