blob: bdb356750e3192b80f1f4b5d40b9a27fb81cfb4d [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
4 * Copyright (C) 2006-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
26/*
27 * References:
28 *
29 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
30 */
31
32#include "polarssl/config.h"
33
34#if defined(POLARSSL_ECDSA_C)
35
36#include "polarssl/ecdsa.h"
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020037#include "polarssl/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010038
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010039/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010040 * Derive a suitable integer for group grp from a buffer of length len
41 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
42 */
43static int derive_mpi( const ecp_group *grp, mpi *x,
44 const unsigned char *buf, size_t blen )
45{
46 size_t n_size = (grp->nbits + 7) / 8;
47 return( mpi_read_binary( x, buf, blen > n_size ? n_size : blen ) );
48}
49
50/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010051 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
52 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
53 */
54int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
55 const mpi *d, const unsigned char *buf, size_t blen,
56 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
57{
58 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010059 ecp_point R;
60 mpi k, e;
61
62 ecp_point_init( &R );
63 mpi_init( &k );
64 mpi_init( &e );
65
66 sign_tries = 0;
67 do
68 {
69 /*
70 * Steps 1-3: generate a suitable ephemeral keypair
71 */
72 key_tries = 0;
73 do
74 {
75 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
76 MPI_CHK( mpi_copy( r, &R.X ) );
77
78 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +020079 {
80 ret = POLARSSL_ERR_ECP_GENERIC;
81 goto cleanup;
82 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010083 }
84 while( mpi_cmp_int( r, 0 ) == 0 );
85
86 /*
87 * Step 5: derive MPI from hashed message
88 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010089 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010090
91 /*
92 * Step 6: compute s = (e + r * d) / k mod n
93 */
94 MPI_CHK( mpi_mul_mpi( s, r, d ) );
95 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
96 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
97 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
98 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
99
100 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200101 {
102 ret = POLARSSL_ERR_ECP_GENERIC;
103 goto cleanup;
104 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100105 }
106 while( mpi_cmp_int( s, 0 ) == 0 );
107
108cleanup:
109 ecp_point_free( &R );
110 mpi_free( &k );
111 mpi_free( &e );
112
113 return( ret );
114}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100115
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100116/*
117 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
118 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
119 */
120int ecdsa_verify( const ecp_group *grp,
121 const unsigned char *buf, size_t blen,
122 const ecp_point *Q, const mpi *r, const mpi *s)
123{
124 int ret;
125 mpi e, s_inv, u1, u2;
126 ecp_point R, P;
127
128 ecp_point_init( &R ); ecp_point_init( &P );
129 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
130
131 /*
132 * Step 1: make sure r and s are in range 1..n-1
133 */
134 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
135 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
136 {
Paul Bakkercca998a2013-07-26 14:20:53 +0200137 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
138 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100139 }
140
141 /*
142 * Additional precaution: make sure Q is valid
143 */
144 MPI_CHK( ecp_check_pubkey( grp, Q ) );
145
146 /*
147 * Step 3: derive MPI from hashed message
148 */
149 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
150
151 /*
152 * Step 4: u1 = e / s mod n, u2 = r / s mod n
153 */
154 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
155
156 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
157 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
158
159 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
160 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
161
162 /*
163 * Step 5: R = u1 G + u2 Q
164 */
165 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G ) );
166 MPI_CHK( ecp_mul( grp, &P, &u2, Q ) );
167 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
168
169 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200170 {
171 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
172 goto cleanup;
173 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100174
175 /*
176 * Step 6: check that xR == r
177 */
178 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200179 {
180 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
181 goto cleanup;
182 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100183
184cleanup:
185 ecp_point_free( &R ); ecp_point_free( &P );
186 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
187
188 return( ret );
189}
190
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200191/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200192 * RFC 4492 page 20:
193 *
194 * Ecdsa-Sig-Value ::= SEQUENCE {
195 * r INTEGER,
196 * s INTEGER
197 * }
198 *
199 * Size is at most
200 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
201 * twice that + 1 (tag) + 2 (len) for the sequence
202 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
203 * and less than 124 (total len <= 255) for the sequence)
204 */
205#if POLARSSL_ECP_MAX_BYTES > 124
206#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
207#endif
208#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
209
210/*
211 * Compute and write signature
212 */
213int ecdsa_write_signature( ecdsa_context *ctx,
214 const unsigned char *hash, size_t hlen,
215 unsigned char *sig, size_t *slen,
216 int (*f_rng)(void *, unsigned char *, size_t),
217 void *p_rng )
218{
219 int ret;
220 unsigned char buf[MAX_SIG_LEN];
221 unsigned char *p = buf + MAX_SIG_LEN - 1;
222 size_t len = 0;
223
224 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
225 hash, hlen, f_rng, p_rng ) ) != 0 )
226 {
227 return( ret );
228 }
229
230 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
231 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
232
233 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
234 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
235 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
236
237 memcpy( sig, p, len );
238 *slen = len;
239
240 return( 0 );
241}
242
243/*
244 * Read and check signature
245 */
246int ecdsa_read_signature( ecdsa_context *ctx,
247 const unsigned char *hash, size_t hlen,
248 const unsigned char *sig, size_t slen )
249{
250 int ret;
251 unsigned char *p = (unsigned char *) sig;
252 const unsigned char *end = sig + slen;
253 size_t len;
254
255 if( ( ret = asn1_get_tag( &p, end, &len,
256 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
257 {
258 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
259 }
260
261 if( p + len != end )
262 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
263 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
264
265 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
266 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
267 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
268
269 if( p != end )
270 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
271 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
272
273 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
274}
275
276/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200277 * Generate key pair
278 */
279int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
280 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
281{
282 return( ecp_use_known_dp( &ctx->grp, gid ) ||
283 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
284}
285
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200286/*
287 * Set context from an ecp_keypair
288 */
289int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
290{
291 int ret = ecp_group_copy( &ctx->grp, &key->grp ) ||
292 mpi_copy( &ctx->d, &key->d ) ||
293 ecp_copy( &ctx->Q, &key->Q );
294
295 if( ret != 0 )
296 ecdsa_free( ctx );
297
298 return( ret );
299}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200300
301/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200302 * Initialize context
303 */
304void ecdsa_init( ecdsa_context *ctx )
305{
306 ecp_group_init( &ctx->grp );
307 mpi_init( &ctx->d );
308 ecp_point_init( &ctx->Q );
309 mpi_init( &ctx->r );
310 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200311}
312
313/*
314 * Free context
315 */
316void ecdsa_free( ecdsa_context *ctx )
317{
318 ecp_group_free( &ctx->grp );
319 mpi_free( &ctx->d );
320 ecp_point_free( &ctx->Q );
321 mpi_free( &ctx->r );
322 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200323}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100324
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100325#if defined(POLARSSL_SELF_TEST)
326
327/*
328 * Checkup routine
329 */
330int ecdsa_self_test( int verbose )
331{
332 return( verbose++ );
333}
334
335#endif
336
337#endif /* defined(POLARSSL_ECDSA_C) */