rsa_encrypt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * RSA simple data encryption 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_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
  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_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
  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_ENTROPY_C and/or MBEDTLS_FS_IO 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. size_t i;
  46. mbedtls_rsa_context rsa;
  47. mbedtls_entropy_context entropy;
  48. mbedtls_ctr_drbg_context ctr_drbg;
  49. unsigned char input[1024];
  50. unsigned char buf[512];
  51. const char *pers = "rsa_encrypt";
  52. mbedtls_mpi N, E;
  53. if (argc != 2) {
  54. mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
  55. #if defined(_WIN32)
  56. mbedtls_printf("\n");
  57. #endif
  58. mbedtls_exit(exit_code);
  59. }
  60. mbedtls_printf("\n . Seeding the random number generator...");
  61. fflush(stdout);
  62. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  63. mbedtls_rsa_init(&rsa);
  64. mbedtls_ctr_drbg_init(&ctr_drbg);
  65. mbedtls_entropy_init(&entropy);
  66. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  67. &entropy, (const unsigned char *) pers,
  68. strlen(pers));
  69. if (ret != 0) {
  70. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  71. ret);
  72. goto exit;
  73. }
  74. mbedtls_printf("\n . Reading public key from rsa_pub.txt");
  75. fflush(stdout);
  76. if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
  77. mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
  78. " ! Please run rsa_genkey first\n\n");
  79. goto exit;
  80. }
  81. if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
  82. (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0) {
  83. mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
  84. ret);
  85. fclose(f);
  86. goto exit;
  87. }
  88. fclose(f);
  89. if ((ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E)) != 0) {
  90. mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
  91. ret);
  92. goto exit;
  93. }
  94. if (strlen(argv[1]) > 100) {
  95. mbedtls_printf(" Input data larger than 100 characters.\n\n");
  96. goto exit;
  97. }
  98. memcpy(input, argv[1], strlen(argv[1]));
  99. /*
  100. * Calculate the RSA encryption of the hash.
  101. */
  102. mbedtls_printf("\n . Generating the RSA encrypted value");
  103. fflush(stdout);
  104. ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random,
  105. &ctr_drbg, strlen(argv[1]), input, buf);
  106. if (ret != 0) {
  107. mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
  108. ret);
  109. goto exit;
  110. }
  111. /*
  112. * Write the signature into result-enc.txt
  113. */
  114. if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
  115. mbedtls_printf(" failed\n ! Could not create %s\n\n", "result-enc.txt");
  116. goto exit;
  117. }
  118. for (i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++) {
  119. mbedtls_fprintf(f, "%02X%s", buf[i],
  120. (i + 1) % 16 == 0 ? "\r\n" : " ");
  121. }
  122. fclose(f);
  123. mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
  124. exit_code = MBEDTLS_EXIT_SUCCESS;
  125. exit:
  126. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  127. mbedtls_ctr_drbg_free(&ctr_drbg);
  128. mbedtls_entropy_free(&entropy);
  129. mbedtls_rsa_free(&rsa);
  130. mbedtls_exit(exit_code);
  131. }
  132. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
  133. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */