blob: aaec2497bc3a7a4a6e384d2c480884e61b84eb3a [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2013, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02007 *
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020038#include "polarssl/entropy.h"
39#include "polarssl/ctr_drbg.h"
40#include "polarssl/ecdsa.h"
41
42#include <string.h>
43#include <stdio.h>
44
45/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020046 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020047 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020048#define VERBOSE
49
50/*
51 * Uncomment to force use of a specific curve
52 */
53#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020054
55#if !defined(ECPARAMS)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020056#define ECPARAMS ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020057#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020058
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020059#if !defined(POLARSSL_ECDSA_C) || \
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020060 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020061int main( int argc, char *argv[] )
62{
63 ((void) argc);
64 ((void) argv);
65
Rich Evansf90016a2015-01-19 14:26:37 +000066 polarssl_printf("POLARSSL_ECDSA_C and/or "
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +020067 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020068 return( 0 );
69}
70#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020071
72#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
Rich Evansf90016a2015-01-19 14:26:37 +000077 polarssl_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020078 for( i = 0; i < len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020080 "0123456789ABCDEF" [buf[i] % 16] );
Rich Evansf90016a2015-01-19 14:26:37 +000081 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020082}
83
Paul Bakker8fc30b12013-11-25 13:29:43 +010084static void dump_pubkey( const char *title, ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020085{
86 unsigned char buf[300];
87 size_t len;
88
89 if( ecp_point_write_binary( &key->grp, &key->Q,
90 POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
91 {
Rich Evansf90016a2015-01-19 14:26:37 +000092 polarssl_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
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200103int main( int argc, char *argv[] )
104{
105 int ret;
106 ecdsa_context ctx_sign, ctx_verify;
107 entropy_context entropy;
108 ctr_drbg_context ctr_drbg;
109 unsigned char hash[] = "This should be the hash of a message.";
110 unsigned char sig[512];
111 size_t sig_len;
112 const char *pers = "ecdsa";
113 ((void) argv);
114
115 ecdsa_init( &ctx_sign );
116 ecdsa_init( &ctx_verify );
117
118 memset(sig, 0, sizeof( sig ) );
119 ret = 1;
120
121 if( argc != 1 )
122 {
Rich Evansf90016a2015-01-19 14:26:37 +0000123 polarssl_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200124
125#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200127#endif
128
129 goto exit;
130 }
131
132 /*
133 * Generate a key pair for signing
134 */
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200136 fflush( stdout );
137
138 entropy_init( &entropy );
139 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
140 (const unsigned char *) pers,
141 strlen( pers ) ) ) != 0 )
142 {
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200144 goto exit;
145 }
146
Rich Evansf90016a2015-01-19 14:26:37 +0000147 polarssl_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200148 fflush( stdout );
149
150 if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS,
151 ctr_drbg_random, &ctr_drbg ) ) != 0 )
152 {
Rich Evansf90016a2015-01-19 14:26:37 +0000153 polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200154 goto exit;
155 }
156
Rich Evansf90016a2015-01-19 14:26:37 +0000157 polarssl_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200158
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200159 dump_pubkey( " + Public key: ", &ctx_sign );
160
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200161 /*
162 * Sign some message hash
163 */
Rich Evansf90016a2015-01-19 14:26:37 +0000164 polarssl_printf( " . Signing message..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200165 fflush( stdout );
166
167 if( ( ret = ecdsa_write_signature( &ctx_sign,
168 hash, sizeof( hash ),
169 sig, &sig_len,
170 ctr_drbg_random, &ctr_drbg ) ) != 0 )
171 {
Rich Evansf90016a2015-01-19 14:26:37 +0000172 polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200173 goto exit;
174 }
Rich Evansf90016a2015-01-19 14:26:37 +0000175 polarssl_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200176
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200177 dump_buf( " + Hash: ", hash, sizeof hash );
178 dump_buf( " + Signature: ", sig, sig_len );
179
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200180 /*
181 * Signature is serialized as defined by RFC 4492 p. 20,
182 * but one can also access 'r' and 's' directly from the context
183 */
184#ifdef POLARSSL_FS_IO
185 mpi_write_file( " r = ", &ctx_sign.r, 16, NULL );
186 mpi_write_file( " s = ", &ctx_sign.s, 16, NULL );
187#endif
188
189 /*
190 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200191 *
192 * We could use the same context for verification and signatures, but we
193 * chose to use a new one in order to make it clear that the verifying
194 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200195 */
Rich Evansf90016a2015-01-19 14:26:37 +0000196 polarssl_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200197 fflush( stdout );
198
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200199 if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200200 {
Rich Evansf90016a2015-01-19 14:26:37 +0000201 polarssl_printf( " failed\n ! ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200202 goto exit;
203 }
204
205 if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
206 {
Rich Evansf90016a2015-01-19 14:26:37 +0000207 polarssl_printf( " failed\n ! ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200208 goto exit;
209 }
210
211 ret = 0;
212
213 /*
214 * Verify signature
215 */
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 fflush( stdout );
218
219 if( ( ret = ecdsa_read_signature( &ctx_verify,
220 hash, sizeof( hash ),
221 sig, sig_len ) ) != 0 )
222 {
Rich Evansf90016a2015-01-19 14:26:37 +0000223 polarssl_printf( " failed\n ! ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200224 goto exit;
225 }
226
Rich Evansf90016a2015-01-19 14:26:37 +0000227 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200228
229exit:
230
231#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000232 polarssl_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200233 fflush( stdout ); getchar();
234#endif
235
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200236 ecdsa_free( &ctx_verify );
237 ecdsa_free( &ctx_sign );
Paul Bakkera317a982014-06-18 16:44:11 +0200238 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200239 entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200240
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200241 return( ret );
242}
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200243#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200244 ECPARAMS */