Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 1 | /* |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 2 | * Zeroize application for debugger-driven testing |
| 3 | * |
Andres Amaya Garcia | ae8e306 | 2018-03-13 19:19:16 +0000 | [diff] [blame] | 4 | * This is a simple test application used for debugger-driven testing to check |
Andres Amaya Garcia | 42defd1 | 2018-03-08 21:21:40 +0000 | [diff] [blame] | 5 | * whether calls to mbedtls_zeroize() are being eliminated by compiler |
| 6 | * optimizations. This application is used by the GDB script at |
| 7 | * tests/scripts/test_zeroize.gdb under the assumption that line numbers do not |
| 8 | * change often (as opposed to the library code) because the script sets a |
| 9 | * breakpoint at the last return statement in the main() function of this |
| 10 | * program. The debugger facilities are then used to manually inspect the |
| 11 | * memory and verify that the call to mbedtls_zeroize() was not eliminated. |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 12 | * |
Andres Amaya Garcia | 757cd72 | 2018-03-08 21:25:25 +0000 | [diff] [blame] | 13 | * Copyright (C) 2018, Arm Limited, All Rights Reserved |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 14 | * SPDX-License-Identifier: Apache-2.0 |
| 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | * |
| 28 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 29 | */ |
| 30 | |
| 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 32 | #include "mbedtls/config.h" |
| 33 | #else |
| 34 | #include MBEDTLS_CONFIG_FILE |
| 35 | #endif |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | |
| 39 | #if defined(MBEDTLS_PLATFORM_C) |
| 40 | #include "mbedtls/platform.h" |
| 41 | #else |
| 42 | #include <stdlib.h> |
| 43 | #define mbedtls_printf printf |
| 44 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 45 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 46 | #endif |
| 47 | |
| 48 | #include "mbedtls/utils.h" |
| 49 | |
| 50 | #define BUFFER_LEN 1024 |
| 51 | |
| 52 | void usage( void ) |
| 53 | { |
| 54 | mbedtls_printf( "Zeroize is a simple program to assist with testing\n" ); |
| 55 | mbedtls_printf( "the mbedtls_zeroize() function by using the\n" ); |
| 56 | mbedtls_printf( "debugger. This program takes a file as input and\n" ); |
| 57 | mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN ); |
| 58 | mbedtls_printf( " zeroize <FILE>\n" ); |
| 59 | } |
| 60 | |
| 61 | int main( int argc, char** argv ) |
| 62 | { |
| 63 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Andres Amaya Garcia | 6e34e63 | 2017-11-01 10:03:09 +0000 | [diff] [blame] | 64 | FILE *fp; |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 65 | char buf[BUFFER_LEN]; |
| 66 | char *p = buf; |
| 67 | char *end = p + BUFFER_LEN; |
| 68 | char c; |
| 69 | |
| 70 | if( argc != 2 ) |
| 71 | { |
| 72 | mbedtls_printf( "This program takes exactly 1 agument\n" ); |
| 73 | usage(); |
| 74 | return( exit_code ); |
| 75 | } |
| 76 | |
| 77 | fp = fopen( argv[1], "r" ); |
| 78 | if( fp == NULL ) |
| 79 | { |
| 80 | mbedtls_printf( "Could not open file '%s'\n", argv[1] ); |
| 81 | return( exit_code ); |
| 82 | } |
| 83 | |
| 84 | while( ( c = fgetc( fp ) ) != EOF && p < end - 1 ) |
| 85 | *p++ = c; |
| 86 | *p = '\0'; |
| 87 | |
| 88 | if( p - buf != 0 ) |
| 89 | { |
| 90 | mbedtls_printf( "%s\n", buf ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 91 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 92 | } |
| 93 | else |
| 94 | mbedtls_printf( "The file is empty!\n" ); |
| 95 | |
| 96 | fclose( fp ); |
Andres Amaya Garcia | 88f8f41 | 2017-10-31 21:27:59 +0000 | [diff] [blame] | 97 | mbedtls_zeroize( buf, sizeof( buf ) ); |
Andres Amaya Garcia | 5ab74a1 | 2017-10-24 21:10:45 +0100 | [diff] [blame] | 98 | |
| 99 | return( exit_code ); |
| 100 | } |