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