blob: 625a2995c3162451b596342a5e8a2365bff3b157 [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 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020096 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020097 goto exit;
98 }
99
100 printf( "\n . Reading private key from '%s'", argv[1] );
101 fflush( stdout );
102
103 pk_init( &pk );
104
105 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
106 {
107 ret = 1;
108 printf( " failed\n ! Could not open '%s'\n", argv[1] );
109 goto exit;
110 }
111
112 /*
113 * Compute the SHA-1 hash of the input file,
114 * then calculate the signature of the hash.
115 */
116 printf( "\n . Generating the SHA-1 signature" );
117 fflush( stdout );
118
119 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
120 {
121 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
122 goto exit;
123 }
124
125 if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA1, hash, 0, buf, &olen,
126 ctr_drbg_random, &ctr_drbg ) ) != 0 )
127 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200128 printf( " failed\n ! pk_sign returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129 goto exit;
130 }
131
132 /*
133 * Write the signature into <filename>-sig.txt
134 */
135 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
136
137 if( ( f = fopen( filename, "wb+" ) ) == NULL )
138 {
139 ret = 1;
140 printf( " failed\n ! Could not create %s\n\n", filename );
141 goto exit;
142 }
143
144 if( fwrite( buf, 1, olen, f ) != olen )
145 {
146 printf( "failed\n ! fwrite failed\n\n" );
147 goto exit;
148 }
149
150 fclose( f );
151
152 printf( "\n . Done (created \"%s\")\n\n", filename );
153
154exit:
155 pk_free( &pk );
156
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200157#if defined(POLARSSL_ERROR_C)
158 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
159 printf( " ! Last error was: %s\n", buf );
160#endif
161
Paul Bakker940f9ce2013-09-18 15:34:57 +0200162#if defined(_WIN32)
163 printf( " + Press Enter to exit this program.\n" );
164 fflush( stdout ); getchar();
165#endif
166
167 return( ret );
168}
169#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C &&
170 POLARSSL_SHA1_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
171 POLARSSL_CTR_DRBG_C */