blob: 94068a18221e0e4c705ba5b8dc90692da757ecec [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple data encryption program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7bc05ff2011-08-09 10:30:36 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker7bc05ff2011-08-09 10:30:36 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
17 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
18 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/rsa.h"
20#include "mbedtls/entropy.h"
21#include "mbedtls/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000022
Rich Evans18b78c72015-02-11 14:06:19 +000023#include <string.h>
24#endif
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
27 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
28 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029int main(void)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
33 "MBEDTLS_CTR_DRBG_C not defined.\n");
34 mbedtls_exit(0);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000035}
36#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000037
Simon Butcher63cb97e2018-12-06 17:43:31 +000038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039int main(int argc, char *argv[])
Paul Bakker7bc05ff2011-08-09 10:30:36 +000040{
41 FILE *f;
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +010042 int ret = 1;
43 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000044 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_rsa_context rsa;
46 mbedtls_entropy_context entropy;
47 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000048 unsigned char input[1024];
49 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020050 const char *pers = "rsa_encrypt";
Hanno Becker0c263932017-08-23 06:47:06 +010051 mbedtls_mpi N, E;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 if (argc != 2) {
54 mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000055
Paul Bakkercce9d772011-11-18 14:26:47 +000056#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_printf("\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000058#endif
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000061 }
62
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 mbedtls_printf("\n . Seeding the random number generator...");
64 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +000065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
67 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
68 mbedtls_ctr_drbg_init(&ctr_drbg);
69 mbedtls_entropy_init(&entropy);
Simon Butcher6b46c622016-04-12 13:25:08 +010070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
72 &entropy, (const unsigned char *) pers,
73 strlen(pers));
74 if (ret != 0) {
75 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
76 ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000077 goto exit;
78 }
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
81 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
84 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
85 " ! Please run rsa_genkey first\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000086 goto exit;
87 }
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
90 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0) {
91 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
92 ret);
93 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000094 goto exit;
95 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if ((ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E)) != 0) {
99 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
100 ret);
Hanno Becker0c263932017-08-23 06:47:06 +0100101 goto exit;
102 }
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 if (strlen(argv[1]) > 100) {
105 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000106 goto exit;
107 }
108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 memcpy(input, argv[1], strlen(argv[1]));
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000110
111 /*
112 * Calculate the RSA encryption of the hash.
113 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_printf("\n . Generating the RSA encrypted value");
115 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random,
118 &ctr_drbg, MBEDTLS_RSA_PUBLIC,
119 strlen(argv[1]), input, buf);
120 if (ret != 0) {
121 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
122 ret);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000123 goto exit;
124 }
125
126 /*
127 * Write the signature into result-enc.txt
128 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
130 mbedtls_printf(" failed\n ! Could not create %s\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000131 goto exit;
132 }
133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 for (i = 0; i < rsa.len; i++) {
135 mbedtls_fprintf(f, "%02X%s", buf[i],
136 (i + 1) % 16 == 0 ? "\r\n" : " ");
137 }
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000142
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +0100143 exit_code = MBEDTLS_EXIT_SUCCESS;
144
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000145exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
147 mbedtls_ctr_drbg_free(&ctr_drbg);
148 mbedtls_entropy_free(&entropy);
149 mbedtls_rsa_free(&rsa);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000150
Paul Bakkercce9d772011-11-18 14:26:47 +0000151#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 mbedtls_printf(" + Press Enter to exit this program.\n");
153 fflush(stdout); getchar();
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000154#endif
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
159 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */