rsa_genkey.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Example RSA key generation program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_exit exit
  58. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  59. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  60. #endif /* MBEDTLS_PLATFORM_C */
  61. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
  62. defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
  63. defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
  64. #include "mbedtls/entropy.h"
  65. #include "mbedtls/ctr_drbg.h"
  66. #include "mbedtls/bignum.h"
  67. #include "mbedtls/x509.h"
  68. #include "mbedtls/rsa.h"
  69. #include <stdio.h>
  70. #include <string.h>
  71. #endif
  72. #define KEY_SIZE 2048
  73. #define EXPONENT 65537
  74. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  75. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
  76. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
  77. int main( void )
  78. {
  79. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  80. "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
  81. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
  82. mbedtls_exit( 0 );
  83. }
  84. #else
  85. int main( void )
  86. {
  87. int ret = 1;
  88. int exit_code = MBEDTLS_EXIT_FAILURE;
  89. mbedtls_rsa_context rsa;
  90. mbedtls_entropy_context entropy;
  91. mbedtls_ctr_drbg_context ctr_drbg;
  92. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  93. FILE *fpub = NULL;
  94. FILE *fpriv = NULL;
  95. const char *pers = "rsa_genkey";
  96. mbedtls_ctr_drbg_init( &ctr_drbg );
  97. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
  98. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  99. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  100. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  101. mbedtls_printf( "\n . Seeding the random number generator..." );
  102. fflush( stdout );
  103. mbedtls_entropy_init( &entropy );
  104. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  105. (const unsigned char *) pers,
  106. strlen( pers ) ) ) != 0 )
  107. {
  108. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  109. goto exit;
  110. }
  111. mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  112. fflush( stdout );
  113. if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
  114. EXPONENT ) ) != 0 )
  115. {
  116. mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
  117. goto exit;
  118. }
  119. mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  120. fflush( stdout );
  121. if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
  122. ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
  123. {
  124. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  125. goto exit;
  126. }
  127. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  128. {
  129. mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  130. goto exit;
  131. }
  132. if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
  133. ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
  134. {
  135. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  136. goto exit;
  137. }
  138. mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  139. fflush( stdout );
  140. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  141. {
  142. mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  143. goto exit;
  144. }
  145. if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
  146. ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
  147. ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
  148. ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
  149. ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
  150. ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
  151. ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
  152. ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
  153. {
  154. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  155. goto exit;
  156. }
  157. /*
  158. mbedtls_printf( " ok\n . Generating the certificate..." );
  159. x509write_init_raw( &cert );
  160. x509write_add_pubkey( &cert, &rsa );
  161. x509write_add_subject( &cert, "CN='localhost'" );
  162. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  163. "2010-09-06 17:00:32" );
  164. x509write_create_selfsign( &cert, &rsa );
  165. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  166. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  167. x509write_free_raw( &cert );
  168. */
  169. mbedtls_printf( " ok\n\n" );
  170. exit_code = MBEDTLS_EXIT_SUCCESS;
  171. exit:
  172. if( fpub != NULL )
  173. fclose( fpub );
  174. if( fpriv != NULL )
  175. fclose( fpriv );
  176. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  177. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  178. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  179. mbedtls_rsa_free( &rsa );
  180. mbedtls_ctr_drbg_free( &ctr_drbg );
  181. mbedtls_entropy_free( &entropy );
  182. #if defined(_WIN32)
  183. mbedtls_printf( " Press Enter to exit this program.\n" );
  184. fflush( stdout ); getchar();
  185. #endif
  186. mbedtls_exit( exit_code );
  187. }
  188. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
  189. MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */