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