blob: 9665dcfbd385fcba6d435960c15674b52392f356 [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
2 * RSASSA-PSS/SHA-1 signature creation program
3 *
4 * Copyright (C) 2006-2010, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * 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
33#include "polarssl/havege.h"
34#include "polarssl/md.h"
35#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
37#include "polarssl/x509.h"
38
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000039#if defined _MSC_VER && !defined snprintf
40#define snprintf _snprintf
41#endif
42
Paul Bakker2291f6c2011-03-25 14:07:53 +000043int main( int argc, char *argv[] )
44{
45 FILE *f;
46 int ret;
47 rsa_context rsa;
48 havege_state hs;
49 unsigned char hash[20];
50 unsigned char buf[512];
51 char filename[512];
52
53 ret = 1;
54
55 if( argc != 3 )
56 {
57 printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
58
59#ifdef WIN32
60 printf( "\n" );
61#endif
62
63 goto exit;
64 }
65
66 printf( "\n . Reading private key from '%s'", argv[1] );
67 fflush( stdout );
68
69 havege_init( &hs );
70 rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
71
72 if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 )
73 {
74 ret = 1;
75 printf( " failed\n ! Could not open '%s'\n", argv[1] );
76 goto exit;
77 }
78
79 /*
80 * Compute the SHA-1 hash of the input file,
81 * then calculate the RSA signature of the hash.
82 */
83 printf( "\n . Generating the RSA/SHA-1 signature" );
84 fflush( stdout );
85
86 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
87 {
88 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
89 goto exit;
90 }
91
92 if( ( ret = rsa_pkcs1_sign( &rsa, havege_rand, &hs, RSA_PRIVATE, SIG_RSA_SHA1,
93 20, hash, buf ) ) != 0 )
94 {
95 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
96 goto exit;
97 }
98
99 /*
100 * Write the signature into <filename>-sig.txt
101 */
102 snprintf( filename, 512, "%s.sig", argv[2] );
103
104 if( ( f = fopen( filename, "wb+" ) ) == NULL )
105 {
106 ret = 1;
107 printf( " failed\n ! Could not create %s\n\n", filename );
108 goto exit;
109 }
110
111 if( fwrite( buf, 1, rsa.len, f ) != (size_t) rsa.len )
112 {
113 printf( "failed\n ! fwrite failed\n\n" );
114 goto exit;
115 }
116
117 fclose( f );
118
119 printf( "\n . Done (created \"%s\")\n\n", filename );
120
121exit:
122
123#ifdef WIN32
124 printf( " + Press Enter to exit this program.\n" );
125 fflush( stdout ); getchar();
126#endif
127
128 return( ret );
129}