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