blob: b851c3173ad026c5a827a6ac45276b391bd8ec20 [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
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010034#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C) && \
40 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
43#include "mbedtls/ecdsa.h"
Janos Follath0a5154b2017-03-10 11:31:41 +000044#include "mbedtls/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020045
46#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000047#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020048
49/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020050 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020051 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020052#define VERBOSE
53
54/*
55 * Uncomment to force use of a specific curve
56 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020058
59#if !defined(ECPARAMS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020061#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
64 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000065int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
68 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020069 return( 0 );
70}
71#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020072#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010073static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020074{
75 size_t i;
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020078 for( i = 0; i < len; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020080 "0123456789ABCDEF" [buf[i] % 16] );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020082}
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020085{
86 unsigned char buf[300];
87 size_t len;
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
90 MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020093 return;
94 }
95
96 dump_buf( title, buf, len );
97}
98#else
99#define dump_buf( a, b, c )
100#define dump_pubkey( a, b )
101#endif
102
Simon Butcher63cb97e2018-12-06 17:43:31 +0000103
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200104int main( int argc, char *argv[] )
105{
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100106 int ret = 1;
107 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_ecdsa_context ctx_sign, ctx_verify;
109 mbedtls_entropy_context entropy;
110 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath0a5154b2017-03-10 11:31:41 +0000111 unsigned char message[100];
112 unsigned char hash[32];
113 unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200114 size_t sig_len;
115 const char *pers = "ecdsa";
116 ((void) argv);
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_ecdsa_init( &ctx_sign );
119 mbedtls_ecdsa_init( &ctx_verify );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200120 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200121
Janos Follath0a5154b2017-03-10 11:31:41 +0000122 memset( sig, 0, sizeof( sig ) );
123 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200124
125 if( argc != 1 )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128
129#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200131#endif
132
133 goto exit;
134 }
135
136 /*
137 * Generate a key pair for signing
138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200140 fflush( stdout );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200143 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200144 (const unsigned char *) pers,
145 strlen( pers ) ) ) != 0 )
146 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200147 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200148 goto exit;
149 }
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200152 fflush( stdout );
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
155 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200158 goto exit;
159 }
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200162
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200163 dump_pubkey( " + Public key: ", &ctx_sign );
164
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200165 /*
Janos Follath0a5154b2017-03-10 11:31:41 +0000166 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200167 */
Janos Follath0a5154b2017-03-10 11:31:41 +0000168 mbedtls_printf( " . Computing message hash..." );
169 fflush( stdout );
170
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100171 if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100172 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100173 mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100174 goto exit;
175 }
Janos Follath0a5154b2017-03-10 11:31:41 +0000176
177 mbedtls_printf( " ok\n" );
178
179 dump_buf( " + Hash: ", hash, sizeof( hash ) );
180
181 /*
182 * Sign message hash
183 */
184 mbedtls_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200185 fflush( stdout );
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200188 hash, sizeof( hash ),
189 sig, &sig_len,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200193 goto exit;
194 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200196
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200197 dump_buf( " + Signature: ", sig, sig_len );
198
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200199 /*
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200200 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200201 *
202 * We could use the same context for verification and signatures, but we
203 * chose to use a new one in order to make it clear that the verifying
204 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200207 fflush( stdout );
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200212 goto exit;
213 }
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200218 goto exit;
219 }
220
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200221 /*
222 * Verify signature
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200225 fflush( stdout );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200228 hash, sizeof( hash ),
229 sig, sig_len ) ) != 0 )
230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232 goto exit;
233 }
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200236
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100237 exit_code = MBEDTLS_EXIT_SUCCESS;
238
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200239exit:
240
241#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200243 fflush( stdout ); getchar();
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_ecdsa_free( &ctx_verify );
247 mbedtls_ecdsa_free( &ctx_sign );
248 mbedtls_ctr_drbg_free( &ctr_drbg );
249 mbedtls_entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200250
Andres Amaya Garcia2602a1f2018-04-29 19:45:25 +0100251 return( exit_code );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200254 ECPARAMS */