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