blob: 61a4868877cac28e60d5d34f9954c8f499d1e818 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature creation program
3 *
4 * Copyright (C) 2006-2013, 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/config.h"
34
35#include "polarssl/error.h"
36#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
38#include "polarssl/md.h"
39#include "polarssl/pk.h"
40#include "polarssl/sha1.h"
41
42#if defined _MSC_VER && !defined snprintf
43#define snprintf _snprintf
44#endif
45
46#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
47 !defined(POLARSSL_SHA1_C) || \
48 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \
49 !defined(POLARSSL_CTR_DRBG_C)
50int main( int argc, char *argv[] )
51{
52 ((void) argc);
53 ((void) argv);
54
55 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
56 "POLARSSL_SHA1_C and/or "
57 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO and/or "
58 "POLARSSL_CTR_DRBG_C not defined.\n");
59 return( 0 );
60}
61#else
62int main( int argc, char *argv[] )
63{
64 FILE *f;
65 int ret;
66 pk_context pk;
67 entropy_context entropy;
68 ctr_drbg_context ctr_drbg;
69 unsigned char hash[20];
70 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
71 char filename[512];
72 const char *pers = "pk_sign";
73 size_t olen = 0;
74
75 ret = 1;
76
77 if( argc != 3 )
78 {
79 printf( "usage: pk_sign <key_file> <filename>\n" );
80
81#if defined(_WIN32)
82 printf( "\n" );
83#endif
84
85 goto exit;
86 }
87
88 printf( "\n . Seeding the random number generator..." );
89 fflush( stdout );
90
91 entropy_init( &entropy );
92 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
93 (const unsigned char *) pers,
94 strlen( pers ) ) ) != 0 )
95 {
96 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
97 printf( " failed\n ! ctr_drbg_init returned -0x%04x - %s\n\n", -ret, buf );
98 goto exit;
99 }
100
101 printf( "\n . Reading private key from '%s'", argv[1] );
102 fflush( stdout );
103
104 pk_init( &pk );
105
106 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
107 {
108 ret = 1;
109 printf( " failed\n ! Could not open '%s'\n", argv[1] );
110 goto exit;
111 }
112
113 /*
114 * Compute the SHA-1 hash of the input file,
115 * then calculate the signature of the hash.
116 */
117 printf( "\n . Generating the SHA-1 signature" );
118 fflush( stdout );
119
120 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
121 {
122 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
123 goto exit;
124 }
125
126 if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA1, hash, 0, buf, &olen,
127 ctr_drbg_random, &ctr_drbg ) ) != 0 )
128 {
129 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
130 printf( " failed\n ! pk_sign returned -0x%04x - %s\n\n", -ret, buf );
131 goto exit;
132 }
133
134 /*
135 * Write the signature into <filename>-sig.txt
136 */
137 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
138
139 if( ( f = fopen( filename, "wb+" ) ) == NULL )
140 {
141 ret = 1;
142 printf( " failed\n ! Could not create %s\n\n", filename );
143 goto exit;
144 }
145
146 if( fwrite( buf, 1, olen, f ) != olen )
147 {
148 printf( "failed\n ! fwrite failed\n\n" );
149 goto exit;
150 }
151
152 fclose( f );
153
154 printf( "\n . Done (created \"%s\")\n\n", filename );
155
156exit:
157 pk_free( &pk );
158
159#if defined(_WIN32)
160 printf( " + Press Enter to exit this program.\n" );
161 fflush( stdout ); getchar();
162#endif
163
164 return( ret );
165}
166#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C &&
167 POLARSSL_SHA1_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
168 POLARSSL_CTR_DRBG_C */