| 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 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 12 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 13 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 14 | */ | 
|  | 15 |  | 
|  | 16 | #if !defined(MBEDTLS_CONFIG_FILE) | 
|  | 17 | #include "mbedtls/config.h" | 
|  | 18 | #else | 
|  | 19 | #include MBEDTLS_CONFIG_FILE | 
|  | 20 | #endif | 
|  | 21 |  | 
|  | 22 | #include <stdio.h> | 
|  | 23 |  | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 24 | #include "mbedtls/platform.h" | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 25 |  | 
| Andres Amaya Garcia | eecea0e | 2018-04-17 10:14:53 -0500 | [diff] [blame] | 26 | #include "mbedtls/platform_util.h" | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 27 |  | 
|  | 28 | #define BUFFER_LEN 1024 | 
|  | 29 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 30 | void usage(void) | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 31 | { | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 32 | mbedtls_printf("Zeroize is a simple program to assist with testing\n"); | 
|  | 33 | mbedtls_printf("the mbedtls_platform_zeroize() function by using the\n"); | 
|  | 34 | mbedtls_printf("debugger. This program takes a file as input and\n"); | 
|  | 35 | mbedtls_printf("prints the first %d characters. Usage:\n\n", BUFFER_LEN); | 
|  | 36 | mbedtls_printf("       zeroize <FILE>\n"); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 39 | int main(int argc, char **argv) | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 40 | { | 
|  | 41 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Andres Amaya Garcia | 6e34e63 | 2017-11-01 10:03:09 +0000 | [diff] [blame] | 42 | FILE *fp; | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 43 | char buf[BUFFER_LEN]; | 
|  | 44 | char *p = buf; | 
|  | 45 | char *end = p + BUFFER_LEN; | 
| Azim Khan | 1a8ef07 | 2018-06-06 03:44:03 +0100 | [diff] [blame] | 46 | int c; | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 47 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | if (argc != 2) { | 
|  | 49 | mbedtls_printf("This program takes exactly 1 argument\n"); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 50 | usage(); | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 51 | mbedtls_exit(exit_code); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | fp = fopen(argv[1], "r"); | 
|  | 55 | if (fp == NULL) { | 
|  | 56 | mbedtls_printf("Could not open file '%s'\n", argv[1]); | 
|  | 57 | mbedtls_exit(exit_code); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 60 | while ((c = fgetc(fp)) != EOF && p < end - 1) { | 
|  | 61 | *p++ = (char) c; | 
|  | 62 | } | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 63 | *p = '\0'; | 
|  | 64 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | if (p - buf != 0) { | 
|  | 66 | mbedtls_printf("%s\n", buf); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 67 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | } else { | 
|  | 69 | mbedtls_printf("The file is empty!\n"); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 70 | } | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 71 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | fclose(fp); | 
|  | 73 | mbedtls_platform_zeroize(buf, sizeof(buf)); | 
| Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 74 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 75 | 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] | 76 | } |