blob: 3266bb89e2d4a51fbcce3c941beb10ad3c7079e9 [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
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.
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ECDSA_C) && \
29 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
32#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000033#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020034
35#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000036#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020037
38/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020039 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020040 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020041#define VERBOSE
42
43/*
44 * Uncomment to force use of a specific curve
45 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020047
48#if !defined(ECPARAMS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020050#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
53 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000054int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
57 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020058 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020059}
60#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020061#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010062static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020063{
64 size_t i;
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020067 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020069 "0123456789ABCDEF" [buf[i] % 16] );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020071}
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020074{
75 unsigned char buf[300];
76 size_t len;
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
79 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020082 return;
83 }
84
85 dump_buf( title, buf, len );
86}
87#else
88#define dump_buf( a, b, c )
89#define dump_pubkey( a, b )
90#endif
91
Simon Butcher63cb97e2018-12-06 17:43:31 +000092
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020093int main( int argc, char *argv[] )
94{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010095 int ret = 1;
96 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_ecdsa_context ctx_sign, ctx_verify;
98 mbedtls_entropy_context entropy;
99 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000100 unsigned char message[100];
101 unsigned char hash[32];
102 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200103 size_t sig_len;
104 const char *pers = "ecdsa";
105 ((void) argv);
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_ecdsa_init( &ctx_sign );
108 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200109 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200110
Janos Follath0a5154b2017-03-10 11:31:41 +0000111 memset( sig, 0, sizeof( sig ) );
112 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200113
114 if( argc != 1 )
115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200117
118#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200120#endif
121
122 goto exit;
123 }
124
125 /*
126 * Generate a key pair for signing
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200129 fflush( stdout );
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200132 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200133 (const unsigned char *) pers,
134 strlen( pers ) ) ) != 0 )
135 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200136 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200137 goto exit;
138 }
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200141 fflush( stdout );
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
144 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200147 goto exit;
148 }
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200151
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200152 dump_pubkey( " + Public key: ", &ctx_sign );
153
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200154 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000155 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200156 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000157 mbedtls_printf( " . Computing message hash..." );
158 fflush( stdout );
159
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100160 if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100161 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100162 mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100163 goto exit;
164 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000165
166 mbedtls_printf( " ok\n" );
167
168 dump_buf( " + Hash: ", hash, sizeof( hash ) );
169
170 /*
171 * Sign message hash
172 */
173 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200174 fflush( stdout );
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200177 hash, sizeof( hash ),
178 sig, &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200180 {
Ercan Ozturkd4373092020-01-28 21:51:04 -0800181 mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200182 goto exit;
183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200185
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200186 dump_buf( " + Signature: ", sig, sig_len );
187
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200188 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200189 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200190 *
191 * We could use the same context for verification and signatures, but we
192 * chose to use a new one in order to make it clear that the verifying
193 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200196 fflush( stdout );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200201 goto exit;
202 }
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200207 goto exit;
208 }
209
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200210 /*
211 * Verify signature
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200214 fflush( stdout );
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 hash, sizeof( hash ),
218 sig, sig_len ) ) != 0 )
219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221 goto exit;
222 }
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200225
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100226 exit_code = MBEDTLS_EXIT_SUCCESS;
227
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200228exit:
229
230#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232 fflush( stdout ); getchar();
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_ecdsa_free( &ctx_verify );
236 mbedtls_ecdsa_free( &ctx_sign );
237 mbedtls_ctr_drbg_free( &ctr_drbg );
238 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200239
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200240 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200243 ECPARAMS */