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