rsa_verify_pss.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * RSASSA-PSS/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_snprintf snprintf
  30. #define mbedtls_printf printf
  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_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  36. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  37. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  38. !defined(MBEDTLS_CTR_DRBG_C)
  39. int main( void )
  40. {
  41. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  42. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  43. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  44. "MBEDTLS_CTR_DRBG_C not defined.\n");
  45. mbedtls_exit( 0 );
  46. }
  47. #else
  48. #include "mbedtls/md.h"
  49. #include "mbedtls/pem.h"
  50. #include "mbedtls/pk.h"
  51. #include "mbedtls/md.h"
  52. #include <stdio.h>
  53. #include <string.h>
  54. int main( int argc, char *argv[] )
  55. {
  56. FILE *f;
  57. int ret = 1;
  58. int exit_code = MBEDTLS_EXIT_FAILURE;
  59. size_t i;
  60. mbedtls_pk_context pk;
  61. unsigned char hash[32];
  62. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  63. char filename[512];
  64. mbedtls_pk_init( &pk );
  65. if( argc != 3 )
  66. {
  67. mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
  68. #if defined(_WIN32)
  69. mbedtls_printf( "\n" );
  70. #endif
  71. goto exit;
  72. }
  73. mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
  74. fflush( stdout );
  75. if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  76. {
  77. mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  78. mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
  79. goto exit;
  80. }
  81. if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
  82. {
  83. mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
  84. goto exit;
  85. }
  86. mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
  87. /*
  88. * Extract the RSA signature from the file
  89. */
  90. mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
  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 = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
  97. fclose( f );
  98. /*
  99. * Compute the SHA-256 hash of the input file and
  100. * verify the signature
  101. */
  102. mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
  103. fflush( stdout );
  104. if( ( ret = mbedtls_md_file(
  105. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  106. argv[2], hash ) ) != 0 )
  107. {
  108. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  109. goto exit;
  110. }
  111. if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
  112. buf, i ) ) != 0 )
  113. {
  114. mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
  115. goto exit;
  116. }
  117. mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
  118. exit_code = MBEDTLS_EXIT_SUCCESS;
  119. exit:
  120. mbedtls_pk_free( &pk );
  121. #if defined(_WIN32)
  122. mbedtls_printf( " + Press Enter to exit this program.\n" );
  123. fflush( stdout ); getchar();
  124. #endif
  125. mbedtls_exit( exit_code );
  126. }
  127. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  128. MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */