rsa_genkey.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Example RSA key generation program
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_PLATFORM_C)
  29. #include "mbedtls/platform.h"
  30. #else
  31. #include <stdio.h>
  32. #define mbedtls_printf printf
  33. #endif
  34. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
  35. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
  36. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
  37. #include "mbedtls/entropy.h"
  38. #include "mbedtls/ctr_drbg.h"
  39. #include "mbedtls/bignum.h"
  40. #include "mbedtls/x509.h"
  41. #include "mbedtls/rsa.h"
  42. #include <stdio.h>
  43. #include <string.h>
  44. #endif
  45. #define KEY_SIZE 2048
  46. #define EXPONENT 65537
  47. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  48. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
  49. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
  50. int main( void )
  51. {
  52. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  53. "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
  54. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  55. return( 0 );
  56. }
  57. #else
  58. int main( void )
  59. {
  60. int ret;
  61. mbedtls_rsa_context rsa;
  62. mbedtls_entropy_context entropy;
  63. mbedtls_ctr_drbg_context ctr_drbg;
  64. FILE *fpub = NULL;
  65. FILE *fpriv = NULL;
  66. const char *pers = "rsa_genkey";
  67. mbedtls_ctr_drbg_init( &ctr_drbg );
  68. mbedtls_printf( "\n . Seeding the random number generator..." );
  69. fflush( stdout );
  70. mbedtls_entropy_init( &entropy );
  71. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  72. (const unsigned char *) pers,
  73. strlen( pers ) ) ) != 0 )
  74. {
  75. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  76. goto exit;
  77. }
  78. mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  79. fflush( stdout );
  80. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  81. if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
  82. EXPONENT ) ) != 0 )
  83. {
  84. mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
  85. goto exit;
  86. }
  87. mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  88. fflush( stdout );
  89. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  90. {
  91. mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  92. ret = 1;
  93. goto exit;
  94. }
  95. if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
  96. ( ret = mbedtls_mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
  97. {
  98. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  99. goto exit;
  100. }
  101. mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  102. fflush( stdout );
  103. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  104. {
  105. mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  106. ret = 1;
  107. goto exit;
  108. }
  109. if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
  110. ( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
  111. ( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
  112. ( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
  113. ( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
  114. ( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
  115. ( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
  116. ( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
  117. {
  118. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  119. goto exit;
  120. }
  121. /*
  122. mbedtls_printf( " ok\n . Generating the certificate..." );
  123. x509write_init_raw( &cert );
  124. x509write_add_pubkey( &cert, &rsa );
  125. x509write_add_subject( &cert, "CN='localhost'" );
  126. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  127. "2010-09-06 17:00:32" );
  128. x509write_create_selfsign( &cert, &rsa );
  129. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  130. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  131. x509write_free_raw( &cert );
  132. */
  133. mbedtls_printf( " ok\n\n" );
  134. exit:
  135. if( fpub != NULL )
  136. fclose( fpub );
  137. if( fpriv != NULL )
  138. fclose( fpriv );
  139. mbedtls_rsa_free( &rsa );
  140. mbedtls_ctr_drbg_free( &ctr_drbg );
  141. mbedtls_entropy_free( &entropy );
  142. #if defined(_WIN32)
  143. mbedtls_printf( " Press Enter to exit this program.\n" );
  144. fflush( stdout ); getchar();
  145. #endif
  146. return( ret );
  147. }
  148. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  149. MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */