blob: 7f3e8b401309113485ed5026fff1e05f846df761 [file] [log] [blame]
Andres Amaya Garcia5ab74a12017-10-24 21:10:45 +01001/*
2 * Zeroize demonstration program
3 *
4 * Copyright (C) 2017, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#include <stdio.h>
29
30#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdlib.h>
34#define mbedtls_printf printf
35#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif
38
39#include "mbedtls/utils.h"
40
41#define BUFFER_LEN 1024
42
43void usage( void )
44{
45 mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
46 mbedtls_printf( "the mbedtls_zeroize() function by using the\n" );
47 mbedtls_printf( "debugger. This program takes a file as input and\n" );
48 mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
49 mbedtls_printf( " zeroize <FILE>\n" );
50}
51
52int main( int argc, char** argv )
53{
54 int exit_code = MBEDTLS_EXIT_FAILURE;
55 FILE * fp;
56 char buf[BUFFER_LEN];
57 char *p = buf;
58 char *end = p + BUFFER_LEN;
59 char c;
60
61 if( argc != 2 )
62 {
63 mbedtls_printf( "This program takes exactly 1 agument\n" );
64 usage();
65 return( exit_code );
66 }
67
68 fp = fopen( argv[1], "r" );
69 if( fp == NULL )
70 {
71 mbedtls_printf( "Could not open file '%s'\n", argv[1] );
72 return( exit_code );
73 }
74
75 while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
76 *p++ = c;
77 *p = '\0';
78
79 if( p - buf != 0 )
80 {
81 mbedtls_printf( "%s\n", buf );
82 mbedtls_zeroize( buf, sizeof( buf ) );
83 exit_code = MBEDTLS_EXIT_SUCCESS;
84 }
85 else
86 mbedtls_printf( "The file is empty!\n" );
87
88 fclose( fp );
89
90 return( exit_code );
91}