1 /*
2 * Public key-based 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_PK_PARSE_C) && \
17 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
18 defined(MBEDTLS_CTR_DRBG_C)
19 #include "mbedtls/error.h"
20 #include "mbedtls/pk.h"
21 #include "mbedtls/entropy.h"
22 #include "mbedtls/ctr_drbg.h"
23
24 #include <stdio.h>
25 #include <string.h>
26 #endif
27
28 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
29 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
30 !defined(MBEDTLS_CTR_DRBG_C)
main(void)31 int main(void)
32 {
33 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
34 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
35 "MBEDTLS_CTR_DRBG_C not defined.\n");
36 mbedtls_exit(0);
37 }
38 #else
39
40
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 FILE *f;
44 int ret = 1;
45 unsigned c;
46 int exit_code = MBEDTLS_EXIT_FAILURE;
47 size_t i, olen = 0;
48 mbedtls_pk_context pk;
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 = "mbedtls_pk_decrypt";
54 ((void) argv);
55
56 mbedtls_pk_init(&pk);
57 mbedtls_entropy_init(&entropy);
58 mbedtls_ctr_drbg_init(&ctr_drbg);
59
60 memset(result, 0, sizeof(result));
61
62 #if defined(MBEDTLS_USE_PSA_CRYPTO)
63 psa_status_t status = psa_crypto_init();
64 if (status != PSA_SUCCESS) {
65 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
66 (int) status);
67 goto exit;
68 }
69 #endif /* MBEDTLS_USE_PSA_CRYPTO */
70
71 if (argc != 2) {
72 mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");
73
74 #if defined(_WIN32)
75 mbedtls_printf("\n");
76 #endif
77
78 goto exit;
79 }
80
81 mbedtls_printf("\n . Seeding the random number generator...");
82 fflush(stdout);
83
84 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
85 &entropy, (const unsigned char *) pers,
86 strlen(pers))) != 0) {
87 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
88 (unsigned int) -ret);
89 goto exit;
90 }
91
92 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
93 fflush(stdout);
94
95 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "")) != 0) {
96 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
97 (unsigned int) -ret);
98 goto exit;
99 }
100
101 /*
102 * Extract the RSA encrypted value from the text file
103 */
104 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
105 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
106 ret = 1;
107 goto exit;
108 }
109
110 i = 0;
111 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
112 i < (int) sizeof(buf)) {
113 buf[i++] = (unsigned char) c;
114 }
115
116 fclose(f);
117
118 /*
119 * Decrypt the encrypted RSA data and print the result.
120 */
121 mbedtls_printf("\n . Decrypting the encrypted data");
122 fflush(stdout);
123
124 if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result),
125 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
126 mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
127 (unsigned int) -ret);
128 goto exit;
129 }
130
131 mbedtls_printf("\n . OK\n\n");
132
133 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
134
135 exit_code = MBEDTLS_EXIT_SUCCESS;
136
137 exit:
138
139 mbedtls_pk_free(&pk);
140 mbedtls_entropy_free(&entropy);
141 mbedtls_ctr_drbg_free(&ctr_drbg);
142 #if defined(MBEDTLS_USE_PSA_CRYPTO)
143 mbedtls_psa_crypto_free();
144 #endif /* MBEDTLS_USE_PSA_CRYPTO */
145
146 #if defined(MBEDTLS_ERROR_C)
147 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
148 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
149 mbedtls_printf(" ! Last error was: %s\n", buf);
150 }
151 #endif
152
153 #if defined(_WIN32)
154 mbedtls_printf(" + Press Enter to exit this program.\n");
155 fflush(stdout); getchar();
156 #endif
157
158 mbedtls_exit(exit_code);
159 }
160 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
161 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
162