rsa_genkey.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Example RSA key generation 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_ENTROPY_C) && \
  22. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
  23. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
  24. #include "mbedtls/entropy.h"
  25. #include "mbedtls/ctr_drbg.h"
  26. #include "mbedtls/bignum.h"
  27. #include "mbedtls/rsa.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #endif
  31. #define KEY_SIZE 2048
  32. #define EXPONENT 65537
  33. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  34. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
  35. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
  36. int main(void)
  37. {
  38. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  39. "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
  40. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  41. mbedtls_exit(0);
  42. }
  43. #else
  44. int main(void)
  45. {
  46. int ret = 1;
  47. int exit_code = MBEDTLS_EXIT_FAILURE;
  48. mbedtls_rsa_context rsa;
  49. mbedtls_entropy_context entropy;
  50. mbedtls_ctr_drbg_context ctr_drbg;
  51. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  52. FILE *fpub = NULL;
  53. FILE *fpriv = NULL;
  54. const char *pers = "rsa_genkey";
  55. mbedtls_ctr_drbg_init(&ctr_drbg);
  56. mbedtls_rsa_init(&rsa);
  57. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  58. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
  59. mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
  60. mbedtls_printf("\n . Seeding the random number generator...");
  61. fflush(stdout);
  62. mbedtls_entropy_init(&entropy);
  63. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  64. (const unsigned char *) pers,
  65. strlen(pers))) != 0) {
  66. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  67. goto exit;
  68. }
  69. mbedtls_printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
  70. fflush(stdout);
  71. if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
  72. EXPONENT)) != 0) {
  73. mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret);
  74. goto exit;
  75. }
  76. mbedtls_printf(" ok\n . Exporting the public key in rsa_pub.txt....");
  77. fflush(stdout);
  78. if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
  79. (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP)) != 0) {
  80. mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
  81. goto exit;
  82. }
  83. if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
  84. mbedtls_printf(" failed\n ! could not open rsa_pub.txt for writing\n\n");
  85. goto exit;
  86. }
  87. if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
  88. (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
  89. mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
  90. goto exit;
  91. }
  92. mbedtls_printf(" ok\n . Exporting the private key in rsa_priv.txt...");
  93. fflush(stdout);
  94. if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
  95. mbedtls_printf(" failed\n ! could not open rsa_priv.txt for writing\n");
  96. goto exit;
  97. }
  98. if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
  99. (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
  100. (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
  101. (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
  102. (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
  103. (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
  104. (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
  105. (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
  106. mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
  107. goto exit;
  108. }
  109. mbedtls_printf(" ok\n\n");
  110. exit_code = MBEDTLS_EXIT_SUCCESS;
  111. exit:
  112. if (fpub != NULL) {
  113. fclose(fpub);
  114. }
  115. if (fpriv != NULL) {
  116. fclose(fpriv);
  117. }
  118. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  119. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
  120. mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
  121. mbedtls_rsa_free(&rsa);
  122. mbedtls_ctr_drbg_free(&ctr_drbg);
  123. mbedtls_entropy_free(&entropy);
  124. mbedtls_exit(exit_code);
  125. }
  126. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  127. MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */