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