rsa_verify_pss.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  22. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  23. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  24. !defined(MBEDTLS_CTR_DRBG_C)
  25. int main(void)
  26. {
  27. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  28. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  29. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  30. "MBEDTLS_CTR_DRBG_C not defined.\n");
  31. mbedtls_exit(0);
  32. }
  33. #else
  34. #include "mbedtls/md.h"
  35. #include "mbedtls/pem.h"
  36. #include "mbedtls/pk.h"
  37. #include "mbedtls/md.h"
  38. #include <stdio.h>
  39. #include <string.h>
  40. int main(int argc, char *argv[])
  41. {
  42. FILE *f;
  43. int ret = 1;
  44. int exit_code = MBEDTLS_EXIT_FAILURE;
  45. size_t i;
  46. mbedtls_pk_context pk;
  47. unsigned char hash[32];
  48. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  49. char filename[512];
  50. mbedtls_pk_init(&pk);
  51. if (argc != 3) {
  52. mbedtls_printf("usage: rsa_verify_pss <key_file> <filename>\n");
  53. #if defined(_WIN32)
  54. mbedtls_printf("\n");
  55. #endif
  56. goto exit;
  57. }
  58. mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
  59. fflush(stdout);
  60. if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
  61. mbedtls_printf(" failed\n ! Could not read key from '%s'\n", argv[1]);
  62. mbedtls_printf(" ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret);
  63. goto exit;
  64. }
  65. if (!mbedtls_pk_can_do(&pk, MBEDTLS_PK_RSA)) {
  66. mbedtls_printf(" failed\n ! Key is not an RSA key\n");
  67. goto exit;
  68. }
  69. if ((ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(pk),
  70. MBEDTLS_RSA_PKCS_V21,
  71. MBEDTLS_MD_SHA256)) != 0) {
  72. mbedtls_printf(" failed\n ! Invalid padding\n");
  73. goto exit;
  74. }
  75. /*
  76. * Extract the RSA signature from the file
  77. */
  78. mbedtls_snprintf(filename, 512, "%s.sig", argv[2]);
  79. if ((f = fopen(filename, "rb")) == NULL) {
  80. mbedtls_printf("\n ! Could not open %s\n\n", filename);
  81. goto exit;
  82. }
  83. i = fread(buf, 1, MBEDTLS_MPI_MAX_SIZE, f);
  84. fclose(f);
  85. /*
  86. * Compute the SHA-256 hash of the input file and
  87. * verify the signature
  88. */
  89. mbedtls_printf("\n . Verifying the RSA/SHA-256 signature");
  90. fflush(stdout);
  91. if ((ret = mbedtls_md_file(
  92. mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
  93. argv[2], hash)) != 0) {
  94. mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
  95. goto exit;
  96. }
  97. if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
  98. buf, i)) != 0) {
  99. mbedtls_printf(" failed\n ! mbedtls_pk_verify returned %d\n\n", ret);
  100. goto exit;
  101. }
  102. mbedtls_printf("\n . OK (the signature is valid)\n\n");
  103. exit_code = MBEDTLS_EXIT_SUCCESS;
  104. exit:
  105. mbedtls_pk_free(&pk);
  106. mbedtls_exit(exit_code);
  107. }
  108. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  109. MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */