zeroize.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  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
  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.
  26. */
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stdio.h>
  33. #if defined(MBEDTLS_PLATFORM_C)
  34. #include "mbedtls/platform.h"
  35. #else
  36. #include <stdlib.h>
  37. #define mbedtls_printf printf
  38. #define mbedtls_exit exit
  39. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  40. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  41. #endif
  42. #include "mbedtls/platform_util.h"
  43. #define BUFFER_LEN 1024
  44. void usage( void )
  45. {
  46. mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
  47. mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
  48. mbedtls_printf( "debugger. This program takes a file as input and\n" );
  49. mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
  50. mbedtls_printf( " zeroize <FILE>\n" );
  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. int c;
  60. if( argc != 2 )
  61. {
  62. mbedtls_printf( "This program takes exactly 1 agument\n" );
  63. usage();
  64. mbedtls_exit( exit_code );
  65. }
  66. fp = fopen( argv[1], "r" );
  67. if( fp == NULL )
  68. {
  69. mbedtls_printf( "Could not open file '%s'\n", argv[1] );
  70. mbedtls_exit( exit_code );
  71. }
  72. while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
  73. *p++ = (char)c;
  74. *p = '\0';
  75. if( p - buf != 0 )
  76. {
  77. mbedtls_printf( "%s\n", buf );
  78. exit_code = MBEDTLS_EXIT_SUCCESS;
  79. }
  80. else
  81. mbedtls_printf( "The file is empty!\n" );
  82. fclose( fp );
  83. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  84. mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment!
  85. }