rsa_verify.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * RSA/SHA-256 signature verification program
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_snprintf snprintf
  31. #define mbedtls_exit exit
  32. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  33. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  34. #endif /* MBEDTLS_PLATFORM_C */
  35. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  36. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
  37. !defined(MBEDTLS_FS_IO)
  38. int main( void )
  39. {
  40. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  41. "MBEDTLS_MD_C and/or "
  42. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
  43. mbedtls_exit( 0 );
  44. }
  45. #else
  46. #include "mbedtls/rsa.h"
  47. #include "mbedtls/md.h"
  48. #include <stdio.h>
  49. #include <string.h>
  50. int main( int argc, char *argv[] )
  51. {
  52. FILE *f;
  53. int ret = 1;
  54. unsigned c;
  55. int exit_code = MBEDTLS_EXIT_FAILURE;
  56. size_t i;
  57. mbedtls_rsa_context rsa;
  58. unsigned char hash[32];
  59. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  60. char filename[512];
  61. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  62. if( argc != 2 )
  63. {
  64. mbedtls_printf( "usage: rsa_verify <filename>\n" );
  65. #if defined(_WIN32)
  66. mbedtls_printf( "\n" );
  67. #endif
  68. goto exit;
  69. }
  70. mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
  71. fflush( stdout );
  72. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  73. {
  74. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
  75. " ! Please run rsa_genkey first\n\n" );
  76. goto exit;
  77. }
  78. if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  79. ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  80. {
  81. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
  82. fclose( f );
  83. goto exit;
  84. }
  85. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  86. fclose( f );
  87. /*
  88. * Extract the RSA signature from the text file
  89. */
  90. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
  91. if( ( f = fopen( filename, "rb" ) ) == NULL )
  92. {
  93. mbedtls_printf( "\n ! Could not open %s\n\n", filename );
  94. goto exit;
  95. }
  96. i = 0;
  97. while( fscanf( f, "%02X", (unsigned int*) &c ) > 0 &&
  98. i < (int) sizeof( buf ) )
  99. buf[i++] = (unsigned char) c;
  100. fclose( f );
  101. if( i != rsa.len )
  102. {
  103. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  104. goto exit;
  105. }
  106. /*
  107. * Compute the SHA-256 hash of the input file and
  108. * verify the signature
  109. */
  110. mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
  111. fflush( stdout );
  112. if( ( ret = mbedtls_md_file(
  113. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  114. argv[1], hash ) ) != 0 )
  115. {
  116. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
  117. goto exit;
  118. }
  119. if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
  120. MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
  121. {
  122. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", (unsigned int) -ret );
  123. goto exit;
  124. }
  125. mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
  126. exit_code = MBEDTLS_EXIT_SUCCESS;
  127. exit:
  128. mbedtls_rsa_free( &rsa );
  129. #if defined(_WIN32)
  130. mbedtls_printf( " + Press Enter to exit this program.\n" );
  131. fflush( stdout ); getchar();
  132. #endif
  133. mbedtls_exit( exit_code );
  134. }
  135. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  136. MBEDTLS_FS_IO */