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

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

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4