blob: 2126a9b9bbe20117bac79303043f3e420061dc9d [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker7bc05ff2011-08-09 10:30:36 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
28#include "mbedtls/entropy.h"
29#include "mbedtls/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000030
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <string.h>
32#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#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 Peskine449bd832023-01-11 14:50:10 +010037int main(void)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010040 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
41 "MBEDTLS_CTR_DRBG_C not defined.\n");
42 mbedtls_exit(0);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043}
44#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000045
Simon Butcher63cb97e2018-12-06 17:43:31 +000046
Gilles Peskine449bd832023-01-11 14:50:10 +010047int main(int argc, char *argv[])
Paul Bakker7bc05ff2011-08-09 10:30:36 +000048{
49 FILE *f;
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +010050 int ret = 1;
51 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000052 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_rsa_context rsa;
54 mbedtls_entropy_context entropy;
55 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000056 unsigned char input[1024];
57 unsigned char buf[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020058 const char *pers = "rsa_encrypt";
Hanno Becker0c263932017-08-23 06:47:06 +010059 mbedtls_mpi N, E;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (argc != 2) {
62 mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000063
Paul Bakkercce9d772011-11-18 14:26:47 +000064#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010065 mbedtls_printf("\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000066#endif
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000069 }
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_printf("\n . Seeding the random number generator...");
72 fflush(stdout);
Paul Bakker508ad5a2011-12-04 17:09:26 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 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 Butcher6b46c622016-04-12 13:25:08 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 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 Bakker508ad5a2011-12-04 17:09:26 +000085 goto exit;
86 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
89 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 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 Bakker7bc05ff2011-08-09 10:30:36 +000094 goto exit;
95 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 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 Bakker7bc05ff2011-08-09 10:30:36 +0000102 goto exit;
103 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 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 Becker0c263932017-08-23 06:47:06 +0100109 goto exit;
110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (strlen(argv[1]) > 100) {
113 mbedtls_printf(" Input data larger than 100 characters.\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000114 goto exit;
115 }
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 memcpy(input, argv[1], strlen(argv[1]));
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000118
119 /*
120 * Calculate the RSA encryption of the hash.
121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_printf("\n . Generating the RSA encrypted value");
123 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 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 Bakker7bc05ff2011-08-09 10:30:36 +0000130 goto exit;
131 }
132
133 /*
134 * Write the signature into result-enc.txt
135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
137 mbedtls_printf(" failed\n ! Could not create %s\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000138 goto exit;
139 }
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 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 Bakker7bc05ff2011-08-09 10:30:36 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_printf("\n . Done (created \"%s\")\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000149
Andres Amaya Garcia25b5af52018-04-30 22:08:36 +0100150 exit_code = MBEDTLS_EXIT_SUCCESS;
151
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 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 Bakker7bc05ff2011-08-09 10:30:36 +0000157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
161 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */