blob: 27d14743bfc89747b7aef3d2a2c366d7c634da72 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * RSA/SHA-1 signature creation program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
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 Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038#include <string.h>
39#include <stdio.h>
40
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/rsa.h"
42#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakker5690efc2011-05-26 13:16:06 +000044#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
45 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
Paul Bakker10527842012-01-14 18:00:00 +000046int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Rich Evansf90016a2015-01-19 14:26:37 +000051 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000052 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
53 return( 0 );
54}
55#else
Paul Bakker5121ce52009-01-03 21:22:43 +000056int main( int argc, char *argv[] )
57{
58 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000059 int ret;
60 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000061 rsa_context rsa;
62 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000063 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65 ret = 1;
66
67 if( argc != 2 )
68 {
Rich Evansf90016a2015-01-19 14:26:37 +000069 polarssl_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkercce9d772011-11-18 14:26:47 +000071#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000072 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000073#endif
74
75 goto exit;
76 }
77
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000079 fflush( stdout );
80
81 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
82 {
83 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +000084 polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000085 " ! Please run rsa_genkey first\n\n" );
86 goto exit;
87 }
88
Paul Bakkera802e1a2010-08-16 11:56:45 +000089 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
93 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
95 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
96 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
97 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
98 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
99 {
Rich Evansf90016a2015-01-19 14:26:37 +0000100 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 goto exit;
102 }
103
104 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
105
106 fclose( f );
107
Rich Evansf90016a2015-01-19 14:26:37 +0000108 polarssl_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000109 fflush( stdout );
110 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
111 {
Rich Evansf90016a2015-01-19 14:26:37 +0000112 polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000113 goto exit;
114 }
115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 /*
117 * Compute the SHA-1 hash of the input file,
118 * then calculate the RSA signature of the hash.
119 */
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( "\n . Generating the RSA/SHA-1 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 fflush( stdout );
122
123 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
124 {
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 goto exit;
127 }
128
Paul Bakkerc70b9822013-04-07 22:00:46 +0200129 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 20, hash, buf ) ) != 0 )
131 {
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 goto exit;
134 }
135
136 /*
137 * Write the signature into <filename>-sig.txt
138 */
139 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
140
141 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
142 {
143 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
148 for( i = 0; i < rsa.len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000149 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
151
152 fclose( f );
153
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156exit:
157
Paul Bakkercce9d772011-11-18 14:26:47 +0000158#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 fflush( stdout ); getchar();
161#endif
162
163 return( ret );
164}
Paul Bakker5690efc2011-05-26 13:16:06 +0000165#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
166 POLARSSL_FS_IO */