| 6 |
| * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | * |
| 57 | * The DSS routines are based on patches supplied by |
| 58 | * Steven Schoch <schoch@sheba.arc.nasa.gov>. */ |
| 59 |
| 60 | #include <openssl/dsa.h> |
| 61 |
| 62 | #include <string.h> |
| 63 |
| 64 | #include <openssl/bn.h> |
| 65 | #include <openssl/dh.h> |
| 66 | #include <openssl/digest.h> |
| 67 | #include <openssl/engine.h> |
| 68 | #include <openssl/err.h> |
| 69 | #include <openssl/ex_data.h> |
| 70 | #include <openssl/mem.h> |
| 71 | #include <openssl/rand.h> |
| 72 | #include <openssl/sha.h> |
| 73 | #include <openssl/thread.h> |
| 74 |
| 75 | #include "../fipsmodule/bn/internal.h" |
| 76 | #include "../internal.h" |
| 77 |
| 78 |
| 79 | #define OPENSSL_DSA_MAX_MODULUS_BITS 10000 |
| 80 |
| 81 | // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of |
| 82 | // Rabin-Miller |
| 83 | #define DSS_prime_checks 50 |
| 84 |
| 85 | static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv, |
| 86 | BIGNUM **out_r); |
| 87 |
| 88 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; |
| 89 |
| 90 | DSA *DSA_new(void) { |
| 91 | DSA *dsa = OPENSSL_malloc(sizeof(DSA)); |
| 92 | if (dsa == NULL) { |
| 93 | OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE); |
| 94 | return NULL; |
| 95 | } |
| 96 |
| 97 | OPENSSL_memset(dsa, 0, sizeof(DSA)); |
| 98 |
| 99 | dsa->references = 1; |
| 100 |
| 101 | CRYPTO_MUTEX_init(&dsa->method_mont_lock); |
| 102 | CRYPTO_new_ex_data(&dsa->ex_data); |
| 103 |
| 104 | return dsa; |
| 105 | } |
| 106 |
| 107 | void DSA_free(DSA *dsa) { |
| 108 | if (dsa == NULL) { |
| 109 | return; |
| 110 | } |
| 111 |
| 112 | if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) { |
| 113 | return; |
| 114 | } |
| 115 |
| 116 | CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data); |
| 117 |
| 118 | BN_clear_free(dsa->p); |
| 119 | BN_clear_free(dsa->q); |
| 120 | BN_clear_free(dsa->g); |
| 121 | BN_clear_free(dsa->pub_key); |
| 122 | BN_clear_free(dsa->priv_key); |
| 123 | BN_MONT_CTX_free(dsa->method_mont_p); |
| 124 | BN_MONT_CTX_free(dsa->method_mont_q); |
| 125 | CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock); |
| 126 | OPENSSL_free(dsa); |
| 127 | } |
| 128 |
| 129 | int DSA_up_ref(DSA *dsa) { |
| 130 | CRYPTO_refcount_inc(&dsa->references); |
| 131 | return 1; |
| 132 | } |
| 133 |
| 134 | void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key, |
| 135 | const BIGNUM **out_priv_key) { |
| 136 | if (out_pub_key != NULL) { |
| 137 | *out_pub_key = dsa->pub_key; |
| 138 | } |
| 139 | if (out_priv_key != NULL) { |
| 140 | *out_priv_key = dsa->priv_key; |
| 141 | } |
| 142 | } |
| 143 |
| 144 | void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q, |
| 145 | const BIGNUM **out_g) { |
| 146 | if (out_p != NULL) { |
| 147 | *out_p = dsa->p; |
| 148 | } |
| 149 | if (out_q != NULL) { |
| 150 | *out_q = dsa->q; |
| 151 | } |
| 152 | if (out_g != NULL) { |
| 153 | *out_g = dsa->g; |
| 154 | } |
| 155 | } |
| 156 |
| 157 | int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) { |
| 158 | if (dsa->pub_key == NULL && pub_key == NULL) { |
| 159 | return 0; |
| 160 | } |
| 161 |
| 162 | if (pub_key != NULL) { |
| 163 | BN_free(dsa->pub_key); |
| 164 | dsa->pub_key = pub_key; |
| 165 | } |
| 166 | if (priv_key != NULL) { |
| 167 | BN_free(dsa->priv_key); |
| 168 | dsa->priv_key = priv_key; |
| 169 | } |
| 170 |
| 171 | return 1; |
| 172 | } |
| 173 |
| 174 | int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) { |
| 175 | if ((dsa->p == NULL && p == NULL) || |
| 176 | (dsa->q == NULL && q == NULL) || |
| 177 | (dsa->g == NULL && g == NULL)) { |
| 178 | return 0; |
| 179 | } |
| 180 |
| 181 | if (p != NULL) { |
| 182 | BN_free(dsa->p); |
| 183 | dsa->p = p; |
| 184 | } |
| 185 | if (q != NULL) { |
| 186 | BN_free(dsa->q); |
| 187 | dsa->q = q; |
| 188 | } |
| 189 | if (g != NULL) { |
| 190 | BN_free(dsa->g); |
| 191 | dsa->g = g; |
| 192 | } |
| 193 |
| 194 | return 1; |
| 195 | } |
| 196 |
| 197 | int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in, |
| 198 | size_t seed_len, int *out_counter, |
| 199 | unsigned long *out_h, BN_GENCB *cb) { |
| 200 | int ok = 0; |
| 201 | unsigned char seed[SHA256_DIGEST_LENGTH]; |
| 202 | unsigned char md[SHA256_DIGEST_LENGTH]; |
| 203 | unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH]; |
| 204 | BIGNUM *r0, *W, *X, *c, *test; |
| 205 | BIGNUM *g = NULL, *q = NULL, *p = NULL; |
| 206 | BN_MONT_CTX *mont = NULL; |
| 207 | int k, n = 0, m = 0; |
| 208 | unsigned i; |
| 209 | int counter = 0; |
| 210 | int r = 0; |
| 211 | BN_CTX *ctx = NULL; |
| 212 | unsigned int h = 2; |
| 213 | unsigned qsize; |
| 214 | const EVP_MD *evpmd; |
| 215 |
| 216 | evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1(); |
| 217 | qsize = EVP_MD_size(evpmd); |
| 218 |
| 219 | if (bits < 512) { |
| 220 | bits = 512; |
| 221 | } |
| 222 |
| 223 | bits = (bits + 63) / 64 * 64; |
| 224 |
| 225 | if (seed_in != NULL) { |
| 226 | if (seed_len < (size_t)qsize) { |
| 227 | return 0; |
| 228 | } |
| 229 | if (seed_len > (size_t)qsize) { |
| 230 | // Only consume as much seed as is expected. |
| 231 | seed_len = qsize; |
| 232 | } |
| 233 | OPENSSL_memcpy(seed, seed_in, seed_len); |
| 234 | } |
| 235 |
| 236 | ctx = BN_CTX_new(); |
| 237 | if (ctx == NULL) { |
| 238 | goto err; |
| 239 | } |
| 240 | BN_CTX_start(ctx); |
| 241 |
| 242 | mont = BN_MONT_CTX_new(); |
| 243 | if (mont == NULL) { |
| 244 | goto err; |
| 245 | } |
| 246 |
| 247 | r0 = BN_CTX_get(ctx); |
| 248 | g = BN_CTX_get(ctx); |
| 249 | W = BN_CTX_get(ctx); |
| 250 | q = BN_CTX_get(ctx); |
| 251 | X = BN_CTX_get(ctx); |
| 252 | c = BN_CTX_get(ctx); |
| 253 | p = BN_CTX_get(ctx); |
| 254 | test = BN_CTX_get(ctx); |
| 255 |
| 256 | if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) { |
| 257 | goto err; |
| 258 | } |
| 259 |
| 260 | for (;;) { |
| 261 | // Find q. |
| 262 | for (;;) { |
| 263 | // step 1 |
| 264 | if (!BN_GENCB_call(cb, 0, m++)) { |
| 265 | goto err; |
| 266 | } |
| 267 |
| 268 | int use_random_seed = (seed_in == NULL); |
| 269 | if (use_random_seed) { |
| 270 | if (!RAND_bytes(seed, qsize)) { |
| 271 | goto err; |
| 272 | } |
| 273 | } else { |
| 274 | // If we come back through, use random seed next time. |
| 275 | seed_in = NULL; |
| 276 | } |
| 277 | OPENSSL_memcpy(buf, seed, qsize); |
| 278 | OPENSSL_memcpy(buf2, seed, qsize); |
| 279 | // precompute "SEED + 1" for step 7: |
| 280 | for (i = qsize - 1; i < qsize; i--) { |
| 281 | buf[i]++; |
| 282 | if (buf[i] != 0) { |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 |
| 287 | // step 2 |
| 288 | if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) || |
| 289 | !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) { |
| 290 | goto err; |
| 291 | } |
| 292 | for (i = 0; i < qsize; i++) { |
| 293 | md[i] ^= buf2[i]; |
| 294 | } |
| 295 |
| 296 | // step 3 |
| 297 | md[0] |= 0x80; |
| 298 | md[qsize - 1] |= 0x01; |
| 299 | if (!BN_bin2bn(md, qsize, q)) { |
| 300 | goto err; |
| 301 | } |
| 302 |
| 303 | // step 4 |
| 304 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb); |
| 305 | if (r > 0) { |
| 306 | break; |
| 307 | } |
| 308 | if (r != 0) { |
| 309 | goto err; |
| 310 | } |
| 311 |
| 312 | // do a callback call |
| 313 | // step 5 |
| 314 | } |
| 315 |
| 316 | if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) { |
| 317 | goto err; |
| 318 | } |
| 319 |
| 320 | // step 6 |
| 321 | counter = 0; |
| 322 | // "offset = 2" |
| 323 |
| 324 | n = (bits - 1) / 160; |
| 325 |
| 326 | for (;;) { |
| 327 | if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) { |
| 328 | goto err; |
| 329 | } |
| 330 |
| 331 | // step 7 |
| 332 | BN_zero(W); |
| 333 | // now 'buf' contains "SEED + offset - 1" |
| 334 | for (k = 0; k <= n; k++) { |
| 335 | // obtain "SEED + offset + k" by incrementing: |
| 336 | for (i = qsize - 1; i < qsize; i--) { |
| 337 | buf[i]++; |
| 338 | if (buf[i] != 0) { |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 |
| 343 | if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) { |
| 344 | goto err; |
| 345 | } |
| 346 |
| 347 | // step 8 |
| 348 | if (!BN_bin2bn(md, qsize, r0) || |
| 349 | !BN_lshift(r0, r0, (qsize << 3) * k) || |
| 350 | !BN_add(W, W, r0)) { |
| 351 | goto err; |
| 352 | } |
| 353 | } |
| 354 |
| 355 | // more of step 8 |
| 356 | if (!BN_mask_bits(W, bits - 1) || |
| 357 | !BN_copy(X, W) || |
| 358 | !BN_add(X, X, test)) { |
| 359 | goto err; |
| 360 | } |
| 361 |
| 362 | // step 9 |
| 363 | if (!BN_lshift1(r0, q) || |
| 364 | !BN_mod(c, X, r0, ctx) || |
| 365 | !BN_sub(r0, c, BN_value_one()) || |
| 366 | !BN_sub(p, X, r0)) { |
| 367 | goto err; |
| 368 | } |
| 369 |
| 370 | // step 10 |
| 371 | if (BN_cmp(p, test) >= 0) { |
| 372 | // step 11 |
| 373 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb); |
| 374 | if (r > 0) { |
| 375 | goto end; // found it |
| 376 | } |
| 377 | if (r != 0) { |
| 378 | goto err; |
| 379 | } |
| 380 | } |
| 381 |
| 382 | // step 13 |
| 383 | counter++; |
| 384 | // "offset = offset + n + 1" |
| 385 |
| 386 | // step 14 |
| 387 | if (counter >= 4096) { |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | end: |
| 393 | if (!BN_GENCB_call(cb, 2, 1)) { |
| 394 | goto err; |
| 395 | } |
| 396 |
| 397 | // We now need to generate g |
| 398 | // Set r0=(p-1)/q |
| 399 | if (!BN_sub(test, p, BN_value_one()) || |
| 400 | !BN_div(r0, NULL, test, q, ctx)) { |
| 401 | goto err; |
| 402 | } |
| 403 |
| 404 | if (!BN_set_word(test, h) || |
| 405 | !BN_MONT_CTX_set(mont, p, ctx)) { |
| 406 | goto err; |
| 407 | } |
| 408 |
| 409 | for (;;) { |
| 410 | // g=test^r0%p |
| 411 | if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) { |
| 412 | goto err; |
| 413 | } |
| 414 | if (!BN_is_one(g)) { |
| 415 | break; |
| 416 | } |
| 417 | if (!BN_add(test, test, BN_value_one())) { |
| 418 | goto err; |
| 419 | } |
| 420 | h++; |
| 421 | } |
| 422 |
| 423 | if (!BN_GENCB_call(cb, 3, 1)) { |
| 424 | goto err; |
| 425 | } |
| 426 |
| 427 | ok = 1; |
| 428 |
| 429 | err: |
| 430 | if (ok) { |
| 431 | BN_free(dsa->p); |
| 432 | BN_free(dsa->q); |
| 433 | BN_free(dsa->g); |
| 434 | dsa->p = BN_dup(p); |
| 435 | dsa->q = BN_dup(q); |
| 436 | dsa->g = BN_dup(g); |
| 437 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
| 438 | ok = 0; |
| 439 | goto err; |
| 440 | } |
| 441 | if (out_counter != NULL) { |
| 442 | *out_counter = counter; |
| 443 | } |
| 444 | if (out_h != NULL) { |
| 445 | *out_h = h; |
| 446 | } |
| 447 | } |
| 448 |
| 449 | if (ctx) { |
| 450 | BN_CTX_end(ctx); |
| 451 | BN_CTX_free(ctx); |
| 452 | } |
| 453 |
| 454 | BN_MONT_CTX_free(mont); |
| 455 |
| 456 | return ok; |
| 457 | } |
| 458 |
| 459 | DSA *DSAparams_dup(const DSA *dsa) { |
| 460 | DSA *ret = DSA_new(); |
| 461 | if (ret == NULL) { |
| 462 | return NULL; |
| 463 | } |
| 464 | ret->p = BN_dup(dsa->p); |
| 465 | ret->q = BN_dup(dsa->q); |
| 466 | ret->g = BN_dup(dsa->g); |
| 467 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) { |
| 468 | DSA_free(ret); |
| 469 | return NULL; |
| 470 | } |
| 471 | return ret; |
| 472 | } |
| 473 |
| 474 | int DSA_generate_key(DSA *dsa) { |
| 475 | int ok = 0; |
| 476 | BN_CTX *ctx = NULL; |
| 477 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
| 478 |
| 479 | ctx = BN_CTX_new(); |
| 480 | if (ctx == NULL) { |
| 481 | goto err; |
| 482 | } |
| 483 |
| 484 | priv_key = dsa->priv_key; |
| 485 | if (priv_key == NULL) { |
| 486 | priv_key = BN_new(); |
| 487 | if (priv_key == NULL) { |
| 488 | goto err; |
| 489 | } |
| 490 | } |
| 491 |
| 492 | if (!BN_rand_range_ex(priv_key, 1, dsa->q)) { |
| 493 | goto err; |
| 494 | } |
| 495 |
| 496 | pub_key = dsa->pub_key; |
| 497 | if (pub_key == NULL) { |
| 498 | pub_key = BN_new(); |
| 499 | if (pub_key == NULL) { |
| 500 | goto err; |
| 501 | } |
| 502 | } |
| 503 |
| 504 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock, |
| 505 | dsa->p, ctx) || |
| 506 | !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx, |
| 507 | dsa->method_mont_p)) { |
| 508 | goto err; |
| 509 | } |
| 510 |
| 511 | dsa->priv_key = priv_key; |
| 512 | dsa->pub_key = pub_key; |
| 513 | ok = 1; |
| 514 |
| 515 | err: |
| 516 | if (dsa->pub_key == NULL) { |
| 517 | BN_free(pub_key); |
| 518 | } |
| 519 | if (dsa->priv_key == NULL) { |
| 520 | BN_free(priv_key); |
| 521 | } |
| 522 | BN_CTX_free(ctx); |
| 523 |
| 524 | return ok; |
| 525 | } |
| 526 |
| 527 | DSA_SIG *DSA_SIG_new(void) { |
| 528 | DSA_SIG *sig; |
| 529 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); |
| 530 | if (!sig) { |
| 531 | return NULL; |
| 532 | } |
| 533 | sig->r = NULL; |
| 534 | sig->s = NULL; |
| 535 | return sig; |
| 536 | } |
| 537 |
| 538 | void DSA_SIG_free(DSA_SIG *sig) { |
| 539 | if (!sig) { |
| 540 | return; |
| 541 | } |
| 542 |
| 543 | BN_free(sig->r); |
| 544 | BN_free(sig->s); |
| 545 | OPENSSL_free(sig); |
| 546 | } |
| 547 |
| 548 | DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { |
| 549 | BIGNUM *kinv = NULL, *r = NULL, *s = NULL; |
| 550 | BIGNUM m; |
| 551 | BIGNUM xr; |
| 552 | BN_CTX *ctx = NULL; |
| 553 | int reason = ERR_R_BN_LIB; |
| 554 | DSA_SIG *ret = NULL; |
| 555 |
| 556 | BN_init(&m); |
| 557 | BN_init(&xr); |
| 558 |
| 559 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 560 | reason = DSA_R_MISSING_PARAMETERS; |
| 561 | goto err; |
| 562 | } |
| 563 |
| 564 | s = BN_new(); |
| 565 | if (s == NULL) { |
| 566 | goto err; |
| 567 | } |
| 568 | ctx = BN_CTX_new(); |
| 569 | if (ctx == NULL) { |
| 570 | goto err; |
| 571 | } |
| 572 |
| 573 | redo: |
| 574 | if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) { |
| 575 | goto err; |
| 576 | } |
| 577 |
| 578 | if (digest_len > BN_num_bytes(dsa->q)) { |
| 579 | // if the digest length is greater than the size of q use the |
| 580 | // BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 581 | // fips 186-3, 4.2 |
| 582 | digest_len = BN_num_bytes(dsa->q); |
| 583 | } |
| 584 |
| 585 | if (BN_bin2bn(digest, digest_len, &m) == NULL) { |
| 586 | goto err; |
| 587 | } |
| 588 |
| 589 | // Compute s = inv(k) (m + xr) mod q |
| 590 | if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) { |
| 591 | goto err; // s = xr |
| 592 | } |
| 593 | if (!BN_add(s, &xr, &m)) { |
| 594 | goto err; // s = m + xr |
| 595 | } |
| 596 | if (BN_cmp(s, dsa->q) > 0) { |
| 597 | if (!BN_sub(s, s, dsa->q)) { |
| 598 | goto err; |
| 599 | } |
| 600 | } |
| 601 | if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) { |
| 602 | goto err; |
| 603 | } |
| 604 |
| 605 | // Redo if r or s is zero as required by FIPS 186-3: this is |
| 606 | // very unlikely. |
| 607 | if (BN_is_zero(r) || BN_is_zero(s)) { |
| 608 | goto redo; |
| 609 | } |
| 610 | ret = DSA_SIG_new(); |
| 611 | if (ret == NULL) { |
| 612 | goto err; |
| 613 | } |
| 614 | ret->r = r; |
| 615 | ret->s = s; |
| 616 |
| 617 | err: |
| 618 | if (ret == NULL) { |
| 619 | OPENSSL_PUT_ERROR(DSA, reason); |
| 620 | BN_free(r); |
| 621 | BN_free(s); |
| 622 | } |
| 623 | BN_CTX_free(ctx); |
| 624 | BN_clear_free(&m); |
| 625 | BN_clear_free(&xr); |
| 626 | BN_clear_free(kinv); |
| 627 |
| 628 | return ret; |
| 629 | } |
| 630 |
| 631 | int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig, |
| 632 | const DSA *dsa) { |
| 633 | int valid; |
| 634 | if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) { |
| 635 | return -1; |
| 636 | } |
| 637 | return valid; |
| 638 | } |
| 639 |
| 640 | int DSA_do_check_signature(int *out_valid, const uint8_t *digest, |
| 641 | size_t digest_len, DSA_SIG *sig, const DSA *dsa) { |
| 642 | BN_CTX *ctx; |
| 643 | BIGNUM u1, u2, t1; |
| 644 | int ret = 0; |
| 645 | unsigned i; |
| 646 |
| 647 | *out_valid = 0; |
| 648 |
| 649 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 650 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 651 | return 0; |
| 652 | } |
| 653 |
| 654 | i = BN_num_bits(dsa->q); |
| 655 | // fips 186-3 allows only different sizes for q |
| 656 | if (i != 160 && i != 224 && i != 256) { |
| 657 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE); |
| 658 | return 0; |
| 659 | } |
| 660 |
| 661 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { |
| 662 | OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE); |
| 663 | return 0; |
| 664 | } |
| 665 |
| 666 | BN_init(&u1); |
| 667 | BN_init(&u2); |
| 668 | BN_init(&t1); |
| 669 |
| 670 | ctx = BN_CTX_new(); |
| 671 | if (ctx == NULL) { |
| 672 | goto err; |
| 673 | } |
| 674 |
| 675 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
| 676 | BN_ucmp(sig->r, dsa->q) >= 0) { |
| 677 | ret = 1; |
| 678 | goto err; |
| 679 | } |
| 680 | if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || |
| 681 | BN_ucmp(sig->s, dsa->q) >= 0) { |
| 682 | ret = 1; |
| 683 | goto err; |
| 684 | } |
| 685 |
| 686 | // Calculate W = inv(S) mod Q |
| 687 | // save W in u2 |
| 688 | if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) { |
| 689 | goto err; |
| 690 | } |
| 691 |
| 692 | // save M in u1 |
| 693 | if (digest_len > (i >> 3)) { |
| 694 | // if the digest length is greater than the size of q use the |
| 695 | // BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 696 | // fips 186-3, 4.2 |
| 697 | digest_len = (i >> 3); |
| 698 | } |
| 699 |
| 700 | if (BN_bin2bn(digest, digest_len, &u1) == NULL) { |
| 701 | goto err; |
| 702 | } |
| 703 |
| 704 | // u1 = M * w mod q |
| 705 | if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) { |
| 706 | goto err; |
| 707 | } |
| 708 |
| 709 | // u2 = r * w mod q |
| 710 | if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) { |
| 711 | goto err; |
| 712 | } |
| 713 |
| 714 | if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 715 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p, |
| 716 | ctx)) { |
| 717 | goto err; |
| 718 | } |
| 719 |
| 720 | if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, |
| 721 | dsa->method_mont_p)) { |
| 722 | goto err; |
| 723 | } |
| 724 |
| 725 | // BN_copy(&u1,&t1); |
| 726 | // let u1 = u1 mod q |
| 727 | if (!BN_mod(&u1, &t1, dsa->q, ctx)) { |
| 728 | goto err; |
| 729 | } |
| 730 |
| 731 | // V is now in u1. If the signature is correct, it will be |
| 732 | // equal to R. |
| 733 | *out_valid = BN_ucmp(&u1, sig->r) == 0; |
| 734 | ret = 1; |
| 735 |
| 736 | err: |
| 737 | if (ret != 1) { |
| 738 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 739 | } |
| 740 | BN_CTX_free(ctx); |
| 741 | BN_free(&u1); |
| 742 | BN_free(&u2); |
| 743 | BN_free(&t1); |
| 744 |
| 745 | return ret; |
| 746 | } |
| 747 |
| 748 | int DSA_sign(int type, const uint8_t *digest, size_t digest_len, |
| 749 | uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) { |
| 750 | DSA_SIG *s; |
| 751 |
| 752 | s = DSA_do_sign(digest, digest_len, dsa); |
| 753 | if (s == NULL) { |
| 754 | *out_siglen = 0; |
| 755 | return 0; |
| 756 | } |
| 757 |
| 758 | *out_siglen = i2d_DSA_SIG(s, &out_sig); |
| 759 | DSA_SIG_free(s); |
| 760 | return 1; |
| 761 | } |
| 762 |
| 763 | int DSA_verify(int type, const uint8_t *digest, size_t digest_len, |
| 764 | const uint8_t *sig, size_t sig_len, const DSA *dsa) { |
| 765 | int valid; |
| 766 | if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) { |
| 767 | return -1; |
| 768 | } |
| 769 | return valid; |
| 770 | } |
| 771 |
| 772 | int DSA_check_signature(int *out_valid, const uint8_t *digest, |
| 773 | size_t digest_len, const uint8_t *sig, size_t sig_len, |
| 774 | const DSA *dsa) { |
| 775 | DSA_SIG *s = NULL; |
| 776 | int ret = 0; |
| 777 | uint8_t *der = NULL; |
| 778 |
| 779 | s = DSA_SIG_new(); |
| 780 | if (s == NULL) { |
| 781 | goto err; |
| 782 | } |
| 783 |
| 784 | const uint8_t *sigp = sig; |
| 785 | if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) { |
| 786 | goto err; |
| 787 | } |
| 788 |
| 789 | // Ensure that the signature uses DER and doesn't have trailing garbage. |
| 790 | int der_len = i2d_DSA_SIG(s, &der); |
| 791 | if (der_len < 0 || (size_t)der_len != sig_len || |
| 792 | OPENSSL_memcmp(sig, der, sig_len)) { |
| 793 | goto err; |
| 794 | } |
| 795 |
| 796 | ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa); |
| 797 |
| 798 | err: |
| 799 | OPENSSL_free(der); |
| 800 | DSA_SIG_free(s); |
| 801 | return ret; |
| 802 | } |
| 803 |
| 804 | // der_len_len returns the number of bytes needed to represent a length of |len| |
| 805 | // in DER. |
| 806 | static size_t der_len_len(size_t len) { |
| 807 | if (len < 0x80) { |
| 808 | return 1; |
| 809 | } |
| 810 | size_t ret = 1; |
| 811 | while (len > 0) { |
| 812 | ret++; |
| 813 | len >>= 8; |
| 814 | } |
| 815 | return ret; |
| 816 | } |
| 817 |
| 818 | int DSA_size(const DSA *dsa) { |
| 819 | size_t order_len = BN_num_bytes(dsa->q); |
| 820 | // Compute the maximum length of an |order_len| byte integer. Defensively |
| 821 | // assume that the leading 0x00 is included. |
| 822 | size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len; |
| 823 | if (integer_len < order_len) { |
| 824 | return 0; |
| 825 | } |
| 826 | // A DSA signature is two INTEGERs. |
| 827 | size_t value_len = 2 * integer_len; |
| 828 | if (value_len < integer_len) { |
| 829 | return 0; |
| 830 | } |
| 831 | // Add the header. |
| 832 | size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len; |
| 833 | if (ret < value_len) { |
| 834 | return 0; |
| 835 | } |
| 836 | return ret; |
| 837 | } |
| 838 |
| 839 | static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv, |
| 840 | BIGNUM **out_r) { |
| 841 | BN_CTX *ctx; |
| 842 | BIGNUM k, kq, *kinv = NULL, *r = NULL; |
| 843 | int ret = 0; |
| 844 |
| 845 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 846 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 847 | return 0; |
| 848 | } |
| 849 |
| 850 | BN_init(&k); |
| 851 | BN_init(&kq); |
| 852 |
| 853 | ctx = ctx_in; |
| 854 | if (ctx == NULL) { |
| 855 | ctx = BN_CTX_new(); |
| 856 | if (ctx == NULL) { |
| 857 | goto err; |
| 858 | } |
| 859 | } |
| 860 |
| 861 | r = BN_new(); |
| 862 | if (r == NULL) { |
| 863 | goto err; |
| 864 | } |
| 865 |
| 866 | // Get random k |
| 867 | if (!BN_rand_range_ex(&k, 1, dsa->q)) { |
| 868 | goto err; |
| 869 | } |
| 870 |
| 871 | if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 872 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p, |
| 873 | ctx) || |
| 874 | !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q, |
| 875 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q, |
| 876 | ctx)) { |
| 877 | goto err; |
| 878 | } |
| 879 |
| 880 | // Compute r = (g^k mod p) mod q |
| 881 | if (!BN_copy(&kq, &k)) { |
| 882 | goto err; |
| 883 | } |
| 884 |
| 885 | // We do not want timing information to leak the length of k, |
| 886 | // so we compute g^k using an equivalent exponent of fixed length. |
| 887 | // |
| 888 | // (This is a kludge that we need because the BN_mod_exp_mont() |
| 889 | // does not let us specify the desired timing behaviour.) |
| 890 |
| 891 | if (!BN_add(&kq, &kq, dsa->q)) { |
| 892 | goto err; |
| 893 | } |
| 894 | if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) { |
| 895 | goto err; |
| 896 | } |
| 897 |
| 898 | if (!BN_mod_exp_mont_consttime(r, dsa->g, &kq, dsa->p, ctx, |
| 899 | dsa->method_mont_p)) { |
| 900 | goto err; |
| 901 | } |
| 902 | if (!BN_mod(r, r, dsa->q, ctx)) { |
| 903 | goto err; |
| 904 | } |
| 905 |
| 906 | // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little |
| 907 | // Theorem. |
| 908 | kinv = BN_new(); |
| 909 | if (kinv == NULL || |
| 910 | !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) { |
| 911 | goto err; |
| 912 | } |
| 913 |
| 914 | BN_clear_free(*out_kinv); |
| 915 | *out_kinv = kinv; |
| 916 | kinv = NULL; |
| 917 | BN_clear_free(*out_r); |
| 918 | *out_r = r; |
| 919 | ret = 1; |
| 920 |
| 921 | err: |
| 922 | if (!ret) { |
| 923 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 924 | if (r != NULL) { |
| 925 | BN_clear_free(r); |
| 926 | } |
| 927 | } |
| 928 |
| 929 | if (ctx_in == NULL) { |
| 930 | BN_CTX_free(ctx); |
| 931 | } |
| 932 | BN_clear_free(&k); |
| 933 | BN_clear_free(&kq); |
| 934 | BN_clear_free(kinv); |
| 935 | return ret; |
| 936 | } |
| 937 |
| 938 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 939 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
| 940 | int index; |
| 941 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 942 | free_func)) { |
| 943 | return -1; |
| 944 | } |
| 945 | return index; |
| 946 | } |
| 947 |
| 948 | int DSA_set_ex_data(DSA *dsa, int idx, void *arg) { |
| 949 | return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg); |
| 950 | } |
| 951 |
| 952 | void *DSA_get_ex_data(const DSA *dsa, int idx) { |
| 953 | return CRYPTO_get_ex_data(&dsa->ex_data, idx); |
| 954 | } |
| 955 |
| 956 | DH *DSA_dup_DH(const DSA *dsa) { |
| 957 | if (dsa == NULL) { |
| 958 | return NULL; |
| 959 | } |
| 960 |
| 961 | DH *ret = DH_new(); |
| 962 | if (ret == NULL) { |
| 963 | goto err; |
| 964 | } |
| 965 | if (dsa->q != NULL) { |
| 966 | ret->priv_length = BN_num_bits(dsa->q); |
| 967 | if ((ret->q = BN_dup(dsa->q)) == NULL) { |
| 968 | goto err; |
| 969 | } |
| 970 | } |
| 971 | if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) || |
| 972 | (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) || |
| 973 | (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) || |
| 974 | (dsa->priv_key != NULL && |
| 975 | (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) { |
| 976 | goto err; |
| 977 | } |
| 978 |
| 979 | return ret; |
| 980 |
| 981 | err: |
| 982 | DH_free(ret); |
| 983 | return NULL; |
| 984 | } |
| 985 |