blob: 3ff411abb775f2f10a817931101467ee8d2234d9 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020035#define mbedtls_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020039 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
40 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000041int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020044 "MBEDLTS_MD_C and/or "
45 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000046 return( 0 );
47}
48#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020049
50#include "mbedtls/rsa.h"
51#include "mbedtls/md.h"
52
53#include <stdio.h>
54#include <string.h>
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056int main( int argc, char *argv[] )
57{
58 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000059 int ret;
60 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +000062 unsigned char hash[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020064 char filename[512];
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66 ret = 1;
67
68 if( argc != 2 )
69 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Paul Bakkercce9d772011-11-18 14:26:47 +000072#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000074#endif
75
76 goto exit;
77 }
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000080 fflush( stdout );
81
82 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
83 {
84 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000086 " ! Please run rsa_genkey first\n\n" );
87 goto exit;
88 }
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
93 ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
94 ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
95 ( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
96 ( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
97 ( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
98 ( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
99 ( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 goto exit;
103 }
104
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200105 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 fclose( f );
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000110 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
Paul Bakker5ef9db22012-09-27 13:19:22 +0000112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n", -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000114 goto exit;
115 }
116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000118 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * then calculate the RSA signature of the hash.
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 fflush( stdout );
123
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200124 if( ( ret = mbedtls_md_file(
125 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
126 argv[1], hash ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 goto exit;
130 }
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 20, hash, buf ) ) != 0 )
134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 goto exit;
137 }
138
139 /*
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200140 * Write the signature into <filename>.sig
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200142 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200144 if( ( f = fopen( filename, "wb+" ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 {
146 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
151 for( i = 0; i < rsa.len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
154
155 fclose( f );
156
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200157 mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159exit:
160
Paul Bakkercce9d772011-11-18 14:26:47 +0000161#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 fflush( stdout ); getchar();
164#endif
165
166 return( ret );
167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
169 MBEDTLS_FS_IO */