generic_sum.c 6.2 KB

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