| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  RSA simple data encryption 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 | 7bc05ff | 2011-08-09 10:30:36 +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 | 7bc05ff | 2011-08-09 10:30:36 +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_RSA_C) && \ | 
|  | 13 | defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \ | 
|  | 14 | defined(MBEDTLS_CTR_DRBG_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 15 | #include "mbedtls/rsa.h" | 
|  | 16 | #include "mbedtls/entropy.h" | 
|  | 17 | #include "mbedtls/ctr_drbg.h" | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 18 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 19 | #include <string.h> | 
|  | 20 | #endif | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \ | 
|  | 23 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \ | 
|  | 24 | !defined(MBEDTLS_CTR_DRBG_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | int main(void) | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 26 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or " | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 28 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or " | 
|  | 29 | "MBEDTLS_CTR_DRBG_C not defined.\n"); | 
|  | 30 | mbedtls_exit(0); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 31 | } | 
|  | 32 | #else | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 33 |  | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 34 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | int main(int argc, char *argv[]) | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 36 | { | 
|  | 37 | FILE *f; | 
| Andres Amaya Garcia | 25b5af5 | 2018-04-30 22:08:36 +0100 | [diff] [blame] | 38 | int ret = 1; | 
|  | 39 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 40 | size_t i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | mbedtls_rsa_context rsa; | 
|  | 42 | mbedtls_entropy_context entropy; | 
|  | 43 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 44 | unsigned char input[1024]; | 
|  | 45 | unsigned char buf[512]; | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 46 | const char *pers = "rsa_encrypt"; | 
| Hanno Becker | 0c26393 | 2017-08-23 06:47:06 +0100 | [diff] [blame] | 47 | mbedtls_mpi N, E; | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 48 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | if (argc != 2) { | 
|  | 50 | mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 51 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 52 | #if defined(_WIN32) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | mbedtls_printf("\n"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 54 | #endif | 
|  | 55 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | mbedtls_exit(exit_code); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | mbedtls_printf("\n  . Seeding the random number generator..."); | 
|  | 60 | fflush(stdout); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 61 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | mbedtls_mpi_init(&N); mbedtls_mpi_init(&E); | 
|  | 63 | mbedtls_rsa_init(&rsa); | 
|  | 64 | mbedtls_ctr_drbg_init(&ctr_drbg); | 
|  | 65 | mbedtls_entropy_init(&entropy); | 
| Simon Butcher | 6b46c62 | 2016-04-12 13:25:08 +0100 | [diff] [blame] | 66 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, | 
|  | 68 | &entropy, (const unsigned char *) pers, | 
|  | 69 | strlen(pers)); | 
|  | 70 | if (ret != 0) { | 
|  | 71 | mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", | 
|  | 72 | ret); | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 73 | goto exit; | 
|  | 74 | } | 
|  | 75 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | mbedtls_printf("\n  . Reading public key from rsa_pub.txt"); | 
|  | 77 | fflush(stdout); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 78 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | if ((f = fopen("rsa_pub.txt", "rb")) == NULL) { | 
|  | 80 | mbedtls_printf(" failed\n  ! Could not open rsa_pub.txt\n" \ | 
|  | 81 | "  ! Please run rsa_genkey first\n\n"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 82 | goto exit; | 
|  | 83 | } | 
|  | 84 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 || | 
|  | 86 | (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0) { | 
|  | 87 | mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n", | 
|  | 88 | ret); | 
|  | 89 | fclose(f); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 90 | goto exit; | 
|  | 91 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | fclose(f); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 93 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | if ((ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E)) != 0) { | 
|  | 95 | mbedtls_printf(" failed\n  ! mbedtls_rsa_import returned %d\n\n", | 
|  | 96 | ret); | 
| Hanno Becker | 0c26393 | 2017-08-23 06:47:06 +0100 | [diff] [blame] | 97 | goto exit; | 
|  | 98 | } | 
|  | 99 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | if (strlen(argv[1]) > 100) { | 
|  | 101 | mbedtls_printf(" Input data larger than 100 characters.\n\n"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 102 | goto exit; | 
|  | 103 | } | 
|  | 104 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | memcpy(input, argv[1], strlen(argv[1])); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 106 |  | 
|  | 107 | /* | 
|  | 108 | * Calculate the RSA encryption of the hash. | 
|  | 109 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | mbedtls_printf("\n  . Generating the RSA encrypted value"); | 
|  | 111 | fflush(stdout); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 112 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random, | 
|  | 114 | &ctr_drbg, strlen(argv[1]), input, buf); | 
|  | 115 | if (ret != 0) { | 
|  | 116 | mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n", | 
|  | 117 | ret); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 118 | goto exit; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | /* | 
|  | 122 | * Write the signature into result-enc.txt | 
|  | 123 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | if ((f = fopen("result-enc.txt", "wb+")) == NULL) { | 
|  | 125 | mbedtls_printf(" failed\n  ! Could not create %s\n\n", "result-enc.txt"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 126 | goto exit; | 
|  | 127 | } | 
|  | 128 |  | 
| Minos Galanakis | ee757d3 | 2024-01-12 15:06:20 +0000 | [diff] [blame] | 129 | for (i = 0; i < mbedtls_rsa_get_len(&rsa); i++) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | mbedtls_fprintf(f, "%02X%s", buf[i], | 
|  | 131 | (i + 1) % 16 == 0 ? "\r\n" : " "); | 
|  | 132 | } | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 133 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | fclose(f); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 135 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | mbedtls_printf("\n  . Done (created \"%s\")\n\n", "result-enc.txt"); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 137 |  | 
| Andres Amaya Garcia | 25b5af5 | 2018-04-30 22:08:36 +0100 | [diff] [blame] | 138 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 139 |  | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 140 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | mbedtls_mpi_free(&N); mbedtls_mpi_free(&E); | 
|  | 142 | mbedtls_ctr_drbg_free(&ctr_drbg); | 
|  | 143 | mbedtls_entropy_free(&entropy); | 
|  | 144 | mbedtls_rsa_free(&rsa); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 145 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | mbedtls_exit(exit_code); | 
| Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 147 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C && | 
|  | 149 | MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ |