Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / programs / pkey / rsa_verify.c
1 /*
2  *  RSA/SHA-256 signature verification 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_snprintf        snprintf
35 #define mbedtls_exit            exit
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39
40 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) ||  \
41     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
42     !defined(MBEDTLS_FS_IO)
43 int main( void )
44 {
45     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
46             "MBEDTLS_MD_C and/or "
47             "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
48     return( 0 );
49 }
50 #else
51
52 #include "mbedtls/rsa.h"
53 #include "mbedtls/md.h"
54
55 #include <stdio.h>
56 #include <string.h>
57
58 #if defined(MBEDTLS_CHECK_PARAMS)
59 #include "mbedtls/platform_util.h"
60 void mbedtls_param_failed( const char *failure_condition,
61                            const char *file,
62                            int line )
63 {
64     mbedtls_printf( "%s:%i: Input param failed - %s\n",
65                     file, line, failure_condition );
66     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
67 }
68 #endif
69
70 int main( int argc, char *argv[] )
71 {
72     FILE *f;
73     int ret = 1, c;
74     int exit_code = MBEDTLS_EXIT_FAILURE;
75     size_t i;
76     mbedtls_rsa_context rsa;
77     unsigned char hash[32];
78     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
79     char filename[512];
80
81     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
82
83     if( argc != 2 )
84     {
85         mbedtls_printf( "usage: rsa_verify <filename>\n" );
86
87 #if defined(_WIN32)
88         mbedtls_printf( "\n" );
89 #endif
90
91         goto exit;
92     }
93
94     mbedtls_printf( "\n  . Reading public key from rsa_pub.txt" );
95     fflush( stdout );
96
97     if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
98     {
99         mbedtls_printf( " failed\n  ! Could not open rsa_pub.txt\n" \
100                 "  ! Please run rsa_genkey first\n\n" );
101         goto exit;
102     }
103
104     if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
105         ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
106     {
107         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_file returned %d\n\n", ret );
108         fclose( f );
109         goto exit;
110     }
111
112     rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
113
114     fclose( f );
115
116     /*
117      * Extract the RSA signature from the text file
118      */
119     mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
120
121     if( ( f = fopen( filename, "rb" ) ) == NULL )
122     {
123         mbedtls_printf( "\n  ! Could not open %s\n\n", filename );
124         goto exit;
125     }
126
127     i = 0;
128     while( fscanf( f, "%02X", &c ) > 0 &&
129            i < (int) sizeof( buf ) )
130         buf[i++] = (unsigned char) c;
131
132     fclose( f );
133
134     if( i != rsa.len )
135     {
136         mbedtls_printf( "\n  ! Invalid RSA signature format\n\n" );
137         goto exit;
138     }
139
140     /*
141      * Compute the SHA-256 hash of the input file and
142      * verify the signature
143      */
144     mbedtls_printf( "\n  . Verifying the RSA/SHA-256 signature" );
145     fflush( stdout );
146
147     if( ( ret = mbedtls_md_file(
148                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
149                     argv[1], hash ) ) != 0 )
150     {
151         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[1] );
152         goto exit;
153     }
154
155     if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
156                                   MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
157     {
158         mbedtls_printf( " failed\n  ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
159         goto exit;
160     }
161
162     mbedtls_printf( "\n  . OK (the signature is valid)\n\n" );
163
164     exit_code = MBEDTLS_EXIT_SUCCESS;
165
166 exit:
167
168     mbedtls_rsa_free( &rsa );
169
170 #if defined(_WIN32)
171     mbedtls_printf( "  + Press Enter to exit this program.\n" );
172     fflush( stdout ); getchar();
173 #endif
174
175     return( exit_code );
176 }
177 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
178           MBEDTLS_FS_IO */