1 /* 2 * Elliptic curve DSA 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: GPL-2.0 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 24 /* 25 * References: 26 * 27 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg 28 */ 29 30 #if !defined(MBEDTLS_CONFIG_FILE) 31 #include "mbedtls/config.h" 32 #else 33 #include MBEDTLS_CONFIG_FILE 34 #endif 35 36 #if defined(MBEDTLS_ECDSA_C) 37 38 #include "mbedtls/ecdsa.h" 39 #include "mbedtls/asn1write.h" 40 41 #include <string.h> 42 43 #if defined(MBEDTLS_ECDSA_DETERMINISTIC) 44 #include "mbedtls/hmac_drbg.h" 45 #endif 46 47 /* 48 * Derive a suitable integer for group grp from a buffer of length len 49 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 50 */ 51 static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x, 52 const unsigned char *buf, size_t blen ) 53 { 54 int ret; 55 size_t n_size = ( grp->nbits + 7 ) / 8; 56 size_t use_size = blen > n_size ? n_size : blen; 57 58 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) ); 59 if( use_size * 8 > grp->nbits ) 60 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) ); 61 62 /* While at it, reduce modulo N */ 63 if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 ) 64 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) ); 65 66 cleanup: 67 return( ret ); 68 } 69 70 #if !defined(MBEDTLS_ECDSA_SIGN_ALT) 71 /* 72 * Compute ECDSA signature of a hashed message (SEC1 4.1.3) 73 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) 74 */ 75 static int ecdsa_sign_internal( mbedtls_ecp_group *grp, mbedtls_mpi *r, 76 mbedtls_mpi *s, const mbedtls_mpi *d, 77 const unsigned char *buf, size_t blen, 78 int (*f_rng)(void *, unsigned char *, size_t), 79 void *p_rng, 80 int (*f_rng_blind)(void *, unsigned char *, 81 size_t), 82 void *p_rng_blind ) 83 { 84 int ret, key_tries, sign_tries, blind_tries; 85 mbedtls_ecp_point R; 86 mbedtls_mpi k, e, t; 87 88 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ 89 if( grp->N.p == NULL ) 90 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 91 92 /* Make sure d is in range 1..n-1 */ 93 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 ) 94 return( MBEDTLS_ERR_ECP_INVALID_KEY ); 95 96 mbedtls_ecp_point_init( &R ); 97 mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t ); 98 99 sign_tries = 0; 100 do 101 { 102 /* 103 * Steps 1-3: generate a suitable ephemeral keypair 104 * and set r = xR mod n 105 */ 106 key_tries = 0; 107 do 108 { 109 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &k, f_rng, p_rng ) ); 110 111 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &R, &k, &grp->G, 112 f_rng_blind, p_rng_blind ) ); 113 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) ); 114 115 if( key_tries++ > 10 ) 116 { 117 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; 118 goto cleanup; 119 } 120 } 121 while( mbedtls_mpi_cmp_int( r, 0 ) == 0 ); 122 123 /* 124 * Step 5: derive MPI from hashed message 125 */ 126 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); 127 128 /* 129 * Generate a random value to blind inv_mod in next step, 130 * avoiding a potential timing leak. 131 * 132 * This loop does the same job as mbedtls_ecp_gen_privkey() and it is 133 * replaced by a call to it in the mainline. This change is not 134 * necessary to backport the fix separating the blinding and ephemeral 135 * key generating RNGs, therefore the original code is kept. 136 */ 137 blind_tries = 0; 138 do 139 { 140 size_t n_size = ( grp->nbits + 7 ) / 8; 141 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng_blind, 142 p_rng_blind ) ); 143 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) ); 144 145 if( ++blind_tries > 30 ) 146 return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); 147 } 148 while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 || 149 mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 ); 150 151 /* 152 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n 153 */ 154 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) ); 155 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) ); 156 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) ); 157 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) ); 158 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) ); 159 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) ); 160 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) ); 161 162 if( sign_tries++ > 10 ) 163 { 164 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; 165 goto cleanup; 166 } 167 } 168 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 ); 169 170 cleanup: 171 mbedtls_ecp_point_free( &R ); 172 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t ); 173 174 return( ret ); 175 } 176 177 int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, 178 const mbedtls_mpi *d, const unsigned char *buf, 179 size_t blen, 180 int (*f_rng)(void *, unsigned char *, size_t), 181 void *p_rng ) 182 { 183 /* Use the same RNG for both blinding and ephemeral key generation */ 184 return( ecdsa_sign_internal( grp, r, s, d, buf, blen, f_rng, p_rng, 185 f_rng, p_rng ) ); 186 } 187 #endif /* MBEDTLS_ECDSA_SIGN_ALT */ 188 189 #if defined(MBEDTLS_ECDSA_DETERMINISTIC) 190 static int ecdsa_sign_det_internal( mbedtls_ecp_group *grp, mbedtls_mpi *r, 191 mbedtls_mpi *s, const mbedtls_mpi *d, 192 const unsigned char *buf, size_t blen, 193 mbedtls_md_type_t md_alg, 194 int (*f_rng_blind)(void *, unsigned char *, 195 size_t), 196 void *p_rng_blind ) 197 { 198 int ret; 199 mbedtls_hmac_drbg_context rng_ctx; 200 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; 201 size_t grp_len = ( grp->nbits + 7 ) / 8; 202 const mbedtls_md_info_t *md_info; 203 mbedtls_mpi h; 204 /* Variables for deterministic blinding fallback */ 205 const char* blind_label = "BLINDING CONTEXT"; 206 mbedtls_hmac_drbg_context rng_ctx_blind; 207 208 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) 209 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 210 211 mbedtls_mpi_init( &h ); 212 mbedtls_hmac_drbg_init( &rng_ctx ); 213 mbedtls_hmac_drbg_init( &rng_ctx_blind ); 214 215 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ 216 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) ); 217 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); 218 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) ); 219 mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len ); 220 221 if( f_rng_blind != NULL ) 222 ret = ecdsa_sign_internal( grp, r, s, d, buf, blen, 223 mbedtls_hmac_drbg_random, &rng_ctx, 224 f_rng_blind, p_rng_blind ); 225 else 226 { 227 /* 228 * To avoid reusing rng_ctx and risking incorrect behavior we seed a 229 * second HMAC-DRBG with the same seed. We also apply a label to avoid 230 * reusing the bits of the ephemeral key for blinding and eliminate the 231 * risk that they leak this way. 232 */ 233 234 mbedtls_hmac_drbg_seed_buf( &rng_ctx_blind, md_info, 235 data, 2 * grp_len ); 236 ret = mbedtls_hmac_drbg_update_ret( &rng_ctx_blind, 237 (const unsigned char*) blind_label, 238 strlen( blind_label ) ); 239 if( ret != 0 ) 240 goto cleanup; 241 242 /* 243 * Since the output of the RNGs is always the same for the same key and 244 * message, this limits the efficiency of blinding and leaks information 245 * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL 246 * won't be a valid value for f_rng_blind anymore. Therefore it should 247 * be checked by the caller and this branch and check can be removed. 248 */ 249 ret = ecdsa_sign_internal( grp, r, s, d, buf, blen, 250 mbedtls_hmac_drbg_random, &rng_ctx, 251 mbedtls_hmac_drbg_random, &rng_ctx_blind ); 252 253 } 254 255 cleanup: 256 mbedtls_hmac_drbg_free( &rng_ctx ); 257 mbedtls_hmac_drbg_free( &rng_ctx_blind ); 258 mbedtls_mpi_free( &h ); 259 260 return( ret ); 261 } 262 263 /* 264 * Deterministic signature wrappers 265 */ 266 int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, 267 mbedtls_mpi *s, const mbedtls_mpi *d, 268 const unsigned char *buf, size_t blen, 269 mbedtls_md_type_t md_alg ) 270 { 271 return( ecdsa_sign_det_internal( grp, r, s, d, buf, blen, md_alg, 272 NULL, NULL ) ); 273 } 274 275 int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r, 276 mbedtls_mpi *s, const mbedtls_mpi *d, 277 const unsigned char *buf, size_t blen, 278 mbedtls_md_type_t md_alg, 279 int (*f_rng_blind)(void *, unsigned char *, 280 size_t), 281 void *p_rng_blind ) 282 { 283 return( ecdsa_sign_det_internal( grp, r, s, d, buf, blen, md_alg, 284 f_rng_blind, p_rng_blind ) ); 285 } 286 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ 287 288 #if !defined(MBEDTLS_ECDSA_VERIFY_ALT) 289 /* 290 * Verify ECDSA signature of hashed message (SEC1 4.1.4) 291 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) 292 */ 293 int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, 294 const unsigned char *buf, size_t blen, 295 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) 296 { 297 int ret; 298 mbedtls_mpi e, s_inv, u1, u2; 299 mbedtls_ecp_point R; 300 301 mbedtls_ecp_point_init( &R ); 302 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 ); 303 304 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ 305 if( grp->N.p == NULL ) 306 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 307 308 /* 309 * Step 1: make sure r and s are in range 1..n-1 310 */ 311 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 || 312 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 ) 313 { 314 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 315 goto cleanup; 316 } 317 318 /* 319 * Additional precaution: make sure Q is valid 320 */ 321 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); 322 323 /* 324 * Step 3: derive MPI from hashed message 325 */ 326 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); 327 328 /* 329 * Step 4: u1 = e / s mod n, u2 = r / s mod n 330 */ 331 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) ); 332 333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) ); 334 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) ); 335 336 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) ); 337 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) ); 338 339 /* 340 * Step 5: R = u1 G + u2 Q 341 * 342 * Since we're not using any secret data, no need to pass a RNG to 343 * mbedtls_ecp_mul() for countermesures. 344 */ 345 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) ); 346 347 if( mbedtls_ecp_is_zero( &R ) ) 348 { 349 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 350 goto cleanup; 351 } 352 353 /* 354 * Step 6: convert xR to an integer (no-op) 355 * Step 7: reduce xR mod n (gives v) 356 */ 357 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); 358 359 /* 360 * Step 8: check if v (that is, R.X) is equal to r 361 */ 362 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 ) 363 { 364 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 365 goto cleanup; 366 } 367 368 cleanup: 369 mbedtls_ecp_point_free( &R ); 370 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 ); 371 372 return( ret ); 373 } 374 #endif /* MBEDTLS_ECDSA_VERIFY_ALT */ 375 376 /* 377 * Convert a signature (given by context) to ASN.1 378 */ 379 static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s, 380 unsigned char *sig, size_t *slen ) 381 { 382 int ret; 383 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN]; 384 unsigned char *p = buf + sizeof( buf ); 385 size_t len = 0; 386 387 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) ); 388 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) ); 389 390 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) ); 391 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf, 392 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); 393 394 memcpy( sig, p, len ); 395 *slen = len; 396 397 return( 0 ); 398 } 399 400 /* 401 * Compute and write signature 402 */ 403 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, 404 const unsigned char *hash, size_t hlen, 405 unsigned char *sig, size_t *slen, 406 int (*f_rng)(void *, unsigned char *, size_t), 407 void *p_rng ) 408 { 409 int ret; 410 mbedtls_mpi r, s; 411 412 mbedtls_mpi_init( &r ); 413 mbedtls_mpi_init( &s ); 414 415 #if defined(MBEDTLS_ECDSA_DETERMINISTIC) 416 MBEDTLS_MPI_CHK( ecdsa_sign_det_internal( &ctx->grp, &r, &s, &ctx->d, 417 hash, hlen, md_alg, 418 f_rng, p_rng ) ); 419 #else 420 (void) md_alg; 421 422 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d, 423 hash, hlen, f_rng, p_rng ) ); 424 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ 425 426 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) ); 427 428 cleanup: 429 mbedtls_mpi_free( &r ); 430 mbedtls_mpi_free( &s ); 431 432 return( ret ); 433 } 434 435 #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \ 436 defined(MBEDTLS_ECDSA_DETERMINISTIC) 437 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, 438 const unsigned char *hash, size_t hlen, 439 unsigned char *sig, size_t *slen, 440 mbedtls_md_type_t md_alg ) 441 { 442 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, 443 NULL, NULL ) ); 444 } 445 #endif 446 447 /* 448 * Read and check signature 449 */ 450 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, 451 const unsigned char *hash, size_t hlen, 452 const unsigned char *sig, size_t slen ) 453 { 454 int ret; 455 unsigned char *p = (unsigned char *) sig; 456 const unsigned char *end = sig + slen; 457 size_t len; 458 mbedtls_mpi r, s; 459 460 mbedtls_mpi_init( &r ); 461 mbedtls_mpi_init( &s ); 462 463 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 464 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 465 { 466 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 467 goto cleanup; 468 } 469 470 if( p + len != end ) 471 { 472 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA + 473 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; 474 goto cleanup; 475 } 476 477 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 || 478 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 ) 479 { 480 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 481 goto cleanup; 482 } 483 484 if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen, 485 &ctx->Q, &r, &s ) ) != 0 ) 486 goto cleanup; 487 488 /* At this point we know that the buffer starts with a valid signature. 489 * Return 0 if the buffer just contains the signature, and a specific 490 * error code if the valid signature is followed by more data. */ 491 if( p != end ) 492 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; 493 494 cleanup: 495 mbedtls_mpi_free( &r ); 496 mbedtls_mpi_free( &s ); 497 498 return( ret ); 499 } 500 501 #if !defined(MBEDTLS_ECDSA_GENKEY_ALT) 502 /* 503 * Generate key pair 504 */ 505 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, 506 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) 507 { 508 int ret = 0; 509 ret = mbedtls_ecp_group_load( &ctx->grp, gid ); 510 if( ret != 0 ) 511 return( ret ); 512 513 return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, 514 &ctx->Q, f_rng, p_rng ) ); 515 } 516 #endif /* MBEDTLS_ECDSA_GENKEY_ALT */ 517 518 /* 519 * Set context from an mbedtls_ecp_keypair 520 */ 521 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) 522 { 523 int ret; 524 525 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || 526 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || 527 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) 528 { 529 mbedtls_ecdsa_free( ctx ); 530 } 531 532 return( ret ); 533 } 534 535 /* 536 * Initialize context 537 */ 538 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) 539 { 540 mbedtls_ecp_keypair_init( ctx ); 541 } 542 543 /* 544 * Free context 545 */ 546 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) 547 { 548 mbedtls_ecp_keypair_free( ctx ); 549 } 550 551 #endif /* MBEDTLS_ECDSA_C */ 552