blob: 2c355d93903d9550268e96a39fe272994a60661c [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020031
32#include <string.h>
33#include <stdio.h>
34
Paul Bakker940f9ce2013-09-18 15:34:57 +020035#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;
Paul Bakker0c226102014-04-17 16:02:36 +020065 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020066 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
Paul Bakker0c226102014-04-17 16:02:36 +020075 entropy_init( &entropy );
76 pk_init( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +020077
78 if( argc != 3 )
79 {
80 printf( "usage: pk_sign <key_file> <filename>\n" );
81
82#if defined(_WIN32)
83 printf( "\n" );
84#endif
85
86 goto exit;
87 }
88
89 printf( "\n . Seeding the random number generator..." );
90 fflush( stdout );
91
Paul Bakker940f9ce2013-09-18 15:34:57 +020092 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
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
104 {
105 ret = 1;
106 printf( " failed\n ! Could not open '%s'\n", argv[1] );
107 goto exit;
108 }
109
110 /*
111 * Compute the SHA-1 hash of the input file,
112 * then calculate the signature of the hash.
113 */
114 printf( "\n . Generating the SHA-1 signature" );
115 fflush( stdout );
116
117 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
118 {
119 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
120 goto exit;
121 }
122
123 if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA1, hash, 0, buf, &olen,
124 ctr_drbg_random, &ctr_drbg ) ) != 0 )
125 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200126 printf( " failed\n ! pk_sign returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200127 goto exit;
128 }
129
130 /*
131 * Write the signature into <filename>-sig.txt
132 */
133 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
134
135 if( ( f = fopen( filename, "wb+" ) ) == NULL )
136 {
137 ret = 1;
138 printf( " failed\n ! Could not create %s\n\n", filename );
139 goto exit;
140 }
141
142 if( fwrite( buf, 1, olen, f ) != olen )
143 {
144 printf( "failed\n ! fwrite failed\n\n" );
145 goto exit;
146 }
147
148 fclose( f );
149
150 printf( "\n . Done (created \"%s\")\n\n", filename );
151
152exit:
153 pk_free( &pk );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200154 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200155
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200156#if defined(POLARSSL_ERROR_C)
157 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
158 printf( " ! Last error was: %s\n", buf );
159#endif
160
Paul Bakker940f9ce2013-09-18 15:34:57 +0200161#if defined(_WIN32)
162 printf( " + Press Enter to exit this program.\n" );
163 fflush( stdout ); getchar();
164#endif
165
166 return( ret );
167}
168#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C &&
169 POLARSSL_SHA1_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
170 POLARSSL_CTR_DRBG_C */