blob: 2479164a60b88010a9ee68e10bfcdbc20cf3e56f [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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 Garcia2602a1f2018-04-29 19:45:25 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia2602a1f2018-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 Stachowiak3b0c4302019-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
Simon Butcher63cb97e2018-12-06 17:43:31 +0000128
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200129int main( int argc, char *argv[] )
130{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100131 int ret = 1;
132 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_ecdsa_context ctx_sign, ctx_verify;
134 mbedtls_entropy_context entropy;
135 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000136 unsigned char message[100];
137 unsigned char hash[32];
138 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200139 size_t sig_len;
140 const char *pers = "ecdsa";
141 ((void) argv);
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_ecdsa_init( &ctx_sign );
144 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200145 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200146
Janos Follath0a5154b2017-03-10 11:31:41 +0000147 memset( sig, 0, sizeof( sig ) );
148 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200149
150 if( argc != 1 )
151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200153
154#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200156#endif
157
158 goto exit;
159 }
160
161 /*
162 * Generate a key pair for signing
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200165 fflush( stdout );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200168 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200169 (const unsigned char *) pers,
170 strlen( pers ) ) ) != 0 )
171 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200172 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200173 goto exit;
174 }
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200177 fflush( stdout );
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
180 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200183 goto exit;
184 }
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200187
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200188 dump_pubkey( " + Public key: ", &ctx_sign );
189
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200190 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000191 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200192 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000193 mbedtls_printf( " . Computing message hash..." );
194 fflush( stdout );
195
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100196 if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100197 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100198 mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100199 goto exit;
200 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000201
202 mbedtls_printf( " ok\n" );
203
204 dump_buf( " + Hash: ", hash, sizeof( hash ) );
205
206 /*
207 * Sign message hash
208 */
209 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200210 fflush( stdout );
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200213 hash, sizeof( hash ),
214 sig, &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200216 {
Ercan Ozturka46f75c2020-01-28 21:51:04 -0800217 mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200218 goto exit;
219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200222 dump_buf( " + Signature: ", sig, sig_len );
223
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200224 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200225 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200226 *
227 * We could use the same context for verification and signatures, but we
228 * chose to use a new one in order to make it clear that the verifying
229 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232 fflush( stdout );
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200237 goto exit;
238 }
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200243 goto exit;
244 }
245
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200246 /*
247 * Verify signature
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200250 fflush( stdout );
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200253 hash, sizeof( hash ),
254 sig, sig_len ) ) != 0 )
255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200257 goto exit;
258 }
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200261
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100262 exit_code = MBEDTLS_EXIT_SUCCESS;
263
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200264exit:
265
266#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200268 fflush( stdout ); getchar();
269#endif
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_ecdsa_free( &ctx_verify );
272 mbedtls_ecdsa_free( &ctx_sign );
273 mbedtls_ctr_drbg_free( &ctr_drbg );
274 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200275
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200276 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200277}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200279 ECPARAMS */