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