blob: dc49fac114097d5251bf6c926926d07dc9989602 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature creation program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
Paul Bakker43f97992013-09-23 11:23:31 +020037#include "polarssl/ctr_drbg.h"
38#include "polarssl/entropy.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker5690efc2011-05-26 13:16:06 +000040#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker43f97992013-09-23 11:23:31 +020041 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) || \
42 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker10527842012-01-14 18:00:00 +000043int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000044{
Paul Bakkercce9d772011-11-18 14:26:47 +000045 ((void) argc);
46 ((void) argv);
47
Paul Bakker5690efc2011-05-26 13:16:06 +000048 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker43f97992013-09-23 11:23:31 +020049 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO "
50 "and/or POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
51 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000052 return( 0 );
53}
54#else
Paul Bakker5121ce52009-01-03 21:22:43 +000055int main( int argc, char *argv[] )
56{
57 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000058 int ret;
59 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 rsa_context rsa;
Paul Bakker43f97992013-09-23 11:23:31 +020061 entropy_context entropy;
62 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000063 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000064 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker43f97992013-09-23 11:23:31 +020065 const char *pers = "rsa_decrypt";
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67 ret = 1;
68
69 if( argc != 2 )
70 {
71 printf( "usage: rsa_sign <filename>\n" );
72
Paul Bakkercce9d772011-11-18 14:26:47 +000073#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000074 printf( "\n" );
75#endif
76
77 goto exit;
78 }
79
Paul Bakker43f97992013-09-23 11:23:31 +020080 printf( "\n . Seeding the random number generator..." );
81 fflush( stdout );
82
83 entropy_init( &entropy );
84 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
85 (const unsigned char *) pers,
86 strlen( pers ) ) ) != 0 )
87 {
88 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
89 goto exit;
90 }
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092 printf( "\n . Reading private key from rsa_priv.txt" );
93 fflush( stdout );
94
95 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
96 {
97 ret = 1;
98 printf( " failed\n ! Could not open rsa_priv.txt\n" \
99 " ! Please run rsa_genkey first\n\n" );
100 goto exit;
101 }
102
Paul Bakkera802e1a2010-08-16 11:56:45 +0000103 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
106 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
107 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
108 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
109 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
110 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
111 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
112 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
113 {
114 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
115 goto exit;
116 }
117
118 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
119
120 fclose( f );
121
Paul Bakker5ef9db22012-09-27 13:19:22 +0000122 printf( "\n . Checking the private key" );
123 fflush( stdout );
124 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
125 {
126 printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
127 goto exit;
128 }
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 /*
131 * Compute the SHA-1 hash of the input file,
132 * then calculate the RSA signature of the hash.
133 */
134 printf( "\n . Generating the RSA/SHA-1 signature" );
135 fflush( stdout );
136
137 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
138 {
139 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
140 goto exit;
141 }
142
Paul Bakker43f97992013-09-23 11:23:31 +0200143 if( ( ret = rsa_pkcs1_sign( &rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE,
144 SIG_RSA_SHA1, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 {
Paul Bakker5ef9db22012-09-27 13:19:22 +0000146 printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
150 /*
151 * Write the signature into <filename>-sig.txt
152 */
153 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
154
155 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
156 {
157 ret = 1;
158 printf( " failed\n ! Could not create %s\n\n", argv[1] );
159 goto exit;
160 }
161
162 for( i = 0; i < rsa.len; i++ )
163 fprintf( f, "%02X%s", buf[i],
164 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
165
166 fclose( f );
167
168 printf( "\n . Done (created \"%s\")\n\n", argv[1] );
169
170exit:
171
Paul Bakkercce9d772011-11-18 14:26:47 +0000172#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 printf( " + Press Enter to exit this program.\n" );
174 fflush( stdout ); getchar();
175#endif
176
177 return( ret );
178}
Paul Bakker5690efc2011-05-26 13:16:06 +0000179#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
180 POLARSSL_FS_IO */