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