blob: f65c2a7459c170232192b54d2664c523b874da96 [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
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker5121ce52009-01-03 21:22:43 +000036#include <string.h>
37#include <stdio.h>
38
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
Paul Bakker5690efc2011-05-26 13:16:06 +000042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000043 !defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
Paul Bakker10527842012-01-14 18:00:00 +000044int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000045{
Paul Bakkercce9d772011-11-18 14:26:47 +000046 ((void) argc);
47 ((void) argv);
48
Rich Evansf90016a2015-01-19 14:26:37 +000049 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000050 "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakker5121ce52009-01-03 21:22:43 +000054int main( int argc, char *argv[] )
55{
56 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000057 int ret;
58 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000059 rsa_context rsa;
60 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000061 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63 ret = 1;
64
65 if( argc != 2 )
66 {
Rich Evansf90016a2015-01-19 14:26:37 +000067 polarssl_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Paul Bakkercce9d772011-11-18 14:26:47 +000069#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000070 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000071#endif
72
73 goto exit;
74 }
75
Rich Evansf90016a2015-01-19 14:26:37 +000076 polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000077 fflush( stdout );
78
79 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
80 {
81 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000083 " ! Please run rsa_genkey first\n\n" );
84 goto exit;
85 }
86
Paul Bakkera802e1a2010-08-16 11:56:45 +000087 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
90 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
91 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
93 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
95 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
96 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
97 {
Rich Evansf90016a2015-01-19 14:26:37 +000098 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000099 goto exit;
100 }
101
102 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
103
104 fclose( f );
105
Rich Evansf90016a2015-01-19 14:26:37 +0000106 polarssl_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000107 fflush( stdout );
108 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
109 {
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000111 goto exit;
112 }
113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000115 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * then calculate the RSA signature of the hash.
117 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000118 polarssl_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 fflush( stdout );
120
121 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
122 {
Rich Evansf90016a2015-01-19 14:26:37 +0000123 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 goto exit;
125 }
126
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000127 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 20, hash, buf ) ) != 0 )
129 {
Rich Evansf90016a2015-01-19 14:26:37 +0000130 polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
133
134 /*
135 * Write the signature into <filename>-sig.txt
136 */
137 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
138
139 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
140 {
141 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 goto exit;
144 }
145
146 for( i = 0; i < rsa.len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
149
150 fclose( f );
151
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154exit:
155
Paul Bakkercce9d772011-11-18 14:26:47 +0000156#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000157 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 fflush( stdout ); getchar();
159#endif
160
161 return( ret );
162}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000163#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000164 POLARSSL_FS_IO */