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