generic_sum.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * generic message digest layer demonstration program
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #define mbedtls_fprintf fprintf
  31. #define mbedtls_printf printf
  32. #endif
  33. #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
  34. #include "mbedtls/md.h"
  35. #include <stdio.h>
  36. #include <string.h>
  37. #endif
  38. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
  39. int main( void )
  40. {
  41. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
  42. return( 0 );
  43. }
  44. #else
  45. static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
  46. {
  47. int ret = mbedtls_md_file( md_info, filename, sum );
  48. if( ret == 1 )
  49. mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
  50. if( ret == 2 )
  51. mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
  52. return( ret );
  53. }
  54. static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
  55. {
  56. int i;
  57. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  58. if( generic_wrapper( md_info, filename, sum ) != 0 )
  59. return( 1 );
  60. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  61. mbedtls_printf( "%02x", sum[i] );
  62. mbedtls_printf( " %s\n", filename );
  63. return( 0 );
  64. }
  65. static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
  66. {
  67. int i;
  68. size_t n;
  69. FILE *f;
  70. int nb_err1, nb_err2;
  71. int nb_tot1, nb_tot2;
  72. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  73. char line[1024];
  74. char diff;
  75. #if defined(__clang_analyzer__)
  76. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
  77. #else
  78. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
  79. #endif
  80. if( ( f = fopen( filename, "rb" ) ) == NULL )
  81. {
  82. mbedtls_printf( "failed to open: %s\n", filename );
  83. return( 1 );
  84. }
  85. nb_err1 = nb_err2 = 0;
  86. nb_tot1 = nb_tot2 = 0;
  87. memset( line, 0, sizeof( line ) );
  88. n = sizeof( line );
  89. while( fgets( line, (int) n - 1, f ) != NULL )
  90. {
  91. n = strlen( line );
  92. if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
  93. {
  94. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
  95. continue;
  96. }
  97. if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
  98. {
  99. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
  100. continue;
  101. }
  102. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  103. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  104. nb_tot1++;
  105. if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
  106. {
  107. nb_err1++;
  108. continue;
  109. }
  110. nb_tot2++;
  111. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  112. sprintf( buf + i * 2, "%02x", sum[i] );
  113. /* Use constant-time buffer comparison */
  114. diff = 0;
  115. for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
  116. diff |= line[i] ^ buf[i];
  117. if( diff != 0 )
  118. {
  119. nb_err2++;
  120. mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
  121. }
  122. n = sizeof( line );
  123. }
  124. if( nb_err1 != 0 )
  125. {
  126. mbedtls_printf( "WARNING: %d (out of %d) input files could "
  127. "not be read\n", nb_err1, nb_tot1 );
  128. }
  129. if( nb_err2 != 0 )
  130. {
  131. mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
  132. "not match\n", nb_err2, nb_tot2 );
  133. }
  134. fclose( f );
  135. return( nb_err1 != 0 || nb_err2 != 0 );
  136. }
  137. int main( int argc, char *argv[] )
  138. {
  139. int ret, i;
  140. const mbedtls_md_info_t *md_info;
  141. mbedtls_md_context_t md_ctx;
  142. mbedtls_md_init( &md_ctx );
  143. if( argc == 1 )
  144. {
  145. const int *list;
  146. mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
  147. mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
  148. mbedtls_printf( "\nAvailable message digests:\n" );
  149. list = mbedtls_md_list();
  150. while( *list )
  151. {
  152. md_info = mbedtls_md_info_from_type( *list );
  153. mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
  154. list++;
  155. }
  156. #if defined(_WIN32)
  157. mbedtls_printf( "\n Press Enter to exit this program.\n" );
  158. fflush( stdout ); getchar();
  159. #endif
  160. return( 1 );
  161. }
  162. /*
  163. * Read the MD from the command line
  164. */
  165. md_info = mbedtls_md_info_from_string( argv[1] );
  166. if( md_info == NULL )
  167. {
  168. mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
  169. return( 1 );
  170. }
  171. if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
  172. {
  173. mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
  174. return( 1 );
  175. }
  176. ret = 0;
  177. if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
  178. {
  179. ret |= generic_check( md_info, argv[3] );
  180. goto exit;
  181. }
  182. for( i = 2; i < argc; i++ )
  183. ret |= generic_print( md_info, argv[i] );
  184. exit:
  185. mbedtls_md_free( &md_ctx );
  186. return( ret );
  187. }
  188. #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */