pk_decrypt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Public key-based 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_PK_PARSE_C) && \
  22. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
  23. defined(MBEDTLS_CTR_DRBG_C)
  24. #include "mbedtls/error.h"
  25. #include "mbedtls/pk.h"
  26. #include "mbedtls/entropy.h"
  27. #include "mbedtls/ctr_drbg.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #endif
  31. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
  32. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  33. !defined(MBEDTLS_CTR_DRBG_C)
  34. int main(void)
  35. {
  36. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
  37. "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
  38. "MBEDTLS_CTR_DRBG_C not defined.\n");
  39. mbedtls_exit(0);
  40. }
  41. #else
  42. int main(int argc, char *argv[])
  43. {
  44. FILE *f;
  45. int ret = 1;
  46. unsigned c;
  47. int exit_code = MBEDTLS_EXIT_FAILURE;
  48. size_t i, olen = 0;
  49. mbedtls_pk_context pk;
  50. mbedtls_entropy_context entropy;
  51. mbedtls_ctr_drbg_context ctr_drbg;
  52. unsigned char result[1024];
  53. unsigned char buf[512];
  54. const char *pers = "mbedtls_pk_decrypt";
  55. ((void) argv);
  56. mbedtls_pk_init(&pk);
  57. mbedtls_entropy_init(&entropy);
  58. mbedtls_ctr_drbg_init(&ctr_drbg);
  59. memset(result, 0, sizeof(result));
  60. if (argc != 2) {
  61. mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");
  62. #if defined(_WIN32)
  63. mbedtls_printf("\n");
  64. #endif
  65. goto exit;
  66. }
  67. mbedtls_printf("\n . Seeding the random number generator...");
  68. fflush(stdout);
  69. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  70. &entropy, (const unsigned char *) pers,
  71. strlen(pers))) != 0) {
  72. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  73. (unsigned int) -ret);
  74. goto exit;
  75. }
  76. mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
  77. fflush(stdout);
  78. if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
  79. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  80. mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
  81. (unsigned int) -ret);
  82. goto exit;
  83. }
  84. /*
  85. * Extract the RSA encrypted value from the text file
  86. */
  87. if ((f = fopen("result-enc.txt", "rb")) == NULL) {
  88. mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
  89. ret = 1;
  90. goto exit;
  91. }
  92. i = 0;
  93. while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
  94. i < (int) sizeof(buf)) {
  95. buf[i++] = (unsigned char) c;
  96. }
  97. fclose(f);
  98. /*
  99. * Decrypt the encrypted RSA data and print the result.
  100. */
  101. mbedtls_printf("\n . Decrypting the encrypted data");
  102. fflush(stdout);
  103. if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result),
  104. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  105. mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
  106. (unsigned int) -ret);
  107. goto exit;
  108. }
  109. mbedtls_printf("\n . OK\n\n");
  110. mbedtls_printf("The decrypted result is: '%s'\n\n", result);
  111. exit_code = MBEDTLS_EXIT_SUCCESS;
  112. exit:
  113. mbedtls_pk_free(&pk);
  114. mbedtls_entropy_free(&entropy);
  115. mbedtls_ctr_drbg_free(&ctr_drbg);
  116. #if defined(MBEDTLS_ERROR_C)
  117. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  118. mbedtls_strerror(ret, (char *) buf, sizeof(buf));
  119. mbedtls_printf(" ! Last error was: %s\n", buf);
  120. }
  121. #endif
  122. mbedtls_exit(exit_code);
  123. }
  124. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
  125. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */