blob: f691871eafc72baf25d888530630a28953d39553 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
37 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
38 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/entropy.h"
40#include "mbedtls/ctr_drbg.h"
41#include "mbedtls/bignum.h"
42#include "mbedtls/x509.h"
43#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <string.h>
47#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020049#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000050#define EXPONENT 65537
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
53 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
54 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000055int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
58 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
59 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000060 return( 0 );
61}
62#else
Rich Evans85b05ec2015-02-12 11:37:29 +000063int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
65 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_rsa_context rsa;
67 mbedtls_entropy_context entropy;
68 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000069 FILE *fpub = NULL;
70 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020073 mbedtls_ctr_drbg_init( &ctr_drbg );
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000076 fflush( stdout );
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020079 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020080 (const unsigned char *) pers,
81 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000082 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020083 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000084 goto exit;
85 }
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000088 fflush( stdout );
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000096 goto exit;
97 }
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 fflush( stdout );
101
102 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 ret = 1;
106 goto exit;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
110 ( ret = mbedtls_mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 goto exit;
114 }
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 fflush( stdout );
118
119 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 ret = 1;
123 goto exit;
124 }
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
127 ( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
128 ( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
129 ( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
130 ( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
131 ( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
132 ( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
133 ( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 goto exit;
137 }
138/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 x509write_init_raw( &cert );
142 x509write_add_pubkey( &cert, &rsa );
143 x509write_add_subject( &cert, "CN='localhost'" );
144 x509write_add_validity( &cert, "2007-09-06 17:00:32",
145 "2010-09-06 17:00:32" );
146 x509write_create_selfsign( &cert, &rsa );
147 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
148 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
149 x509write_free_raw( &cert );
150*/
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153exit:
154
155 if( fpub != NULL )
156 fclose( fpub );
157
158 if( fpriv != NULL )
159 fclose( fpriv );
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_rsa_free( &rsa );
162 mbedtls_ctr_drbg_free( &ctr_drbg );
163 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakkercce9d772011-11-18 14:26:47 +0000165#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 fflush( stdout ); getchar();
168#endif
169
170 return( ret );
171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
173 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */