blob: 7acda81394265eab8e36cb55769ffcff3afba569 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
25 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
26 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/entropy.h"
28#include "mbedtls/ctr_drbg.h"
29#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000031
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
33#include <string.h>
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020036#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000037#define EXPONENT 65537
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
40 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
41 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000042int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
45 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
46 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020047 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000048}
49#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000050
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Rich Evans85b05ec2015-02-12 11:37:29 +000052int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000053{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010054 int ret = 1;
55 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_rsa_context rsa;
57 mbedtls_entropy_context entropy;
58 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010059 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 FILE *fpub = NULL;
61 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020062 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020064 mbedtls_ctr_drbg_init( &ctr_drbg );
Ronald Cronc1905a12021-06-05 11:11:14 +020065 mbedtls_rsa_init( &rsa );
Hanno Beckerd4d60572018-01-10 07:12:01 +000066 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
67 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
68 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000071 fflush( stdout );
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020074 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 (const unsigned char *) pers,
76 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000077 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020078 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000079 goto exit;
80 }
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000083 fflush( stdout );
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Hanno Beckerf073de02017-08-23 07:42:28 +010086 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000089 goto exit;
90 }
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +000093 fflush( stdout );
94
Hanno Beckerf073de02017-08-23 07:42:28 +010095 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
96 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
97 {
98 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Hanno Beckerf073de02017-08-23 07:42:28 +010099 goto exit;
100 }
101
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 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 goto exit;
106 }
107
Hanno Beckerf073de02017-08-23 07:42:28 +0100108 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
109 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 goto exit;
113 }
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 goto exit;
122 }
123
Hanno Beckerf073de02017-08-23 07:42:28 +0100124 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
125 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
126 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
127 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
128 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
129 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
130 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
131 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100138 exit_code = MBEDTLS_EXIT_SUCCESS;
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140exit:
141
142 if( fpub != NULL )
143 fclose( fpub );
144
145 if( fpriv != NULL )
146 fclose( fpriv );
147
Hanno Beckerf073de02017-08-23 07:42:28 +0100148 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
149 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
150 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_rsa_free( &rsa );
152 mbedtls_ctr_drbg_free( &ctr_drbg );
153 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200155 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
158 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */