blob: 2b358c16c438f29639e95c36459be7b1875f79e2 [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +00002 * RSASSA-PSS/SHA-256 signature creation program
Paul Bakker2291f6c2011-03-25 14:07:53 +00003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker2291f6c2011-03-25 14:07:53 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2291f6c2011-03-25 14:07:53 +00007 *
Paul Bakker2291f6c2011-03-25 14:07:53 +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 Bakker2291f6c2011-03-25 14:07:53 +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_snprintf snprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020037#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
39 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
40 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000041int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000042{
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020043 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
45 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000047 return( 0 );
48}
49#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020050
51#include "mbedtls/entropy.h"
52#include "mbedtls/ctr_drbg.h"
53#include "mbedtls/md.h"
54#include "mbedtls/rsa.h"
55#include "mbedtls/md.h"
56#include "mbedtls/x509.h"
57
58#include <stdio.h>
59#include <string.h>
60
Paul Bakker2291f6c2011-03-25 14:07:53 +000061int main( int argc, char *argv[] )
62{
63 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020064 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_pk_context pk;
66 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020068 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker2291f6c2011-03-25 14:07:53 +000070 char filename[512];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 const char *pers = "rsa_sign_pss";
Paul Bakker30520d12013-09-17 11:39:31 +020072 size_t olen = 0;
Paul Bakker2291f6c2011-03-25 14:07:53 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_entropy_init( &entropy );
75 mbedtls_pk_init( &pk );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020076 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker2291f6c2011-03-25 14:07:53 +000077
78 if( argc != 3 )
79 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000081
Paul Bakkercce9d772011-11-18 14:26:47 +000082#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_printf( "\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +000084#endif
85
86 goto exit;
87 }
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker508ad5a2011-12-04 17:09:26 +000090 fflush( stdout );
91
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020092 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020093 (const unsigned char *) pers,
94 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000095 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020096 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000097 goto exit;
98 }
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000101 fflush( stdout );
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000104 {
105 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
107 mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000108 goto exit;
109 }
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
Paul Bakker30520d12013-09-17 11:39:31 +0200112 {
113 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
Paul Bakker30520d12013-09-17 11:39:31 +0200115 goto exit;
116 }
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100119
Paul Bakker2291f6c2011-03-25 14:07:53 +0000120 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000121 * Compute the SHA-256 hash of the input file,
Paul Bakker2291f6c2011-03-25 14:07:53 +0000122 * then calculate the RSA signature of the hash.
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000125 fflush( stdout );
126
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200127 if( ( ret = mbedtls_md_file(
128 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
129 argv[2], hash ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000132 goto exit;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
136 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " failed\n ! mbedtls_pk_sign returned %d\n\n", ret );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000139 goto exit;
140 }
141
142 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200143 * Write the signature into <filename>.sig
Paul Bakker2291f6c2011-03-25 14:07:53 +0000144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000146
147 if( ( f = fopen( filename, "wb+" ) ) == NULL )
148 {
149 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000151 goto exit;
152 }
153
Paul Bakker30520d12013-09-17 11:39:31 +0200154 if( fwrite( buf, 1, olen, f ) != olen )
Paul Bakker2291f6c2011-03-25 14:07:53 +0000155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( "failed\n ! fwrite failed\n\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000157 goto exit;
158 }
159
160 fclose( f );
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000163
164exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_pk_free( &pk );
166 mbedtls_ctr_drbg_free( &ctr_drbg );
167 mbedtls_entropy_free( &entropy );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker2291f6c2011-03-25 14:07:53 +0000171 fflush( stdout ); getchar();
172#endif
173
174 return( ret );
175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
177 MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
178 MBEDTLS_CTR_DRBG_C */