rsa_genkey.c 5.8 KB

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