blob: 380596a6daa4b6efb99c4f428f427e68ec55ad4c [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature creation program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker940f9ce2013-09-18 15:34:57 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andrzej Kurekffaee092018-04-03 04:36:52 -040057#include <stdlib.h>
Andres Amaya Garciaa5236b12018-04-29 22:26:25 +010058#define mbedtls_snprintf snprintf
59#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020060#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010061#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaa5236b12018-04-29 22:26:25 +010062#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020066 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
68 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000069int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Manuel Pégourié-Gonnard2ed05a02015-09-09 11:52:28 +020072 "MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
74 "MBEDTLS_CTR_DRBG_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Paul Bakker940f9ce2013-09-18 15:34:57 +020076}
77#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020078
79#include "mbedtls/error.h"
80#include "mbedtls/entropy.h"
81#include "mbedtls/ctr_drbg.h"
82#include "mbedtls/md.h"
83#include "mbedtls/pk.h"
84
85#include <stdio.h>
86#include <string.h>
87
k-stachowiakb50c39c2019-05-31 20:11:26 +020088/*
89 * For the currently used signature algorithms the buffer to store any signature
90 * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
91 */
92#if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
93#define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
94#else
95#define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
96#endif
97
Paul Bakker940f9ce2013-09-18 15:34:57 +020098int main( int argc, char *argv[] )
99{
100 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +0200101 int ret = 1;
Andres Amaya Garciaa5236b12018-04-29 22:26:25 +0100102 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_pk_context pk;
104 mbedtls_entropy_context entropy;
105 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +0200106 unsigned char hash[32];
k-stachowiakb50c39c2019-05-31 20:11:26 +0200107 unsigned char buf[SIGNATURE_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +0200108 char filename[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 const char *pers = "mbedtls_pk_sign";
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 size_t olen = 0;
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200113 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_pk_init( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115
116 if( argc != 3 )
117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( "usage: mbedtls_pk_sign <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200119
120#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122#endif
123
124 goto exit;
125 }
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128 fflush( stdout );
129
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200130 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131 (const unsigned char *) pers,
132 strlen( pers ) ) ) != 0 )
133 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200134 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200139 fflush( stdout );
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142 {
Andrzej Kurekd9594922018-03-26 04:13:24 -0400143 mbedtls_printf( " failed\n ! Could not parse '%s'\n", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200144 goto exit;
145 }
146
147 /*
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000148 * Compute the SHA-256 hash of the input file,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200149 * then calculate the signature of the hash.
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "\n . Generating the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152 fflush( stdout );
153
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200154 if( ( ret = mbedtls_md_file(
155 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
156 argv[2], hash ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
163 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " failed\n ! mbedtls_pk_sign returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200166 goto exit;
167 }
168
169 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200170 * Write the signature into <filename>.sig
Paul Bakker940f9ce2013-09-18 15:34:57 +0200171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200173
174 if( ( f = fopen( filename, "wb+" ) ) == NULL )
175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200177 goto exit;
178 }
179
180 if( fwrite( buf, 1, olen, f ) != olen )
181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( "failed\n ! fwrite failed\n\n" );
Janos Follath98e28a72016-05-31 14:03:54 +0100183 fclose( f );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200184 goto exit;
185 }
186
187 fclose( f );
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200190
Andres Amaya Garciaa5236b12018-04-29 22:26:25 +0100191 exit_code = MBEDTLS_EXIT_SUCCESS;
192
Paul Bakker940f9ce2013-09-18 15:34:57 +0200193exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_pk_free( &pk );
195 mbedtls_ctr_drbg_free( &ctr_drbg );
196 mbedtls_entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garciaa5236b12018-04-29 22:26:25 +0100199 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200200 {
201 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
202 mbedtls_printf( " ! Last error was: %s\n", buf );
203 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200204#endif
205
Paul Bakker940f9ce2013-09-18 15:34:57 +0200206#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200208 fflush( stdout ); getchar();
209#endif
210
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200211 mbedtls_exit( exit_code );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
214 MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
215 MBEDTLS_CTR_DRBG_C */