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