Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 43 | void 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 | |
| 52 | int 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 ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 82 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 83 | } |
| 84 | else |
| 85 | mbedtls_printf( "The file is empty!\n" ); |
| 86 | |
| 87 | fclose( fp ); |
Andres Amaya Garcia | 88f8f41 | 2017-10-31 21:27:59 +0000 | [diff] [blame] | 88 | mbedtls_zeroize( buf, sizeof( buf ) ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 89 | |
| 90 | return( exit_code ); |
| 91 | } |