rsa_verify_pss.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #define mbedtls_snprintf snprintf
  31. #define mbedtls_printf printf
  32. #endif
  33. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  34. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  35. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  36. !defined(MBEDTLS_CTR_DRBG_C)
  37. int main( void )
  38. {
  39. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  40. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  41. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  42. "MBEDTLS_CTR_DRBG_C not defined.\n");
  43. return( 0 );
  44. }
  45. #else
  46. #include "mbedtls/md.h"
  47. #include "mbedtls/pem.h"
  48. #include "mbedtls/pk.h"
  49. #include "mbedtls/md.h"
  50. #include "mbedtls/x509.h"
  51. #include <stdio.h>
  52. #include <string.h>
  53. int main( int argc, char *argv[] )
  54. {
  55. FILE *f;
  56. int ret = 1;
  57. size_t i;
  58. mbedtls_pk_context pk;
  59. unsigned char hash[32];
  60. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  61. char filename[512];
  62. mbedtls_pk_init( &pk );
  63. if( argc != 3 )
  64. {
  65. mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
  66. #if defined(_WIN32)
  67. mbedtls_printf( "\n" );
  68. #endif
  69. goto exit;
  70. }
  71. mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
  72. fflush( stdout );
  73. if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  74. {
  75. mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  76. mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
  77. goto exit;
  78. }
  79. if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
  80. {
  81. ret = 1;
  82. mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
  83. goto exit;
  84. }
  85. mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
  86. /*
  87. * Extract the RSA signature from the file
  88. */
  89. ret = 1;
  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. ret = 0;
  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. return( ret );
  126. }
  127. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  128. MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */