zeroize.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 OR GPL-2.0-or-later
  14. *
  15. * This file is provided under the Apache License 2.0, or the
  16. * GNU General Public License v2.0 or later.
  17. *
  18. * **********
  19. * Apache License 2.0:
  20. *
  21. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  22. * not use this file except in compliance with the License.
  23. * You may obtain a copy of the License at
  24. *
  25. * http://www.apache.org/licenses/LICENSE-2.0
  26. *
  27. * Unless required by applicable law or agreed to in writing, software
  28. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  29. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. * See the License for the specific language governing permissions and
  31. * limitations under the License.
  32. *
  33. * **********
  34. *
  35. * **********
  36. * GNU General Public License v2.0 or later:
  37. *
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation; either version 2 of the License, or
  41. * (at your option) any later version.
  42. *
  43. * This program is distributed in the hope that it will be useful,
  44. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. * GNU General Public License for more details.
  47. *
  48. * You should have received a copy of the GNU General Public License along
  49. * with this program; if not, write to the Free Software Foundation, Inc.,
  50. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  51. *
  52. * **********
  53. */
  54. #if !defined(MBEDTLS_CONFIG_FILE)
  55. #include "mbedtls/config.h"
  56. #else
  57. #include MBEDTLS_CONFIG_FILE
  58. #endif
  59. #include <stdio.h>
  60. #if defined(MBEDTLS_PLATFORM_C)
  61. #include "mbedtls/platform.h"
  62. #else
  63. #include <stdlib.h>
  64. #define mbedtls_printf printf
  65. #define mbedtls_exit exit
  66. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  67. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  68. #endif
  69. #include "mbedtls/platform_util.h"
  70. #define BUFFER_LEN 1024
  71. void usage( void )
  72. {
  73. mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
  74. mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
  75. mbedtls_printf( "debugger. This program takes a file as input and\n" );
  76. mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
  77. mbedtls_printf( " zeroize <FILE>\n" );
  78. }
  79. int main( int argc, char** argv )
  80. {
  81. int exit_code = MBEDTLS_EXIT_FAILURE;
  82. FILE *fp;
  83. char buf[BUFFER_LEN];
  84. char *p = buf;
  85. char *end = p + BUFFER_LEN;
  86. int c;
  87. if( argc != 2 )
  88. {
  89. mbedtls_printf( "This program takes exactly 1 agument\n" );
  90. usage();
  91. mbedtls_exit( exit_code );
  92. }
  93. fp = fopen( argv[1], "r" );
  94. if( fp == NULL )
  95. {
  96. mbedtls_printf( "Could not open file '%s'\n", argv[1] );
  97. mbedtls_exit( exit_code );
  98. }
  99. while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
  100. *p++ = (char)c;
  101. *p = '\0';
  102. if( p - buf != 0 )
  103. {
  104. mbedtls_printf( "%s\n", buf );
  105. exit_code = MBEDTLS_EXIT_SUCCESS;
  106. }
  107. else
  108. mbedtls_printf( "The file is empty!\n" );
  109. fclose( fp );
  110. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  111. mbedtls_exit( exit_code ); // GDB_BREAK_HERE -- don't remove this comment!
  112. }