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