blob: e2d39fcbb5cac6b9a84cec659b34152829d3b4fb [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020038 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
39 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000040int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020043 "MBEDLTS_MD_C and/or "
44 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000045 return( 0 );
46}
47#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020048
49#include "mbedtls/rsa.h"
50#include "mbedtls/md.h"
51
52#include <stdio.h>
53#include <string.h>
54
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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +000061 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64 ret = 1;
65
66 if( argc != 2 )
67 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_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)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000072#endif
73
74 goto exit;
75 }
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
91 ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
92 ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
93 ( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
94 ( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
95 ( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
96 ( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
97 ( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 goto exit;
101 }
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 fclose( f );
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000108 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
Paul Bakker5ef9db22012-09-27 13:19:22 +0000110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( " failed\n ! mbedtls_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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 fflush( stdout );
121
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200122 if( ( ret = mbedtls_md_file(
123 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
124 argv[1], hash ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 goto exit;
128 }
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 20, hash, buf ) ) != 0 )
132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 goto exit;
135 }
136
137 /*
138 * Write the signature into <filename>-sig.txt
139 */
140 memcpy( argv[1] + strlen( argv[1] ), ".sig", 5 );
141
142 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
143 {
144 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 for( i = 0; i < rsa.len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
152
153 fclose( f );
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157exit:
158
Paul Bakkercce9d772011-11-18 14:26:47 +0000159#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 fflush( stdout ); getchar();
162#endif
163
164 return( ret );
165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
167 MBEDTLS_FS_IO */