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