blob: ece1588df4851525a9ec7977d1745072f9a38e8d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSA/SHA-256 signature verification program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Manuel Pégourié-Gonnardc728f942015-08-31 12:28:30 +020034#define polarssl_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000038 defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO)
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/rsa.h"
40#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <stdio.h>
43#include <string.h>
44#endif
45
Manuel Pégourié-Gonnardc728f942015-08-31 12:28:30 +020046#if defined _MSC_VER && !defined snprintf
47#define snprintf _snprintf
48#endif
49
Paul Bakker5690efc2011-05-26 13:16:06 +000050#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000051 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000052int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000053{
Rich Evansf90016a2015-01-19 14:26:37 +000054 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000055 "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000056 return( 0 );
57}
58#else
Paul Bakker5121ce52009-01-03 21:22:43 +000059int main( int argc, char *argv[] )
60{
61 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000062 int ret, c;
63 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000064 rsa_context rsa;
Manuel Pégourié-Gonnardee7db9c2015-08-31 11:32:03 +020065 unsigned char hash[32];
Paul Bakker1d569582012-10-03 20:35:44 +000066 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Manuel Pégourié-Gonnard6432c7e2015-08-31 11:30:07 +020067 char filename[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Janos Follath347552d2016-03-17 15:21:39 +000069 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000070 ret = 1;
Janos Follath347552d2016-03-17 15:21:39 +000071
Paul Bakker5121ce52009-01-03 21:22:43 +000072 if( argc != 2 )
73 {
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "usage: rsa_verify <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakkercce9d772011-11-18 14:26:47 +000076#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000077 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000078#endif
79
80 goto exit;
81 }
82
Rich Evansf90016a2015-01-19 14:26:37 +000083 polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 fflush( stdout );
85
86 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
87 {
Rich Evansf90016a2015-01-19 14:26:37 +000088 polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000089 " ! Please run rsa_genkey first\n\n" );
90 goto exit;
91 }
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
95 {
Rich Evansf90016a2015-01-19 14:26:37 +000096 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000097 goto exit;
98 }
99
100 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
101
102 fclose( f );
103
104 /*
105 * Extract the RSA signature from the text file
106 */
107 ret = 1;
Manuel Pégourié-Gonnard8fbb5a32015-08-31 12:38:12 +0200108 polarssl_snprintf( filename, sizeof( filename ), "%s.sig", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Manuel Pégourié-Gonnard6432c7e2015-08-31 11:30:07 +0200110 if( ( f = fopen( filename, "rb" ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 {
Manuel Pégourié-Gonnard6432c7e2015-08-31 11:30:07 +0200112 polarssl_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 goto exit;
114 }
115
Manuel Pégourié-Gonnard6432c7e2015-08-31 11:30:07 +0200116 i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 while( fscanf( f, "%02X", &c ) > 0 &&
118 i < (int) sizeof( buf ) )
119 buf[i++] = (unsigned char) c;
120
121 fclose( f );
122
123 if( i != rsa.len )
124 {
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( "\n ! Invalid RSA signature format\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 goto exit;
127 }
128
129 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000130 * Compute the SHA-256 hash of the input file and compare
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * it with the hash decrypted from the RSA signature.
132 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000133 polarssl_printf( "\n . Verifying the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 fflush( stdout );
135
136 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
137 {
Rich Evansf90016a2015-01-19 14:26:37 +0000138 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 goto exit;
140 }
141
Paul Bakker548957d2013-08-30 10:30:02 +0200142 if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000143 POLARSSL_MD_SHA256, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 {
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_printf( " failed\n ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000149 polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 ret = 0;
152
153exit:
154
Janos Follath347552d2016-03-17 15:21:39 +0000155 rsa_free( &rsa );
156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 fflush( stdout ); getchar();
160#endif
161
162 return( ret );
163}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000164#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000165 POLARSSL_FS_IO */