rsa_verify.c 4.4 KB

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