1 /*
2  *  RSA simple data encryption program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 
20 #include "mbedtls/build_info.h"
21 
22 #include "mbedtls/platform.h"
23 
24 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
25     defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
26     defined(MBEDTLS_CTR_DRBG_C)
27 #include "mbedtls/rsa.h"
28 #include "mbedtls/entropy.h"
29 #include "mbedtls/ctr_drbg.h"
30 
31 #include <string.h>
32 #endif
33 
34 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
35     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
36     !defined(MBEDTLS_CTR_DRBG_C)
main(void)37 int main(void)
38 {
39     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
40                    "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
41                    "MBEDTLS_CTR_DRBG_C not defined.\n");
42     mbedtls_exit(0);
43 }
44 #else
45 
46 
main(int argc,char * argv[])47 int main(int argc, char *argv[])
48 {
49     FILE *f;
50     int ret = 1;
51     int exit_code = MBEDTLS_EXIT_FAILURE;
52     size_t i;
53     mbedtls_rsa_context rsa;
54     mbedtls_entropy_context entropy;
55     mbedtls_ctr_drbg_context ctr_drbg;
56     unsigned char input[1024];
57     unsigned char buf[512];
58     const char *pers = "rsa_encrypt";
59     mbedtls_mpi N, E;
60 
61     if (argc != 2) {
62         mbedtls_printf("usage: rsa_encrypt <string of max 100 characters>\n");
63 
64 #if defined(_WIN32)
65         mbedtls_printf("\n");
66 #endif
67 
68         mbedtls_exit(exit_code);
69     }
70 
71     mbedtls_printf("\n  . Seeding the random number generator...");
72     fflush(stdout);
73 
74     mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
75     mbedtls_rsa_init(&rsa);
76     mbedtls_ctr_drbg_init(&ctr_drbg);
77     mbedtls_entropy_init(&entropy);
78 
79     ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
80                                 &entropy, (const unsigned char *) pers,
81                                 strlen(pers));
82     if (ret != 0) {
83         mbedtls_printf(" failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
84                        ret);
85         goto exit;
86     }
87 
88     mbedtls_printf("\n  . Reading public key from rsa_pub.txt");
89     fflush(stdout);
90 
91     if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
92         mbedtls_printf(" failed\n  ! Could not open rsa_pub.txt\n" \
93                        "  ! Please run rsa_genkey first\n\n");
94         goto exit;
95     }
96 
97     if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
98         (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0) {
99         mbedtls_printf(" failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
100                        ret);
101         fclose(f);
102         goto exit;
103     }
104     fclose(f);
105 
106     if ((ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E)) != 0) {
107         mbedtls_printf(" failed\n  ! mbedtls_rsa_import returned %d\n\n",
108                        ret);
109         goto exit;
110     }
111 
112     if (strlen(argv[1]) > 100) {
113         mbedtls_printf(" Input data larger than 100 characters.\n\n");
114         goto exit;
115     }
116 
117     memcpy(input, argv[1], strlen(argv[1]));
118 
119     /*
120      * Calculate the RSA encryption of the hash.
121      */
122     mbedtls_printf("\n  . Generating the RSA encrypted value");
123     fflush(stdout);
124 
125     ret = mbedtls_rsa_pkcs1_encrypt(&rsa, mbedtls_ctr_drbg_random,
126                                     &ctr_drbg, strlen(argv[1]), input, buf);
127     if (ret != 0) {
128         mbedtls_printf(" failed\n  ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
129                        ret);
130         goto exit;
131     }
132 
133     /*
134      * Write the signature into result-enc.txt
135      */
136     if ((f = fopen("result-enc.txt", "wb+")) == NULL) {
137         mbedtls_printf(" failed\n  ! Could not create %s\n\n", "result-enc.txt");
138         goto exit;
139     }
140 
141     for (i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++) {
142         mbedtls_fprintf(f, "%02X%s", buf[i],
143                         (i + 1) % 16 == 0 ? "\r\n" : " ");
144     }
145 
146     fclose(f);
147 
148     mbedtls_printf("\n  . Done (created \"%s\")\n\n", "result-enc.txt");
149 
150     exit_code = MBEDTLS_EXIT_SUCCESS;
151 
152 exit:
153     mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
154     mbedtls_ctr_drbg_free(&ctr_drbg);
155     mbedtls_entropy_free(&entropy);
156     mbedtls_rsa_free(&rsa);
157 
158     mbedtls_exit(exit_code);
159 }
160 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
161           MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
162