zeroize.c 2.8 KB

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