blob: ee26694e9562abf4b56e809ac499baf913a55e04 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature verification program
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifndef _CRT_SECURE_NO_DEPRECATE
25#define _CRT_SECURE_NO_DEPRECATE 1
26#endif
27
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
34int main( int argc, char *argv[] )
35{
36 FILE *f;
37 int ret, i, c;
38 rsa_context rsa;
39 unsigned char hash[20];
40 unsigned char buf[512];
41
42 ret = 1;
43 if( argc != 2 )
44 {
45 printf( "usage: rsa_verify <filename>\n" );
46
47#ifdef WIN32
48 printf( "\n" );
49#endif
50
51 goto exit;
52 }
53
54 printf( "\n . Reading public key from rsa_pub.txt" );
55 fflush( stdout );
56
57 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
58 {
59 printf( " failed\n ! Could not open rsa_pub.txt\n" \
60 " ! Please run rsa_genkey first\n\n" );
61 goto exit;
62 }
63
64 rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
65
66 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
67 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
68 {
69 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
70 goto exit;
71 }
72
73 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
74
75 fclose( f );
76
77 /*
78 * Extract the RSA signature from the text file
79 */
80 ret = 1;
81 i = strlen( argv[1] );
82 memcpy( argv[1] + i, ".sig", 5 );
83
84 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
85 {
86 printf( "\n ! Could not open %s\n\n", argv[1] );
87 goto exit;
88 }
89
90 argv[1][i] = '\0', i = 0;
91
92 while( fscanf( f, "%02X", &c ) > 0 &&
93 i < (int) sizeof( buf ) )
94 buf[i++] = (unsigned char) c;
95
96 fclose( f );
97
98 if( i != rsa.len )
99 {
100 printf( "\n ! Invalid RSA signature format\n\n" );
101 goto exit;
102 }
103
104 /*
105 * Compute the SHA-1 hash of the input file and compare
106 * it with the hash decrypted from the RSA signature.
107 */
108 printf( "\n . Verifying the RSA/SHA-1 signature" );
109 fflush( stdout );
110
111 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
112 {
113 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
114 goto exit;
115 }
116
Paul Bakker4593aea2009-02-09 22:32:35 +0000117 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 20, hash, buf ) ) != 0 )
119 {
120 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
121 goto exit;
122 }
123
124 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
125
126 ret = 0;
127
128exit:
129
130#ifdef WIN32
131 printf( " + Press Enter to exit this program.\n" );
132 fflush( stdout ); getchar();
133#endif
134
135 return( ret );
136}