blob: 26a8925044f385622103628e9cabcf5cd9040a55 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000028#else
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
38 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
39 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/entropy.h"
41#include "mbedtls/ctr_drbg.h"
42#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#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");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020060 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000061}
62#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000063
Simon Butcher63cb97e2018-12-06 17:43:31 +000064
Rich Evans85b05ec2015-02-12 11:37:29 +000065int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010067 int ret = 1;
68 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_rsa_context rsa;
70 mbedtls_entropy_context entropy;
71 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010072 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000073 FILE *fpub = NULL;
74 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020077 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerd4d60572018-01-10 07:12:01 +000078 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
79 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
80 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
81 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 fflush( stdout );
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020087 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020088 (const unsigned char *) pers,
89 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000090 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020091 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000092 goto exit;
93 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000096 fflush( stdout );
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Hanno Beckerf073de02017-08-23 07:42:28 +010099 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 goto exit;
103 }
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 fflush( stdout );
107
Hanno Beckerf073de02017-08-23 07:42:28 +0100108 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
109 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
110 {
111 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Hanno Beckerf073de02017-08-23 07:42:28 +0100112 goto exit;
113 }
114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 goto exit;
119 }
120
Hanno Beckerf073de02017-08-23 07:42:28 +0100121 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
122 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 goto exit;
126 }
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 fflush( stdout );
130
131 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
136
Hanno Beckerf073de02017-08-23 07:42:28 +0100137 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
138 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
139 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
140 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
141 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
142 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
143 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
144 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100151 exit_code = MBEDTLS_EXIT_SUCCESS;
152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153exit:
154
155 if( fpub != NULL )
156 fclose( fpub );
157
158 if( fpriv != NULL )
159 fclose( fpriv );
160
Hanno Beckerf073de02017-08-23 07:42:28 +0100161 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
162 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
163 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_rsa_free( &rsa );
165 mbedtls_ctr_drbg_free( &ctr_drbg );
166 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakkercce9d772011-11-18 14:26:47 +0000168#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 fflush( stdout ); getchar();
171#endif
172
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200173 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
176 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */