blob: 5aa592d03c677a6fc727772af7743d9ae63e9786 [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garcia1a660562018-04-29 20:16:46 +010026#include <stdlib.h>
27#define mbedtls_fprintf fprintf
28#define mbedtls_printf printf
29#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010030#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010031#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia1a660562018-04-29 20:16:46 +010032#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
33#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020036 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
37 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000038int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard2ed05a02015-09-09 11:52:28 +020041 "MBEDTLS_MD_C and/or "
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020042 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020043 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000044}
45#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020046
47#include "mbedtls/rsa.h"
48#include "mbedtls/md.h"
49
50#include <stdio.h>
51#include <string.h>
52
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010053
Paul Bakker5121ce52009-01-03 21:22:43 +000054int main( int argc, char *argv[] )
55{
56 FILE *f;
Andres Amaya Garcia1a660562018-04-29 20:16:46 +010057 int ret = 1;
58 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000059 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020061 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +020063 char filename[512];
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010064 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Ronald Cronc1905a12021-06-05 11:11:14 +020066 mbedtls_rsa_init( &rsa );
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010067
68 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
69 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
70 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072 if( argc != 2 )
73 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_printf( "usage: rsa_sign <filename>\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakkercce9d772011-11-18 14:26:47 +000076#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000078#endif
79
80 goto exit;
81 }
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +000084 fflush( stdout );
85
86 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
87 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +000089 " ! Please run rsa_genkey first\n\n" );
90 goto exit;
91 }
92
Hanno Beckerd6ba5e32017-08-23 06:48:07 +010093 if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
94 ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
95 ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
96 ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
97 ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
98 ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
99 ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
100 ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Janos Follath98e28a72016-05-31 14:03:54 +0100103 fclose( f );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 goto exit;
105 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 fclose( f );
107
Hanno Beckerd6ba5e32017-08-23 06:48:07 +0100108 if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
109 {
110 mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
111 ret );
112 goto exit;
113 }
114
Hanno Becker7f25f852017-10-10 16:56:22 +0100115 if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
Hanno Beckerd6ba5e32017-08-23 06:48:07 +0100116 {
117 mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
118 ret );
119 goto exit;
120 }
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( "\n . Checking the private key" );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000123 fflush( stdout );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
Paul Bakker5ef9db22012-09-27 13:19:22 +0000125 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200126 mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n", (unsigned int) -ret );
Paul Bakker5ef9db22012-09-27 13:19:22 +0000127 goto exit;
128 }
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000131 * Compute the SHA-256 hash of the input file,
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 * then calculate the RSA signature of the hash.
133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 fflush( stdout );
136
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200137 if( ( ret = mbedtls_md_file(
138 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
139 argv[1], hash ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
Thomas Daubney140184d2021-05-18 16:04:07 +0100145 if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_MD_SHA256,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200146 32, hash, buf ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 {
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200148 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", (unsigned int) -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 goto exit;
150 }
151
152 /*
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200153 * Write the signature into <filename>.sig
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200155 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200157 if( ( f = fopen( filename, "wb+" ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 goto exit;
161 }
162
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200163 for( i = 0; i < rsa.MBEDTLS_PRIVATE(len); i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_fprintf( f, "%02X%s", buf[i],
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 ( i + 1 ) % 16 == 0 ? "\r\n" : " " );
166
167 fclose( f );
168
Manuel Pégourié-Gonnardd74c6972015-08-27 21:39:40 +0200169 mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Andres Amaya Garcia1a660562018-04-29 20:16:46 +0100171 exit_code = MBEDTLS_EXIT_SUCCESS;
172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173exit:
174
Janos Follathf713b0a2016-03-17 15:21:39 +0000175 mbedtls_rsa_free( &rsa );
Hanno Beckerd6ba5e32017-08-23 06:48:07 +0100176 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
177 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
178 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
Janos Follathf713b0a2016-03-17 15:21:39 +0000179
Paul Bakkercce9d772011-11-18 14:26:47 +0000180#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 fflush( stdout ); getchar();
183#endif
184
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200185 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
188 MBEDTLS_FS_IO */