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