1 /*
2  *  RSA simple decryption 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 #if defined(MBEDTLS_PLATFORM_C)
23 #include "mbedtls/platform.h"
24 #else
25 #include <stdio.h>
26 #include <stdlib.h>
27 #define mbedtls_printf       printf
28 #define mbedtls_exit         exit
29 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
30 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31 #endif /* MBEDTLS_PLATFORM_C */
32 
33 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
34     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
35     defined(MBEDTLS_CTR_DRBG_C)
36 #include "mbedtls/rsa.h"
37 #include "mbedtls/entropy.h"
38 #include "mbedtls/ctr_drbg.h"
39 
40 #include <string.h>
41 
42 #endif
43 
44 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
45     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
46     !defined(MBEDTLS_CTR_DRBG_C)
main(void)47 int main( void )
48 {
49     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
50            "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
51            "MBEDTLS_CTR_DRBG_C not defined.\n");
52     mbedtls_exit( 0 );
53 }
54 #else
55 
56 
main(int argc,char * argv[])57 int main( int argc, char *argv[] )
58 {
59     FILE *f;
60     int ret = 1;
61     int exit_code = MBEDTLS_EXIT_FAILURE;
62     unsigned c;
63     size_t i;
64     mbedtls_rsa_context rsa;
65     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
66     mbedtls_entropy_context entropy;
67     mbedtls_ctr_drbg_context ctr_drbg;
68     unsigned char result[1024];
69     unsigned char buf[512];
70     const char *pers = "rsa_decrypt";
71     ((void) argv);
72 
73     memset(result, 0, sizeof( result ) );
74 
75     if( argc != 1 )
76     {
77         mbedtls_printf( "usage: rsa_decrypt\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_rsa_init( &rsa );
90     mbedtls_ctr_drbg_init( &ctr_drbg );
91     mbedtls_entropy_init( &entropy );
92     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
93     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
94     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
95 
96     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
97                                         &entropy, (const unsigned char *) pers,
98                                         strlen( pers ) );
99     if( ret != 0 )
100     {
101         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
102                         ret );
103         goto exit;
104     }
105 
106     mbedtls_printf( "\n  . Reading private key from rsa_priv.txt" );
107     fflush( stdout );
108 
109     if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
110     {
111         mbedtls_printf( " failed\n  ! Could not open rsa_priv.txt\n" \
112                 "  ! Please run rsa_genkey first\n\n" );
113         goto exit;
114     }
115 
116     if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) )  != 0 ||
117         ( ret = mbedtls_mpi_read_file( &E , 16, f ) )  != 0 ||
118         ( ret = mbedtls_mpi_read_file( &D , 16, f ) )  != 0 ||
119         ( ret = mbedtls_mpi_read_file( &P , 16, f ) )  != 0 ||
120         ( ret = mbedtls_mpi_read_file( &Q , 16, f ) )  != 0 ||
121         ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
122         ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
123         ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
124     {
125         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
126                         ret );
127         fclose( f );
128         goto exit;
129     }
130     fclose( f );
131 
132     if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
133     {
134         mbedtls_printf( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
135                         ret );
136         goto exit;
137     }
138 
139     if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
140     {
141         mbedtls_printf( " failed\n  ! mbedtls_rsa_complete returned %d\n\n",
142                         ret );
143         goto exit;
144     }
145 
146     /*
147      * Extract the RSA encrypted value from the text file
148      */
149     if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
150     {
151         mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" );
152         goto exit;
153     }
154 
155     i = 0;
156 
157     while( fscanf( f, "%02X", (unsigned int*) &c ) > 0 &&
158            i < (int) sizeof( buf ) )
159         buf[i++] = (unsigned char) c;
160 
161     fclose( f );
162 
163     if( i != rsa.MBEDTLS_PRIVATE(len) )
164     {
165         mbedtls_printf( "\n  ! Invalid RSA signature format\n\n" );
166         goto exit;
167     }
168 
169     /*
170      * Decrypt the encrypted RSA data and print the result.
171      */
172     mbedtls_printf( "\n  . Decrypting the encrypted data" );
173     fflush( stdout );
174 
175     ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
176                                             &ctr_drbg, &i,
177                                             buf, result, 1024 );
178     if( ret != 0 )
179     {
180         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
181                         ret );
182         goto exit;
183     }
184 
185     mbedtls_printf( "\n  . OK\n\n" );
186 
187     mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
188 
189     exit_code = MBEDTLS_EXIT_SUCCESS;
190 
191 exit:
192     mbedtls_ctr_drbg_free( &ctr_drbg );
193     mbedtls_entropy_free( &entropy );
194     mbedtls_rsa_free( &rsa );
195     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
196     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
197     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
198 
199 #if defined(_WIN32)
200     mbedtls_printf( "  + Press Enter to exit this program.\n" );
201     fflush( stdout ); getchar();
202 #endif
203 
204     mbedtls_exit( exit_code );
205 }
206 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
207