generic_sum.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * generic message digest layer demonstration program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_fprintf fprintf
  57. #define mbedtls_printf printf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
  63. #include "mbedtls/md.h"
  64. #include <stdio.h>
  65. #include <string.h>
  66. #endif
  67. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
  68. int main( void )
  69. {
  70. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
  71. mbedtls_exit( 0 );
  72. }
  73. #else
  74. static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
  75. {
  76. int ret = mbedtls_md_file( md_info, filename, sum );
  77. if( ret == 1 )
  78. mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
  79. if( ret == 2 )
  80. mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
  81. return( ret );
  82. }
  83. static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
  84. {
  85. int i;
  86. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  87. if( generic_wrapper( md_info, filename, sum ) != 0 )
  88. return( 1 );
  89. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  90. mbedtls_printf( "%02x", sum[i] );
  91. mbedtls_printf( " %s\n", filename );
  92. return( 0 );
  93. }
  94. static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
  95. {
  96. int i;
  97. size_t n;
  98. FILE *f;
  99. int nb_err1, nb_err2;
  100. int nb_tot1, nb_tot2;
  101. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  102. char line[1024];
  103. char diff;
  104. #if defined(__clang_analyzer__)
  105. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
  106. #else
  107. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
  108. #endif
  109. if( ( f = fopen( filename, "rb" ) ) == NULL )
  110. {
  111. mbedtls_printf( "failed to open: %s\n", filename );
  112. return( 1 );
  113. }
  114. nb_err1 = nb_err2 = 0;
  115. nb_tot1 = nb_tot2 = 0;
  116. memset( line, 0, sizeof( line ) );
  117. n = sizeof( line );
  118. while( fgets( line, (int) n - 1, f ) != NULL )
  119. {
  120. n = strlen( line );
  121. if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
  122. {
  123. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
  124. continue;
  125. }
  126. if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
  127. {
  128. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
  129. continue;
  130. }
  131. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  132. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  133. nb_tot1++;
  134. if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
  135. {
  136. nb_err1++;
  137. continue;
  138. }
  139. nb_tot2++;
  140. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  141. sprintf( buf + i * 2, "%02x", sum[i] );
  142. /* Use constant-time buffer comparison */
  143. diff = 0;
  144. for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
  145. diff |= line[i] ^ buf[i];
  146. if( diff != 0 )
  147. {
  148. nb_err2++;
  149. mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
  150. }
  151. n = sizeof( line );
  152. }
  153. if( nb_err1 != 0 )
  154. {
  155. mbedtls_printf( "WARNING: %d (out of %d) input files could "
  156. "not be read\n", nb_err1, nb_tot1 );
  157. }
  158. if( nb_err2 != 0 )
  159. {
  160. mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
  161. "not match\n", nb_err2, nb_tot2 );
  162. }
  163. fclose( f );
  164. return( nb_err1 != 0 || nb_err2 != 0 );
  165. }
  166. int main( int argc, char *argv[] )
  167. {
  168. int ret = 1, i;
  169. int exit_code = MBEDTLS_EXIT_FAILURE;
  170. const mbedtls_md_info_t *md_info;
  171. mbedtls_md_context_t md_ctx;
  172. mbedtls_md_init( &md_ctx );
  173. if( argc == 1 )
  174. {
  175. const int *list;
  176. mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
  177. mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
  178. mbedtls_printf( "\nAvailable message digests:\n" );
  179. list = mbedtls_md_list();
  180. while( *list )
  181. {
  182. md_info = mbedtls_md_info_from_type( *list );
  183. mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
  184. list++;
  185. }
  186. #if defined(_WIN32)
  187. mbedtls_printf( "\n Press Enter to exit this program.\n" );
  188. fflush( stdout ); getchar();
  189. #endif
  190. mbedtls_exit( exit_code );
  191. }
  192. /*
  193. * Read the MD from the command line
  194. */
  195. md_info = mbedtls_md_info_from_string( argv[1] );
  196. if( md_info == NULL )
  197. {
  198. mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
  199. mbedtls_exit( exit_code );
  200. }
  201. if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
  202. {
  203. mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
  204. mbedtls_exit( exit_code );
  205. }
  206. ret = 0;
  207. if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
  208. {
  209. ret |= generic_check( md_info, argv[3] );
  210. goto exit;
  211. }
  212. for( i = 2; i < argc; i++ )
  213. ret |= generic_print( md_info, argv[i] );
  214. if ( ret == 0 )
  215. exit_code = MBEDTLS_EXIT_SUCCESS;
  216. exit:
  217. mbedtls_md_free( &md_ctx );
  218. mbedtls_exit( exit_code );
  219. }
  220. #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */