blob: f45e0f9a59f710eb759ef7591d639183231ca861 [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple decryption 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_printf printf
28# define mbedtls_exit exit
29# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
30# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garcia7fe4edf2018-04-30 22:07:15 +010031#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
35 defined(MBEDTLS_CTR_DRBG_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036# include "mbedtls/rsa.h"
37# include "mbedtls/entropy.h"
38# include "mbedtls/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000039
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# include <string.h>
Simon Butcher6b46c622016-04-12 13:25:08 +010041
Rich Evans18b78c72015-02-11 14:06:19 +000042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
45 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
46 !defined(MBEDTLS_CTR_DRBG_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047int main(void)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
51 "MBEDTLS_CTR_DRBG_C not defined.\n");
52 mbedtls_exit(0);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000053}
54#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056int main(int argc, char *argv[])
Paul Bakker7bc05ff2011-08-09 10:30:36 +000057{
58 FILE *f;
Andres Amaya Garcia7fe4edf2018-04-30 22:07:15 +010059 int ret = 1;
60 int exit_code = MBEDTLS_EXIT_FAILURE;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020061 unsigned c;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000062 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_rsa_context rsa;
Hanno Beckerccef18c2017-08-23 06:46:45 +010064 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_entropy_context entropy;
66 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000067 unsigned char result[1024];
68 unsigned char buf[512];
Paul Bakker548957d2013-08-30 10:30:02 +020069 const char *pers = "rsa_decrypt";
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070 ((void)argv);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 memset(result, 0, sizeof(result));
Paul Bakker310c25e2011-12-04 17:06:56 +000073
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020074 if (argc != 1) {
75 mbedtls_printf("usage: rsa_decrypt\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000076
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077# if defined(_WIN32)
78 mbedtls_printf("\n");
79# endif
Paul Bakker7bc05ff2011-08-09 10:30:36 +000080
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +000082 }
83
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084 mbedtls_printf("\n . Seeding the random number generator...");
85 fflush(stdout);
Paul Bakker548957d2013-08-30 10:30:02 +020086
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020087 mbedtls_rsa_init(&rsa);
88 mbedtls_ctr_drbg_init(&ctr_drbg);
89 mbedtls_entropy_init(&entropy);
90 mbedtls_mpi_init(&N);
91 mbedtls_mpi_init(&P);
92 mbedtls_mpi_init(&Q);
93 mbedtls_mpi_init(&D);
94 mbedtls_mpi_init(&E);
95 mbedtls_mpi_init(&DP);
96 mbedtls_mpi_init(&DQ);
97 mbedtls_mpi_init(&QP);
Simon Butcher6b46c622016-04-12 13:25:08 +010098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
100 (const unsigned char *)pers, strlen(pers));
101 if (ret != 0) {
102 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker548957d2013-08-30 10:30:02 +0200103 goto exit;
104 }
105
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
107 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000108
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
110 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n"
111 " ! Please run rsa_genkey first\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000112 goto exit;
113 }
114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
116 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
117 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
118 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
119 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
120 (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
121 (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
122 (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
123 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
124 ret);
125 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000126 goto exit;
127 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000129
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
131 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n", ret);
Hanno Beckerccef18c2017-08-23 06:46:45 +0100132 goto exit;
133 }
134
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
136 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
137 ret);
Hanno Beckerccef18c2017-08-23 06:46:45 +0100138 goto exit;
139 }
140
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000141 /*
142 * Extract the RSA encrypted value from the text file
143 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
145 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000146 goto exit;
147 }
148
149 i = 0;
150
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 while (fscanf(f, "%02X", (unsigned int *)&c) > 0 && i < (int)sizeof(buf))
152 buf[i++] = (unsigned char)c;
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 fclose(f);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 if (i != rsa.MBEDTLS_PRIVATE(len)) {
157 mbedtls_printf("\n ! Invalid RSA signature format\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000158 goto exit;
159 }
160
161 /*
162 * Decrypt the encrypted RSA data and print the result.
163 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 mbedtls_printf("\n . Decrypting the encrypted data");
165 fflush(stdout);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000166
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167 ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
168 &i, buf, result, 1024);
169 if (ret != 0) {
170 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
171 ret);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000172 goto exit;
173 }
174
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 mbedtls_printf("\n . OK\n\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000178
Andres Amaya Garcia7fe4edf2018-04-30 22:07:15 +0100179 exit_code = MBEDTLS_EXIT_SUCCESS;
180
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000181exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182 mbedtls_ctr_drbg_free(&ctr_drbg);
183 mbedtls_entropy_free(&entropy);
184 mbedtls_rsa_free(&rsa);
185 mbedtls_mpi_free(&N);
186 mbedtls_mpi_free(&P);
187 mbedtls_mpi_free(&Q);
188 mbedtls_mpi_free(&D);
189 mbedtls_mpi_free(&E);
190 mbedtls_mpi_free(&DP);
191 mbedtls_mpi_free(&DQ);
192 mbedtls_mpi_free(&QP);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000193
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200194# if defined(_WIN32)
195 mbedtls_printf(" + Press Enter to exit this program.\n");
196 fflush(stdout);
197 getchar();
198# endif
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000199
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200 mbedtls_exit(exit_code);
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */