blob: b1bd86b0a5cec5da8b9bfa5b36dbf0d70f8bd23b [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
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
44#include "mbedtls/bignum.h"
45#include "mbedtls/x509.h"
46#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000047
Rich Evans18b78c72015-02-11 14:06:19 +000048#include <stdio.h>
49#include <string.h>
50#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020052#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000053#define EXPONENT 65537
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
56 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
57 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000058int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
61 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
62 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040063 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000064}
65#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000066
Jarno Lamsa642596e2019-10-04 12:52:42 +030067#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
68int mbedtls_hardware_poll( void *data, unsigned char *output,
69 size_t len, size_t *olen )
70{
71 size_t i;
72 (void) data;
73 for( i = 0; i < len; ++i )
74 output[i] = rand();
75 *olen = len;
76 return( 0 );
77}
78#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +000079
Rich Evans85b05ec2015-02-12 11:37:29 +000080int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000081{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010082 int ret = 1;
83 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_rsa_context rsa;
85 mbedtls_entropy_context entropy;
86 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010087 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000088 FILE *fpub = NULL;
89 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020090 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020092 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerd4d60572018-01-10 07:12:01 +000093 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
94 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
95 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
96 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000099 fflush( stdout );
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200102 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200103 (const unsigned char *) pers,
104 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000105 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200106 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000107 goto exit;
108 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 fflush( stdout );
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Hanno Beckerf073de02017-08-23 07:42:28 +0100114 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 goto exit;
118 }
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 fflush( stdout );
122
Hanno Beckerf073de02017-08-23 07:42:28 +0100123 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
124 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
125 {
126 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
Hanno Beckerf073de02017-08-23 07:42:28 +0100127 goto exit;
128 }
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 goto exit;
134 }
135
Hanno Beckerf073de02017-08-23 07:42:28 +0100136 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
137 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 fflush( stdout );
145
146 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 goto exit;
150 }
151
Hanno Beckerf073de02017-08-23 07:42:28 +0100152 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
153 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
154 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
155 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
156 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
157 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
158 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
159 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 goto exit;
163 }
164/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167 x509write_init_raw( &cert );
168 x509write_add_pubkey( &cert, &rsa );
169 x509write_add_subject( &cert, "CN='localhost'" );
170 x509write_add_validity( &cert, "2007-09-06 17:00:32",
171 "2010-09-06 17:00:32" );
172 x509write_create_selfsign( &cert, &rsa );
173 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
174 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
175 x509write_free_raw( &cert );
176*/
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100179 exit_code = MBEDTLS_EXIT_SUCCESS;
180
Paul Bakker5121ce52009-01-03 21:22:43 +0000181exit:
182
183 if( fpub != NULL )
184 fclose( fpub );
185
186 if( fpriv != NULL )
187 fclose( fpriv );
188
Hanno Beckerf073de02017-08-23 07:42:28 +0100189 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
190 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
191 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_rsa_free( &rsa );
193 mbedtls_ctr_drbg_free( &ctr_drbg );
194 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Paul Bakkercce9d772011-11-18 14:26:47 +0000196#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 fflush( stdout ); getchar();
199#endif
200
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400201 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
204 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */