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