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