generic_sum.c 6.1 KB

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