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