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