blob: 4fcf60984b73822b8f515f1a5ab815bcb940353f [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
2 * RSASSA-PSS/SHA-1 signature creation program
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker2291f6c2011-03-25 14:07:53 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker2291f6c2011-03-25 14:07:53 +00007 *
8 * 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
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdio.h>
29
Paul Bakker5690efc2011-05-26 13:16:06 +000030#include "polarssl/config.h"
31
Paul Bakker508ad5a2011-12-04 17:09:26 +000032#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
Paul Bakker2291f6c2011-03-25 14:07:53 +000034#include "polarssl/md.h"
35#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
37#include "polarssl/x509.h"
38
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000039#if defined _MSC_VER && !defined snprintf
40#define snprintf _snprintf
41#endif
42
Paul Bakker508ad5a2011-12-04 17:09:26 +000043#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000044 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000045 !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) || \
46 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000047int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Paul Bakkercce9d772011-11-18 14:26:47 +000049 ((void) argc);
50 ((void) argv);
51
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000053 "POLARSSL_RSA_C and/or POLARSSL_SHA1_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000054 "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO and/or "
55 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000056 return( 0 );
57}
58#else
Paul Bakker2291f6c2011-03-25 14:07:53 +000059int main( int argc, char *argv[] )
60{
61 FILE *f;
Paul Bakker39148402014-04-17 16:02:36 +020062 int ret = 1;
Paul Bakker2291f6c2011-03-25 14:07:53 +000063 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000064 entropy_context entropy;
65 ctr_drbg_context ctr_drbg;
Paul Bakker2291f6c2011-03-25 14:07:53 +000066 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000067 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker2291f6c2011-03-25 14:07:53 +000068 char filename[512];
Paul Bakkere0225e42013-06-06 12:52:24 +020069 const char *pers = "rsa_sign_pss";
Paul Bakker2291f6c2011-03-25 14:07:53 +000070
Paul Bakker39148402014-04-17 16:02:36 +020071 entropy_init( &entropy );
72 rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
Paul Bakker2291f6c2011-03-25 14:07:53 +000073
74 if( argc != 3 )
75 {
76 printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
77
Paul Bakkercce9d772011-11-18 14:26:47 +000078#if defined(_WIN32)
Paul Bakker2291f6c2011-03-25 14:07:53 +000079 printf( "\n" );
80#endif
81
82 goto exit;
83 }
84
Paul Bakker508ad5a2011-12-04 17:09:26 +000085 printf( "\n . Seeding the random number generator..." );
86 fflush( stdout );
87
Paul Bakker508ad5a2011-12-04 17:09:26 +000088 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +020089 (const unsigned char *) pers,
90 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 {
92 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
93 goto exit;
94 }
95
Paul Bakker2291f6c2011-03-25 14:07:53 +000096 printf( "\n . Reading private key from '%s'", argv[1] );
97 fflush( stdout );
98
Paul Bakker2291f6c2011-03-25 14:07:53 +000099 if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 )
100 {
101 ret = 1;
102 printf( " failed\n ! Could not open '%s'\n", argv[1] );
103 goto exit;
104 }
105
106 /*
107 * Compute the SHA-1 hash of the input file,
108 * then calculate the RSA signature of the hash.
109 */
110 printf( "\n . Generating the RSA/SHA-1 signature" );
111 fflush( stdout );
112
113 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
114 {
115 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
116 goto exit;
117 }
118
Paul Bakker508ad5a2011-12-04 17:09:26 +0000119 if( ( ret = rsa_pkcs1_sign( &rsa, ctr_drbg_random, &ctr_drbg,
120 RSA_PRIVATE, SIG_RSA_SHA1,
Paul Bakker2291f6c2011-03-25 14:07:53 +0000121 20, hash, buf ) ) != 0 )
122 {
123 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
124 goto exit;
125 }
126
127 /*
128 * Write the signature into <filename>-sig.txt
129 */
130 snprintf( filename, 512, "%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, rsa.len, f ) != (size_t) rsa.len )
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
Paul Bakkercce9d772011-11-18 14:26:47 +0000151#if defined(_WIN32)
Paul Bakker2291f6c2011-03-25 14:07:53 +0000152 printf( " + Press Enter to exit this program.\n" );
153 fflush( stdout ); getchar();
154#endif
155
156 return( ret );
157}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000158#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
159 POLARSSL_SHA1_C && POLARSSL_X509_PARSE_C && POLARSSL_FS_IO &&
160 POLARSSL_CTR_DRBG_C */