generic_sum.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * generic message digest layer demonstration program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "polarssl/config.h"
  31. #include "polarssl/md.h"
  32. #if !defined(POLARSSL_MD_C)
  33. int main( void )
  34. {
  35. printf("POLARSSL_MD_C not defined.\n");
  36. return( 0 );
  37. }
  38. #else
  39. static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
  40. {
  41. int ret = md_file( md_info, filename, sum );
  42. if( ret == 1 )
  43. fprintf( stderr, "failed to open: %s\n", filename );
  44. if( ret == 2 )
  45. fprintf( stderr, "failed to read: %s\n", filename );
  46. return( ret );
  47. }
  48. static int generic_print( const md_info_t *md_info, char *filename )
  49. {
  50. int i;
  51. unsigned char sum[POLARSSL_MD_MAX_SIZE];
  52. if( generic_wrapper( md_info, filename, sum ) != 0 )
  53. return( 1 );
  54. for( i = 0; i < md_info->size; i++ )
  55. printf( "%02x", sum[i] );
  56. printf( " %s\n", filename );
  57. return( 0 );
  58. }
  59. static int generic_check( const md_info_t *md_info, char *filename )
  60. {
  61. int i;
  62. size_t n;
  63. FILE *f;
  64. int nb_err1, nb_err2;
  65. int nb_tot1, nb_tot2;
  66. unsigned char sum[POLARSSL_MD_MAX_SIZE];
  67. char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
  68. if( ( f = fopen( filename, "rb" ) ) == NULL )
  69. {
  70. printf( "failed to open: %s\n", filename );
  71. return( 1 );
  72. }
  73. nb_err1 = nb_err2 = 0;
  74. nb_tot1 = nb_tot2 = 0;
  75. memset( line, 0, sizeof( line ) );
  76. n = sizeof( line );
  77. while( fgets( line, (int) n - 1, f ) != NULL )
  78. {
  79. n = strlen( line );
  80. if( n < (size_t) 2 * md_info->size + 4 )
  81. {
  82. printf("No '%s' hash found on line.\n", md_info->name);
  83. continue;
  84. }
  85. if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
  86. {
  87. printf("No '%s' hash found on line.\n", md_info->name);
  88. continue;
  89. }
  90. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  91. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  92. nb_tot1++;
  93. if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
  94. {
  95. nb_err1++;
  96. continue;
  97. }
  98. nb_tot2++;
  99. for( i = 0; i < md_info->size; i++ )
  100. sprintf( buf + i * 2, "%02x", sum[i] );
  101. if( memcmp( line, buf, 2 * md_info->size ) != 0 )
  102. {
  103. nb_err2++;
  104. fprintf( stderr, "wrong checksum: %s\n", line + 66 );
  105. }
  106. n = sizeof( line );
  107. }
  108. if( nb_err1 != 0 )
  109. {
  110. printf( "WARNING: %d (out of %d) input files could "
  111. "not be read\n", nb_err1, nb_tot1 );
  112. }
  113. if( nb_err2 != 0 )
  114. {
  115. printf( "WARNING: %d (out of %d) computed checksums did "
  116. "not match\n", nb_err2, nb_tot2 );
  117. }
  118. return( nb_err1 != 0 || nb_err2 != 0 );
  119. }
  120. int main( int argc, char *argv[] )
  121. {
  122. int ret, i;
  123. const md_info_t *md_info;
  124. md_context_t md_ctx;
  125. memset( &md_ctx, 0, sizeof( md_context_t ));
  126. if( argc == 1 )
  127. {
  128. const int *list;
  129. printf( "print mode: generic_sum <md> <file> <file> ...\n" );
  130. printf( "check mode: generic_sum <md> -c <checksum file>\n" );
  131. printf( "\nAvailable message digests:\n" );
  132. list = md_list();
  133. while( *list )
  134. {
  135. md_info = md_info_from_type( *list );
  136. printf( " %s\n", md_info->name );
  137. list++;
  138. }
  139. #ifdef WIN32
  140. printf( "\n Press Enter to exit this program.\n" );
  141. fflush( stdout ); getchar();
  142. #endif
  143. return( 1 );
  144. }
  145. /*
  146. * Read the MD from the command line
  147. */
  148. md_info = md_info_from_string( argv[1] );
  149. if( md_info == NULL )
  150. {
  151. fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
  152. return( 1 );
  153. }
  154. if( md_init_ctx( &md_ctx, md_info) )
  155. {
  156. fprintf( stderr, "Failed to initialize context.\n" );
  157. return( 1 );
  158. }
  159. ret = 0;
  160. if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
  161. {
  162. ret |= generic_check( md_info, argv[3] );
  163. goto exit;
  164. }
  165. for( i = 2; i < argc; i++ )
  166. ret |= generic_print( md_info, argv[i] );
  167. exit:
  168. md_free_ctx( &md_ctx );
  169. return( ret );
  170. }
  171. #endif /* POLARSSL_MD_C */