blob: 65c66f30e3dead87d2d08d7392fee6d61ee7ed49 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature creation program
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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 Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/rsa.h"
33#include "polarssl/sha1.h"
Paul Bakker43f97992013-09-23 11:23:31 +020034#include "polarssl/ctr_drbg.h"
35#include "polarssl/entropy.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker5690efc2011-05-26 13:16:06 +000037#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker43f97992013-09-23 11:23:31 +020038 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) || \
39 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakker10527842012-01-14 18:00:00 +000040int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000041{
Paul Bakkercce9d772011-11-18 14:26:47 +000042 ((void) argc);
43 ((void) argv);
44
Paul Bakker5690efc2011-05-26 13:16:06 +000045 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker43f97992013-09-23 11:23:31 +020046 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO "
47 "and/or POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
48 "not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000049 return( 0 );
50}
51#else
Paul Bakker5121ce52009-01-03 21:22:43 +000052int main( int argc, char *argv[] )
53{
54 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000055 int ret;
56 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000057 rsa_context rsa;
Paul Bakker43f97992013-09-23 11:23:31 +020058 entropy_context entropy;
59 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000061 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker43f97992013-09-23 11:23:31 +020062 const char *pers = "rsa_decrypt";
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64 ret = 1;
65
66 if( argc != 2 )
67 {
68 printf( "usage: rsa_sign <filename>\n" );
69
Paul Bakkercce9d772011-11-18 14:26:47 +000070#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000071 printf( "\n" );
72#endif
73
74 goto exit;
75 }
76
Paul Bakker43f97992013-09-23 11:23:31 +020077 printf( "\n . Seeding the random number generator..." );
78 fflush( stdout );
79
80 entropy_init( &entropy );
81 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
82 (const unsigned char *) pers,
83 strlen( pers ) ) ) != 0 )
84 {
85 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
86 goto exit;
87 }
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089 printf( "\n . Reading private key from rsa_priv.txt" );
90 fflush( stdout );
91
92 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
93 {
94 ret = 1;
95 printf( " failed\n ! Could not open rsa_priv.txt\n" \
96 " ! Please run rsa_genkey first\n\n" );
97 goto exit;
98 }
99
Paul Bakkera802e1a2010-08-16 11:56:45 +0000100 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
103 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
104 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
105 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
106 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
107 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
108 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
109 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
110 {
111 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
112 goto exit;
113 }
114
115 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
116
117 fclose( f );
118
Paul Bakker5ef9db22012-09-27 13:19:22 +0000119 printf( "\n . Checking the private key" );
120 fflush( stdout );
121 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
122 {
123 printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
124 goto exit;
125 }
126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 /*
128 * Compute the SHA-1 hash of the input file,
129 * then calculate the RSA signature of the hash.
130 */
131 printf( "\n . Generating the RSA/SHA-1 signature" );
132 fflush( stdout );
133
134 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
135 {
136 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
137 goto exit;
138 }
139
Paul Bakker43f97992013-09-23 11:23:31 +0200140 if( ( ret = rsa_pkcs1_sign( &rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE,
141 SIG_RSA_SHA1, 20, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 {
Paul Bakker5ef9db22012-09-27 13:19:22 +0000143 printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 goto exit;
145 }
146
147 /*
148 * Write the signature into <filename>-sig.txt
149 */
150 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
151
152 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
153 {
154 ret = 1;
155 printf( " failed\n ! Could not create %s\n\n", argv[1] );
156 goto exit;
157 }
158
159 for( i = 0; i < rsa.len; i++ )
160 fprintf( f, "%02X%s", buf[i],
161 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
162
163 fclose( f );
164
165 printf( "\n . Done (created \"%s\")\n\n", argv[1] );
166
167exit:
168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 printf( " + Press Enter to exit this program.\n" );
171 fflush( stdout ); getchar();
172#endif
173
174 return( ret );
175}
Paul Bakker5690efc2011-05-26 13:16:06 +0000176#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
177 POLARSSL_FS_IO */