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