1 /*
2  *  Public key-based 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_PK_PARSE_C) && \
34     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
35     defined(MBEDTLS_CTR_DRBG_C)
36 #include "mbedtls/error.h"
37 #include "mbedtls/pk.h"
38 #include "mbedtls/entropy.h"
39 #include "mbedtls/ctr_drbg.h"
40 
41 #include <stdio.h>
42 #include <string.h>
43 #endif
44 
45 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) ||  \
46     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
47     !defined(MBEDTLS_CTR_DRBG_C)
main(void)48 int main( void )
49 {
50     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
51            "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
52            "MBEDTLS_CTR_DRBG_C not defined.\n");
53     mbedtls_exit( 0 );
54 }
55 #else
56 
57 
main(int argc,char * argv[])58 int main( int argc, char *argv[] )
59 {
60     FILE *f;
61     int ret = 1;
62     unsigned c;
63     int exit_code = MBEDTLS_EXIT_FAILURE;
64     size_t i, olen = 0;
65     mbedtls_pk_context pk;
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 = "mbedtls_pk_decrypt";
71     ((void) argv);
72 
73     mbedtls_pk_init( &pk );
74     mbedtls_entropy_init( &entropy );
75     mbedtls_ctr_drbg_init( &ctr_drbg );
76 
77     memset(result, 0, sizeof( result ) );
78 
79     if( argc != 2 )
80     {
81         mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
82 
83 #if defined(_WIN32)
84         mbedtls_printf( "\n" );
85 #endif
86 
87         goto exit;
88     }
89 
90     mbedtls_printf( "\n  . Seeding the random number generator..." );
91     fflush( stdout );
92 
93     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
94                                        &entropy, (const unsigned char *) pers,
95                                        strlen( pers ) ) ) != 0 )
96     {
97         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
98                         (unsigned int) -ret );
99         goto exit;
100     }
101 
102     mbedtls_printf( "\n  . Reading private key from '%s'", argv[1] );
103     fflush( stdout );
104 
105     if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "",
106                     mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
107     {
108         mbedtls_printf( " failed\n  ! mbedtls_pk_parse_keyfile returned -0x%04x\n", (unsigned int) -ret );
109         goto exit;
110     }
111 
112     /*
113      * Extract the RSA encrypted value from the text file
114      */
115     if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
116     {
117         mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" );
118         ret = 1;
119         goto exit;
120     }
121 
122     i = 0;
123     while( fscanf( f, "%02X", (unsigned int*) &c ) > 0 &&
124            i < (int) sizeof( buf ) )
125     {
126         buf[i++] = (unsigned char) c;
127     }
128 
129     fclose( f );
130 
131     /*
132      * Decrypt the encrypted RSA data and print the result.
133      */
134     mbedtls_printf( "\n  . Decrypting the encrypted data" );
135     fflush( stdout );
136 
137     if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
138                             mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
139     {
140         mbedtls_printf( " failed\n  ! mbedtls_pk_decrypt returned -0x%04x\n",
141                         (unsigned int) -ret );
142         goto exit;
143     }
144 
145     mbedtls_printf( "\n  . OK\n\n" );
146 
147     mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
148 
149     exit_code = MBEDTLS_EXIT_SUCCESS;
150 
151 exit:
152 
153     mbedtls_pk_free( &pk );
154     mbedtls_entropy_free( &entropy );
155     mbedtls_ctr_drbg_free( &ctr_drbg );
156 
157 #if defined(MBEDTLS_ERROR_C)
158     if( exit_code != MBEDTLS_EXIT_SUCCESS )
159     {
160         mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
161         mbedtls_printf( "  !  Last error was: %s\n", buf );
162     }
163 #endif
164 
165 #if defined(_WIN32)
166     mbedtls_printf( "  + Press Enter to exit this program.\n" );
167     fflush( stdout ); getchar();
168 #endif
169 
170     mbedtls_exit( exit_code );
171 }
172 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
173           MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
174