blob: d0326911372b9461500941d85e6577e47f93b284 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature creation program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +02007 *
Paul Bakker940f9ce2013-09-18 15:34:57 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker940f9ce2013-09-18 15:34:57 +020038#include <string.h>
39#include <stdio.h>
40
Paul Bakker940f9ce2013-09-18 15:34:57 +020041#include "polarssl/error.h"
42#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
44#include "polarssl/md.h"
45#include "polarssl/pk.h"
46#include "polarssl/sha1.h"
47
48#if defined _MSC_VER && !defined snprintf
49#define snprintf _snprintf
50#endif
51
52#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
53 !defined(POLARSSL_SHA1_C) || \
54 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \
55 !defined(POLARSSL_CTR_DRBG_C)
56int main( int argc, char *argv[] )
57{
58 ((void) argc);
59 ((void) argv);
60
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020062 "POLARSSL_SHA1_C and/or "
63 "POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO and/or "
64 "POLARSSL_CTR_DRBG_C not defined.\n");
65 return( 0 );
66}
67#else
68int main( int argc, char *argv[] )
69{
70 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020071 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020072 pk_context pk;
73 entropy_context entropy;
74 ctr_drbg_context ctr_drbg;
75 unsigned char hash[20];
76 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
77 char filename[512];
78 const char *pers = "pk_sign";
79 size_t olen = 0;
80
Paul Bakker0c226102014-04-17 16:02:36 +020081 entropy_init( &entropy );
82 pk_init( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +020083
84 if( argc != 3 )
85 {
Rich Evansf90016a2015-01-19 14:26:37 +000086 polarssl_printf( "usage: pk_sign <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020087
88#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000089 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020090#endif
91
92 goto exit;
93 }
94
Rich Evansf90016a2015-01-19 14:26:37 +000095 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +020096 fflush( stdout );
97
Paul Bakker940f9ce2013-09-18 15:34:57 +020098 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
99 (const unsigned char *) pers,
100 strlen( pers ) ) ) != 0 )
101 {
Rich Evansf90016a2015-01-19 14:26:37 +0000102 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103 goto exit;
104 }
105
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "\n . Reading private key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200107 fflush( stdout );
108
Paul Bakker940f9ce2013-09-18 15:34:57 +0200109 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
110 {
111 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( " failed\n ! Could not open '%s'\n", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113 goto exit;
114 }
115
116 /*
117 * Compute the SHA-1 hash of the input file,
118 * then calculate the signature of the hash.
119 */
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( "\n . Generating the SHA-1 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 fflush( stdout );
122
123 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
124 {
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200126 goto exit;
127 }
128
129 if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA1, hash, 0, buf, &olen,
130 ctr_drbg_random, &ctr_drbg ) ) != 0 )
131 {
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " failed\n ! pk_sign returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200133 goto exit;
134 }
135
136 /*
137 * Write the signature into <filename>-sig.txt
138 */
139 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
140
141 if( ( f = fopen( filename, "wb+" ) ) == NULL )
142 {
143 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " failed\n ! Could not create %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 goto exit;
146 }
147
148 if( fwrite( buf, 1, olen, f ) != olen )
149 {
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( "failed\n ! fwrite failed\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151 goto exit;
152 }
153
154 fclose( f );
155
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( "\n . Done (created \"%s\")\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200157
158exit:
159 pk_free( &pk );
Paul Bakkera317a982014-06-18 16:44:11 +0200160 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200161 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200162
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200163#if defined(POLARSSL_ERROR_C)
164 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200166#endif
167
Paul Bakker940f9ce2013-09-18 15:34:57 +0200168#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000169 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200170 fflush( stdout ); getchar();
171#endif
172
173 return( ret );
174}
175#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C &&
176 POLARSSL_SHA1_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
177 POLARSSL_CTR_DRBG_C */