rsa_decrypt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * RSA simple decryption 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_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  23. defined(MBEDTLS_CTR_DRBG_C)
  24. #include "mbedtls/rsa.h"
  25. #include "mbedtls/entropy.h"
  26. #include "mbedtls/ctr_drbg.h"
  27. #include <string.h>
  28. #endif
  29. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
  30. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  31. !defined(MBEDTLS_CTR_DRBG_C)
  32. int main(void)
  33. {
  34. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
  35. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  36. "MBEDTLS_CTR_DRBG_C not defined.\n");
  37. mbedtls_exit(0);
  38. }
  39. #else
  40. int main(int argc, char *argv[])
  41. {
  42. FILE *f;
  43. int ret = 1;
  44. int exit_code = MBEDTLS_EXIT_FAILURE;
  45. unsigned c;
  46. size_t i;
  47. mbedtls_rsa_context rsa;
  48. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  49. mbedtls_entropy_context entropy;
  50. mbedtls_ctr_drbg_context ctr_drbg;
  51. unsigned char result[1024];
  52. unsigned char buf[512];
  53. const char *pers = "rsa_decrypt";
  54. ((void) argv);
  55. memset(result, 0, sizeof(result));
  56. if (argc != 1) {
  57. mbedtls_printf("usage: rsa_decrypt\n");
  58. #if defined(_WIN32)
  59. mbedtls_printf("\n");
  60. #endif
  61. mbedtls_exit(exit_code);
  62. }
  63. mbedtls_printf("\n . Seeding the random number generator...");
  64. fflush(stdout);
  65. mbedtls_rsa_init(&rsa);
  66. mbedtls_ctr_drbg_init(&ctr_drbg);
  67. mbedtls_entropy_init(&entropy);
  68. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  69. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
  70. mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
  71. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  72. &entropy, (const unsigned char *) pers,
  73. strlen(pers));
  74. if (ret != 0) {
  75. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  76. ret);
  77. goto exit;
  78. }
  79. mbedtls_printf("\n . Reading private key from rsa_priv.txt");
  80. fflush(stdout);
  81. if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
  82. mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
  83. " ! Please run rsa_genkey first\n\n");
  84. goto exit;
  85. }
  86. if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
  87. (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
  88. (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
  89. (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
  90. (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
  91. (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
  92. (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
  93. (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
  94. mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  95. ret);
  96. fclose(f);
  97. goto exit;
  98. }
  99. fclose(f);
  100. if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
  101. mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
  102. ret);
  103. goto exit;
  104. }
  105. if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
  106. mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
  107. ret);
  108. goto exit;
  109. }
  110. /*
  111. * Extract the RSA encrypted value from the text file
  112. */
  113. if ((f = fopen("result-enc.txt", "rb")) == NULL) {
  114. mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
  115. goto exit;
  116. }
  117. i = 0;
  118. while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
  119. i < (int) sizeof(buf)) {
  120. buf[i++] = (unsigned char) c;
  121. }
  122. fclose(f);
  123. if (i != rsa.MBEDTLS_PRIVATE(len)) {
  124. mbedtls_printf("\n ! Invalid RSA signature format\n\n");
  125. goto exit;
  126. }
  127. /*
  128. * Decrypt the encrypted RSA data and print the result.
  129. */
  130. mbedtls_printf("\n . Decrypting the encrypted data");
  131. fflush(stdout);
  132. ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random,
  133. &ctr_drbg, &i,
  134. buf, result, 1024);
  135. if (ret != 0) {
  136. mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
  137. ret);
  138. goto exit;
  139. }
  140. mbedtls_printf("\n . OK\n\n");
  141. mbedtls_printf("The decrypted result is: '%s'\n\n", result);
  142. exit_code = MBEDTLS_EXIT_SUCCESS;
  143. exit:
  144. mbedtls_ctr_drbg_free(&ctr_drbg);
  145. mbedtls_entropy_free(&entropy);
  146. mbedtls_rsa_free(&rsa);
  147. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  148. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
  149. mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
  150. mbedtls_exit(exit_code);
  151. }
  152. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */