rsa_genkey.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Example RSA key generation program
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #define mbedtls_printf printf
  31. #endif
  32. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
  33. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
  34. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
  35. #include "mbedtls/entropy.h"
  36. #include "mbedtls/ctr_drbg.h"
  37. #include "mbedtls/bignum.h"
  38. #include "mbedtls/x509.h"
  39. #include "mbedtls/rsa.h"
  40. #include <stdio.h>
  41. #include <string.h>
  42. #endif
  43. #define KEY_SIZE 2048
  44. #define EXPONENT 65537
  45. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  46. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
  47. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
  48. int main( void )
  49. {
  50. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  51. "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
  52. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  53. return( 0 );
  54. }
  55. #else
  56. int main( void )
  57. {
  58. int ret;
  59. mbedtls_rsa_context rsa;
  60. mbedtls_entropy_context entropy;
  61. mbedtls_ctr_drbg_context ctr_drbg;
  62. FILE *fpub = NULL;
  63. FILE *fpriv = NULL;
  64. const char *pers = "rsa_genkey";
  65. mbedtls_ctr_drbg_init( &ctr_drbg );
  66. mbedtls_printf( "\n . Seeding the random number generator..." );
  67. fflush( stdout );
  68. mbedtls_entropy_init( &entropy );
  69. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  70. (const unsigned char *) pers,
  71. strlen( pers ) ) ) != 0 )
  72. {
  73. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  74. goto exit;
  75. }
  76. mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  77. fflush( stdout );
  78. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  79. if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
  80. EXPONENT ) ) != 0 )
  81. {
  82. mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
  83. goto exit;
  84. }
  85. mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  86. fflush( stdout );
  87. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  88. {
  89. mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  90. ret = 1;
  91. goto exit;
  92. }
  93. if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
  94. ( ret = mbedtls_mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
  95. {
  96. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  97. goto exit;
  98. }
  99. mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  100. fflush( stdout );
  101. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  102. {
  103. mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  104. ret = 1;
  105. goto exit;
  106. }
  107. if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
  108. ( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
  109. ( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
  110. ( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
  111. ( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
  112. ( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
  113. ( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
  114. ( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
  115. {
  116. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  117. goto exit;
  118. }
  119. /*
  120. mbedtls_printf( " ok\n . Generating the certificate..." );
  121. x509write_init_raw( &cert );
  122. x509write_add_pubkey( &cert, &rsa );
  123. x509write_add_subject( &cert, "CN='localhost'" );
  124. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  125. "2010-09-06 17:00:32" );
  126. x509write_create_selfsign( &cert, &rsa );
  127. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  128. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  129. x509write_free_raw( &cert );
  130. */
  131. mbedtls_printf( " ok\n\n" );
  132. exit:
  133. if( fpub != NULL )
  134. fclose( fpub );
  135. if( fpriv != NULL )
  136. fclose( fpriv );
  137. mbedtls_rsa_free( &rsa );
  138. mbedtls_ctr_drbg_free( &ctr_drbg );
  139. mbedtls_entropy_free( &entropy );
  140. #if defined(_WIN32)
  141. mbedtls_printf( " Press Enter to exit this program.\n" );
  142. fflush( stdout ); getchar();
  143. #endif
  144. return( ret );
  145. }
  146. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  147. MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */