Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / programs / pkey / rsa_decrypt.c
1 /*
2  *  RSA 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_RSA_C) && \
40     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
41     defined(MBEDTLS_CTR_DRBG_C)
42 #include "mbedtls/rsa.h"
43 #include "mbedtls/entropy.h"
44 #include "mbedtls/ctr_drbg.h"
45
46 #include <string.h>
47
48 #endif
49
50 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
51     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
52     !defined(MBEDTLS_CTR_DRBG_C)
53 int main( void )
54 {
55     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
56            "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
57            "MBEDTLS_CTR_DRBG_C not defined.\n");
58     return( 0 );
59 }
60 #else
61
62 #if defined(MBEDTLS_CHECK_PARAMS)
63 #include "mbedtls/platform_util.h"
64 void mbedtls_param_failed( const char *failure_condition,
65                            const char *file,
66                            int line )
67 {
68     mbedtls_printf( "%s:%i: Input param failed - %s\n",
69                     file, line, failure_condition );
70     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
71 }
72 #endif
73
74 int main( int argc, char *argv[] )
75 {
76     FILE *f;
77     int ret = 1;
78     int exit_code = MBEDTLS_EXIT_FAILURE;
79     int c;
80     size_t i;
81     mbedtls_rsa_context rsa;
82     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
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 = "rsa_decrypt";
88     ((void) argv);
89
90     memset(result, 0, sizeof( result ) );
91
92     if( argc != 1 )
93     {
94         mbedtls_printf( "usage: rsa_decrypt\n" );
95
96 #if defined(_WIN32)
97         mbedtls_printf( "\n" );
98 #endif
99
100         mbedtls_exit( exit_code );
101     }
102
103     mbedtls_printf( "\n  . Seeding the random number generator..." );
104     fflush( stdout );
105
106     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
107     mbedtls_ctr_drbg_init( &ctr_drbg );
108     mbedtls_entropy_init( &entropy );
109     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
110     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
111     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
112
113     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
114                                         &entropy, (const unsigned char *) pers,
115                                         strlen( pers ) );
116     if( ret != 0 )
117     {
118         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n",
119                         ret );
120         goto exit;
121     }
122
123     mbedtls_printf( "\n  . Reading private key from rsa_priv.txt" );
124     fflush( stdout );
125
126     if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
127     {
128         mbedtls_printf( " failed\n  ! Could not open rsa_priv.txt\n" \
129                 "  ! Please run rsa_genkey first\n\n" );
130         goto exit;
131     }
132
133     if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) )  != 0 ||
134         ( ret = mbedtls_mpi_read_file( &E , 16, f ) )  != 0 ||
135         ( ret = mbedtls_mpi_read_file( &D , 16, f ) )  != 0 ||
136         ( ret = mbedtls_mpi_read_file( &P , 16, f ) )  != 0 ||
137         ( ret = mbedtls_mpi_read_file( &Q , 16, f ) )  != 0 ||
138         ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
139         ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
140         ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
141     {
142         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n",
143                         ret );
144         fclose( f );
145         goto exit;
146     }
147     fclose( f );
148
149     if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
150     {
151         mbedtls_printf( " failed\n  ! mbedtls_rsa_import returned %d\n\n",
152                         ret );
153         goto exit;
154     }
155
156     if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
157     {
158         mbedtls_printf( " failed\n  ! mbedtls_rsa_complete returned %d\n\n",
159                         ret );
160         goto exit;
161     }
162
163     /*
164      * Extract the RSA encrypted value from the text file
165      */
166     if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
167     {
168         mbedtls_printf( "\n  ! Could not open %s\n\n", "result-enc.txt" );
169         goto exit;
170     }
171
172     i = 0;
173
174     while( fscanf( f, "%02X", &c ) > 0 &&
175            i < (int) sizeof( buf ) )
176         buf[i++] = (unsigned char) c;
177
178     fclose( f );
179
180     if( i != rsa.len )
181     {
182         mbedtls_printf( "\n  ! Invalid RSA signature format\n\n" );
183         goto exit;
184     }
185
186     /*
187      * Decrypt the encrypted RSA data and print the result.
188      */
189     mbedtls_printf( "\n  . Decrypting the encrypted data" );
190     fflush( stdout );
191
192     ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
193                                             &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
194                                             buf, result, 1024 );
195     if( ret != 0 )
196     {
197         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
198                         ret );
199         goto exit;
200     }
201
202     mbedtls_printf( "\n  . OK\n\n" );
203
204     mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
205
206     exit_code = MBEDTLS_EXIT_SUCCESS;
207
208 exit:
209     mbedtls_ctr_drbg_free( &ctr_drbg );
210     mbedtls_entropy_free( &entropy );
211     mbedtls_rsa_free( &rsa );
212     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
213     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
214     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
215
216 #if defined(_WIN32)
217     mbedtls_printf( "  + Press Enter to exit this program.\n" );
218     fflush( stdout ); getchar();
219 #endif
220
221     return( exit_code );
222 }
223 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */