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

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

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