rsa_verify.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * RSA/SHA-256 signature verification program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_snprintf snprintf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  63. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
  64. !defined(MBEDTLS_FS_IO)
  65. int main( void )
  66. {
  67. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  68. "MBEDTLS_MD_C and/or "
  69. "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
  70. mbedtls_exit( 0 );
  71. }
  72. #else
  73. #include "mbedtls/rsa.h"
  74. #include "mbedtls/md.h"
  75. #include <stdio.h>
  76. #include <string.h>
  77. int main( int argc, char *argv[] )
  78. {
  79. FILE *f;
  80. int ret = 1;
  81. unsigned c;
  82. int exit_code = MBEDTLS_EXIT_FAILURE;
  83. size_t i;
  84. mbedtls_rsa_context rsa;
  85. unsigned char hash[32];
  86. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  87. char filename[512];
  88. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  89. if( argc != 2 )
  90. {
  91. mbedtls_printf( "usage: rsa_verify <filename>\n" );
  92. #if defined(_WIN32)
  93. mbedtls_printf( "\n" );
  94. #endif
  95. goto exit;
  96. }
  97. mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
  98. fflush( stdout );
  99. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  100. {
  101. mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
  102. " ! Please run rsa_genkey first\n\n" );
  103. goto exit;
  104. }
  105. if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  106. ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  107. {
  108. mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
  109. fclose( f );
  110. goto exit;
  111. }
  112. rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
  113. fclose( f );
  114. /*
  115. * Extract the RSA signature from the text file
  116. */
  117. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
  118. if( ( f = fopen( filename, "rb" ) ) == NULL )
  119. {
  120. mbedtls_printf( "\n ! Could not open %s\n\n", filename );
  121. goto exit;
  122. }
  123. i = 0;
  124. while( fscanf( f, "%02X", &c ) > 0 &&
  125. i < (int) sizeof( buf ) )
  126. buf[i++] = (unsigned char) c;
  127. fclose( f );
  128. if( i != rsa.len )
  129. {
  130. mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
  131. goto exit;
  132. }
  133. /*
  134. * Compute the SHA-256 hash of the input file and
  135. * verify the signature
  136. */
  137. mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
  138. fflush( stdout );
  139. if( ( ret = mbedtls_md_file(
  140. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  141. argv[1], hash ) ) != 0 )
  142. {
  143. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
  144. goto exit;
  145. }
  146. if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
  147. MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
  148. {
  149. mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
  150. goto exit;
  151. }
  152. mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
  153. exit_code = MBEDTLS_EXIT_SUCCESS;
  154. exit:
  155. mbedtls_rsa_free( &rsa );
  156. #if defined(_WIN32)
  157. mbedtls_printf( " + Press Enter to exit this program.\n" );
  158. fflush( stdout ); getchar();
  159. #endif
  160. mbedtls_exit( exit_code );
  161. }
  162. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  163. MBEDTLS_FS_IO */