blob: 1411e42b0254a9ee3c7397a872ee8afe76e802c4 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard013bffe2015-02-13 14:09:44 +000036#if defined(POLARSSL_ECDSA_C) && \
Rich Evans18b78c72015-02-11 14:06:19 +000037 defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_CTR_DRBG_C)
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"
Janos Follath5d96a3d2017-03-10 11:31:41 +000041#include "polarssl/sha256.h"
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020042
43#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000044#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020045
46/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020047 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020048 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020049#define VERBOSE
50
51/*
52 * Uncomment to force use of a specific curve
53 */
54#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020055
56#if !defined(ECPARAMS)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020057#define ECPARAMS ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020058#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020059
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020060#if !defined(POLARSSL_ECDSA_C) || \
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020061 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000062int main( void )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020063{
Rich Evansf90016a2015-01-19 14:26:37 +000064 polarssl_printf("POLARSSL_ECDSA_C and/or "
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +020065 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020066 return( 0 );
67}
68#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020069#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010070static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020071{
72 size_t i;
73
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "%s", title );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020075 for( i = 0; i < len; i++ )
Rich Evansf90016a2015-01-19 14:26:37 +000076 polarssl_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020077 "0123456789ABCDEF" [buf[i] % 16] );
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020079}
80
Paul Bakker8fc30b12013-11-25 13:29:43 +010081static void dump_pubkey( const char *title, ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020082{
83 unsigned char buf[300];
84 size_t len;
85
86 if( ecp_point_write_binary( &key->grp, &key->Q,
87 POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
88 {
Rich Evansf90016a2015-01-19 14:26:37 +000089 polarssl_printf("internal error\n");
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020090 return;
91 }
92
93 dump_buf( title, buf, len );
94}
95#else
96#define dump_buf( a, b, c )
97#define dump_pubkey( a, b )
98#endif
99
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200100int main( int argc, char *argv[] )
101{
102 int ret;
103 ecdsa_context ctx_sign, ctx_verify;
104 entropy_context entropy;
105 ctr_drbg_context ctr_drbg;
Janos Follath5d96a3d2017-03-10 11:31:41 +0000106 sha256_context sha256_ctx;
107 unsigned char message[100];
108 unsigned char hash[32];
109 unsigned char sig[POLARSSL_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200110 size_t sig_len;
111 const char *pers = "ecdsa";
112 ((void) argv);
113
114 ecdsa_init( &ctx_sign );
115 ecdsa_init( &ctx_verify );
Janos Follath5d96a3d2017-03-10 11:31:41 +0000116 sha256_init( &sha256_ctx );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200117
Janos Follath5d96a3d2017-03-10 11:31:41 +0000118 memset( sig, 0, sizeof( sig ) );
119 memset( message, 0x25, sizeof( message ) );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200120 ret = 1;
121
122 if( argc != 1 )
123 {
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf( "usage: ecdsa\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200125
126#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000127 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200128#endif
129
130 goto exit;
131 }
132
133 /*
134 * Generate a key pair for signing
135 */
Rich Evansf90016a2015-01-19 14:26:37 +0000136 polarssl_printf( "\n . Seeding the random number generator..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200137 fflush( stdout );
138
139 entropy_init( &entropy );
140 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
141 (const unsigned char *) pers,
142 strlen( pers ) ) ) != 0 )
143 {
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200145 goto exit;
146 }
147
Rich Evansf90016a2015-01-19 14:26:37 +0000148 polarssl_printf( " ok\n . Generating key pair..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200149 fflush( stdout );
150
151 if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS,
152 ctr_drbg_random, &ctr_drbg ) ) != 0 )
153 {
Rich Evansf90016a2015-01-19 14:26:37 +0000154 polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200155 goto exit;
156 }
157
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200159
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200160 dump_pubkey( " + Public key: ", &ctx_sign );
161
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200162 /*
Janos Follath5d96a3d2017-03-10 11:31:41 +0000163 * Compute message hash
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200164 */
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( " . Signing message..." );
Janos Follath5d96a3d2017-03-10 11:31:41 +0000166 polarssl_printf( " . Computing message hash..." );
167 fflush( stdout );
168
169 sha256_starts( &sha256_ctx, 0 );
170 sha256_update( &sha256_ctx, message, sizeof( message ) );
171 sha256_finish( &sha256_ctx, hash );
172
173 polarssl_printf( " ok\n" );
174
175 dump_buf( " + Hash: ", hash, sizeof( hash ) );
176
177 /*
178 * Sign message hash
179 */
180 polarssl_printf( " . Signing message hash..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200181 fflush( stdout );
182
183 if( ( ret = ecdsa_write_signature( &ctx_sign,
184 hash, sizeof( hash ),
185 sig, &sig_len,
186 ctr_drbg_random, &ctr_drbg ) ) != 0 )
187 {
Rich Evansf90016a2015-01-19 14:26:37 +0000188 polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200189 goto exit;
190 }
Rich Evansf90016a2015-01-19 14:26:37 +0000191 polarssl_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200192
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200193 dump_buf( " + Signature: ", sig, sig_len );
194
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200195 /*
196 * Signature is serialized as defined by RFC 4492 p. 20,
197 * but one can also access 'r' and 's' directly from the context
198 */
199#ifdef POLARSSL_FS_IO
200 mpi_write_file( " r = ", &ctx_sign.r, 16, NULL );
201 mpi_write_file( " s = ", &ctx_sign.s, 16, NULL );
202#endif
203
204 /*
205 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200206 *
207 * We could use the same context for verification and signatures, but we
208 * chose to use a new one in order to make it clear that the verifying
209 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200210 */
Rich Evansf90016a2015-01-19 14:26:37 +0000211 polarssl_printf( " . Preparing verification context..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200212 fflush( stdout );
213
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200214 if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200215 {
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( " failed\n ! ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200217 goto exit;
218 }
219
220 if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
221 {
Rich Evansf90016a2015-01-19 14:26:37 +0000222 polarssl_printf( " failed\n ! ecp_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200223 goto exit;
224 }
225
226 ret = 0;
227
228 /*
229 * Verify signature
230 */
Rich Evansf90016a2015-01-19 14:26:37 +0000231 polarssl_printf( " ok\n . Verifying signature..." );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232 fflush( stdout );
233
234 if( ( ret = ecdsa_read_signature( &ctx_verify,
235 hash, sizeof( hash ),
236 sig, sig_len ) ) != 0 )
237 {
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf( " failed\n ! ecdsa_read_signature returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200239 goto exit;
240 }
241
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " ok\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200243
244exit:
245
246#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " + Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200248 fflush( stdout ); getchar();
249#endif
250
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200251 ecdsa_free( &ctx_verify );
252 ecdsa_free( &ctx_sign );
Paul Bakkera317a982014-06-18 16:44:11 +0200253 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200254 entropy_free( &entropy );
Janos Follath5d96a3d2017-03-10 11:31:41 +0000255 sha256_free( &sha256_ctx );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200256
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200257 return( ret );
258}
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200259#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200260 ECPARAMS */