blob: e1ed252bb22c0d2ce1f29e3642f1e410bfb0929f [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#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é-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/rsa.h"
18#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000020
Rich Evans18b78c72015-02-11 14:06:19 +000021#include <string.h>
22#endif
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#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 Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010030 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
31 "MBEDTLS_CTR_DRBG_C not defined.\n");
32 mbedtls_exit(0);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000033}
34#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Simon Butcher63cb97e2018-12-06 17:43:31 +000036
Gilles Peskine449bd832023-01-11 14:50:10 +010037int main(int argc, char *argv[])
Paul Bakker7bc05ff2011-08-09 10:30:36 +000038{
39 FILE *f;
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +010040 int ret = 1;
41 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000042 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_rsa_context rsa;
44 mbedtls_entropy_context entropy;
45 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000046 unsigned char input[1024];
47 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020048 const char *pers = "rsa_encrypt";
Hanno Becker0c263932017-08-23 06:47:06 +010049 mbedtls_mpi N, E;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 if (argc != 2) {
52 mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000053
Paul Bakkercce9d772011-11-18 14:26:47 +000054#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_printf("\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000056#endif
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000059 }
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_printf("\n . Seeding the random number generator...");
62 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +000063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 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 Butcher6b46c622016-04-12 13:25:08 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 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 Bakker508ad5a2011-12-04 17:09:26 +000075 goto exit;
76 }
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
79 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 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 Bakker7bc05ff2011-08-09 10:30:36 +000084 goto exit;
85 }
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 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 Bakker7bc05ff2011-08-09 10:30:36 +000092 goto exit;
93 }
Gilles Peskine449bd832023-01-11 14:50:10 +010094 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 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 Becker0c263932017-08-23 06:47:06 +010099 goto exit;
100 }
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 if (strlen(argv[1]) > 100) {
103 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000104 goto exit;
105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 memcpy(input, argv[1], strlen(argv[1]));
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000108
109 /*
110 * Calculate the RSA encryption of the hash.
111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 mbedtls_printf("\n . Generating the RSA encrypted value");
113 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 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 Bakker7bc05ff2011-08-09 10:30:36 +0000120 goto exit;
121 }
122
123 /*
124 * Write the signature into result-enc.txt
125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
127 mbedtls_printf(" failed\n ! Could not create %s\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000128 goto exit;
129 }
130
Minos Galanakisee757d32024-01-12 15:06:20 +0000131 for (i = 0; i < mbedtls_rsa_get_len(&rsa); i++) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_fprintf(f, "%02X%s", buf[i],
133 (i + 1) % 16 == 0 ? "\r\n" : " ");
134 }
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000139
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +0100140 exit_code = MBEDTLS_EXIT_SUCCESS;
141
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 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 Bakker7bc05ff2011-08-09 10:30:36 +0000147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
151 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */