rsa_verify.c 3.8 KB

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