blob: 021d154f440ba022ad5ba16e7b9360ca92e38a06 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) &&\
37 defined(POLARSSL_SHA256_C) &&\
38 defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO) &&\
39 defined(POLARSSL_CTR_DRBG_C)
Paul Bakker940f9ce2013-09-18 15:34:57 +020040#include "polarssl/error.h"
41#include "polarssl/entropy.h"
42#include "polarssl/ctr_drbg.h"
43#include "polarssl/md.h"
44#include "polarssl/pk.h"
45#include "polarssl/sha1.h"
46
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <string.h>
49#endif
50
Paul Bakker940f9ce2013-09-18 15:34:57 +020051#if defined _MSC_VER && !defined snprintf
52#define snprintf _snprintf
53#endif
54
55#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000056 !defined(POLARSSL_SHA256_C) || \
Paul Bakker940f9ce2013-09-18 15:34:57 +020057 !defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \
58 !defined(POLARSSL_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000059int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020060{
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000062 "POLARSSL_SHA256_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020063 "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 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000117 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200118 * then calculate the signature of the hash.
119 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000120 polarssl_printf( "\n . Generating the SHA-256 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
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000129 if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA256, hash, 0, buf, &olen,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 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 &&
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000176 POLARSSL_SHA256_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
Paul Bakker940f9ce2013-09-18 15:34:57 +0200177 POLARSSL_CTR_DRBG_C */