blob: 685db517e2f60a676755b814af3a7b52909fda88 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSA/SHA-256 signature creation program
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) &&\
38 defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO)
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/rsa.h"
40#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <stdio.h>
43#include <string.h>
44#endif
45
Paul Bakker5690efc2011-05-26 13:16:06 +000046#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000047 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Paul Bakker10527842012-01-14 18:00:00 +000048int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000049{
Paul Bakkercce9d772011-11-18 14:26:47 +000050 ((void) argc);
51 ((void) argv);
52
Rich Evansf90016a2015-01-19 14:26:37 +000053 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000054 "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000055 return( 0 );
56}
57#else
Paul Bakker5121ce52009-01-03 21:22:43 +000058int main( int argc, char *argv[] )
59{
60 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000061 int ret;
62 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000063 rsa_context rsa;
64 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000065 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67 ret = 1;
68
69 if( argc != 2 )
70 {
Rich Evansf90016a2015-01-19 14:26:37 +000071 polarssl_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakkercce9d772011-11-18 14:26:47 +000073#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000075#endif
76
77 goto exit;
78 }
79
Rich Evansf90016a2015-01-19 14:26:37 +000080 polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000081 fflush( stdout );
82
83 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
84 {
85 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +000086 polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000087 " ! Please run rsa_genkey first\n\n" );
88 goto exit;
89 }
90
Paul Bakkera802e1a2010-08-16 11:56:45 +000091 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
95 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
96 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
97 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
98 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
99 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
100 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
101 {
Rich Evansf90016a2015-01-19 14:26:37 +0000102 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 goto exit;
104 }
105
106 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
107
108 fclose( f );
109
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000111 fflush( stdout );
112 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
113 {
Rich Evansf90016a2015-01-19 14:26:37 +0000114 polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000115 goto exit;
116 }
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000119 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * then calculate the RSA signature of the hash.
121 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000122 polarssl_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 fflush( stdout );
124
125 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
126 {
Rich Evansf90016a2015-01-19 14:26:37 +0000127 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 goto exit;
129 }
130
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000131 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 20, hash, buf ) ) != 0 )
133 {
Rich Evansf90016a2015-01-19 14:26:37 +0000134 polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
138 /*
139 * Write the signature into <filename>-sig.txt
140 */
141 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
142
143 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
144 {
145 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
150 for( i = 0; i < rsa.len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000151 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
153
154 fclose( f );
155
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158exit:
159
Paul Bakkercce9d772011-11-18 14:26:47 +0000160#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000161 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 fflush( stdout ); getchar();
163#endif
164
165 return( ret );
166}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000167#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000168 POLARSSL_FS_IO */