blob: 6538f8a999eb27dc07c8d8036694b68637f279dd [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7bc05ff2011-08-09 10:30:36 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#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é-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/rsa.h"
16#include "mbedtls/entropy.h"
17#include "mbedtls/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000018
Rich Evans18b78c72015-02-11 14:06:19 +000019#include <string.h>
20#endif
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#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 Peskine449bd832023-01-11 14:50:10 +010025int main(void)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010028 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
29 "MBEDTLS_CTR_DRBG_C not defined.\n");
30 mbedtls_exit(0);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000031}
32#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000033
Simon Butcher63cb97e2018-12-06 17:43:31 +000034
Gilles Peskine449bd832023-01-11 14:50:10 +010035int main(int argc, char *argv[])
Paul Bakker7bc05ff2011-08-09 10:30:36 +000036{
37 FILE *f;
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +010038 int ret = 1;
39 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000040 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_rsa_context rsa;
42 mbedtls_entropy_context entropy;
43 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000044 unsigned char input[1024];
45 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020046 const char *pers = "rsa_encrypt";
Hanno Becker0c263932017-08-23 06:47:06 +010047 mbedtls_mpi N, E;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (argc != 2) {
50 mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000051
Paul Bakkercce9d772011-11-18 14:26:47 +000052#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_printf("\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000054#endif
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000057 }
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_printf("\n . Seeding the random number generator...");
60 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +000061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 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 Butcher6b46c622016-04-12 13:25:08 +010066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 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 Bakker508ad5a2011-12-04 17:09:26 +000073 goto exit;
74 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
77 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 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 Bakker7bc05ff2011-08-09 10:30:36 +000082 goto exit;
83 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 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 Bakker7bc05ff2011-08-09 10:30:36 +000090 goto exit;
91 }
Gilles Peskine449bd832023-01-11 14:50:10 +010092 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 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 Becker0c263932017-08-23 06:47:06 +010097 goto exit;
98 }
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (strlen(argv[1]) > 100) {
101 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000102 goto exit;
103 }
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 memcpy(input, argv[1], strlen(argv[1]));
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000106
107 /*
108 * Calculate the RSA encryption of the hash.
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_printf("\n . Generating the RSA encrypted value");
111 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 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 Bakker7bc05ff2011-08-09 10:30:36 +0000118 goto exit;
119 }
120
121 /*
122 * Write the signature into result-enc.txt
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
125 mbedtls_printf(" failed\n ! Could not create %s\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000126 goto exit;
127 }
128
Minos Galanakisee757d32024-01-12 15:06:20 +0000129 for (i = 0; i < mbedtls_rsa_get_len(&rsa); i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_fprintf(f, "%02X%s", buf[i],
131 (i + 1) % 16 == 0 ? "\r\n" : " ");
132 }
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000137
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +0100138 exit_code = MBEDTLS_EXIT_SUCCESS;
139
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 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 Bakker7bc05ff2011-08-09 10:30:36 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
149 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */