1a8e1175bSopenharmony_ci/* 2a8e1175bSopenharmony_ci * Zeroize application for debugger-driven testing 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * This is a simple test application used for debugger-driven testing to check 5a8e1175bSopenharmony_ci * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler 6a8e1175bSopenharmony_ci * optimizations. This application is used by the GDB script at 7a8e1175bSopenharmony_ci * tests/scripts/test_zeroize.gdb: the script sets a breakpoint at the last 8a8e1175bSopenharmony_ci * return statement in the main() function of this program. The debugger 9a8e1175bSopenharmony_ci * facilities are then used to manually inspect the memory and verify that the 10a8e1175bSopenharmony_ci * call to mbedtls_platform_zeroize() was not eliminated. 11a8e1175bSopenharmony_ci * 12a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 13a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 14a8e1175bSopenharmony_ci * 15a8e1175bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); you may 16a8e1175bSopenharmony_ci * not use this file except in compliance with the License. 17a8e1175bSopenharmony_ci * You may obtain a copy of the License at 18a8e1175bSopenharmony_ci * 19a8e1175bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 20a8e1175bSopenharmony_ci * 21a8e1175bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 22a8e1175bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 23a8e1175bSopenharmony_ci * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24a8e1175bSopenharmony_ci * See the License for the specific language governing permissions and 25a8e1175bSopenharmony_ci * limitations under the License. 26a8e1175bSopenharmony_ci */ 27a8e1175bSopenharmony_ci 28a8e1175bSopenharmony_ci#include "mbedtls/build_info.h" 29a8e1175bSopenharmony_ci 30a8e1175bSopenharmony_ci#include <stdio.h> 31a8e1175bSopenharmony_ci 32a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 33a8e1175bSopenharmony_ci 34a8e1175bSopenharmony_ci#include "mbedtls/platform_util.h" 35a8e1175bSopenharmony_ci 36a8e1175bSopenharmony_ci#define BUFFER_LEN 1024 37a8e1175bSopenharmony_ci 38a8e1175bSopenharmony_civoid usage(void) 39a8e1175bSopenharmony_ci{ 40a8e1175bSopenharmony_ci mbedtls_printf("Zeroize is a simple program to assist with testing\n"); 41a8e1175bSopenharmony_ci mbedtls_printf("the mbedtls_platform_zeroize() function by using the\n"); 42a8e1175bSopenharmony_ci mbedtls_printf("debugger. This program takes a file as input and\n"); 43a8e1175bSopenharmony_ci mbedtls_printf("prints the first %d characters. Usage:\n\n", BUFFER_LEN); 44a8e1175bSopenharmony_ci mbedtls_printf(" zeroize <FILE>\n"); 45a8e1175bSopenharmony_ci} 46a8e1175bSopenharmony_ci 47a8e1175bSopenharmony_ciint main(int argc, char **argv) 48a8e1175bSopenharmony_ci{ 49a8e1175bSopenharmony_ci int exit_code = MBEDTLS_EXIT_FAILURE; 50a8e1175bSopenharmony_ci FILE *fp; 51a8e1175bSopenharmony_ci char buf[BUFFER_LEN]; 52a8e1175bSopenharmony_ci char *p = buf; 53a8e1175bSopenharmony_ci char *end = p + BUFFER_LEN; 54a8e1175bSopenharmony_ci int c; 55a8e1175bSopenharmony_ci 56a8e1175bSopenharmony_ci if (argc != 2) { 57a8e1175bSopenharmony_ci mbedtls_printf("This program takes exactly 1 argument\n"); 58a8e1175bSopenharmony_ci usage(); 59a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 60a8e1175bSopenharmony_ci } 61a8e1175bSopenharmony_ci 62a8e1175bSopenharmony_ci fp = fopen(argv[1], "r"); 63a8e1175bSopenharmony_ci if (fp == NULL) { 64a8e1175bSopenharmony_ci mbedtls_printf("Could not open file '%s'\n", argv[1]); 65a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 66a8e1175bSopenharmony_ci } 67a8e1175bSopenharmony_ci 68a8e1175bSopenharmony_ci while ((c = fgetc(fp)) != EOF && p < end - 1) { 69a8e1175bSopenharmony_ci *p++ = (char) c; 70a8e1175bSopenharmony_ci } 71a8e1175bSopenharmony_ci *p = '\0'; 72a8e1175bSopenharmony_ci 73a8e1175bSopenharmony_ci if (p - buf != 0) { 74a8e1175bSopenharmony_ci mbedtls_printf("%s\n", buf); 75a8e1175bSopenharmony_ci exit_code = MBEDTLS_EXIT_SUCCESS; 76a8e1175bSopenharmony_ci } else { 77a8e1175bSopenharmony_ci mbedtls_printf("The file is empty!\n"); 78a8e1175bSopenharmony_ci } 79a8e1175bSopenharmony_ci 80a8e1175bSopenharmony_ci fclose(fp); 81a8e1175bSopenharmony_ci mbedtls_platform_zeroize(buf, sizeof(buf)); 82a8e1175bSopenharmony_ci 83a8e1175bSopenharmony_ci mbedtls_exit(exit_code); // GDB_BREAK_HERE -- don't remove this comment! 84a8e1175bSopenharmony_ci} 85