| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Example RSA key generation program | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| 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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \ | 
|  | 29 | defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \ | 
|  | 30 | defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/entropy.h" | 
|  | 32 | #include "mbedtls/ctr_drbg.h" | 
|  | 33 | #include "mbedtls/bignum.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/rsa.h" | 
| Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 35 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 36 | #include <stdio.h> | 
|  | 37 | #include <string.h> | 
|  | 38 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 |  | 
| Manuel Pégourié-Gonnard | d224ff1 | 2015-08-27 21:42:49 +0200 | [diff] [blame] | 40 | #define KEY_SIZE 2048 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | #define EXPONENT 65537 | 
|  | 42 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \ | 
|  | 44 | !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) ||      \ | 
|  | 45 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 46 | int main(void) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 47 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 49 | "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or " | 
|  | 50 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n"); | 
|  | 51 | mbedtls_exit(0); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 52 | } | 
|  | 53 | #else | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 54 |  | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 55 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | int main(void) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | { | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 58 | int ret = 1; | 
|  | 59 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | mbedtls_rsa_context rsa; | 
|  | 61 | mbedtls_entropy_context entropy; | 
|  | 62 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 63 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | FILE *fpub  = NULL; | 
|  | 65 | FILE *fpriv = NULL; | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 66 | const char *pers = "rsa_genkey"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | mbedtls_ctr_drbg_init(&ctr_drbg); | 
|  | 69 | mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0); | 
|  | 70 | mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); | 
|  | 71 | mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP); | 
|  | 72 | mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP); | 
| Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 73 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | mbedtls_printf("\n  . Seeding the random number generator..."); | 
|  | 75 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | mbedtls_entropy_init(&entropy); | 
|  | 78 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, | 
|  | 79 | (const unsigned char *) pers, | 
|  | 80 | strlen(pers))) != 0) { | 
|  | 81 | mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 82 | goto exit; | 
|  | 83 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | mbedtls_printf(" ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE); | 
|  | 86 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE, | 
|  | 89 | EXPONENT)) != 0) { | 
|  | 90 | 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] | 91 | goto exit; | 
|  | 92 | } | 
|  | 93 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | mbedtls_printf(" ok\n  . Exporting the public  key in rsa_pub.txt...."); | 
|  | 95 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 || | 
|  | 98 | (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP))      != 0) { | 
|  | 99 | mbedtls_printf(" failed\n  ! could not export RSA parameters\n\n"); | 
| Hanno Becker | f073de0 | 2017-08-23 07:42:28 +0100 | [diff] [blame] | 100 | goto exit; | 
|  | 101 | } | 
|  | 102 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) { | 
|  | 104 | 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] | 105 | goto exit; | 
|  | 106 | } | 
|  | 107 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 || | 
|  | 109 | (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) { | 
|  | 110 | 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] | 111 | goto exit; | 
|  | 112 | } | 
|  | 113 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | mbedtls_printf(" ok\n  . Exporting the private key in rsa_priv.txt..."); | 
|  | 115 | fflush(stdout); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 117 | if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) { | 
|  | 118 | 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] | 119 | goto exit; | 
|  | 120 | } | 
|  | 121 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 || | 
|  | 123 | (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 || | 
|  | 124 | (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 || | 
|  | 125 | (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 || | 
|  | 126 | (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 || | 
|  | 127 | (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 || | 
|  | 128 | (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 || | 
|  | 129 | (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) { | 
|  | 130 | 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] | 131 | goto exit; | 
|  | 132 | } | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | mbedtls_printf(" ok\n\n"); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 |  | 
| Andres Amaya Garcia | 70e1ffd | 2018-04-29 20:12:43 +0100 | [diff] [blame] | 135 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 136 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | exit: | 
|  | 138 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | if (fpub  != NULL) { | 
|  | 140 | fclose(fpub); | 
|  | 141 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | if (fpriv != NULL) { | 
|  | 144 | fclose(fpriv); | 
|  | 145 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 147 | mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); | 
|  | 148 | mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP); | 
|  | 149 | mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP); | 
|  | 150 | mbedtls_rsa_free(&rsa); | 
|  | 151 | mbedtls_ctr_drbg_free(&ctr_drbg); | 
|  | 152 | mbedtls_entropy_free(&entropy); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 154 | #if defined(_WIN32) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | mbedtls_printf("  Press Enter to exit this program.\n"); | 
|  | 156 | fflush(stdout); getchar(); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | #endif | 
|  | 158 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | mbedtls_exit(exit_code); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C && | 
|  | 162 | MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ |