pk_encrypt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_PK_PARSE_C) && \
  22. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
  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_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
  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_ENTROPY_C and/or MBEDTLS_FS_IO 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. int exit_code = MBEDTLS_EXIT_FAILURE;
  47. size_t i, olen = 0;
  48. mbedtls_pk_context pk;
  49. mbedtls_entropy_context entropy;
  50. mbedtls_ctr_drbg_context ctr_drbg;
  51. unsigned char input[1024];
  52. unsigned char buf[512];
  53. const char *pers = "mbedtls_pk_encrypt";
  54. mbedtls_ctr_drbg_init(&ctr_drbg);
  55. mbedtls_entropy_init(&entropy);
  56. mbedtls_pk_init(&pk);
  57. if (argc != 3) {
  58. mbedtls_printf("usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n");
  59. #if defined(_WIN32)
  60. mbedtls_printf("\n");
  61. #endif
  62. goto exit;
  63. }
  64. mbedtls_printf("\n . Seeding the random number generator...");
  65. fflush(stdout);
  66. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  67. &entropy, (const unsigned char *) pers,
  68. strlen(pers))) != 0) {
  69. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
  70. (unsigned int) -ret);
  71. goto exit;
  72. }
  73. mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
  74. fflush(stdout);
  75. if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
  76. mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
  77. (unsigned int) -ret);
  78. goto exit;
  79. }
  80. if (strlen(argv[2]) > 100) {
  81. mbedtls_printf(" Input data larger than 100 characters.\n\n");
  82. goto exit;
  83. }
  84. memcpy(input, argv[2], strlen(argv[2]));
  85. /*
  86. * Calculate the RSA encryption of the hash.
  87. */
  88. mbedtls_printf("\n . Generating the encrypted value");
  89. fflush(stdout);
  90. if ((ret = mbedtls_pk_encrypt(&pk, input, strlen(argv[2]),
  91. buf, &olen, sizeof(buf),
  92. mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
  93. mbedtls_printf(" failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
  94. (unsigned int) -ret);
  95. goto exit;
  96. }
  97. /*
  98. * Write the signature into result-enc.txt
  99. */
  100. if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
  101. mbedtls_printf(" failed\n ! Could not create %s\n\n",
  102. "result-enc.txt");
  103. ret = 1;
  104. goto exit;
  105. }
  106. for (i = 0; i < olen; i++) {
  107. mbedtls_fprintf(f, "%02X%s", buf[i],
  108. (i + 1) % 16 == 0 ? "\r\n" : " ");
  109. }
  110. fclose(f);
  111. mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
  112. exit_code = MBEDTLS_EXIT_SUCCESS;
  113. exit:
  114. mbedtls_pk_free(&pk);
  115. mbedtls_entropy_free(&entropy);
  116. mbedtls_ctr_drbg_free(&ctr_drbg);
  117. #if defined(MBEDTLS_ERROR_C)
  118. if (exit_code != MBEDTLS_EXIT_SUCCESS) {
  119. mbedtls_strerror(ret, (char *) buf, sizeof(buf));
  120. mbedtls_printf(" ! Last error was: %s\n", buf);
  121. }
  122. #endif
  123. mbedtls_exit(exit_code);
  124. }
  125. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
  126. MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */