/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/pkey/rsa_sign.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 creation 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;
00038     rsa_context rsa;
00039     unsigned char hash[20];
00040     unsigned char buf[512];
00041 
00042     ret = 1;
00043 
00044     if( argc != 2 )
00045     {
00046         printf( "usage: rsa_sign <filename>\n" );
00047 
00048 #ifdef WIN32
00049         printf( "\n" );
00050 #endif
00051 
00052         goto exit;
00053     }
00054 
00055     printf( "\n  . Reading private key from rsa_priv.txt" );
00056     fflush( stdout );
00057 
00058     if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
00059     {
00060         ret = 1;
00061         printf( " failed\n  ! Could not open rsa_priv.txt\n" \
00062                 "  ! Please run rsa_genkey first\n\n" );
00063         goto exit;
00064     }
00065 
00066     if( ( ret = rsa_read_private( &rsa, f ) ) != 0 )
00067     {
00068         printf( " failed\n  ! rsa_read_private returned %08x\n\n", ret );
00069         goto exit;
00070     }
00071 
00072     fclose( f );
00073 
00074     /*
00075      * Compute the SHA-1 hash of the input file,
00076      * then calculate the RSA signature of the hash.
00077      */
00078     printf( "\n  . Generating the RSA/SHA-1 signature" );
00079     fflush( stdout );
00080 
00081     if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
00082     {
00083         printf( " failed\n  ! Could not open or read %s\n\n", argv[1] );
00084         goto exit;
00085     }
00086 
00087     if( ( ret = rsa_pkcs1_sign( &rsa, RSA_SHA1, hash, 20,
00088                                 buf, rsa.len ) ) != 0 )
00089     {
00090         printf( " failed\n  ! rsa_pkcs1_sign returned %08x\n\n", ret );
00091         goto exit;
00092     }
00093 
00094     /*
00095      * Write the signature into <filename>-sig.txt
00096      */
00097     memcpy( argv[1] + strlen( argv[1] ), "-sig.txt", 9 );
00098 
00099     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
00100     {
00101         ret = 1;
00102         printf( " failed\n  ! Could not create %s\n\n", argv[1] );
00103         goto exit;
00104     }
00105 
00106     for( i = 0; i < rsa.len; i++ )
00107         fprintf( f, "%02X%s", buf[i],
00108                  ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
00109 
00110     fclose( f );
00111 
00112     printf( "\n  . Done (created \"%s\")\n\n", argv[1] );
00113 
00114 exit:
00115 
00116 #ifdef WIN32
00117     printf( "  + Press Enter to exit this program.\n" );
00118     fflush( stdout ); getchar();
00119 #endif
00120 
00121     return( ret );
00122 }

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