blob: 8ff3d4936c73bc17bc9fd7fd6b752cd694614edc [file] [log] [blame]
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +01001/*
Andres Amaya Garcia42defd12018-03-08 21:21:40 +00002 * Zeroize application for debugger-driven testing
3 *
Andres Amaya Garciaae8e3062018-03-13 19:19:16 +00004 * This is a simple test application used for debugger-driven testing to check
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -05005 * whether calls to mbedtls_platform_zeroize() are being eliminated by compiler
Andres Amaya Garcia42defd12018-03-08 21:21:40 +00006 * optimizations. This application is used by the GDB script at
Bence Szépkúti5620d712020-06-09 12:52:04 +02007 * 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 Garcia5ab74a12017-10-24 21:10:45 +010011 *
Bence Szépkúti1e148272020-08-07 13:07:28 +020012 * Copyright The Mbed TLS Contributors
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010013 * 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.
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010026 */
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010029
30#include <stdio.h>
31
32#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033# include "mbedtls/platform.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010034#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# include <stdlib.h>
36# define mbedtls_printf printf
37# define mbedtls_exit exit
38# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
39# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010040#endif
41
Andres Amaya Garciaeecea0e2018-04-17 10:14:53 -050042#include "mbedtls/platform_util.h"
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010043
44#define BUFFER_LEN 1024
45
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046void usage(void)
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010047{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048 mbedtls_printf("Zeroize is a simple program to assist with testing\n");
49 mbedtls_printf("the mbedtls_platform_zeroize() function by using the\n");
50 mbedtls_printf("debugger. This program takes a file as input and\n");
51 mbedtls_printf("prints the first %d characters. Usage:\n\n", BUFFER_LEN);
52 mbedtls_printf(" zeroize <FILE>\n");
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010053}
54
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055int main(int argc, char **argv)
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010056{
57 int exit_code = MBEDTLS_EXIT_FAILURE;
Andres Amaya Garcia6e34e632017-11-01 10:03:09 +000058 FILE *fp;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010059 char buf[BUFFER_LEN];
60 char *p = buf;
61 char *end = p + BUFFER_LEN;
Azim Khan1a8ef072018-06-06 03:44:03 +010062 int c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064 if (argc != 2) {
65 mbedtls_printf("This program takes exactly 1 agument\n");
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010066 usage();
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067 mbedtls_exit(exit_code);
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010068 }
69
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 fp = fopen(argv[1], "r");
71 if (fp == NULL) {
72 mbedtls_printf("Could not open file '%s'\n", argv[1]);
73 mbedtls_exit(exit_code);
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010074 }
75
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076 while ((c = fgetc(fp)) != EOF && p < end - 1)
Azim Khan1a8ef072018-06-06 03:44:03 +010077 *p++ = (char)c;
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010078 *p = '\0';
79
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 if (p - buf != 0) {
81 mbedtls_printf("%s\n", buf);
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010082 exit_code = MBEDTLS_EXIT_SUCCESS;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083 } else
84 mbedtls_printf("The file is empty!\n");
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 fclose(fp);
87 mbedtls_platform_zeroize(buf, sizeof(buf));
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089 mbedtls_exit(exit_code); // GDB_BREAK_HERE -- don't remove this comment!
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +010090}