blob: 0b4f8c42b8ae2263d2a2072a47c881b38a9faf69 [file] [log] [blame]
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +02001/*
2 * Example ECDSA program
3 *
4 * Copyright (C) 2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020031
32#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
34#include "polarssl/ecdsa.h"
35
36#include <string.h>
37#include <stdio.h>
38
39/*
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020040 * Uncomment to show key and signature details
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020041 */
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020042#define VERBOSE
43
44/*
45 * Uncomment to force use of a specific curve
46 */
47#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020048
49#if !defined(ECPARAMS)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020050#define ECPARAMS ecp_curve_list()->grp_id
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020051#endif
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020052
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020053#if !defined(POLARSSL_ECDSA_C) || \
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020054 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020055int main( int argc, char *argv[] )
56{
57 ((void) argc);
58 ((void) argv);
59
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +020060 printf("POLARSSL_ECDSA_C and/or "
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +020061 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n");
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020062 return( 0 );
63}
64#else
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020065
66#if defined(VERBOSE)
Paul Bakker8fc30b12013-11-25 13:29:43 +010067static void dump_buf( const char *title, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020068{
69 size_t i;
70
71 printf( "%s", title );
72 for( i = 0; i < len; i++ )
73 printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
74 "0123456789ABCDEF" [buf[i] % 16] );
75 printf( "\n" );
76}
77
Paul Bakker8fc30b12013-11-25 13:29:43 +010078static void dump_pubkey( const char *title, ecdsa_context *key )
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +020079{
80 unsigned char buf[300];
81 size_t len;
82
83 if( ecp_point_write_binary( &key->grp, &key->Q,
84 POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
85 {
86 printf("internal error\n");
87 return;
88 }
89
90 dump_buf( title, buf, len );
91}
92#else
93#define dump_buf( a, b, c )
94#define dump_pubkey( a, b )
95#endif
96
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020097int main( int argc, char *argv[] )
98{
99 int ret;
100 ecdsa_context ctx_sign, ctx_verify;
101 entropy_context entropy;
102 ctr_drbg_context ctr_drbg;
103 unsigned char hash[] = "This should be the hash of a message.";
104 unsigned char sig[512];
105 size_t sig_len;
106 const char *pers = "ecdsa";
107 ((void) argv);
108
109 ecdsa_init( &ctx_sign );
110 ecdsa_init( &ctx_verify );
111
112 memset(sig, 0, sizeof( sig ) );
113 ret = 1;
114
115 if( argc != 1 )
116 {
117 printf( "usage: ecdsa\n" );
118
119#if defined(_WIN32)
120 printf( "\n" );
121#endif
122
123 goto exit;
124 }
125
126 /*
127 * Generate a key pair for signing
128 */
129 printf( "\n . Seeding the random number generator..." );
130 fflush( stdout );
131
132 entropy_init( &entropy );
133 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
134 (const unsigned char *) pers,
135 strlen( pers ) ) ) != 0 )
136 {
137 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
138 goto exit;
139 }
140
141 printf( " ok\n . Generating key pair..." );
142 fflush( stdout );
143
144 if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS,
145 ctr_drbg_random, &ctr_drbg ) ) != 0 )
146 {
147 printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
148 goto exit;
149 }
150
151 printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
152
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200153 dump_pubkey( " + Public key: ", &ctx_sign );
154
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200155 /*
156 * Sign some message hash
157 */
158 printf( " . Signing message..." );
159 fflush( stdout );
160
161 if( ( ret = ecdsa_write_signature( &ctx_sign,
162 hash, sizeof( hash ),
163 sig, &sig_len,
164 ctr_drbg_random, &ctr_drbg ) ) != 0 )
165 {
166 printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
167 goto exit;
168 }
Paul Bakkercaf0e602013-12-30 19:15:48 +0100169 printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200170
Manuel Pégourié-Gonnardb0a467f2013-09-21 12:31:05 +0200171 dump_buf( " + Hash: ", hash, sizeof hash );
172 dump_buf( " + Signature: ", sig, sig_len );
173
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200174 /*
175 * Signature is serialized as defined by RFC 4492 p. 20,
176 * but one can also access 'r' and 's' directly from the context
177 */
178#ifdef POLARSSL_FS_IO
179 mpi_write_file( " r = ", &ctx_sign.r, 16, NULL );
180 mpi_write_file( " s = ", &ctx_sign.s, 16, NULL );
181#endif
182
183 /*
184 * Transfer public information to verifying context
185 */
186 printf( " . Preparing verification context..." );
187 fflush( stdout );
188
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200189 if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200190 {
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200191 printf( " failed\n ! ecp_group_copy returned %d\n", ret );
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200192 goto exit;
193 }
194
195 if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
196 {
197 printf( " failed\n ! ecp_copy returned %d\n", ret );
198 goto exit;
199 }
200
201 ret = 0;
202
203 /*
204 * Verify signature
205 */
206 printf( " ok\n . Verifying signature..." );
207 fflush( stdout );
208
209 if( ( ret = ecdsa_read_signature( &ctx_verify,
210 hash, sizeof( hash ),
211 sig, sig_len ) ) != 0 )
212 {
213 printf( " failed\n ! ecdsa_read_signature returned %d\n", ret );
214 goto exit;
215 }
216
217 printf( " ok\n" );
218
219exit:
220
221#if defined(_WIN32)
222 printf( " + Press Enter to exit this program.\n" );
223 fflush( stdout ); getchar();
224#endif
225
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200226 ecdsa_free( &ctx_verify );
227 ecdsa_free( &ctx_sign );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200228 entropy_free( &entropy );
Manuel Pégourié-Gonnardbf3109f2013-08-14 21:36:01 +0200229
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200230 return( ret );
231}
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200232#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200233 ECPARAMS */