rsa_verify_pss.c 4.3 KB

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