blob: 1035bb27331fb3256b36dbd5f24fd6bc99e93861 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_ECDSA_C) && \
25 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/entropy.h"
27#include "mbedtls/ctr_drbg.h"
28#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000029#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020030
31#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000032#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020033
34/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020035 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020036 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020037#define VERBOSE
38
39/*
40 * Uncomment to force use of a specific curve
41 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020043
44#if !defined(ECPARAMS)
Gilles Peskine5b8618b2021-09-28 12:34:53 +020045#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020046#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
49 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000050int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
53 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020054 mbedtls_exit( 0 );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020055}
56#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020057#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010058static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020059{
60 size_t i;
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020063 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020065 "0123456789ABCDEF" [buf[i] % 16] );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020067}
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020070{
71 unsigned char buf[300];
72 size_t len;
73
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +020074 if( mbedtls_ecp_point_write_binary( &key->MBEDTLS_PRIVATE(grp), &key->MBEDTLS_PRIVATE(Q),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020078 return;
79 }
80
81 dump_buf( title, buf, len );
82}
83#else
84#define dump_buf( a, b, c )
85#define dump_pubkey( a, b )
86#endif
87
Simon Butcher63cb97e2018-12-06 17:43:31 +000088
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020089int main( int argc, char *argv[] )
90{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010091 int ret = 1;
92 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_ecdsa_context ctx_sign, ctx_verify;
94 mbedtls_entropy_context entropy;
95 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +000096 unsigned char message[100];
97 unsigned char hash[32];
98 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020099 size_t sig_len;
100 const char *pers = "ecdsa";
101 ((void) argv);
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_ecdsa_init( &ctx_sign );
104 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200105 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200106
Janos Follath0a5154b2017-03-10 11:31:41 +0000107 memset( sig, 0, sizeof( sig ) );
108 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200109
110 if( argc != 1 )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200113
114#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200116#endif
117
118 goto exit;
119 }
120
121 /*
122 * Generate a key pair for signing
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200125 fflush( stdout );
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200128 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200129 (const unsigned char *) pers,
130 strlen( pers ) ) ) != 0 )
131 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200132 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200133 goto exit;
134 }
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200137 fflush( stdout );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
140 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200143 goto exit;
144 }
145
Gilles Peskinebf69ea52021-05-27 23:53:07 +0200146 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.MBEDTLS_PRIVATE(grp).pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200147
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200148 dump_pubkey( " + Public key: ", &ctx_sign );
149
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200150 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000151 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200152 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000153 mbedtls_printf( " . Computing message hash..." );
154 fflush( stdout );
155
TRodziewicz26371e42021-06-08 16:45:41 +0200156 if( ( ret = mbedtls_sha256( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100157 {
TRodziewicz26371e42021-06-08 16:45:41 +0200158 mbedtls_printf( " failed\n ! mbedtls_sha256 returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100159 goto exit;
160 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000161
162 mbedtls_printf( " ok\n" );
163
164 dump_buf( " + Hash: ", hash, sizeof( hash ) );
165
166 /*
167 * Sign message hash
168 */
169 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200170 fflush( stdout );
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200173 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +0200174 sig, sizeof( sig ), &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200176 {
Ercan Ozturkd4373092020-01-28 21:51:04 -0800177 mbedtls_printf( " failed\n ! mbedtls_ecdsa_write_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200178 goto exit;
179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200181
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200182 dump_buf( " + Signature: ", sig, sig_len );
183
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200184 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200185 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200186 *
187 * We could use the same context for verification and signatures, but we
188 * chose to use a new one in order to make it clear that the verifying
189 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200192 fflush( stdout );
193
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200194 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.MBEDTLS_PRIVATE(grp), &ctx_sign.MBEDTLS_PRIVATE(grp) ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200197 goto exit;
198 }
199
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200200 if( ( ret = mbedtls_ecp_copy( &ctx_verify.MBEDTLS_PRIVATE(Q), &ctx_sign.MBEDTLS_PRIVATE(Q) ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200203 goto exit;
204 }
205
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200206 /*
207 * Verify signature
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_printf( " ok\n . Verifying signature..." );
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_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200213 hash, sizeof( hash ),
214 sig, sig_len ) ) != 0 )
215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 goto exit;
218 }
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100222 exit_code = MBEDTLS_EXIT_SUCCESS;
223
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200224exit:
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_ecdsa_free( &ctx_verify );
227 mbedtls_ecdsa_free( &ctx_sign );
228 mbedtls_ctr_drbg_free( &ctr_drbg );
229 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200230
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200231 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200234 ECPARAMS */