Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 37 | #include "polarssl/asn1write.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 39 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Simplified HMAC_DRBG context. |
| 42 | * No reseed counter, no prediction resistance flag. |
| 43 | */ |
| 44 | typedef struct |
| 45 | { |
| 46 | md_context_t md_ctx; |
| 47 | unsigned char V[POLARSSL_MD_MAX_SIZE]; |
| 48 | unsigned char K[POLARSSL_MD_MAX_SIZE]; |
| 49 | } hmac_drbg_context; |
| 50 | |
| 51 | /* |
| 52 | * Simplified HMAC_DRBG initialisation. |
| 53 | * |
| 54 | * Uses an entropy buffer rather than callback, |
| 55 | * assumes personalisation is not null, |
| 56 | * assumes md_info is not NULL and valid. |
| 57 | */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame^] | 58 | static void hmac_drbg_init( hmac_drbg_context *ctx, |
| 59 | const md_info_t * md_info, |
| 60 | const unsigned char *entropy, size_t entropy_len, |
| 61 | const unsigned char *pers, size_t pers_len ) |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 62 | { |
| 63 | unsigned char sep[1]; |
| 64 | size_t md_len = md_info->size; |
| 65 | |
| 66 | memset( ctx, 0, sizeof( hmac_drbg_context ) ); |
| 67 | md_init_ctx( &ctx->md_ctx, md_info ); |
| 68 | |
| 69 | memset( ctx->V, 0x01, md_len ); |
| 70 | /* ctx->K is already 0 */ |
| 71 | |
| 72 | sep[0] = 0x00; |
| 73 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 74 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 75 | md_hmac_update( &ctx->md_ctx, sep, 1 ); |
| 76 | md_hmac_update( &ctx->md_ctx, entropy, entropy_len ); |
| 77 | md_hmac_update( &ctx->md_ctx, pers, pers_len ); |
| 78 | md_hmac_finish( &ctx->md_ctx, ctx->K ); |
| 79 | |
| 80 | /* Step e */ |
| 81 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 82 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 83 | md_hmac_finish( &ctx->md_ctx, ctx->V ); |
| 84 | |
| 85 | /* Step f */ |
| 86 | sep[0] = 0x01; |
| 87 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 88 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 89 | md_hmac_update( &ctx->md_ctx, sep, 1 ); |
| 90 | md_hmac_update( &ctx->md_ctx, entropy, entropy_len ); |
| 91 | md_hmac_update( &ctx->md_ctx, pers, pers_len ); |
| 92 | md_hmac_finish( &ctx->md_ctx, ctx->K ); |
| 93 | |
| 94 | /* Step g */ |
| 95 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 96 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 97 | md_hmac_finish( &ctx->md_ctx, ctx->V ); |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Simplified HMAC_DRBG random function |
| 102 | */ |
| 103 | static int hmac_drbg_random( void *state, |
| 104 | unsigned char *output, size_t out_len ) |
| 105 | { |
| 106 | hmac_drbg_context *ctx = (hmac_drbg_context *) state; |
| 107 | unsigned char sep[1] = { 0 }; |
| 108 | size_t md_len = ctx->md_ctx.md_info->size; |
| 109 | size_t left = out_len; |
| 110 | unsigned char *out = output; |
| 111 | |
| 112 | while( left != 0 ) |
| 113 | { |
| 114 | size_t use_len = left > md_len ? md_len : left; |
| 115 | |
| 116 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 117 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 118 | md_hmac_finish( &ctx->md_ctx, ctx->V ); |
| 119 | |
| 120 | memcpy( out, ctx->V, use_len ); |
| 121 | out += use_len; |
| 122 | left -= use_len; |
| 123 | } |
| 124 | |
| 125 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 126 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 127 | md_hmac_update( &ctx->md_ctx, sep, 1 ); |
| 128 | md_hmac_finish( &ctx->md_ctx, ctx->K ); |
| 129 | |
| 130 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 131 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 132 | md_hmac_finish( &ctx->md_ctx, ctx->V ); |
| 133 | |
| 134 | return( 0 ); |
| 135 | } |
| 136 | |
| 137 | static void hmac_drbg_free( hmac_drbg_context *ctx ) |
| 138 | { |
| 139 | if( ctx == NULL ) |
| 140 | return; |
| 141 | |
| 142 | md_free_ctx( &ctx->md_ctx ); |
| 143 | |
| 144 | memset( ctx, 0, sizeof( hmac_drbg_context ) ); |
| 145 | } |
| 146 | #endif |
| 147 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 148 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 149 | * Derive a suitable integer for group grp from a buffer of length len |
| 150 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 151 | */ |
| 152 | static int derive_mpi( const ecp_group *grp, mpi *x, |
| 153 | const unsigned char *buf, size_t blen ) |
| 154 | { |
Manuel Pégourié-Gonnard | e7072f8 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 155 | int ret; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 156 | size_t n_size = (grp->nbits + 7) / 8; |
Manuel Pégourié-Gonnard | e7072f8 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 157 | size_t use_size = blen > n_size ? n_size : blen; |
| 158 | |
| 159 | MPI_CHK( mpi_read_binary( x, buf, use_size ) ); |
| 160 | if( use_size * 8 > grp->nbits ) |
| 161 | MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) ); |
| 162 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 163 | /* While at it, reduce modulo N */ |
| 164 | if( mpi_cmp_mpi( x, &grp->N ) >= 0 ) |
| 165 | MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) ); |
| 166 | |
Manuel Pégourié-Gonnard | e7072f8 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 167 | cleanup: |
| 168 | return( ret ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 172 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 173 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 174 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 175 | int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s, |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 176 | const mpi *d, const unsigned char *buf, size_t blen, |
| 177 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 178 | { |
| 179 | int ret, key_tries, sign_tries; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 180 | ecp_point R; |
| 181 | mpi k, e; |
| 182 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 183 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 184 | if( grp->N.p == NULL ) |
| 185 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 186 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 187 | ecp_point_init( &R ); |
| 188 | mpi_init( &k ); |
| 189 | mpi_init( &e ); |
| 190 | |
| 191 | sign_tries = 0; |
| 192 | do |
| 193 | { |
| 194 | /* |
| 195 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 196 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 197 | */ |
| 198 | key_tries = 0; |
| 199 | do |
| 200 | { |
| 201 | MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 202 | MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 203 | |
| 204 | if( key_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 205 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 206 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 207 | goto cleanup; |
| 208 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 209 | } |
| 210 | while( mpi_cmp_int( r, 0 ) == 0 ); |
| 211 | |
| 212 | /* |
| 213 | * Step 5: derive MPI from hashed message |
| 214 | */ |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 215 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Step 6: compute s = (e + r * d) / k mod n |
| 219 | */ |
| 220 | MPI_CHK( mpi_mul_mpi( s, r, d ) ); |
| 221 | MPI_CHK( mpi_add_mpi( &e, &e, s ) ); |
| 222 | MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) ); |
| 223 | MPI_CHK( mpi_mul_mpi( s, s, &e ) ); |
| 224 | MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) ); |
| 225 | |
| 226 | if( sign_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 227 | { |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 228 | ret = POLARSSL_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 229 | goto cleanup; |
| 230 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 231 | } |
| 232 | while( mpi_cmp_int( s, 0 ) == 0 ); |
| 233 | |
| 234 | cleanup: |
| 235 | ecp_point_free( &R ); |
| 236 | mpi_free( &k ); |
| 237 | mpi_free( &e ); |
| 238 | |
| 239 | return( ret ); |
| 240 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 241 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame^] | 242 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 243 | /* |
| 244 | * Deterministic signature wrapper |
| 245 | */ |
| 246 | int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s, |
| 247 | const mpi *d, const unsigned char *buf, size_t blen, |
| 248 | md_type_t md_alg ) |
| 249 | { |
| 250 | int ret; |
| 251 | hmac_drbg_context rng_ctx; |
| 252 | unsigned char key[POLARSSL_ECP_MAX_BYTES]; |
| 253 | unsigned char hash[POLARSSL_ECP_MAX_BYTES]; |
| 254 | size_t grp_len = ( grp->nbits + 7 ) / 8; |
| 255 | const md_info_t *md_info; |
| 256 | mpi h; |
| 257 | |
| 258 | if( ( md_info = md_info_from_type( md_alg ) ) == NULL ) |
| 259 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 260 | |
| 261 | mpi_init( &h ); |
| 262 | memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) ); |
| 263 | |
| 264 | /* Export private key as entropy source */ |
| 265 | MPI_CHK( mpi_write_binary( d, key, grp_len ) ); |
| 266 | |
| 267 | /* Export message hash as additional data; need to reduce it first */ |
| 268 | MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); |
| 269 | MPI_CHK( mpi_write_binary( &h, hash, grp_len ) ); |
| 270 | |
| 271 | /* Initialize HMAC_DRBG and use it for signature */ |
| 272 | hmac_drbg_init( &rng_ctx, md_info, key, grp_len, hash, grp_len ); |
| 273 | ret = ecdsa_sign( grp, r, s, d, buf, blen, |
| 274 | hmac_drbg_random, &rng_ctx ); |
| 275 | |
| 276 | cleanup: |
| 277 | hmac_drbg_free( &rng_ctx ); |
| 278 | mpi_free( &h ); |
| 279 | |
| 280 | return( ret ); |
| 281 | } |
| 282 | #endif |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 283 | /* |
| 284 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 285 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 286 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 287 | int ecdsa_verify( ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 288 | const unsigned char *buf, size_t blen, |
| 289 | const ecp_point *Q, const mpi *r, const mpi *s) |
| 290 | { |
| 291 | int ret; |
| 292 | mpi e, s_inv, u1, u2; |
| 293 | ecp_point R, P; |
| 294 | |
| 295 | ecp_point_init( &R ); ecp_point_init( &P ); |
| 296 | mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 ); |
| 297 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 298 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 299 | if( grp->N.p == NULL ) |
| 300 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 301 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 302 | /* |
| 303 | * Step 1: make sure r and s are in range 1..n-1 |
| 304 | */ |
| 305 | if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 || |
| 306 | mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
| 307 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 308 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 309 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Additional precaution: make sure Q is valid |
| 314 | */ |
| 315 | MPI_CHK( ecp_check_pubkey( grp, Q ) ); |
| 316 | |
| 317 | /* |
| 318 | * Step 3: derive MPI from hashed message |
| 319 | */ |
| 320 | MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
| 321 | |
| 322 | /* |
| 323 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 324 | */ |
| 325 | MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) ); |
| 326 | |
| 327 | MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) ); |
| 328 | MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) ); |
| 329 | |
| 330 | MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) ); |
| 331 | MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) ); |
| 332 | |
| 333 | /* |
| 334 | * Step 5: R = u1 G + u2 Q |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 335 | * |
| 336 | * Since we're not using any secret data, no need to pass a RNG to |
| 337 | * ecp_mul() for countermesures. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 338 | */ |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 339 | MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) ); |
| 340 | MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 341 | MPI_CHK( ecp_add( grp, &R, &R, &P ) ); |
| 342 | |
| 343 | if( ecp_is_zero( &R ) ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 344 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 345 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 346 | goto cleanup; |
| 347 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 348 | |
| 349 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 350 | * Step 6: convert xR to an integer (no-op) |
| 351 | * Step 7: reduce xR mod n (gives v) |
| 352 | */ |
| 353 | MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); |
| 354 | |
| 355 | /* |
| 356 | * Step 8: check if v (that is, R.X) is equal to r |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 357 | */ |
| 358 | if( mpi_cmp_mpi( &R.X, r ) != 0 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 359 | { |
Manuel Pégourié-Gonnard | db77175 | 2013-08-27 15:11:23 +0200 | [diff] [blame] | 360 | ret = POLARSSL_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 361 | goto cleanup; |
| 362 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 363 | |
| 364 | cleanup: |
| 365 | ecp_point_free( &R ); ecp_point_free( &P ); |
| 366 | mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 ); |
| 367 | |
| 368 | return( ret ); |
| 369 | } |
| 370 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 371 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 372 | * RFC 4492 page 20: |
| 373 | * |
| 374 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 375 | * r INTEGER, |
| 376 | * s INTEGER |
| 377 | * } |
| 378 | * |
| 379 | * Size is at most |
| 380 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 381 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 382 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 383 | * and less than 124 (total len <= 255) for the sequence) |
| 384 | */ |
| 385 | #if POLARSSL_ECP_MAX_BYTES > 124 |
| 386 | #error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN" |
| 387 | #endif |
| 388 | #define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) ) |
| 389 | |
| 390 | /* |
| 391 | * Compute and write signature |
| 392 | */ |
| 393 | int ecdsa_write_signature( ecdsa_context *ctx, |
| 394 | const unsigned char *hash, size_t hlen, |
| 395 | unsigned char *sig, size_t *slen, |
| 396 | int (*f_rng)(void *, unsigned char *, size_t), |
| 397 | void *p_rng ) |
| 398 | { |
| 399 | int ret; |
Manuel Pégourié-Gonnard | 4cf0686 | 2013-09-16 12:07:45 +0200 | [diff] [blame] | 400 | unsigned char buf[MAX_SIG_LEN]; |
| 401 | unsigned char *p = buf + sizeof( buf ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 402 | size_t len = 0; |
| 403 | |
| 404 | if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d, |
| 405 | hash, hlen, f_rng, p_rng ) ) != 0 ) |
| 406 | { |
| 407 | return( ret ); |
| 408 | } |
| 409 | |
| 410 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) ); |
| 411 | ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) ); |
| 412 | |
| 413 | ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) ); |
| 414 | ASN1_CHK_ADD( len, asn1_write_tag( &p, buf, |
| 415 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); |
| 416 | |
| 417 | memcpy( sig, p, len ); |
| 418 | *slen = len; |
| 419 | |
| 420 | return( 0 ); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Read and check signature |
| 425 | */ |
| 426 | int ecdsa_read_signature( ecdsa_context *ctx, |
| 427 | const unsigned char *hash, size_t hlen, |
| 428 | const unsigned char *sig, size_t slen ) |
| 429 | { |
| 430 | int ret; |
| 431 | unsigned char *p = (unsigned char *) sig; |
| 432 | const unsigned char *end = sig + slen; |
| 433 | size_t len; |
| 434 | |
| 435 | if( ( ret = asn1_get_tag( &p, end, &len, |
| 436 | ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) |
| 437 | { |
| 438 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret ); |
| 439 | } |
| 440 | |
| 441 | if( p + len != end ) |
| 442 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + |
| 443 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 444 | |
| 445 | if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 || |
| 446 | ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 ) |
| 447 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret ); |
| 448 | |
| 449 | if( p != end ) |
| 450 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + |
| 451 | POLARSSL_ERR_ASN1_LENGTH_MISMATCH ); |
| 452 | |
| 453 | return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) ); |
| 454 | } |
| 455 | |
| 456 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 457 | * Generate key pair |
| 458 | */ |
| 459 | int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, |
| 460 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 461 | { |
| 462 | return( ecp_use_known_dp( &ctx->grp, gid ) || |
| 463 | ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); |
| 464 | } |
| 465 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 466 | /* |
| 467 | * Set context from an ecp_keypair |
| 468 | */ |
| 469 | int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key ) |
| 470 | { |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 471 | int ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 472 | |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 473 | if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || |
| 474 | ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 || |
| 475 | ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) |
| 476 | { |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 477 | ecdsa_free( ctx ); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 478 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 479 | |
| 480 | return( ret ); |
| 481 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 482 | |
| 483 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 484 | * Initialize context |
| 485 | */ |
| 486 | void ecdsa_init( ecdsa_context *ctx ) |
| 487 | { |
| 488 | ecp_group_init( &ctx->grp ); |
| 489 | mpi_init( &ctx->d ); |
| 490 | ecp_point_init( &ctx->Q ); |
| 491 | mpi_init( &ctx->r ); |
| 492 | mpi_init( &ctx->s ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Free context |
| 497 | */ |
| 498 | void ecdsa_free( ecdsa_context *ctx ) |
| 499 | { |
| 500 | ecp_group_free( &ctx->grp ); |
| 501 | mpi_free( &ctx->d ); |
| 502 | ecp_point_free( &ctx->Q ); |
| 503 | mpi_free( &ctx->r ); |
| 504 | mpi_free( &ctx->s ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 505 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 506 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 507 | #if defined(POLARSSL_SELF_TEST) |
| 508 | |
| 509 | /* |
| 510 | * Checkup routine |
| 511 | */ |
| 512 | int ecdsa_self_test( int verbose ) |
| 513 | { |
| 514 | return( verbose++ ); |
| 515 | } |
| 516 | |
| 517 | #endif |
| 518 | |
| 519 | #endif /* defined(POLARSSL_ECDSA_C) */ |