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