blob: e199ad247da563763703687eccb166331d8a393f [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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
36 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
37 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/entropy.h"
39#include "mbedtls/ctr_drbg.h"
40#include "mbedtls/bignum.h"
41#include "mbedtls/x509.h"
42#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000043
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <stdio.h>
45#include <string.h>
46#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020048#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000049#define EXPONENT 65537
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
52 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
53 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000054int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
57 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
58 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000059 return( 0 );
60}
61#else
Rich Evans85b05ec2015-02-12 11:37:29 +000062int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
64 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_rsa_context rsa;
66 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000068 FILE *fpub = NULL;
69 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020070 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020072 mbedtls_ctr_drbg_init( &ctr_drbg );
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000075 fflush( stdout );
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020078 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 (const unsigned char *) pers,
80 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000081 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020082 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000083 goto exit;
84 }
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000087 fflush( stdout );
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
Paul Bakker508ad5a2011-12-04 17:09:26 +000092 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000095 goto exit;
96 }
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +000099 fflush( stdout );
100
101 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 ret = 1;
105 goto exit;
106 }
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
109 ( ret = mbedtls_mpi_write_file( "E = ", &rsa.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 ret = 1;
122 goto exit;
123 }
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
126 ( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
127 ( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
128 ( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
129 ( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
130 ( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
131 ( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
132 ( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 x509write_init_raw( &cert );
141 x509write_add_pubkey( &cert, &rsa );
142 x509write_add_subject( &cert, "CN='localhost'" );
143 x509write_add_validity( &cert, "2007-09-06 17:00:32",
144 "2010-09-06 17:00:32" );
145 x509write_create_selfsign( &cert, &rsa );
146 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
147 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
148 x509write_free_raw( &cert );
149*/
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152exit:
153
154 if( fpub != NULL )
155 fclose( fpub );
156
157 if( fpriv != NULL )
158 fclose( fpriv );
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_rsa_free( &rsa );
161 mbedtls_ctr_drbg_free( &ctr_drbg );
162 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakkercce9d772011-11-18 14:26:47 +0000164#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 fflush( stdout ); getchar();
167#endif
168
169 return( ret );
170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
172 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */