rsa_verify.c 4.2 KB

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