blob: d0dfc59db81256a83cee9e0a6a7e91f3c8198506 [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia47c04112018-04-29 19:45:25 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia47c04112018-04-29 19:45:25 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_ECDSA_C) && \
67 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/entropy.h"
69#include "mbedtls/ctr_drbg.h"
70#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000071#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020072
73#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000074#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020075
76/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020077 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020078 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020079#define VERBOSE
80
81/*
82 * Uncomment to force use of a specific curve
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020085
86#if !defined(ECPARAMS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020088#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
91 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000092int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
95 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020096 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020097}
98#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020099#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +0100100static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200101{
102 size_t i;
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200105 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200107 "0123456789ABCDEF" [buf[i] % 16] );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200112{
113 unsigned char buf[300];
114 size_t len;
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
117 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200120 return;
121 }
122
123 dump_buf( title, buf, len );
124}
125#else
126#define dump_buf( a, b, c )
127#define dump_pubkey( a, b )
128#endif
129
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200130int main( int argc, char *argv[] )
131{
Andres Amaya Garcia47c04112018-04-29 19:45:25 +0100132 int ret = 1;
133 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_ecdsa_context ctx_sign, ctx_verify;
135 mbedtls_entropy_context entropy;
136 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000137 unsigned char message[100];
138 unsigned char hash[32];
139 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200140 size_t sig_len;
141 const char *pers = "ecdsa";
142 ((void) argv);
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_ecdsa_init( &ctx_sign );
145 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200146 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200147
Janos Follath0a5154b2017-03-10 11:31:41 +0000148 memset( sig, 0, sizeof( sig ) );
149 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200150
151 if( argc != 1 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200154
155#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200157#endif
158
159 goto exit;
160 }
161
162 /*
163 * Generate a key pair for signing
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200166 fflush( stdout );
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200169 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200170 (const unsigned char *) pers,
171 strlen( pers ) ) ) != 0 )
172 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200173 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200174 goto exit;
175 }
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200178 fflush( stdout );
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
181 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200184 goto exit;
185 }
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200188
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200189 dump_pubkey( " + Public key: ", &ctx_sign );
190
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000192 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200193 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000194 mbedtls_printf( " . Computing message hash..." );
195 fflush( stdout );
196
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100197 if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100198 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100199 mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100200 goto exit;
201 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000202
203 mbedtls_printf( " ok\n" );
204
205 dump_buf( " + Hash: ", hash, sizeof( hash ) );
206
207 /*
208 * Sign message hash
209 */
210 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200211 fflush( stdout );
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200214 hash, sizeof( hash ),
215 sig, &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 {
Ercan Ozturkab713a92020-01-28 21:51:04 -0800218 mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200219 goto exit;
220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200222
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200223 dump_buf( " + Signature: ", sig, sig_len );
224
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200225 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200226 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200227 *
228 * We could use the same context for verification and signatures, but we
229 * chose to use a new one in order to make it clear that the verifying
230 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200233 fflush( stdout );
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200238 goto exit;
239 }
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200244 goto exit;
245 }
246
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200247 /*
248 * Verify signature
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200251 fflush( stdout );
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200254 hash, sizeof( hash ),
255 sig, sig_len ) ) != 0 )
256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200258 goto exit;
259 }
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200262
Andres Amaya Garcia47c04112018-04-29 19:45:25 +0100263 exit_code = MBEDTLS_EXIT_SUCCESS;
264
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200265exit:
266
267#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200269 fflush( stdout ); getchar();
270#endif
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_ecdsa_free( &ctx_verify );
273 mbedtls_ecdsa_free( &ctx_sign );
274 mbedtls_ctr_drbg_free( &ctr_drbg );
275 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200276
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200277 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200280 ECPARAMS */