00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _CRT_SECURE_NO_DEPRECATE
00022 #define _CRT_SECURE_NO_DEPRECATE 1
00023 #endif
00024
00025 #include <string.h>
00026 #include <stdio.h>
00027
00028 #include "xyssl/rsa.h"
00029 #include "xyssl/sha1.h"
00030
00031 int main( int argc, char *argv[] )
00032 {
00033 FILE *f;
00034 int ret, i, c;
00035 rsa_context rsa;
00036 unsigned char hash[20];
00037 unsigned char buf[512];
00038
00039 ret = 1;
00040 if( argc != 2 )
00041 {
00042 printf( "usage: rsa_verify <filename>\n" );
00043
00044 #ifdef WIN32
00045 printf( "\n" );
00046 #endif
00047
00048 goto exit;
00049 }
00050
00051 printf( "\n . Reading public key from rsa_pub.txt" );
00052 fflush( stdout );
00053
00054 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
00055 {
00056 printf( " failed\n ! Could not open rsa_pub.txt\n" \
00057 " ! Please run rsa_genkey first\n\n" );
00058 goto exit;
00059 }
00060
00061 if( ( ret = rsa_read_public( &rsa, f ) ) != 0 )
00062 {
00063 printf( " failed\n ! rsa_read_public returned %08x\n\n", ret );
00064 goto exit;
00065 }
00066
00067 fclose( f );
00068
00069
00070
00071
00072 ret = 1;
00073 i = strlen( argv[1] );
00074 memcpy( argv[1] + i, "-sig.txt", 9 );
00075
00076 if( ( f = fopen( argv[1], "rb" ) ) == NULL )
00077 {
00078 printf( "\n ! Could not open %s\n\n", argv[1] );
00079 goto exit;
00080 }
00081
00082 argv[1][i] = '\0', i = 0;
00083
00084 while( fscanf( f, "%02X", &c ) > 0 &&
00085 i < (int) sizeof( buf ) )
00086 buf[i++] = c;
00087
00088 fclose( f );
00089
00090 if( i != rsa.len )
00091 {
00092 printf( "\n ! Invalid RSA signature format\n\n" );
00093 goto exit;
00094 }
00095
00096
00097
00098
00099
00100 printf( "\n . Verifying the RSA/SHA-1 signature" );
00101 fflush( stdout );
00102
00103 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
00104 {
00105 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
00106 goto exit;
00107 }
00108
00109 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_SHA1, hash, 20,
00110 buf, rsa.len ) ) != 0 )
00111 {
00112 printf( " failed\n ! rsa_pkcs1_verify returned %08x\n\n", ret );
00113 goto exit;
00114 }
00115
00116 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
00117
00118 ret = 0;
00119
00120 exit:
00121
00122 #ifdef WIN32
00123 printf( " + Press Enter to exit this program.\n" );
00124 fflush( stdout ); getchar();
00125 #endif
00126
00127 return( ret );
00128 }