blob: 11b7f5e49d9feb00444c26c4951d2815431f9a8f [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"
37
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010038/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010039 * Derive a suitable integer for group grp from a buffer of length len
40 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
41 */
42static int derive_mpi( const ecp_group *grp, mpi *x,
43 const unsigned char *buf, size_t blen )
44{
45 size_t n_size = (grp->nbits + 7) / 8;
46 return( mpi_read_binary( x, buf, blen > n_size ? n_size : blen ) );
47}
48
49/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010050 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
51 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
52 */
53int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
54 const mpi *d, const unsigned char *buf, size_t blen,
55 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
56{
57 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010058 ecp_point R;
59 mpi k, e;
60
61 ecp_point_init( &R );
62 mpi_init( &k );
63 mpi_init( &e );
64
65 sign_tries = 0;
66 do
67 {
68 /*
69 * Steps 1-3: generate a suitable ephemeral keypair
70 */
71 key_tries = 0;
72 do
73 {
74 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
75 MPI_CHK( mpi_copy( r, &R.X ) );
76
77 if( key_tries++ > 10 )
78 return( POLARSSL_ERR_ECP_GENERIC );
79 }
80 while( mpi_cmp_int( r, 0 ) == 0 );
81
82 /*
83 * Step 5: derive MPI from hashed message
84 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010085 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010086
87 /*
88 * Step 6: compute s = (e + r * d) / k mod n
89 */
90 MPI_CHK( mpi_mul_mpi( s, r, d ) );
91 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
92 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
93 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
94 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
95
96 if( sign_tries++ > 10 )
97 return( POLARSSL_ERR_ECP_GENERIC );
98 }
99 while( mpi_cmp_int( s, 0 ) == 0 );
100
101cleanup:
102 ecp_point_free( &R );
103 mpi_free( &k );
104 mpi_free( &e );
105
106 return( ret );
107}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100108
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100109/*
110 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
111 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
112 */
113int ecdsa_verify( const ecp_group *grp,
114 const unsigned char *buf, size_t blen,
115 const ecp_point *Q, const mpi *r, const mpi *s)
116{
117 int ret;
118 mpi e, s_inv, u1, u2;
119 ecp_point R, P;
120
121 ecp_point_init( &R ); ecp_point_init( &P );
122 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
123
124 /*
125 * Step 1: make sure r and s are in range 1..n-1
126 */
127 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
128 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
129 {
130 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
131 }
132
133 /*
134 * Additional precaution: make sure Q is valid
135 */
136 MPI_CHK( ecp_check_pubkey( grp, Q ) );
137
138 /*
139 * Step 3: derive MPI from hashed message
140 */
141 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
142
143 /*
144 * Step 4: u1 = e / s mod n, u2 = r / s mod n
145 */
146 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
147
148 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
149 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
150
151 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
152 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
153
154 /*
155 * Step 5: R = u1 G + u2 Q
156 */
157 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G ) );
158 MPI_CHK( ecp_mul( grp, &P, &u2, Q ) );
159 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
160
161 if( ecp_is_zero( &R ) )
162 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
163
164 /*
165 * Step 6: check that xR == r
166 */
167 if( mpi_cmp_mpi( &R.X, r ) != 0 )
168 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
169
170cleanup:
171 ecp_point_free( &R ); ecp_point_free( &P );
172 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
173
174 return( ret );
175}
176
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200177/*
178 * Initialize context
179 */
180void ecdsa_init( ecdsa_context *ctx )
181{
182 ecp_group_init( &ctx->grp );
183 mpi_init( &ctx->d );
184 ecp_point_init( &ctx->Q );
185 mpi_init( &ctx->r );
186 mpi_init( &ctx->s );
187 mpi_init( &ctx->d );
188 ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED;
189}
190
191/*
192 * Free context
193 */
194void ecdsa_free( ecdsa_context *ctx )
195{
196 ecp_group_free( &ctx->grp );
197 mpi_free( &ctx->d );
198 ecp_point_free( &ctx->Q );
199 mpi_free( &ctx->r );
200 mpi_free( &ctx->s );
201 mpi_free( &ctx->d );
202 ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED;
203}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100204
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100205#if defined(POLARSSL_SELF_TEST)
206
207/*
208 * Checkup routine
209 */
210int ecdsa_self_test( int verbose )
211{
212 return( verbose++ );
213}
214
215#endif
216
217#endif /* defined(POLARSSL_ECDSA_C) */