1 /* 2 * Zeroize application for debugger-driven testing 3 * 4 * This is a simple test application used for debugger-driven testing to check 5 * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler 6 * optimizations. This application is used by the GDB script at 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. 11 * 12 * Copyright The Mbed TLS Contributors 13 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 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 24 #include "mbedtls/platform.h" 25 26 #include "mbedtls/platform_util.h" 27 28 #define BUFFER_LEN 1024 29 usage(void)30void usage(void) 31 { 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"); 37 } 38 main(int argc,char ** argv)39int main(int argc, char **argv) 40 { 41 int exit_code = MBEDTLS_EXIT_FAILURE; 42 FILE *fp; 43 char buf[BUFFER_LEN]; 44 char *p = buf; 45 char *end = p + BUFFER_LEN; 46 int c; 47 48 if (argc != 2) { 49 mbedtls_printf("This program takes exactly 1 argument\n"); 50 usage(); 51 mbedtls_exit(exit_code); 52 } 53 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); 58 } 59 60 while ((c = fgetc(fp)) != EOF && p < end - 1) { 61 *p++ = (char) c; 62 } 63 *p = '\0'; 64 65 if (p - buf != 0) { 66 mbedtls_printf("%s\n", buf); 67 exit_code = MBEDTLS_EXIT_SUCCESS; 68 } else { 69 mbedtls_printf("The file is empty!\n"); 70 } 71 72 fclose(fp); 73 mbedtls_platform_zeroize(buf, sizeof(buf)); 74 75 mbedtls_exit(exit_code); // GDB_BREAK_HERE -- don't remove this comment! 76 } 77