blob: dbe5f84e21541bb6d3fa6692342838ed32c8ae4f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature verification program
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker5690efc2011-05-26 13:16:06 +000038#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
39 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
40int main( void )
41{
42 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
43 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
44 return( 0 );
45}
46#else
Paul Bakker5121ce52009-01-03 21:22:43 +000047int main( int argc, char *argv[] )
48{
49 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000050 int ret, c;
51 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000052 rsa_context rsa;
53 unsigned char hash[20];
54 unsigned char buf[512];
55
56 ret = 1;
57 if( argc != 2 )
58 {
59 printf( "usage: rsa_verify <filename>\n" );
60
61#ifdef WIN32
62 printf( "\n" );
63#endif
64
65 goto exit;
66 }
67
68 printf( "\n . Reading public key from rsa_pub.txt" );
69 fflush( stdout );
70
71 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
72 {
73 printf( " failed\n ! Could not open rsa_pub.txt\n" \
74 " ! Please run rsa_genkey first\n\n" );
75 goto exit;
76 }
77
Paul Bakkera802e1a2010-08-16 11:56:45 +000078 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
81 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
82 {
83 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
84 goto exit;
85 }
86
87 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
88
89 fclose( f );
90
91 /*
92 * Extract the RSA signature from the text file
93 */
94 ret = 1;
95 i = strlen( argv[1] );
96 memcpy( argv[1] + i, ".sig", 5 );
97
98 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
99 {
100 printf( "\n ! Could not open %s\n\n", argv[1] );
101 goto exit;
102 }
103
104 argv[1][i] = '\0', i = 0;
105
106 while( fscanf( f, "%02X", &c ) > 0 &&
107 i < (int) sizeof( buf ) )
108 buf[i++] = (unsigned char) c;
109
110 fclose( f );
111
112 if( i != rsa.len )
113 {
114 printf( "\n ! Invalid RSA signature format\n\n" );
115 goto exit;
116 }
117
118 /*
119 * Compute the SHA-1 hash of the input file and compare
120 * it with the hash decrypted from the RSA signature.
121 */
122 printf( "\n . Verifying the RSA/SHA-1 signature" );
123 fflush( stdout );
124
125 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
126 {
127 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
128 goto exit;
129 }
130
Paul Bakker4593aea2009-02-09 22:32:35 +0000131 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 20, hash, buf ) ) != 0 )
133 {
134 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
135 goto exit;
136 }
137
138 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
139
140 ret = 0;
141
142exit:
143
144#ifdef WIN32
145 printf( " + Press Enter to exit this program.\n" );
146 fflush( stdout ); getchar();
147#endif
148
149 return( ret );
150}
Paul Bakker5690efc2011-05-26 13:16:06 +0000151#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
152 POLARSSL_FS_IO */