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