blob: a036930fcfe0c48258ad0ad8952480f2df666d12 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
67 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
68 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/entropy.h"
70#include "mbedtls/ctr_drbg.h"
71#include "mbedtls/bignum.h"
72#include "mbedtls/x509.h"
73#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000074
Rich Evans18b78c72015-02-11 14:06:19 +000075#include <stdio.h>
76#include <string.h>
77#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020079#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000080#define EXPONENT 65537
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
83 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
84 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000085int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
88 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
89 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020090 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000091}
92#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000093
Simon Butcher63cb97e2018-12-06 17:43:31 +000094
Rich Evans85b05ec2015-02-12 11:37:29 +000095int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000096{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010097 int ret = 1;
98 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_rsa_context rsa;
100 mbedtls_entropy_context entropy;
101 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +0100102 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 FILE *fpub = NULL;
104 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200105 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200107 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerd4d60572018-01-10 07:12:01 +0000108 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
109 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
110 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
111 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 fflush( stdout );
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200117 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200118 (const unsigned char *) pers,
119 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000120 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200121 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000122 goto exit;
123 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 fflush( stdout );
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Hanno Beckerf073de02017-08-23 07:42:28 +0100129 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 fflush( stdout );
137
Hanno Beckerf073de02017-08-23 07:42:28 +0100138 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
139 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
140 {
141 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Hanno Beckerf073de02017-08-23 07:42:28 +0100142 goto exit;
143 }
144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
Hanno Beckerf073de02017-08-23 07:42:28 +0100151 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
152 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 goto exit;
156 }
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 fflush( stdout );
160
161 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 goto exit;
165 }
166
Hanno Beckerf073de02017-08-23 07:42:28 +0100167 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
168 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
169 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
170 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
171 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
172 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
173 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
174 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 goto exit;
178 }
179/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182 x509write_init_raw( &cert );
183 x509write_add_pubkey( &cert, &rsa );
184 x509write_add_subject( &cert, "CN='localhost'" );
185 x509write_add_validity( &cert, "2007-09-06 17:00:32",
186 "2010-09-06 17:00:32" );
187 x509write_create_selfsign( &cert, &rsa );
188 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
189 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
190 x509write_free_raw( &cert );
191*/
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100194 exit_code = MBEDTLS_EXIT_SUCCESS;
195
Paul Bakker5121ce52009-01-03 21:22:43 +0000196exit:
197
198 if( fpub != NULL )
199 fclose( fpub );
200
201 if( fpriv != NULL )
202 fclose( fpriv );
203
Hanno Beckerf073de02017-08-23 07:42:28 +0100204 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
205 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
206 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_rsa_free( &rsa );
208 mbedtls_ctr_drbg_free( &ctr_drbg );
209 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakkercce9d772011-11-18 14:26:47 +0000211#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 fflush( stdout ); getchar();
214#endif
215
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200216 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
219 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */