1 /*
2 * RSA/SHA-256 signature verification program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf printf
59 #define mbedtls_snprintf snprintf
60 #define mbedtls_exit exit
61 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
62 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63 #endif /* MBEDTLS_PLATFORM_C */
64
65 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
66 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
67 !defined(MBEDTLS_FS_IO)
main(void)68 int main( void )
69 {
70 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
71 "MBEDTLS_MD_C and/or "
72 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
73 mbedtls_exit( 0 );
74 }
75 #else
76
77 #include "mbedtls/rsa.h"
78 #include "mbedtls/md.h"
79
80 #include <stdio.h>
81 #include <string.h>
82
83
main(int argc,char * argv[])84 int main( int argc, char *argv[] )
85 {
86 FILE *f;
87 int ret = 1;
88 unsigned c;
89 int exit_code = MBEDTLS_EXIT_FAILURE;
90 size_t i;
91 mbedtls_rsa_context rsa;
92 unsigned char hash[32];
93 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
94 char filename[512];
95
96 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
97
98 if( argc != 2 )
99 {
100 mbedtls_printf( "usage: rsa_verify <filename>\n" );
101
102 #if defined(_WIN32)
103 mbedtls_printf( "\n" );
104 #endif
105
106 goto exit;
107 }
108
109 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
110 fflush( stdout );
111
112 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
113 {
114 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
115 " ! Please run rsa_genkey first\n\n" );
116 goto exit;
117 }
118
119 if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
120 ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
121 {
122 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
123 fclose( f );
124 goto exit;
125 }
126
127 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
128
129 fclose( f );
130
131 /*
132 * Extract the RSA signature from the text file
133 */
134 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
135
136 if( ( f = fopen( filename, "rb" ) ) == NULL )
137 {
138 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
139 goto exit;
140 }
141
142 i = 0;
143 while( fscanf( f, "%02X", &c ) > 0 &&
144 i < (int) sizeof( buf ) )
145 buf[i++] = (unsigned char) c;
146
147 fclose( f );
148
149 if( i != rsa.len )
150 {
151 mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
152 goto exit;
153 }
154
155 /*
156 * Compute the SHA-256 hash of the input file and
157 * verify the signature
158 */
159 mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
160 fflush( stdout );
161
162 if( ( ret = mbedtls_md_file(
163 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
164 argv[1], hash ) ) != 0 )
165 {
166 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
167 goto exit;
168 }
169
170 if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
171 MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
172 {
173 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
174 goto exit;
175 }
176
177 mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
178
179 exit_code = MBEDTLS_EXIT_SUCCESS;
180
181 exit:
182
183 mbedtls_rsa_free( &rsa );
184
185 #if defined(_WIN32)
186 mbedtls_printf( " + Press Enter to exit this program.\n" );
187 fflush( stdout ); getchar();
188 #endif
189
190 mbedtls_exit( exit_code );
191 }
192 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
193 MBEDTLS_FS_IO */
194