rsa_verify.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * RSA/SHA-1 signature verification 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/rsa.h"
  37. #include "polarssl/sha1.h"
  38. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  39. !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
  40. int main( void )
  41. {
  42. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  43. "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
  44. return( 0 );
  45. }
  46. #else
  47. int main( int argc, char *argv[] )
  48. {
  49. FILE *f;
  50. int ret, c;
  51. size_t i;
  52. rsa_context rsa;
  53. unsigned char hash[20];
  54. unsigned char buf[512];
  55. ret = 1;
  56. if( argc != 2 )
  57. {
  58. printf( "usage: rsa_verify <filename>\n" );
  59. #ifdef WIN32
  60. printf( "\n" );
  61. #endif
  62. goto exit;
  63. }
  64. printf( "\n . Reading public key from rsa_pub.txt" );
  65. fflush( stdout );
  66. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  67. {
  68. printf( " failed\n ! Could not open rsa_pub.txt\n" \
  69. " ! Please run rsa_genkey first\n\n" );
  70. goto exit;
  71. }
  72. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  73. if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  74. ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  75. {
  76. printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
  77. goto exit;
  78. }
  79. rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
  80. fclose( f );
  81. /*
  82. * Extract the RSA signature from the text file
  83. */
  84. ret = 1;
  85. i = strlen( argv[1] );
  86. memcpy( argv[1] + i, ".sig", 5 );
  87. if( ( f = fopen( argv[1], "rb" ) ) == NULL )
  88. {
  89. printf( "\n ! Could not open %s\n\n", argv[1] );
  90. goto exit;
  91. }
  92. argv[1][i] = '\0', i = 0;
  93. while( fscanf( f, "%02X", &c ) > 0 &&
  94. i < (int) sizeof( buf ) )
  95. buf[i++] = (unsigned char) c;
  96. fclose( f );
  97. if( i != rsa.len )
  98. {
  99. printf( "\n ! Invalid RSA signature format\n\n" );
  100. goto exit;
  101. }
  102. /*
  103. * Compute the SHA-1 hash of the input file and compare
  104. * it with the hash decrypted from the RSA signature.
  105. */
  106. printf( "\n . Verifying the RSA/SHA-1 signature" );
  107. fflush( stdout );
  108. if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
  109. {
  110. printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
  111. goto exit;
  112. }
  113. if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
  114. 20, hash, buf ) ) != 0 )
  115. {
  116. printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
  117. goto exit;
  118. }
  119. printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
  120. ret = 0;
  121. exit:
  122. #ifdef WIN32
  123. printf( " + Press Enter to exit this program.\n" );
  124. fflush( stdout ); getchar();
  125. #endif
  126. return( ret );
  127. }
  128. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
  129. POLARSSL_FS_IO */