blob: e78723ca55593aade04a086c9f870a873bcfb9af [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA 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.
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +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 * **********
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +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
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +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>
Andres Amaya Garcia47c04112018-04-29 19:45:25 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia47c04112018-04-29 19:45:25 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ECDSA_C) && \
65 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/entropy.h"
67#include "mbedtls/ctr_drbg.h"
68#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000069#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020070
71#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000072#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020073
74/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020075 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020076 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020077#define VERBOSE
78
79/*
80 * Uncomment to force use of a specific curve
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020083
84#if !defined(ECPARAMS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020086#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
89 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000090int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
93 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020094 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020095}
96#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020097#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010098static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020099{
100 size_t i;
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200103 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200105 "0123456789ABCDEF" [buf[i] % 16] );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200110{
111 unsigned char buf[300];
112 size_t len;
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
115 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200118 return;
119 }
120
121 dump_buf( title, buf, len );
122}
123#else
124#define dump_buf( a, b, c )
125#define dump_pubkey( a, b )
126#endif
127
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128int main( int argc, char *argv[] )
129{
Andres Amaya Garcia47c04112018-04-29 19:45:25 +0100130 int ret = 1;
131 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_ecdsa_context ctx_sign, ctx_verify;
133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000135 unsigned char message[100];
136 unsigned char hash[32];
137 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200138 size_t sig_len;
139 const char *pers = "ecdsa";
140 ((void) argv);
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_ecdsa_init( &ctx_sign );
143 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200144 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200145
Janos Follath0a5154b2017-03-10 11:31:41 +0000146 memset( sig, 0, sizeof( sig ) );
147 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200148
149 if( argc != 1 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200152
153#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200155#endif
156
157 goto exit;
158 }
159
160 /*
161 * Generate a key pair for signing
162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200164 fflush( stdout );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200167 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200168 (const unsigned char *) pers,
169 strlen( pers ) ) ) != 0 )
170 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200171 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200172 goto exit;
173 }
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200176 fflush( stdout );
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
179 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200182 goto exit;
183 }
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200186
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200187 dump_pubkey( " + Public key: ", &ctx_sign );
188
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200189 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000190 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000192 mbedtls_printf( " . Computing message hash..." );
193 fflush( stdout );
194
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100195 if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100196 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100197 mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100198 goto exit;
199 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000200
201 mbedtls_printf( " ok\n" );
202
203 dump_buf( " + Hash: ", hash, sizeof( hash ) );
204
205 /*
206 * Sign message hash
207 */
208 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200209 fflush( stdout );
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200212 hash, sizeof( hash ),
213 sig, &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200215 {
Ercan Ozturkab713a92020-01-28 21:51:04 -0800216 mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 goto exit;
218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200220
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200221 dump_buf( " + Signature: ", sig, sig_len );
222
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200223 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200224 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200225 *
226 * We could use the same context for verification and signatures, but we
227 * chose to use a new one in order to make it clear that the verifying
228 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200231 fflush( stdout );
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200236 goto exit;
237 }
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200242 goto exit;
243 }
244
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200245 /*
246 * Verify signature
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200249 fflush( stdout );
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200252 hash, sizeof( hash ),
253 sig, sig_len ) ) != 0 )
254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200256 goto exit;
257 }
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200260
Andres Amaya Garcia47c04112018-04-29 19:45:25 +0100261 exit_code = MBEDTLS_EXIT_SUCCESS;
262
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200263exit:
264
265#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200267 fflush( stdout ); getchar();
268#endif
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_ecdsa_free( &ctx_verify );
271 mbedtls_ecdsa_free( &ctx_sign );
272 mbedtls_ctr_drbg_free( &ctr_drbg );
273 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200274
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200275 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200278 ECPARAMS */