1 /*
2 * RSA simple decryption program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
17 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
18 defined(MBEDTLS_CTR_DRBG_C)
19 #include "mbedtls/rsa.h"
20 #include "mbedtls/entropy.h"
21 #include "mbedtls/ctr_drbg.h"
22
23 #include <string.h>
24
25 #endif
26
27 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
28 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
29 !defined(MBEDTLS_CTR_DRBG_C)
main(void)30 int main(void)
31 {
32 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
33 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
34 "MBEDTLS_CTR_DRBG_C not defined.\n");
35 mbedtls_exit(0);
36 }
37 #else
38
39
main(int argc,char * argv[])40 int main(int argc, char *argv[])
41 {
42 FILE *f;
43 int ret = 1;
44 int exit_code = MBEDTLS_EXIT_FAILURE;
45 unsigned c;
46 size_t i;
47 mbedtls_rsa_context rsa;
48 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
49 mbedtls_entropy_context entropy;
50 mbedtls_ctr_drbg_context ctr_drbg;
51 unsigned char result[1024];
52 unsigned char buf[512];
53 const char *pers = "rsa_decrypt";
54 ((void) argv);
55
56 memset(result, 0, sizeof(result));
57
58 if (argc != 1) {
59 mbedtls_printf("usage: rsa_decrypt\n");
60
61 #if defined(_WIN32)
62 mbedtls_printf("\n");
63 #endif
64
65 mbedtls_exit(exit_code);
66 }
67
68 mbedtls_printf("\n . Seeding the random number generator...");
69 fflush(stdout);
70
71 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
72 mbedtls_ctr_drbg_init(&ctr_drbg);
73 mbedtls_entropy_init(&entropy);
74 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
75 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
76 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
77
78 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
79 &entropy, (const unsigned char *) pers,
80 strlen(pers));
81 if (ret != 0) {
82 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
83 ret);
84 goto exit;
85 }
86
87 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
88 fflush(stdout);
89
90 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
91 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
92 " ! Please run rsa_genkey first\n\n");
93 goto exit;
94 }
95
96 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
97 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
98 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
99 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
100 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0 ||
101 (ret = mbedtls_mpi_read_file(&DP, 16, f)) != 0 ||
102 (ret = mbedtls_mpi_read_file(&DQ, 16, f)) != 0 ||
103 (ret = mbedtls_mpi_read_file(&QP, 16, f)) != 0) {
104 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
105 ret);
106 fclose(f);
107 goto exit;
108 }
109 fclose(f);
110
111 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
112 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
113 ret);
114 goto exit;
115 }
116
117 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
118 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
119 ret);
120 goto exit;
121 }
122
123 /*
124 * Extract the RSA encrypted value from the text file
125 */
126 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
127 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
128 goto exit;
129 }
130
131 i = 0;
132
133 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
134 i < (int) sizeof(buf)) {
135 buf[i++] = (unsigned char) c;
136 }
137
138 fclose(f);
139
140 if (i != rsa.len) {
141 mbedtls_printf("\n ! Invalid RSA signature format\n\n");
142 goto exit;
143 }
144
145 /*
146 * Decrypt the encrypted RSA data and print the result.
147 */
148 mbedtls_printf("\n . Decrypting the encrypted data");
149 fflush(stdout);
150
151 ret = mbedtls_rsa_pkcs1_decrypt(&rsa, mbedtls_ctr_drbg_random,
152 &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
153 buf, result, 1024);
154 if (ret != 0) {
155 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
156 ret);
157 goto exit;
158 }
159
160 mbedtls_printf("\n . OK\n\n");
161
162 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
163
164 exit_code = MBEDTLS_EXIT_SUCCESS;
165
166 exit:
167 mbedtls_ctr_drbg_free(&ctr_drbg);
168 mbedtls_entropy_free(&entropy);
169 mbedtls_rsa_free(&rsa);
170 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
171 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
172 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
173
174 #if defined(_WIN32)
175 mbedtls_printf(" + Press Enter to exit this program.\n");
176 fflush(stdout); getchar();
177 #endif
178
179 mbedtls_exit(exit_code);
180 }
181 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
182