/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/pkey/rsa_verify.c

Go to the documentation of this file.
00001 /*
00002  *  RSA/SHA-1 signature verification program
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License, version 2.1 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *  MA  02110-1301  USA
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      * Extract the RSA signature from the text file
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      * Compute the SHA-1 hash of the input file and compare
00098      * it with the hash decrypted from the RSA signature.
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 }

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4