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 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_fprintf fprintf
59 #define mbedtls_printf printf
60 #define mbedtls_exit exit
61 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
62 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63 #endif /* MBEDTLS_PLATFORM_C */
64
65 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
66 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
67 defined(MBEDTLS_CTR_DRBG_C)
68 #include "mbedtls/rsa.h"
69 #include "mbedtls/entropy.h"
70 #include "mbedtls/ctr_drbg.h"
71
72 #include <string.h>
73 #endif
74
75 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
76 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
77 !defined(MBEDTLS_CTR_DRBG_C)
main(void)78 int main( void )
79 {
80 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
81 "MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
82 "MBEDTLS_CTR_DRBG_C not defined.\n");
83 mbedtls_exit( 0 );
84 }
85 #else
86
87
main(int argc,char * argv[])88 int main( int argc, char *argv[] )
89 {
90 FILE *f;
91 int ret = 1;
92 int exit_code = MBEDTLS_EXIT_FAILURE;
93 size_t i;
94 mbedtls_rsa_context rsa;
95 mbedtls_entropy_context entropy;
96 mbedtls_ctr_drbg_context ctr_drbg;
97 unsigned char input[1024];
98 unsigned char buf[512];
99 const char *pers = "rsa_encrypt";
100 mbedtls_mpi N, E;
101
102 if( argc != 2 )
103 {
104 mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
105
106 #if defined(_WIN32)
107 mbedtls_printf( "\n" );
108 #endif
109
110 mbedtls_exit( exit_code );
111 }
112
113 mbedtls_printf( "\n . Seeding the random number generator..." );
114 fflush( stdout );
115
116 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
117 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
118 mbedtls_ctr_drbg_init( &ctr_drbg );
119 mbedtls_entropy_init( &entropy );
120
121 ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
122 &entropy, (const unsigned char *) pers,
123 strlen( pers ) );
124 if( ret != 0 )
125 {
126 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
127 ret );
128 goto exit;
129 }
130
131 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
132 fflush( stdout );
133
134 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
135 {
136 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
137 " ! Please run rsa_genkey first\n\n" );
138 goto exit;
139 }
140
141 if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
142 ( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
143 {
144 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
145 ret );
146 fclose( f );
147 goto exit;
148 }
149 fclose( f );
150
151 if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
152 {
153 mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
154 ret );
155 goto exit;
156 }
157
158 if( strlen( argv[1] ) > 100 )
159 {
160 mbedtls_printf( " Input data larger than 100 characters.\n\n" );
161 goto exit;
162 }
163
164 memcpy( input, argv[1], strlen( argv[1] ) );
165
166 /*
167 * Calculate the RSA encryption of the hash.
168 */
169 mbedtls_printf( "\n . Generating the RSA encrypted value" );
170 fflush( stdout );
171
172 ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
173 &ctr_drbg, MBEDTLS_RSA_PUBLIC,
174 strlen( argv[1] ), input, buf );
175 if( ret != 0 )
176 {
177 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
178 ret );
179 goto exit;
180 }
181
182 /*
183 * Write the signature into result-enc.txt
184 */
185 if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
186 {
187 mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
188 goto exit;
189 }
190
191 for( i = 0; i < rsa.len; i++ )
192 mbedtls_fprintf( f, "%02X%s", buf[i],
193 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
194
195 fclose( f );
196
197 mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
198
199 exit_code = MBEDTLS_EXIT_SUCCESS;
200
201 exit:
202 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
203 mbedtls_ctr_drbg_free( &ctr_drbg );
204 mbedtls_entropy_free( &entropy );
205 mbedtls_rsa_free( &rsa );
206
207 #if defined(_WIN32)
208 mbedtls_printf( " + Press Enter to exit this program.\n" );
209 fflush( stdout ); getchar();
210 #endif
211
212 mbedtls_exit( exit_code );
213 }
214 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
215 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
216