• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Public key-based 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_PK_PARSE_C) && \
65     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
66     defined(MBEDTLS_CTR_DRBG_C)
67 #include "mbedtls/error.h"
68 #include "mbedtls/pk.h"
69 #include "mbedtls/entropy.h"
70 #include "mbedtls/ctr_drbg.h"
71 
72 #include <stdio.h>
73 #include <string.h>
74 #endif
75 
76 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) ||  \
77     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
78     !defined(MBEDTLS_CTR_DRBG_C)
main(void)79 int main( void )
80 {
81     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
82            "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
83            "MBEDTLS_CTR_DRBG_C not defined.\n");
84     mbedtls_exit( 0 );
85 }
86 #else
87 
88 
main(int argc,char * argv[])89 int main( int argc, char *argv[] )
90 {
91     FILE *f;
92     int ret = 1;
93     unsigned c;
94     int exit_code = MBEDTLS_EXIT_FAILURE;
95     size_t i, olen = 0;
96     mbedtls_pk_context pk;
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 = "mbedtls_pk_decrypt";
102     ((void) argv);
103 
104     mbedtls_pk_init( &pk );
105     mbedtls_entropy_init( &entropy );
106     mbedtls_ctr_drbg_init( &ctr_drbg );
107 
108     memset(result, 0, sizeof( result ) );
109 
110     if( argc != 2 )
111     {
112         mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
113 
114 #if defined(_WIN32)
115         mbedtls_printf( "\n" );
116 #endif
117 
118         goto exit;
119     }
120 
121     mbedtls_printf( "\n  . Seeding the random number generator..." );
122     fflush( stdout );
123 
124     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
125                                        &entropy, (const unsigned char *) pers,
126                                        strlen( pers ) ) ) != 0 )
127     {
128         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
129                         -ret );
130         goto exit;
131     }
132 
133     mbedtls_printf( "\n  . Reading private key from '%s'", argv[1] );
134     fflush( stdout );
135 
136     if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
137     {
138         mbedtls_printf( " failed\n  ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
139         goto exit;
140     }
141 
142     /*
143      * Extract the RSA encrypted value from the text file
144      */
145     if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
146     {
147         mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" );
148         ret = 1;
149         goto exit;
150     }
151 
152     i = 0;
153     while( fscanf( f, "%02X", &c ) > 0 &&
154            i < (int) sizeof( buf ) )
155     {
156         buf[i++] = (unsigned char) c;
157     }
158 
159     fclose( f );
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     if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
168                             mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
169     {
170         mbedtls_printf( " failed\n  ! mbedtls_pk_decrypt returned -0x%04x\n",
171                         -ret );
172         goto exit;
173     }
174 
175     mbedtls_printf( "\n  . OK\n\n" );
176 
177     mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
178 
179     exit_code = MBEDTLS_EXIT_SUCCESS;
180 
181 exit:
182 
183     mbedtls_pk_free( &pk );
184     mbedtls_entropy_free( &entropy );
185     mbedtls_ctr_drbg_free( &ctr_drbg );
186 
187 #if defined(MBEDTLS_ERROR_C)
188     if( exit_code != MBEDTLS_EXIT_SUCCESS )
189     {
190         mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
191         mbedtls_printf( "  !  Last error was: %s\n", buf );
192     }
193 #endif
194 
195 #if defined(_WIN32)
196     mbedtls_printf( "  + Press Enter to exit this program.\n" );
197     fflush( stdout ); getchar();
198 #endif
199 
200     mbedtls_exit( exit_code );
201 }
202 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
203           MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
204