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