rsa_genkey.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include <stdio.h>
  29. #include "polarssl/config.h"
  30. #include "polarssl/havege.h"
  31. #include "polarssl/bignum.h"
  32. #include "polarssl/x509.h"
  33. #include "polarssl/rsa.h"
  34. #define KEY_SIZE 1024
  35. #define EXPONENT 65537
  36. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  37. !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
  38. !defined(POLARSSL_FS_IO)
  39. int main( void )
  40. {
  41. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  42. "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
  43. "POLARSSL_FS_IO not defined.\n");
  44. return( 0 );
  45. }
  46. #else
  47. int main( void )
  48. {
  49. int ret;
  50. rsa_context rsa;
  51. havege_state hs;
  52. FILE *fpub = NULL;
  53. FILE *fpriv = NULL;
  54. printf( "\n . Seeding the random number generator..." );
  55. fflush( stdout );
  56. havege_init( &hs );
  57. printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
  58. fflush( stdout );
  59. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  60. if( ( ret = rsa_gen_key( &rsa, havege_rand, &hs, KEY_SIZE, EXPONENT ) ) != 0 )
  61. {
  62. printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
  63. goto exit;
  64. }
  65. printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
  66. fflush( stdout );
  67. if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
  68. {
  69. printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
  70. ret = 1;
  71. goto exit;
  72. }
  73. if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
  74. ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
  75. {
  76. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  77. goto exit;
  78. }
  79. printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
  80. fflush( stdout );
  81. if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
  82. {
  83. printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
  84. ret = 1;
  85. goto exit;
  86. }
  87. if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
  88. ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
  89. ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
  90. ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
  91. ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
  92. ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
  93. ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
  94. ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
  95. {
  96. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  97. goto exit;
  98. }
  99. /*
  100. printf( " ok\n . Generating the certificate..." );
  101. x509write_init_raw( &cert );
  102. x509write_add_pubkey( &cert, &rsa );
  103. x509write_add_subject( &cert, "CN='localhost'" );
  104. x509write_add_validity( &cert, "2007-09-06 17:00:32",
  105. "2010-09-06 17:00:32" );
  106. x509write_create_selfsign( &cert, &rsa );
  107. x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
  108. x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
  109. x509write_free_raw( &cert );
  110. */
  111. printf( " ok\n\n" );
  112. exit:
  113. if( fpub != NULL )
  114. fclose( fpub );
  115. if( fpriv != NULL )
  116. fclose( fpriv );
  117. rsa_free( &rsa );
  118. #ifdef WIN32
  119. printf( " Press Enter to exit this program.\n" );
  120. fflush( stdout ); getchar();
  121. #endif
  122. return( ret );
  123. }
  124. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C &&
  125. POLARSSL_GENPRIME && POLARSSL_FS_IO */