2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Example RSA key generation program
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2013-09-20 11:30:43 +00:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
#if defined(POLARSSL_PLATFORM_C)
|
|
|
|
#include "polarssl/platform.h"
|
|
|
|
#else
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
2015-01-19 14:26:37 +00:00
|
|
|
#define polarssl_printf printf
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 14:09:44 +00:00
|
|
|
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
|
|
|
|
defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) && \
|
2015-02-11 14:06:19 +00:00
|
|
|
defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C)
|
2011-12-04 17:09:26 +00:00
|
|
|
#include "polarssl/entropy.h"
|
|
|
|
#include "polarssl/ctr_drbg.h"
|
2009-01-03 21:51:57 +00:00
|
|
|
#include "polarssl/bignum.h"
|
|
|
|
#include "polarssl/x509.h"
|
|
|
|
#include "polarssl/rsa.h"
|
2015-02-13 14:12:07 +00:00
|
|
|
|
2015-02-11 14:06:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#define KEY_SIZE 1024
|
|
|
|
#define EXPONENT 65537
|
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
2011-05-26 13:16:06 +00:00
|
|
|
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
|
2011-12-04 17:09:26 +00:00
|
|
|
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2011-05-26 13:16:06 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
2011-05-26 13:16:06 +00:00
|
|
|
"POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
|
2011-12-04 17:09:26 +00:00
|
|
|
"POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
|
2011-05-26 13:16:06 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
2015-02-12 11:37:29 +00:00
|
|
|
int main( void )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
rsa_context rsa;
|
2011-12-04 17:09:26 +00:00
|
|
|
entropy_context entropy;
|
|
|
|
ctr_drbg_context ctr_drbg;
|
2009-01-03 21:22:43 +00:00
|
|
|
FILE *fpub = NULL;
|
|
|
|
FILE *fpriv = NULL;
|
2013-06-24 11:01:08 +00:00
|
|
|
const char *pers = "rsa_genkey";
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( "\n . Seeding the random number generator..." );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
entropy_init( &entropy );
|
|
|
|
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
2013-06-24 11:01:08 +00:00
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen( pers ) ) ) != 0 )
|
2011-12-04 17:09:26 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
2011-12-04 17:09:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
2010-08-16 11:56:45 +00:00
|
|
|
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
2015-02-13 14:12:07 +00:00
|
|
|
|
2011-12-04 17:09:26 +00:00
|
|
|
if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
|
|
|
|
EXPONENT ) ) != 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
|
|
|
|
( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
|
|
|
|
{
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/*
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n . Generating the certificate..." );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
x509write_init_raw( &cert );
|
|
|
|
x509write_add_pubkey( &cert, &rsa );
|
|
|
|
x509write_add_subject( &cert, "CN='localhost'" );
|
|
|
|
x509write_add_validity( &cert, "2007-09-06 17:00:32",
|
|
|
|
"2010-09-06 17:00:32" );
|
|
|
|
x509write_create_selfsign( &cert, &rsa );
|
|
|
|
x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
|
|
|
|
x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
|
|
|
|
x509write_free_raw( &cert );
|
|
|
|
*/
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " ok\n\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if( fpub != NULL )
|
|
|
|
fclose( fpub );
|
|
|
|
|
|
|
|
if( fpriv != NULL )
|
|
|
|
fclose( fpriv );
|
|
|
|
|
|
|
|
rsa_free( &rsa );
|
2014-06-18 14:44:11 +00:00
|
|
|
ctr_drbg_free( &ctr_drbg );
|
2013-09-28 13:23:03 +00:00
|
|
|
entropy_free( &entropy );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-11-18 14:26:47 +00:00
|
|
|
#if defined(_WIN32)
|
2015-01-19 14:26:37 +00:00
|
|
|
polarssl_printf( " Press Enter to exit this program.\n" );
|
2009-01-03 21:22:43 +00:00
|
|
|
fflush( stdout ); getchar();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2011-12-04 17:09:26 +00:00
|
|
|
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
|
|
|
|
POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|