blob: 72c8c4dc208139eea2cb813ef04054aefb3cfe80 [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
29#include "polarssl/entropy.h"
30#include "polarssl/ctr_drbg.h"
31#include "polarssl/ecdsa.h"
32
33#include <string.h>
34#include <stdio.h>
35
36/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020037 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020038 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020039#define VERBOSE
40
41/*
42 * Uncomment to force use of a specific curve
43 */
44#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020045
46#if !defined(ECPARAMS)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020047#define ECPARAMS ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020048#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020049
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020050#if !defined(POLARSSL_ECDSA_C) || \
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020051 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020052int main( int argc, char *argv[] )
53{
54 ((void) argc);
55 ((void) argv);
56
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020057 printf("POLARSSL_ECDSA_C and/or "
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +020058 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020059 return( 0 );
60}
61#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020062
63#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010064static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020065{
66 size_t i;
67
68 printf( "%s", title );
69 for( i = 0; i < len; i++ )
70 printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
71 "0123456789ABCDEF" [buf[i] % 16] );
72 printf( "\n" );
73}
74
Paul Bakker8fc30b12013-11-25 13:29:43 +010075static void dump_pubkey( const char *title, ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020076{
77 unsigned char buf[300];
78 size_t len;
79
80 if( ecp_point_write_binary( &key->grp, &key->Q,
81 POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
82 {
83 printf("internal error\n");
84 return;
85 }
86
87 dump_buf( title, buf, len );
88}
89#else
90#define dump_buf( a, b, c )
91#define dump_pubkey( a, b )
92#endif
93
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020094int main( int argc, char *argv[] )
95{
96 int ret;
97 ecdsa_context ctx_sign, ctx_verify;
98 entropy_context entropy;
99 ctr_drbg_context ctr_drbg;
100 unsigned char hash[] = "This should be the hash of a message.";
101 unsigned char sig[512];
102 size_t sig_len;
103 const char *pers = "ecdsa";
104 ((void) argv);
105
106 ecdsa_init( &ctx_sign );
107 ecdsa_init( &ctx_verify );
108
109 memset(sig, 0, sizeof( sig ) );
110 ret = 1;
111
112 if( argc != 1 )
113 {
114 printf( "usage: ecdsa\n" );
115
116#if defined(_WIN32)
117 printf( "\n" );
118#endif
119
120 goto exit;
121 }
122
123 /*
124 * Generate a key pair for signing
125 */
126 printf( "\n . Seeding the random number generator..." );
127 fflush( stdout );
128
129 entropy_init( &entropy );
130 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
131 (const unsigned char *) pers,
132 strlen( pers ) ) ) != 0 )
133 {
134 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
135 goto exit;
136 }
137
138 printf( " ok\n . Generating key pair..." );
139 fflush( stdout );
140
141 if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS,
142 ctr_drbg_random, &ctr_drbg ) ) != 0 )
143 {
144 printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
145 goto exit;
146 }
147
148 printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
149
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200150 dump_pubkey( " + Public key: ", &ctx_sign );
151
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200152 /*
153 * Sign some message hash
154 */
155 printf( " . Signing message..." );
156 fflush( stdout );
157
158 if( ( ret = ecdsa_write_signature( &ctx_sign,
159 hash, sizeof( hash ),
160 sig, &sig_len,
161 ctr_drbg_random, &ctr_drbg ) ) != 0 )
162 {
163 printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
164 goto exit;
165 }
Paul Bakkercaf0e602013-12-30 19:15:48 +0100166 printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200167
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200168 dump_buf( " + Hash: ", hash, sizeof hash );
169 dump_buf( " + Signature: ", sig, sig_len );
170
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200171 /*
172 * Signature is serialized as defined by RFC 4492 p. 20,
173 * but one can also access 'r' and 's' directly from the context
174 */
175#ifdef POLARSSL_FS_IO
176 mpi_write_file( " r = ", &ctx_sign.r, 16, NULL );
177 mpi_write_file( " s = ", &ctx_sign.s, 16, NULL );
178#endif
179
180 /*
181 * Transfer public information to verifying context
Manuel Pégourié-Gonnard4e3e7c22014-07-08 13:05:49 +0200182 *
183 * We could use the same context for verification and signatures, but we
184 * chose to use a new one in order to make it clear that the verifying
185 * context only needs the public key (Q), and not the private key (d).
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200186 */
187 printf( " . Preparing verification context..." );
188 fflush( stdout );
189
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200190 if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200191 {
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200192 printf( " failed\n ! ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200193 goto exit;
194 }
195
196 if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
197 {
198 printf( " failed\n ! ecp_copy returned %d\n", ret );
199 goto exit;
200 }
201
202 ret = 0;
203
204 /*
205 * Verify signature
206 */
207 printf( " ok\n . Verifying signature..." );
208 fflush( stdout );
209
210 if( ( ret = ecdsa_read_signature( &ctx_verify,
211 hash, sizeof( hash ),
212 sig, sig_len ) ) != 0 )
213 {
214 printf( " failed\n ! ecdsa_read_signature returned %d\n", ret );
215 goto exit;
216 }
217
218 printf( " ok\n" );
219
220exit:
221
222#if defined(_WIN32)
223 printf( " + Press Enter to exit this program.\n" );
224 fflush( stdout ); getchar();
225#endif
226
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200227 ecdsa_free( &ctx_verify );
228 ecdsa_free( &ctx_sign );
Paul Bakkera317a982014-06-18 16:44:11 +0200229 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200230 entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200231
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200232 return( ret );
233}
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200234#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200235 ECPARAMS */