rsa_genkey.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <stdlib.h>
  31. #define mbedtls_printf printf
  32. #define mbedtls_exit exit
  33. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  34. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  35. #endif /* MBEDTLS_PLATFORM_C */
  36. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
  37. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
  38. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
  39. #include "mbedtls/entropy.h"
  40. #include "mbedtls/ctr_drbg.h"
  41. #include "mbedtls/bignum.h"
  42. #include "mbedtls/x509.h"
  43. #include "mbedtls/rsa.h"
  44. #include <stdio.h>
  45. #include <string.h>
  46. #endif
  47. #define KEY_SIZE 2048
  48. #define EXPONENT 65537
  49. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  50. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
  51. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
  52. int main( void )
  53. {
  54. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  55. "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
  56. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  57. return( 0 );
  58. }
  59. #else
  60. int main( void )
  61. {
  62. int ret = 1;
  63. int exit_code = MBEDTLS_EXIT_FAILURE;
  64. mbedtls_rsa_context rsa;
  65. mbedtls_entropy_context entropy;
  66. mbedtls_ctr_drbg_context ctr_drbg;
  67. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  68. FILE *fpub = NULL;
  69. FILE *fpriv = NULL;
  70. const char *pers = "rsa_genkey";
  71. mbedtls_ctr_drbg_init( &ctr_drbg );
  72. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  73. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  74. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  75. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  76. mbedtls_printf( "\n . Seeding the random number generator..." );
  77. fflush( stdout );
  78. mbedtls_entropy_init( &entropy );
  79. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  80. (const unsigned char *) pers,
  81. strlen( pers ) ) ) != 0 )
  82. {
  83. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  84. goto exit;
  85. }
  86. mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  87. fflush( stdout );
  88. if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
  89. EXPONENT ) ) != 0 )
  90. {
  91. mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
  92. goto exit;
  93. }
  94. mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  95. fflush( stdout );
  96. if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
  97. ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
  98. {
  99. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  100. goto exit;
  101. }
  102. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  103. {
  104. mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  105. goto exit;
  106. }
  107. if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
  108. ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
  109. {
  110. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  111. goto exit;
  112. }
  113. mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  114. fflush( stdout );
  115. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  116. {
  117. mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  118. goto exit;
  119. }
  120. if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
  121. ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
  122. ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
  123. ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
  124. ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
  125. ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
  126. ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
  127. ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
  128. {
  129. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  130. goto exit;
  131. }
  132. /*
  133. mbedtls_printf( " ok\n . Generating the certificate..." );
  134. x509write_init_raw( &cert );
  135. x509write_add_pubkey( &cert, &rsa );
  136. x509write_add_subject( &cert, "CN='localhost'" );
  137. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  138. "2010-09-06 17:00:32" );
  139. x509write_create_selfsign( &cert, &rsa );
  140. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  141. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  142. x509write_free_raw( &cert );
  143. */
  144. mbedtls_printf( " ok\n\n" );
  145. exit_code = MBEDTLS_EXIT_SUCCESS;
  146. exit:
  147. if( fpub != NULL )
  148. fclose( fpub );
  149. if( fpriv != NULL )
  150. fclose( fpriv );
  151. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  152. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  153. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  154. mbedtls_rsa_free( &rsa );
  155. mbedtls_ctr_drbg_free( &ctr_drbg );
  156. mbedtls_entropy_free( &entropy );
  157. #if defined(_WIN32)
  158. mbedtls_printf( " Press Enter to exit this program.\n" );
  159. fflush( stdout ); getchar();
  160. #endif
  161. return( exit_code );
  162. }
  163. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  164. MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */