| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Example RSA key generation program | 
|  | 3 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 32 | #include <stdlib.h> | 
|  | 33 | #define mbedtls_printf          printf | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 34 | #define mbedtls_exit            exit | 
| Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 36 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
|  | 37 | #endif /* MBEDTLS_PLATFORM_C */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #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é-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/entropy.h" | 
|  | 43 | #include "mbedtls/ctr_drbg.h" | 
|  | 44 | #include "mbedtls/bignum.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 45 | #include "mbedtls/rsa.h" | 
| Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 46 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 47 | #include <stdio.h> | 
|  | 48 | #include <string.h> | 
|  | 49 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 |  | 
| Manuel Pégourié-Gonnard | d224ff1 | 2015-08-27 21:42:49 +0200 | [diff] [blame] | 51 | #define KEY_SIZE 2048 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | #define EXPONENT 65537 | 
|  | 53 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \ | 
|  | 55 | !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) ||      \ | 
|  | 56 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 57 | int main( void ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 58 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " | 
|  | 60 | "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or " | 
|  | 61 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n"); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 62 | return( 0 ); | 
|  | 63 | } | 
|  | 64 | #else | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 65 |  | 
|  | 66 | #if defined(MBEDTLS_CHECK_PARAMS) | 
|  | 67 | #include "mbedtls/platform_util.h" | 
|  | 68 | void mbedtls_param_failed( const char *failure_condition, | 
|  | 69 | const char *file, | 
|  | 70 | int line ) | 
|  | 71 | { | 
|  | 72 | mbedtls_printf( "%s:%i: Input param failed - %s\n", | 
|  | 73 | file, line, failure_condition ); | 
|  | 74 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); | 
|  | 75 | } | 
|  | 76 | #endif | 
|  | 77 |  | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 78 | int main( void ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | { | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 80 | int ret = 1; | 
|  | 81 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | mbedtls_rsa_context rsa; | 
|  | 83 | mbedtls_entropy_context entropy; | 
|  | 84 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 85 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | FILE *fpub  = NULL; | 
|  | 87 | FILE *fpriv = NULL; | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 88 | const char *pers = "rsa_genkey"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 |  | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 90 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 
| Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 91 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); | 
|  | 92 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); | 
|  | 93 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); | 
|  | 94 | mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 95 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | mbedtls_printf( "\n  . Seeding the random number generator..." ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | fflush( stdout ); | 
|  | 98 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_entropy_init( &entropy ); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 100 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 101 | (const unsigned char *) pers, | 
|  | 102 | strlen( pers ) ) ) != 0 ) | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 103 | { | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 104 | mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret ); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 105 | goto exit; | 
|  | 106 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | mbedtls_printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | fflush( stdout ); | 
|  | 110 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE, | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 112 | EXPONENT ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | mbedtls_printf( " failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | goto exit; | 
|  | 116 | } | 
|  | 117 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_printf( " ok\n  . Exporting the public  key in rsa_pub.txt...." ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | fflush( stdout ); | 
|  | 120 |  | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 121 | if( ( ret = mbedtls_rsa_export    ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 || | 
|  | 122 | ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) )      != 0 ) | 
|  | 123 | { | 
|  | 124 | mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" ); | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 125 | goto exit; | 
|  | 126 | } | 
|  | 127 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL ) | 
|  | 129 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_printf( " failed\n  ! could not open rsa_pub.txt for writing\n\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | goto exit; | 
|  | 132 | } | 
|  | 133 |  | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 134 | if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 || | 
|  | 135 | ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | goto exit; | 
|  | 139 | } | 
|  | 140 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | mbedtls_printf( " ok\n  . Exporting the private key in rsa_priv.txt..." ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | fflush( stdout ); | 
|  | 143 |  | 
|  | 144 | if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL ) | 
|  | 145 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | mbedtls_printf( " failed\n  ! could not open rsa_priv.txt for writing\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | goto exit; | 
|  | 148 | } | 
|  | 149 |  | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 150 | if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 || | 
|  | 151 | ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 || | 
|  | 152 | ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 || | 
|  | 153 | ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 || | 
|  | 154 | ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 || | 
|  | 155 | ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 || | 
|  | 156 | ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 || | 
|  | 157 | ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | goto exit; | 
|  | 161 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_printf( " ok\n\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 |  | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 164 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 165 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | exit: | 
|  | 167 |  | 
|  | 168 | if( fpub  != NULL ) | 
|  | 169 | fclose( fpub ); | 
|  | 170 |  | 
|  | 171 | if( fpriv != NULL ) | 
|  | 172 | fclose( fpriv ); | 
|  | 173 |  | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 174 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); | 
|  | 175 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP ); | 
|  | 176 | mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | mbedtls_rsa_free( &rsa ); | 
|  | 178 | mbedtls_ctr_drbg_free( &ctr_drbg ); | 
|  | 179 | mbedtls_entropy_free( &entropy ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 181 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | mbedtls_printf( "  Press Enter to exit this program.\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | fflush( stdout ); getchar(); | 
|  | 184 | #endif | 
|  | 185 |  | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 186 | return( exit_code ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C && | 
|  | 189 | MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ |