blob: cc3506d7131003bf75f356d6104591be75a864ef [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature verification program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/rsa.h"
32#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker5690efc2011-05-26 13:16:06 +000034#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
35 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000036int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000037{
Paul Bakkercce9d772011-11-18 14:26:47 +000038 ((void) argc);
39 ((void) argv);
40
Paul Bakker5690efc2011-05-26 13:16:06 +000041 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
42 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
43 return( 0 );
44}
45#else
Paul Bakker5121ce52009-01-03 21:22:43 +000046int main( int argc, char *argv[] )
47{
48 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000049 int ret, c;
50 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000051 rsa_context rsa;
52 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000053 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55 ret = 1;
56 if( argc != 2 )
57 {
58 printf( "usage: rsa_verify <filename>\n" );
59
Paul Bakkercce9d772011-11-18 14:26:47 +000060#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000061 printf( "\n" );
62#endif
63
64 goto exit;
65 }
66
67 printf( "\n . Reading public key from rsa_pub.txt" );
68 fflush( stdout );
69
70 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
71 {
72 printf( " failed\n ! Could not open rsa_pub.txt\n" \
73 " ! Please run rsa_genkey first\n\n" );
74 goto exit;
75 }
76
Paul Bakkera802e1a2010-08-16 11:56:45 +000077 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
80 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
81 {
82 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
83 goto exit;
84 }
85
86 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
87
88 fclose( f );
89
90 /*
91 * Extract the RSA signature from the text file
92 */
93 ret = 1;
94 i = strlen( argv[1] );
95 memcpy( argv[1] + i, ".sig", 5 );
96
97 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
98 {
99 printf( "\n ! Could not open %s\n\n", argv[1] );
100 goto exit;
101 }
102
103 argv[1][i] = '\0', i = 0;
104
105 while( fscanf( f, "%02X", &c ) > 0 &&
106 i < (int) sizeof( buf ) )
107 buf[i++] = (unsigned char) c;
108
109 fclose( f );
110
111 if( i != rsa.len )
112 {
113 printf( "\n ! Invalid RSA signature format\n\n" );
114 goto exit;
115 }
116
117 /*
118 * Compute the SHA-1 hash of the input file and compare
119 * it with the hash decrypted from the RSA signature.
120 */
121 printf( "\n . Verifying the RSA/SHA-1 signature" );
122 fflush( stdout );
123
124 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
125 {
126 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
127 goto exit;
128 }
129
Paul Bakker548957d2013-08-30 10:30:02 +0200130 if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
131 POLARSSL_MD_SHA1, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 {
Paul Bakker5ef9db22012-09-27 13:19:22 +0000133 printf( " failed\n ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
136
137 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
138
139 ret = 0;
140
141exit:
142
Paul Bakkercce9d772011-11-18 14:26:47 +0000143#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 printf( " + Press Enter to exit this program.\n" );
145 fflush( stdout ); getchar();
146#endif
147
148 return( ret );
149}
Paul Bakker5690efc2011-05-26 13:16:06 +0000150#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
151 POLARSSL_FS_IO */