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