blob: 56d32737227b2cdd7b7b5977750270556af8e446 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker5121ce52009-01-03 21:22:43 +000035#include <stdio.h>
Manuel Pégourié-Gonnard1cc0a342015-02-10 12:18:15 +000036#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker508ad5a2011-12-04 17:09:26 +000038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/bignum.h"
41#include "polarssl/x509.h"
42#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define KEY_SIZE 1024
45#define EXPONENT 65537
46
Paul Bakker508ad5a2011-12-04 17:09:26 +000047#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000048 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000050int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000051{
Paul Bakkercce9d772011-11-18 14:26:47 +000052 ((void) argc);
53 ((void) argv);
54
Rich Evansf90016a2015-01-19 14:26:37 +000055 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000056 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000057 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000058 return( 0 );
59}
60#else
Paul Bakkercce9d772011-11-18 14:26:47 +000061int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000062{
63 int ret;
64 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 entropy_context entropy;
66 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000067 FILE *fpub = NULL;
68 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020069 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkercce9d772011-11-18 14:26:47 +000071 ((void) argc);
72 ((void) argv);
73
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000075 fflush( stdout );
76
Paul Bakker508ad5a2011-12-04 17:09:26 +000077 entropy_init( &entropy );
78 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 (const unsigned char *) pers,
80 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000081 {
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000083 goto exit;
84 }
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Rich Evansf90016a2015-01-19 14:26:37 +000086 polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000087 fflush( stdout );
88
Paul Bakkera802e1a2010-08-16 11:56:45 +000089 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
92 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000093 {
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000095 goto exit;
96 }
97
Rich Evansf90016a2015-01-19 14:26:37 +000098 polarssl_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +000099 fflush( stdout );
100
101 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
102 {
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 ret = 1;
105 goto exit;
106 }
107
108 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
109 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
110 {
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 goto exit;
113 }
114
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 fflush( stdout );
117
118 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
119 {
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 ret = 1;
122 goto exit;
123 }
124
125 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
126 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
127 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
128 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
129 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
130 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
131 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
132 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
133 {
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137/*
Rich Evansf90016a2015-01-19 14:26:37 +0000138 polarssl_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 x509write_init_raw( &cert );
141 x509write_add_pubkey( &cert, &rsa );
142 x509write_add_subject( &cert, "CN='localhost'" );
143 x509write_add_validity( &cert, "2007-09-06 17:00:32",
144 "2010-09-06 17:00:32" );
145 x509write_create_selfsign( &cert, &rsa );
146 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
147 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
148 x509write_free_raw( &cert );
149*/
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152exit:
153
154 if( fpub != NULL )
155 fclose( fpub );
156
157 if( fpriv != NULL )
158 fclose( fpriv );
159
160 rsa_free( &rsa );
Paul Bakkera317a982014-06-18 16:44:11 +0200161 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200162 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakkercce9d772011-11-18 14:26:47 +0000164#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 fflush( stdout ); getchar();
167#endif
168
169 return( ret );
170}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000171#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
172 POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */