1 /*
2  *  RSA simple data encryption 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_RSA_C) && \
13     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
14     defined(MBEDTLS_CTR_DRBG_C)
15 #include "mbedtls/rsa.h"
16 #include "mbedtls/entropy.h"
17 #include "mbedtls/ctr_drbg.h"
18 
19 #include <string.h>
20 #endif
21 
22 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
23     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
24     !defined(MBEDTLS_CTR_DRBG_C)
main(void)25 int main(void)
26 {
27     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
28                    "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
29                    "MBEDTLS_CTR_DRBG_C not defined.\n");
30     mbedtls_exit(0);
31 }
32 #else
33 
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37     FILE *f;
38     int ret = 1;
39     int exit_code = MBEDTLS_EXIT_FAILURE;
40     size_t i;
41     mbedtls_rsa_context rsa;
42     mbedtls_entropy_context entropy;
43     mbedtls_ctr_drbg_context ctr_drbg;
44     unsigned char input[1024];
45     unsigned char buf[512];
46     const char *pers = "rsa_encrypt";
47     mbedtls_mpi N, E;
48 
49     if (argc != 2) {
50         mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
51 
52 #if defined(_WIN32)
53         mbedtls_printf("\n");
54 #endif
55 
56         mbedtls_exit(exit_code);
57     }
58 
59     mbedtls_printf("\n  . Seeding the random number generator...");
60     fflush(stdout);
61 
62     mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
63     mbedtls_rsa_init(&rsa);
64     mbedtls_ctr_drbg_init(&ctr_drbg);
65     mbedtls_entropy_init(&entropy);
66 
67     ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
68                                 &entropy, (const unsigned char *) pers,
69                                 strlen(pers));
70     if (ret != 0) {
71         mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
72                        ret);
73         goto exit;
74     }
75 
76     mbedtls_printf("\n  . Reading public key from rsa_pub.txt");
77     fflush(stdout);
78 
79     if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
80         mbedtls_printf(" failed\n  ! Could not open rsa_pub.txt\n" \
81                        "  ! Please run rsa_genkey first\n\n");
82         goto exit;
83     }
84 
85     if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
86         (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0) {
87         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
88                        ret);
89         fclose(f);
90         goto exit;
91     }
92     fclose(f);
93 
94     if ((ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E)) != 0) {
95         mbedtls_printf(" failed\n  ! mbedtls_rsa_import returned %d\n\n",
96                        ret);
97         goto exit;
98     }
99 
100     if (strlen(argv[1]) > 100) {
101         mbedtls_printf(" Input data larger than 100 characters.\n\n");
102         goto exit;
103     }
104 
105     memcpy(input, argv[1], strlen(argv[1]));
106 
107     /*
108      * Calculate the RSA encryption of the hash.
109      */
110     mbedtls_printf("\n  . Generating the RSA encrypted value");
111     fflush(stdout);
112 
113     ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random,
114                                     &ctr_drbg, strlen(argv[1]), input, buf);
115     if (ret != 0) {
116         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
117                        ret);
118         goto exit;
119     }
120 
121     /*
122      * Write the signature into result-enc.txt
123      */
124     if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
125         mbedtls_printf(" failed\n  ! Could not create %s\n\n", "result-enc.txt");
126         goto exit;
127     }
128 
129     for (i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++) {
130         mbedtls_fprintf(f, "%02X%s", buf[i],
131                         (i + 1) % 16 == 0 ? "\r\n" : " ");
132     }
133 
134     fclose(f);
135 
136     mbedtls_printf("\n  . Done (created \"%s\")\n\n", "result-enc.txt");
137 
138     exit_code = MBEDTLS_EXIT_SUCCESS;
139 
140 exit:
141     mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
142     mbedtls_ctr_drbg_free(&ctr_drbg);
143     mbedtls_entropy_free(&entropy);
144     mbedtls_rsa_free(&rsa);
145 
146     mbedtls_exit(exit_code);
147 }
148 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
149           MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
150