• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  RSA simple decryption 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_printf       printf
59 #define mbedtls_exit         exit
60 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
61 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62 #endif /* MBEDTLS_PLATFORM_C */
63 
64 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
65     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
66     defined(MBEDTLS_CTR_DRBG_C)
67 #include "mbedtls/rsa.h"
68 #include "mbedtls/entropy.h"
69 #include "mbedtls/ctr_drbg.h"
70 
71 #include <string.h>
72 
73 #endif
74 
75 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
76     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
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_FS_IO and/or MBEDTLS_ENTROPY_C 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     unsigned c;
94     size_t i;
95     mbedtls_rsa_context rsa;
96     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
97     mbedtls_entropy_context entropy;
98     mbedtls_ctr_drbg_context ctr_drbg;
99     unsigned char result[1024];
100     unsigned char buf[512];
101     const char *pers = "rsa_decrypt";
102     ((void) argv);
103 
104     memset(result, 0, sizeof( result ) );
105 
106     if( argc != 1 )
107     {
108         mbedtls_printf( "usage: rsa_decrypt\n" );
109 
110 #if defined(_WIN32)
111         mbedtls_printf( "\n" );
112 #endif
113 
114         mbedtls_exit( exit_code );
115     }
116 
117     mbedtls_printf( "\n  . Seeding the random number generator..." );
118     fflush( stdout );
119 
120     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
121     mbedtls_ctr_drbg_init( &ctr_drbg );
122     mbedtls_entropy_init( &entropy );
123     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
124     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
125     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
126 
127     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
128                                         &entropy, (const unsigned char *) pers,
129                                         strlen( pers ) );
130     if( ret != 0 )
131     {
132         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
133                         ret );
134         goto exit;
135     }
136 
137     mbedtls_printf( "\n  . Reading private key from rsa_priv.txt" );
138     fflush( stdout );
139 
140     if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
141     {
142         mbedtls_printf( " failed\n  ! Could not open rsa_priv.txt\n" \
143                 "  ! Please run rsa_genkey first\n\n" );
144         goto exit;
145     }
146 
147     if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) )  != 0 ||
148         ( ret = mbedtls_mpi_read_file( &E , 16, f ) )  != 0 ||
149         ( ret = mbedtls_mpi_read_file( &D , 16, f ) )  != 0 ||
150         ( ret = mbedtls_mpi_read_file( &P , 16, f ) )  != 0 ||
151         ( ret = mbedtls_mpi_read_file( &Q , 16, f ) )  != 0 ||
152         ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
153         ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
154         ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
155     {
156         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
157                         ret );
158         fclose( f );
159         goto exit;
160     }
161     fclose( f );
162 
163     if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
164     {
165         mbedtls_printf( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
166                         ret );
167         goto exit;
168     }
169 
170     if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
171     {
172         mbedtls_printf( " failed\n  ! mbedtls_rsa_complete returned %d\n\n",
173                         ret );
174         goto exit;
175     }
176 
177     /*
178      * Extract the RSA encrypted value from the text file
179      */
180     if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
181     {
182         mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" );
183         goto exit;
184     }
185 
186     i = 0;
187 
188     while( fscanf( f, "%02X", &c ) > 0 &&
189            i < (int) sizeof( buf ) )
190         buf[i++] = (unsigned char) c;
191 
192     fclose( f );
193 
194     if( i != rsa.len )
195     {
196         mbedtls_printf( "\n  ! Invalid RSA signature format\n\n" );
197         goto exit;
198     }
199 
200     /*
201      * Decrypt the encrypted RSA data and print the result.
202      */
203     mbedtls_printf( "\n  . Decrypting the encrypted data" );
204     fflush( stdout );
205 
206     ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
207                                             &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
208                                             buf, result, 1024 );
209     if( ret != 0 )
210     {
211         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
212                         ret );
213         goto exit;
214     }
215 
216     mbedtls_printf( "\n  . OK\n\n" );
217 
218     mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
219 
220     exit_code = MBEDTLS_EXIT_SUCCESS;
221 
222 exit:
223     mbedtls_ctr_drbg_free( &ctr_drbg );
224     mbedtls_entropy_free( &entropy );
225     mbedtls_rsa_free( &rsa );
226     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
227     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
228     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
229 
230 #if defined(_WIN32)
231     mbedtls_printf( "  + Press Enter to exit this program.\n" );
232     fflush( stdout ); getchar();
233 #endif
234 
235     mbedtls_exit( exit_code );
236 }
237 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
238