blob: 1e9b98d71ea5e39bc47f2f33f0b6554094c3e28f [file] [log] [blame]
Tom Rini0344c602024-10-08 13:56:50 -06001/*
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#include "mbedtls/build_info.h"
17
18#include <stdio.h>
19
20#include "mbedtls/platform.h"
21
22#include "mbedtls/platform_util.h"
23
24#define BUFFER_LEN 1024
25
26void usage(void)
27{
28 mbedtls_printf("Zeroize is a simple program to assist with testing\n");
29 mbedtls_printf("the mbedtls_platform_zeroize() function by using the\n");
30 mbedtls_printf("debugger. This program takes a file as input and\n");
31 mbedtls_printf("prints the first %d characters. Usage:\n\n", BUFFER_LEN);
32 mbedtls_printf(" zeroize <FILE>\n");
33}
34
35int main(int argc, char **argv)
36{
37 int exit_code = MBEDTLS_EXIT_FAILURE;
38 FILE *fp;
39 char buf[BUFFER_LEN];
40 char *p = buf;
41 char *end = p + BUFFER_LEN;
42 int c;
43
44 if (argc != 2) {
45 mbedtls_printf("This program takes exactly 1 argument\n");
46 usage();
47 mbedtls_exit(exit_code);
48 }
49
50 fp = fopen(argv[1], "r");
51 if (fp == NULL) {
52 mbedtls_printf("Could not open file '%s'\n", argv[1]);
53 mbedtls_exit(exit_code);
54 }
55
56 while ((c = fgetc(fp)) != EOF && p < end - 1) {
57 *p++ = (char) c;
58 }
59 *p = '\0';
60
61 if (p - buf != 0) {
62 mbedtls_printf("%s\n", buf);
63 exit_code = MBEDTLS_EXIT_SUCCESS;
64 } else {
65 mbedtls_printf("The file is empty!\n");
66 }
67
68 fclose(fp);
69 mbedtls_platform_zeroize(buf, sizeof(buf));
70
71 mbedtls_exit(exit_code); // GDB_BREAK_HERE -- don't remove this comment!
72}