rsa_genkey.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Example RSA key generation program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #ifdef PRINTF_STDLIB
  29. #include <stdio.h>
  30. #endif
  31. #ifdef PRINTF_CUSTOM
  32. #include "tinystdio.h"
  33. #endif
  34. #include "polarssl/config.h"
  35. #include "polarssl/havege.h"
  36. #include "polarssl/bignum.h"
  37. #include "polarssl/x509.h"
  38. #include "polarssl/rsa.h"
  39. #define KEY_SIZE 1024
  40. #define EXPONENT 65537
  41. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  42. !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
  43. !defined(POLARSSL_FS_IO)
  44. int main( void )
  45. {
  46. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  47. "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
  48. "POLARSSL_FS_IO not defined.\n");
  49. return( 0 );
  50. }
  51. #else
  52. int main( void )
  53. {
  54. int ret;
  55. rsa_context rsa;
  56. havege_state hs;
  57. FILE *fpub = NULL;
  58. FILE *fpriv = NULL;
  59. printf( "\n . Seeding the random number generator..." );
  60. fflush( stdout );
  61. havege_init( &hs );
  62. printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  63. fflush( stdout );
  64. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  65. if( ( ret = rsa_gen_key( &rsa, havege_rand, &hs, KEY_SIZE, EXPONENT ) ) != 0 )
  66. {
  67. printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
  68. goto exit;
  69. }
  70. printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  71. fflush( stdout );
  72. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  73. {
  74. printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  75. ret = 1;
  76. goto exit;
  77. }
  78. if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
  79. ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
  80. {
  81. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  82. goto exit;
  83. }
  84. printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  85. fflush( stdout );
  86. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  87. {
  88. printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  89. ret = 1;
  90. goto exit;
  91. }
  92. if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
  93. ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
  94. ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
  95. ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
  96. ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
  97. ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
  98. ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
  99. ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
  100. {
  101. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  102. goto exit;
  103. }
  104. /*
  105. printf( " ok\n . Generating the certificate..." );
  106. x509write_init_raw( &cert );
  107. x509write_add_pubkey( &cert, &rsa );
  108. x509write_add_subject( &cert, "CN='localhost'" );
  109. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  110. "2010-09-06 17:00:32" );
  111. x509write_create_selfsign( &cert, &rsa );
  112. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  113. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  114. x509write_free_raw( &cert );
  115. */
  116. printf( " ok\n\n" );
  117. exit:
  118. if( fpub != NULL )
  119. fclose( fpub );
  120. if( fpriv != NULL )
  121. fclose( fpriv );
  122. rsa_free( &rsa );
  123. #ifdef WIN32
  124. printf( " Press Enter to exit this program.\n" );
  125. fflush( stdout ); getchar();
  126. #endif
  127. return( ret );
  128. }
  129. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C &&
  130. POLARSSL_GENPRIME && POLARSSL_FS_IO */