blob: c466919c743244647173190e21d62669ba27d828 [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
29#include <string.h>
30#include <stdio.h>
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/rsa.h"
33#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker5690efc2011-05-26 13:16:06 +000035#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
36 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO)
Paul Bakker10527842012-01-14 18:00:00 +000037int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000038{
Paul Bakkercce9d772011-11-18 14:26:47 +000039 ((void) argc);
40 ((void) argv);
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
43 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO not defined.\n");
44 return( 0 );
45}
46#else
Paul Bakker5121ce52009-01-03 21:22:43 +000047int main( int argc, char *argv[] )
48{
49 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000050 int ret;
51 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000052 rsa_context rsa;
53 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000054 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000055
56 ret = 1;
57
58 if( argc != 2 )
59 {
60 printf( "usage: rsa_sign <filename>\n" );
61
Paul Bakkercce9d772011-11-18 14:26:47 +000062#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +000063 printf( "\n" );
64#endif
65
66 goto exit;
67 }
68
69 printf( "\n . Reading private key from rsa_priv.txt" );
70 fflush( stdout );
71
72 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
73 {
74 ret = 1;
75 printf( " failed\n ! Could not open rsa_priv.txt\n" \
76 " ! Please run rsa_genkey first\n\n" );
77 goto exit;
78 }
79
Paul Bakkera802e1a2010-08-16 11:56:45 +000080 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
83 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
84 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
85 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
86 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
88 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
90 {
91 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
92 goto exit;
93 }
94
95 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
96
97 fclose( f );
98
Paul Bakker5ef9db22012-09-27 13:19:22 +000099 printf( "\n . Checking the private key" );
100 fflush( stdout );
101 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
102 {
103 printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
104 goto exit;
105 }
106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 /*
108 * Compute the SHA-1 hash of the input file,
109 * then calculate the RSA signature of the hash.
110 */
111 printf( "\n . Generating the RSA/SHA-1 signature" );
112 fflush( stdout );
113
114 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
115 {
116 printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
117 goto exit;
118 }
119
Paul Bakkerc70b9822013-04-07 22:00:46 +0200120 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 20, hash, buf ) ) != 0 )
122 {
Paul Bakker5ef9db22012-09-27 13:19:22 +0000123 printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 goto exit;
125 }
126
127 /*
128 * Write the signature into <filename>-sig.txt
129 */
130 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
131
132 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
133 {
134 ret = 1;
135 printf( " failed\n ! Could not create %s\n\n", argv[1] );
136 goto exit;
137 }
138
139 for( i = 0; i < rsa.len; i++ )
140 fprintf( f, "%02X%s", buf[i],
141 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
142
143 fclose( f );
144
145 printf( "\n . Done (created \"%s\")\n\n", argv[1] );
146
147exit:
148
Paul Bakkercce9d772011-11-18 14:26:47 +0000149#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 printf( " + Press Enter to exit this program.\n" );
151 fflush( stdout ); getchar();
152#endif
153
154 return( ret );
155}
Paul Bakker5690efc2011-05-26 13:16:06 +0000156#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
157 POLARSSL_FS_IO */