generic_sum.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef PRINTF_STDLIB
  30. #include <stdio.h>
  31. #endif
  32. #ifdef PRINTF_CUSTOM
  33. #include "tinystdio.h"
  34. #endif
  35. #include "polarssl/config.h"
  36. #include "polarssl/md.h"
  37. #if !defined(POLARSSL_MD_C)
  38. int main( void )
  39. {
  40. printf("POLARSSL_MD_C not defined.\n");
  41. return( 0 );
  42. }
  43. #else
  44. static int generic_wrapper( const md_info_t *md_info, char *filename, unsigned char *sum )
  45. {
  46. int ret = md_file( md_info, filename, sum );
  47. if( ret == 1 )
  48. fprintf( stderr, "failed to open: %s\n", filename );
  49. if( ret == 2 )
  50. fprintf( stderr, "failed to read: %s\n", filename );
  51. return( ret );
  52. }
  53. static int generic_print( const md_info_t *md_info, char *filename )
  54. {
  55. int i;
  56. unsigned char sum[POLARSSL_MD_MAX_SIZE];
  57. if( generic_wrapper( md_info, filename, sum ) != 0 )
  58. return( 1 );
  59. for( i = 0; i < md_info->size; i++ )
  60. printf( "%02x", sum[i] );
  61. printf( " %s\n", filename );
  62. return( 0 );
  63. }
  64. static int generic_check( const md_info_t *md_info, char *filename )
  65. {
  66. int i;
  67. size_t n;
  68. FILE *f;
  69. int nb_err1, nb_err2;
  70. int nb_tot1, nb_tot2;
  71. unsigned char sum[POLARSSL_MD_MAX_SIZE];
  72. char buf[POLARSSL_MD_MAX_SIZE * 2 + 1], line[1024];
  73. if( ( f = fopen( filename, "rb" ) ) == NULL )
  74. {
  75. printf( "failed to open: %s\n", filename );
  76. return( 1 );
  77. }
  78. nb_err1 = nb_err2 = 0;
  79. nb_tot1 = nb_tot2 = 0;
  80. memset( line, 0, sizeof( line ) );
  81. n = sizeof( line );
  82. while( fgets( line, (int) n - 1, f ) != NULL )
  83. {
  84. n = strlen( line );
  85. if( n < (size_t) 2 * md_info->size + 4 )
  86. {
  87. printf("No '%s' hash found on line.\n", md_info->name);
  88. continue;
  89. }
  90. if( line[2 * md_info->size] != ' ' || line[2 * md_info->size + 1] != ' ' )
  91. {
  92. printf("No '%s' hash found on line.\n", md_info->name);
  93. continue;
  94. }
  95. if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
  96. if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
  97. nb_tot1++;
  98. if( generic_wrapper( md_info, line + 2 + 2 * md_info->size, sum ) != 0 )
  99. {
  100. nb_err1++;
  101. continue;
  102. }
  103. nb_tot2++;
  104. for( i = 0; i < md_info->size; i++ )
  105. sprintf( buf + i * 2, "%02x", sum[i] );
  106. if( memcmp( line, buf, 2 * md_info->size ) != 0 )
  107. {
  108. nb_err2++;
  109. fprintf( stderr, "wrong checksum: %s\n", line + 66 );
  110. }
  111. n = sizeof( line );
  112. }
  113. if( nb_err1 != 0 )
  114. {
  115. printf( "WARNING: %d (out of %d) input files could "
  116. "not be read\n", nb_err1, nb_tot1 );
  117. }
  118. if( nb_err2 != 0 )
  119. {
  120. printf( "WARNING: %d (out of %d) computed checksums did "
  121. "not match\n", nb_err2, nb_tot2 );
  122. }
  123. return( nb_err1 != 0 || nb_err2 != 0 );
  124. }
  125. int main( int argc, char *argv[] )
  126. {
  127. int ret, i;
  128. const md_info_t *md_info;
  129. md_context_t md_ctx;
  130. memset( &md_ctx, 0, sizeof( md_context_t ));
  131. if( argc == 1 )
  132. {
  133. const int *list;
  134. printf( "print mode: generic_sum <md> <file> <file> ...\n" );
  135. printf( "check mode: generic_sum <md> -c <checksum file>\n" );
  136. printf( "\nAvailable message digests:\n" );
  137. list = md_list();
  138. while( *list )
  139. {
  140. md_info = md_info_from_type( *list );
  141. printf( " %s\n", md_info->name );
  142. list++;
  143. }
  144. #ifdef WIN32
  145. printf( "\n Press Enter to exit this program.\n" );
  146. fflush( stdout ); getchar();
  147. #endif
  148. return( 1 );
  149. }
  150. /*
  151. * Read the MD from the command line
  152. */
  153. md_info = md_info_from_string( argv[1] );
  154. if( md_info == NULL )
  155. {
  156. fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
  157. return( 1 );
  158. }
  159. if( md_init_ctx( &md_ctx, md_info) )
  160. {
  161. fprintf( stderr, "Failed to initialize context.\n" );
  162. return( 1 );
  163. }
  164. ret = 0;
  165. if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
  166. {
  167. ret |= generic_check( md_info, argv[3] );
  168. goto exit;
  169. }
  170. for( i = 2; i < argc; i++ )
  171. ret |= generic_print( md_info, argv[i] );
  172. exit:
  173. md_free_ctx( &md_ctx );
  174. return( ret );
  175. }
  176. #endif /* POLARSSL_MD_C */