blob: 69c7374f263a9a1b3f938c273bc1c960b39a2dd9 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#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
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000037#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000038 defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/rsa.h"
40#include "mbedtls/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)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000049{
Rich Evansf90016a2015-01-19 14:26:37 +000050 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +000051 "POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000052 return( 0 );
53}
54#else
Paul Bakker5121ce52009-01-03 21:22:43 +000055int main( int argc, char *argv[] )
56{
57 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000058 int ret;
59 size_t i;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 rsa_context rsa;
61 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000062 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64 ret = 1;
65
66 if( argc != 2 )
67 {
Rich Evansf90016a2015-01-19 14:26:37 +000068 polarssl_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakkercce9d772011-11-18 14:26:47 +000070#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000071 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000072#endif
73
74 goto exit;
75 }
76
Rich Evansf90016a2015-01-19 14:26:37 +000077 polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000078 fflush( stdout );
79
80 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
81 {
82 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +000083 polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000084 " ! Please run rsa_genkey first\n\n" );
85 goto exit;
86 }
87
Paul Bakkera802e1a2010-08-16 11:56:45 +000088 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000089
Paul Bakker5121ce52009-01-03 21:22:43 +000090 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
91 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
93 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
94 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
95 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
96 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
97 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
98 {
Rich Evansf90016a2015-01-19 14:26:37 +000099 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 goto exit;
101 }
102
103 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
104
105 fclose( f );
106
Rich Evansf90016a2015-01-19 14:26:37 +0000107 polarssl_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000108 fflush( stdout );
109 if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
110 {
Rich Evansf90016a2015-01-19 14:26:37 +0000111 polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000112 goto exit;
113 }
114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000116 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 * then calculate the RSA signature of the hash.
118 */
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000119 polarssl_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 fflush( stdout );
121
122 if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
123 {
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 goto exit;
126 }
127
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000128 if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 20, hash, buf ) ) != 0 )
130 {
Rich Evansf90016a2015-01-19 14:26:37 +0000131 polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 goto exit;
133 }
134
135 /*
136 * Write the signature into <filename>-sig.txt
137 */
138 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
139
140 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
141 {
142 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 goto exit;
145 }
146
147 for( i = 0; i < rsa.len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +0000148 polarssl_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
150
151 fclose( f );
152
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155exit:
156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 fflush( stdout ); getchar();
160#endif
161
162 return( ret );
163}
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000164#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000165 POLARSSL_FS_IO */