blob: ce2e51789b29c576500c17a979d1652c725fdc41 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature creation program
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker5121ce52009-01-03 21:22:43 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#ifndef _CRT_SECURE_NO_DEPRECATE
23#define _CRT_SECURE_NO_DEPRECATE 1
24#endif
25
26#include <string.h>
27#include <stdio.h>
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/rsa.h"
30#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32int main( int argc, char *argv[] )
33{
34 FILE *f;
35 int ret, i;
36 rsa_context rsa;
37 unsigned char hash[20];
38 unsigned char buf[512];
39
40 ret = 1;
41
42 if( argc != 2 )
43 {
44 printf( "usage: rsa_sign <filename>\n" );
45
46#ifdef WIN32
47 printf( "\n" );
48#endif
49
50 goto exit;
51 }
52
53 printf( "\n . Reading private key from rsa_priv.txt" );
54 fflush( stdout );
55
56 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
57 {
58 ret = 1;
59 printf( " failed\n ! Could not open rsa_priv.txt\n" \
60 " ! Please run rsa_genkey first\n\n" );
61 goto exit;
62 }
63
64 rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
65
66 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
67 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
68 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
69 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
70 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
71 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
72 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
73 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
74 {
75 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
76 goto exit;
77 }
78
79 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
80
81 fclose( f );
82
83 /*
84 * Compute the SHA-1 hash of the input file,
85 * then calculate the RSA signature of the hash.
86 */
87 printf( "\n . Generating the RSA/SHA-1 signature" );
88 fflush( stdout );
89
90 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
91 {
92 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
93 goto exit;
94 }
95
Paul Bakker4593aea2009-02-09 22:32:35 +000096 if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, SIG_RSA_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +000097 20, hash, buf ) ) != 0 )
98 {
99 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
100 goto exit;
101 }
102
103 /*
104 * Write the signature into <filename>-sig.txt
105 */
106 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
107
108 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
109 {
110 ret = 1;
111 printf( " failed\n ! Could not create %s\n\n", argv[1] );
112 goto exit;
113 }
114
115 for( i = 0; i < rsa.len; i++ )
116 fprintf( f, "%02X%s", buf[i],
117 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
118
119 fclose( f );
120
121 printf( "\n . Done (created \"%s\")\n\n", argv[1] );
122
123exit:
124
125#ifdef WIN32
126 printf( " + Press Enter to exit this program.\n" );
127 fflush( stdout ); getchar();
128#endif
129
130 return( ret );
131}