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_mod_mpi( &k, &k, &grp->N ) ); 159 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) ); 160 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) ); 161 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) ); 162 163 if( sign_tries++ > 10 ) 164 { 165 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; 166 goto cleanup; 167 } 168 } 169 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 ); 170 171 cleanup: 172 mbedtls_ecp_point_free( &R ); 173 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t ); 174 175 return( ret ); 176 } 177 178 int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, 179 const mbedtls_mpi *d, const unsigned char *buf, 180 size_t blen, 181 int (*f_rng)(void *, unsigned char *, size_t), 182 void *p_rng ) 183 { 184 /* Use the same RNG for both blinding and ephemeral key generation */ 185 return( ecdsa_sign_internal( grp, r, s, d, buf, blen, f_rng, p_rng, 186 f_rng, p_rng ) ); 187 } 188 #endif /* MBEDTLS_ECDSA_SIGN_ALT */ 189 190 #if defined(MBEDTLS_ECDSA_DETERMINISTIC) 191 static int ecdsa_sign_det_internal( mbedtls_ecp_group *grp, mbedtls_mpi *r, 192 mbedtls_mpi *s, const mbedtls_mpi *d, 193 const unsigned char *buf, size_t blen, 194 mbedtls_md_type_t md_alg, 195 int (*f_rng_blind)(void *, unsigned char *, 196 size_t), 197 void *p_rng_blind ) 198 { 199 int ret; 200 mbedtls_hmac_drbg_context rng_ctx; 201 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; 202 size_t grp_len = ( grp->nbits + 7 ) / 8; 203 const mbedtls_md_info_t *md_info; 204 mbedtls_mpi h; 205 /* Variables for deterministic blinding fallback */ 206 const char* blind_label = "BLINDING CONTEXT"; 207 mbedtls_hmac_drbg_context rng_ctx_blind; 208 209 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) 210 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 211 212 mbedtls_mpi_init( &h ); 213 mbedtls_hmac_drbg_init( &rng_ctx ); 214 mbedtls_hmac_drbg_init( &rng_ctx_blind ); 215 216 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ 217 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) ); 218 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); 219 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) ); 220 mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len ); 221 222 if( f_rng_blind != NULL ) 223 ret = ecdsa_sign_internal( grp, r, s, d, buf, blen, 224 mbedtls_hmac_drbg_random, &rng_ctx, 225 f_rng_blind, p_rng_blind ); 226 else 227 { 228 /* 229 * To avoid reusing rng_ctx and risking incorrect behavior we seed a 230 * second HMAC-DRBG with the same seed. We also apply a label to avoid 231 * reusing the bits of the ephemeral key for blinding and eliminate the 232 * risk that they leak this way. 233 */ 234 235 mbedtls_hmac_drbg_seed_buf( &rng_ctx_blind, md_info, 236 data, 2 * grp_len ); 237 ret = mbedtls_hmac_drbg_update_ret( &rng_ctx_blind, 238 (const unsigned char*) blind_label, 239 strlen( blind_label ) ); 240 if( ret != 0 ) 241 goto cleanup; 242 243 /* 244 * Since the output of the RNGs is always the same for the same key and 245 * message, this limits the efficiency of blinding and leaks information 246 * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL 247 * won't be a valid value for f_rng_blind anymore. Therefore it should 248 * be checked by the caller and this branch and check can be removed. 249 */ 250 ret = ecdsa_sign_internal( grp, r, s, d, buf, blen, 251 mbedtls_hmac_drbg_random, &rng_ctx, 252 mbedtls_hmac_drbg_random, &rng_ctx_blind ); 253 254 } 255 256 cleanup: 257 mbedtls_hmac_drbg_free( &rng_ctx ); 258 mbedtls_hmac_drbg_free( &rng_ctx_blind ); 259 mbedtls_mpi_free( &h ); 260 261 return( ret ); 262 } 263 264 /* 265 * Deterministic signature wrappers 266 */ 267 int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, 268 mbedtls_mpi *s, const mbedtls_mpi *d, 269 const unsigned char *buf, size_t blen, 270 mbedtls_md_type_t md_alg ) 271 { 272 return( ecdsa_sign_det_internal( grp, r, s, d, buf, blen, md_alg, 273 NULL, NULL ) ); 274 } 275 276 int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r, 277 mbedtls_mpi *s, const mbedtls_mpi *d, 278 const unsigned char *buf, size_t blen, 279 mbedtls_md_type_t md_alg, 280 int (*f_rng_blind)(void *, unsigned char *, 281 size_t), 282 void *p_rng_blind ) 283 { 284 return( ecdsa_sign_det_internal( grp, r, s, d, buf, blen, md_alg, 285 f_rng_blind, p_rng_blind ) ); 286 } 287 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ 288 289 #if !defined(MBEDTLS_ECDSA_VERIFY_ALT) 290 /* 291 * Verify ECDSA signature of hashed message (SEC1 4.1.4) 292 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) 293 */ 294 int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, 295 const unsigned char *buf, size_t blen, 296 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) 297 { 298 int ret; 299 mbedtls_mpi e, s_inv, u1, u2; 300 mbedtls_ecp_point R; 301 302 mbedtls_ecp_point_init( &R ); 303 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 ); 304 305 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ 306 if( grp->N.p == NULL ) 307 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 308 309 /* 310 * Step 1: make sure r and s are in range 1..n-1 311 */ 312 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 || 313 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 ) 314 { 315 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 316 goto cleanup; 317 } 318 319 /* 320 * Additional precaution: make sure Q is valid 321 */ 322 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); 323 324 /* 325 * Step 3: derive MPI from hashed message 326 */ 327 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); 328 329 /* 330 * Step 4: u1 = e / s mod n, u2 = r / s mod n 331 */ 332 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) ); 333 334 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) ); 335 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) ); 336 337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) ); 338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) ); 339 340 /* 341 * Step 5: R = u1 G + u2 Q 342 * 343 * Since we're not using any secret data, no need to pass a RNG to 344 * mbedtls_ecp_mul() for countermesures. 345 */ 346 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) ); 347 348 if( mbedtls_ecp_is_zero( &R ) ) 349 { 350 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 351 goto cleanup; 352 } 353 354 /* 355 * Step 6: convert xR to an integer (no-op) 356 * Step 7: reduce xR mod n (gives v) 357 */ 358 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); 359 360 /* 361 * Step 8: check if v (that is, R.X) is equal to r 362 */ 363 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 ) 364 { 365 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; 366 goto cleanup; 367 } 368 369 cleanup: 370 mbedtls_ecp_point_free( &R ); 371 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 ); 372 373 return( ret ); 374 } 375 #endif /* MBEDTLS_ECDSA_VERIFY_ALT */ 376 377 /* 378 * Convert a signature (given by context) to ASN.1 379 */ 380 static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s, 381 unsigned char *sig, size_t *slen ) 382 { 383 int ret; 384 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN]; 385 unsigned char *p = buf + sizeof( buf ); 386 size_t len = 0; 387 388 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) ); 389 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) ); 390 391 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) ); 392 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf, 393 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); 394 395 memcpy( sig, p, len ); 396 *slen = len; 397 398 return( 0 ); 399 } 400 401 /* 402 * Compute and write signature 403 */ 404 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, 405 const unsigned char *hash, size_t hlen, 406 unsigned char *sig, size_t *slen, 407 int (*f_rng)(void *, unsigned char *, size_t), 408 void *p_rng ) 409 { 410 int ret; 411 mbedtls_mpi r, s; 412 413 mbedtls_mpi_init( &r ); 414 mbedtls_mpi_init( &s ); 415 416 #if defined(MBEDTLS_ECDSA_DETERMINISTIC) 417 MBEDTLS_MPI_CHK( ecdsa_sign_det_internal( &ctx->grp, &r, &s, &ctx->d, 418 hash, hlen, md_alg, 419 f_rng, p_rng ) ); 420 #else 421 (void) md_alg; 422 423 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d, 424 hash, hlen, f_rng, p_rng ) ); 425 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ 426 427 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) ); 428 429 cleanup: 430 mbedtls_mpi_free( &r ); 431 mbedtls_mpi_free( &s ); 432 433 return( ret ); 434 } 435 436 #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \ 437 defined(MBEDTLS_ECDSA_DETERMINISTIC) 438 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, 439 const unsigned char *hash, size_t hlen, 440 unsigned char *sig, size_t *slen, 441 mbedtls_md_type_t md_alg ) 442 { 443 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, 444 NULL, NULL ) ); 445 } 446 #endif 447 448 /* 449 * Read and check signature 450 */ 451 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, 452 const unsigned char *hash, size_t hlen, 453 const unsigned char *sig, size_t slen ) 454 { 455 int ret; 456 unsigned char *p = (unsigned char *) sig; 457 const unsigned char *end = sig + slen; 458 size_t len; 459 mbedtls_mpi r, s; 460 461 mbedtls_mpi_init( &r ); 462 mbedtls_mpi_init( &s ); 463 464 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 465 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 466 { 467 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 468 goto cleanup; 469 } 470 471 if( p + len != end ) 472 { 473 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA + 474 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; 475 goto cleanup; 476 } 477 478 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 || 479 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 ) 480 { 481 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 482 goto cleanup; 483 } 484 485 if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen, 486 &ctx->Q, &r, &s ) ) != 0 ) 487 goto cleanup; 488 489 /* At this point we know that the buffer starts with a valid signature. 490 * Return 0 if the buffer just contains the signature, and a specific 491 * error code if the valid signature is followed by more data. */ 492 if( p != end ) 493 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; 494 495 cleanup: 496 mbedtls_mpi_free( &r ); 497 mbedtls_mpi_free( &s ); 498 499 return( ret ); 500 } 501 502 #if !defined(MBEDTLS_ECDSA_GENKEY_ALT) 503 /* 504 * Generate key pair 505 */ 506 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, 507 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) 508 { 509 int ret = 0; 510 ret = mbedtls_ecp_group_load( &ctx->grp, gid ); 511 if( ret != 0 ) 512 return( ret ); 513 514 return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, 515 &ctx->Q, f_rng, p_rng ) ); 516 } 517 #endif /* MBEDTLS_ECDSA_GENKEY_ALT */ 518 519 /* 520 * Set context from an mbedtls_ecp_keypair 521 */ 522 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) 523 { 524 int ret; 525 526 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || 527 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || 528 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) 529 { 530 mbedtls_ecdsa_free( ctx ); 531 } 532 533 return( ret ); 534 } 535 536 /* 537 * Initialize context 538 */ 539 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) 540 { 541 mbedtls_ecp_keypair_init( ctx ); 542 } 543 544 /* 545 * Free context 546 */ 547 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) 548 { 549 mbedtls_ecp_keypair_free( ctx ); 550 } 551 552 #endif /* MBEDTLS_ECDSA_C */ 553