| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/dsa.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/bn.h> |
| 20 | #include <openssl/dh.h> |
| 21 | #include <openssl/digest.h> |
| 22 | #include <openssl/engine.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/ex_data.h> |
| 25 | #include <openssl/mem.h> |
| 26 | #include <openssl/rand.h> |
| 27 | #include <openssl/sha.h> |
| 28 | #include <openssl/thread.h> |
| 29 |
| 30 | #include "../fipsmodule/bn/internal.h" |
| 31 | #include "../fipsmodule/dh/internal.h" |
| 32 | #include "../internal.h" |
| 33 | #include "internal.h" |
| 34 |
| 35 |
| 36 | static_assert(OPENSSL_DSA_MAX_MODULUS_BITS <= |
| 37 | BN_MONTGOMERY_MAX_WORDS * BN_BITS2, |
| 38 | "Max DSA size too big for Montgomery arithmetic"); |
| 39 |
| 40 | // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of |
| 41 | // Miller-Rabin. |
| 42 | #define DSS_prime_checks 50 |
| 43 |
| 44 | static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv, |
| 45 | BIGNUM **out_r); |
| 46 |
| 47 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; |
| 48 |
| 49 | DSA *DSA_new(void) { |
| 50 | DSA *dsa = reinterpret_cast<DSA *>(OPENSSL_zalloc(sizeof(DSA))); |
| 51 | if (dsa == NULL) { |
| 52 | return NULL; |
| 53 | } |
| 54 |
| 55 | dsa->references = 1; |
| 56 | CRYPTO_MUTEX_init(&dsa->method_mont_lock); |
| 57 | CRYPTO_new_ex_data(&dsa->ex_data); |
| 58 | return dsa; |
| 59 | } |
| 60 |
| 61 | void DSA_free(DSA *dsa) { |
| 62 | if (dsa == NULL) { |
| 63 | return; |
| 64 | } |
| 65 |
| 66 | if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) { |
| 67 | return; |
| 68 | } |
| 69 |
| 70 | CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data); |
| 71 |
| 72 | BN_clear_free(dsa->p); |
| 73 | BN_clear_free(dsa->q); |
| 74 | BN_clear_free(dsa->g); |
| 75 | BN_clear_free(dsa->pub_key); |
| 76 | BN_clear_free(dsa->priv_key); |
| 77 | BN_MONT_CTX_free(dsa->method_mont_p); |
| 78 | BN_MONT_CTX_free(dsa->method_mont_q); |
| 79 | CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock); |
| 80 | OPENSSL_free(dsa); |
| 81 | } |
| 82 |
| 83 | int DSA_up_ref(DSA *dsa) { |
| 84 | CRYPTO_refcount_inc(&dsa->references); |
| 85 | return 1; |
| 86 | } |
| 87 |
| 88 | unsigned DSA_bits(const DSA *dsa) { return BN_num_bits(dsa->p); } |
| 89 |
| 90 | const BIGNUM *DSA_get0_pub_key(const DSA *dsa) { return dsa->pub_key; } |
| 91 |
| 92 | const BIGNUM *DSA_get0_priv_key(const DSA *dsa) { return dsa->priv_key; } |
| 93 |
| 94 | const BIGNUM *DSA_get0_p(const DSA *dsa) { return dsa->p; } |
| 95 |
| 96 | const BIGNUM *DSA_get0_q(const DSA *dsa) { return dsa->q; } |
| 97 |
| 98 | const BIGNUM *DSA_get0_g(const DSA *dsa) { return dsa->g; } |
| 99 |
| 100 | void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key, |
| 101 | const BIGNUM **out_priv_key) { |
| 102 | if (out_pub_key != NULL) { |
| 103 | *out_pub_key = dsa->pub_key; |
| 104 | } |
| 105 | if (out_priv_key != NULL) { |
| 106 | *out_priv_key = dsa->priv_key; |
| 107 | } |
| 108 | } |
| 109 |
| 110 | void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q, |
| 111 | const BIGNUM **out_g) { |
| 112 | if (out_p != NULL) { |
| 113 | *out_p = dsa->p; |
| 114 | } |
| 115 | if (out_q != NULL) { |
| 116 | *out_q = dsa->q; |
| 117 | } |
| 118 | if (out_g != NULL) { |
| 119 | *out_g = dsa->g; |
| 120 | } |
| 121 | } |
| 122 |
| 123 | int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) { |
| 124 | if (dsa->pub_key == NULL && pub_key == NULL) { |
| 125 | return 0; |
| 126 | } |
| 127 |
| 128 | if (pub_key != NULL) { |
| 129 | BN_free(dsa->pub_key); |
| 130 | dsa->pub_key = pub_key; |
| 131 | } |
| 132 | if (priv_key != NULL) { |
| 133 | BN_free(dsa->priv_key); |
| 134 | dsa->priv_key = priv_key; |
| 135 | } |
| 136 |
| 137 | return 1; |
| 138 | } |
| 139 |
| 140 | int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) { |
| 141 | if ((dsa->p == NULL && p == NULL) || (dsa->q == NULL && q == NULL) || |
| 142 | (dsa->g == NULL && g == NULL)) { |
| 143 | return 0; |
| 144 | } |
| 145 |
| 146 | if (p != NULL) { |
| 147 | BN_free(dsa->p); |
| 148 | dsa->p = p; |
| 149 | } |
| 150 | if (q != NULL) { |
| 151 | BN_free(dsa->q); |
| 152 | dsa->q = q; |
| 153 | } |
| 154 | if (g != NULL) { |
| 155 | BN_free(dsa->g); |
| 156 | dsa->g = g; |
| 157 | } |
| 158 |
| 159 | BN_MONT_CTX_free(dsa->method_mont_p); |
| 160 | dsa->method_mont_p = NULL; |
| 161 | BN_MONT_CTX_free(dsa->method_mont_q); |
| 162 | dsa->method_mont_q = NULL; |
| 163 | return 1; |
| 164 | } |
| 165 |
| 166 | int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in, |
| 167 | size_t seed_len, int *out_counter, |
| 168 | unsigned long *out_h, BN_GENCB *cb) { |
| 169 | if (bits > OPENSSL_DSA_MAX_MODULUS_BITS) { |
| 170 | OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS); |
| 171 | return 0; |
| 172 | } |
| 173 |
| 174 | unsigned char seed[SHA256_DIGEST_LENGTH]; |
| 175 | unsigned char md[SHA256_DIGEST_LENGTH]; |
| 176 | unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH]; |
| 177 | BIGNUM *r0, *W, *X, *c, *test; |
| 178 | BIGNUM *g = NULL, *q = NULL, *p = NULL; |
| 179 | int k, n = 0, m = 0; |
| 180 | int counter = 0; |
| 181 | int r = 0; |
| 182 | unsigned int h = 2; |
| 183 | const EVP_MD *evpmd; |
| 184 |
| 185 | evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1(); |
| 186 | size_t qsize = EVP_MD_size(evpmd); |
| 187 |
| 188 | if (bits < 512) { |
| 189 | bits = 512; |
| 190 | } |
| 191 |
| 192 | bits = (bits + 63) / 64 * 64; |
| 193 |
| 194 | if (seed_in != NULL) { |
| 195 | if (seed_len < qsize) { |
| 196 | return 0; |
| 197 | } |
| 198 | if (seed_len > qsize) { |
| 199 | // Only consume as much seed as is expected. |
| 200 | seed_len = qsize; |
| 201 | } |
| 202 | OPENSSL_memcpy(seed, seed_in, seed_len); |
| 203 | } |
| 204 |
| 205 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 206 | if (ctx == nullptr) { |
| 207 | return 0; |
| 208 | } |
| 209 | bssl::BN_CTXScope scope(ctx.get()); |
| 210 |
| 211 | r0 = BN_CTX_get(ctx.get()); |
| 212 | g = BN_CTX_get(ctx.get()); |
| 213 | W = BN_CTX_get(ctx.get()); |
| 214 | q = BN_CTX_get(ctx.get()); |
| 215 | X = BN_CTX_get(ctx.get()); |
| 216 | c = BN_CTX_get(ctx.get()); |
| 217 | p = BN_CTX_get(ctx.get()); |
| 218 | test = BN_CTX_get(ctx.get()); |
| 219 |
| 220 | if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) { |
| 221 | return 0; |
| 222 | } |
| 223 |
| 224 | for (;;) { |
| 225 | // Find q. |
| 226 | for (;;) { |
| 227 | // step 1 |
| 228 | if (!BN_GENCB_call(cb, BN_GENCB_GENERATED, m++)) { |
| 229 | return 0; |
| 230 | } |
| 231 |
| 232 | int use_random_seed = (seed_in == NULL); |
| 233 | if (use_random_seed) { |
| 234 | if (!RAND_bytes(seed, qsize)) { |
| 235 | return 0; |
| 236 | } |
| 237 | // DSA parameters are public. |
| 238 | CONSTTIME_DECLASSIFY(seed, qsize); |
| 239 | } else { |
| 240 | // If we come back through, use random seed next time. |
| 241 | seed_in = NULL; |
| 242 | } |
| 243 | OPENSSL_memcpy(buf, seed, qsize); |
| 244 | OPENSSL_memcpy(buf2, seed, qsize); |
| 245 | // precompute "SEED + 1" for step 7: |
| 246 | for (size_t i = qsize - 1; i < qsize; i--) { |
| 247 | buf[i]++; |
| 248 | if (buf[i] != 0) { |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 |
| 253 | // step 2 |
| 254 | if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) || |
| 255 | !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) { |
| 256 | return 0; |
| 257 | } |
| 258 | for (size_t i = 0; i < qsize; i++) { |
| 259 | md[i] ^= buf2[i]; |
| 260 | } |
| 261 |
| 262 | // step 3 |
| 263 | md[0] |= 0x80; |
| 264 | md[qsize - 1] |= 0x01; |
| 265 | if (!BN_bin2bn(md, qsize, q)) { |
| 266 | return 0; |
| 267 | } |
| 268 |
| 269 | // step 4 |
| 270 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx.get(), |
| 271 | use_random_seed, cb); |
| 272 | if (r > 0) { |
| 273 | break; |
| 274 | } |
| 275 | if (r != 0) { |
| 276 | return 0; |
| 277 | } |
| 278 |
| 279 | // do a callback call |
| 280 | // step 5 |
| 281 | } |
| 282 |
| 283 | if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) { |
| 284 | return 0; |
| 285 | } |
| 286 |
| 287 | // step 6 |
| 288 | counter = 0; |
| 289 | // "offset = 2" |
| 290 |
| 291 | n = (bits - 1) / 160; |
| 292 |
| 293 | for (;;) { |
| 294 | if ((counter != 0) && !BN_GENCB_call(cb, BN_GENCB_GENERATED, counter)) { |
| 295 | return 0; |
| 296 | } |
| 297 |
| 298 | // step 7 |
| 299 | BN_zero(W); |
| 300 | // now 'buf' contains "SEED + offset - 1" |
| 301 | for (k = 0; k <= n; k++) { |
| 302 | // obtain "SEED + offset + k" by incrementing: |
| 303 | for (size_t i = qsize - 1; i < qsize; i--) { |
| 304 | buf[i]++; |
| 305 | if (buf[i] != 0) { |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 |
| 310 | if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) { |
| 311 | return 0; |
| 312 | } |
| 313 |
| 314 | // step 8 |
| 315 | if (!BN_bin2bn(md, qsize, r0) || !BN_lshift(r0, r0, (qsize << 3) * k) || |
| 316 | !BN_add(W, W, r0)) { |
| 317 | return 0; |
| 318 | } |
| 319 | } |
| 320 |
| 321 | // more of step 8 |
| 322 | if (!BN_mask_bits(W, bits - 1) || !BN_copy(X, W) || !BN_add(X, X, test)) { |
| 323 | return 0; |
| 324 | } |
| 325 |
| 326 | // step 9 |
| 327 | if (!BN_lshift1(r0, q) || !BN_mod(c, X, r0, ctx.get()) || |
| 328 | !BN_sub(r0, c, BN_value_one()) || !BN_sub(p, X, r0)) { |
| 329 | return 0; |
| 330 | } |
| 331 |
| 332 | // step 10 |
| 333 | if (BN_cmp(p, test) >= 0) { |
| 334 | // step 11 |
| 335 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx.get(), 1, cb); |
| 336 | if (r > 0) { |
| 337 | goto end; // found it |
| 338 | } |
| 339 | if (r != 0) { |
| 340 | return 0; |
| 341 | } |
| 342 | } |
| 343 |
| 344 | // step 13 |
| 345 | counter++; |
| 346 | // "offset = offset + n + 1" |
| 347 |
| 348 | // step 14 |
| 349 | if (counter >= 4096) { |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | end: |
| 355 | if (!BN_GENCB_call(cb, 2, 1)) { |
| 356 | return 0; |
| 357 | } |
| 358 |
| 359 | // We now need to generate g |
| 360 | // Set r0=(p-1)/q |
| 361 | if (!BN_sub(test, p, BN_value_one()) || |
| 362 | !BN_div(r0, NULL, test, q, ctx.get())) { |
| 363 | return 0; |
| 364 | } |
| 365 |
| 366 | bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new_for_modulus(p, ctx.get())); |
| 367 | if (mont == nullptr || !BN_set_word(test, h)) { |
| 368 | return 0; |
| 369 | } |
| 370 |
| 371 | for (;;) { |
| 372 | // g=test^r0%p |
| 373 | if (!BN_mod_exp_mont(g, test, r0, p, ctx.get(), mont.get())) { |
| 374 | return 0; |
| 375 | } |
| 376 | if (!BN_is_one(g)) { |
| 377 | break; |
| 378 | } |
| 379 | if (!BN_add(test, test, BN_value_one())) { |
| 380 | return 0; |
| 381 | } |
| 382 | h++; |
| 383 | } |
| 384 |
| 385 | if (!BN_GENCB_call(cb, 3, 1)) { |
| 386 | return 0; |
| 387 | } |
| 388 |
| 389 | BN_free(dsa->p); |
| 390 | BN_free(dsa->q); |
| 391 | BN_free(dsa->g); |
| 392 | dsa->p = BN_dup(p); |
| 393 | dsa->q = BN_dup(q); |
| 394 | dsa->g = BN_dup(g); |
| 395 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
| 396 | return 0; |
| 397 | } |
| 398 | if (out_counter != NULL) { |
| 399 | *out_counter = counter; |
| 400 | } |
| 401 | if (out_h != NULL) { |
| 402 | *out_h = h; |
| 403 | } |
| 404 |
| 405 | return 1; |
| 406 | } |
| 407 |
| 408 | DSA *DSAparams_dup(const DSA *dsa) { |
| 409 | DSA *ret = DSA_new(); |
| 410 | if (ret == NULL) { |
| 411 | return NULL; |
| 412 | } |
| 413 | ret->p = BN_dup(dsa->p); |
| 414 | ret->q = BN_dup(dsa->q); |
| 415 | ret->g = BN_dup(dsa->g); |
| 416 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) { |
| 417 | DSA_free(ret); |
| 418 | return NULL; |
| 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 |
| 423 | int DSA_generate_key(DSA *dsa) { |
| 424 | if (!dsa_check_key(dsa)) { |
| 425 | return 0; |
| 426 | } |
| 427 |
| 428 | int ok = 0; |
| 429 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
| 430 | BN_CTX *ctx = BN_CTX_new(); |
| 431 | if (ctx == NULL) { |
| 432 | goto err; |
| 433 | } |
| 434 |
| 435 | priv_key = dsa->priv_key; |
| 436 | if (priv_key == NULL) { |
| 437 | priv_key = BN_new(); |
| 438 | if (priv_key == NULL) { |
| 439 | goto err; |
| 440 | } |
| 441 | } |
| 442 |
| 443 | if (!BN_rand_range_ex(priv_key, 1, dsa->q)) { |
| 444 | goto err; |
| 445 | } |
| 446 |
| 447 | pub_key = dsa->pub_key; |
| 448 | if (pub_key == NULL) { |
| 449 | pub_key = BN_new(); |
| 450 | if (pub_key == NULL) { |
| 451 | goto err; |
| 452 | } |
| 453 | } |
| 454 |
| 455 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock, |
| 456 | dsa->p, ctx) || |
| 457 | !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx, |
| 458 | dsa->method_mont_p)) { |
| 459 | goto err; |
| 460 | } |
| 461 |
| 462 | // The public key is computed from the private key, but is public. |
| 463 | bn_declassify(pub_key); |
| 464 |
| 465 | dsa->priv_key = priv_key; |
| 466 | dsa->pub_key = pub_key; |
| 467 | ok = 1; |
| 468 |
| 469 | err: |
| 470 | if (dsa->pub_key == NULL) { |
| 471 | BN_free(pub_key); |
| 472 | } |
| 473 | if (dsa->priv_key == NULL) { |
| 474 | BN_free(priv_key); |
| 475 | } |
| 476 | BN_CTX_free(ctx); |
| 477 |
| 478 | return ok; |
| 479 | } |
| 480 |
| 481 | DSA_SIG *DSA_SIG_new(void) { |
| 482 | return reinterpret_cast<DSA_SIG *>(OPENSSL_zalloc(sizeof(DSA_SIG))); |
| 483 | } |
| 484 |
| 485 | void DSA_SIG_free(DSA_SIG *sig) { |
| 486 | if (!sig) { |
| 487 | return; |
| 488 | } |
| 489 |
| 490 | BN_free(sig->r); |
| 491 | BN_free(sig->s); |
| 492 | OPENSSL_free(sig); |
| 493 | } |
| 494 |
| 495 | void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r, |
| 496 | const BIGNUM **out_s) { |
| 497 | if (out_r != NULL) { |
| 498 | *out_r = sig->r; |
| 499 | } |
| 500 | if (out_s != NULL) { |
| 501 | *out_s = sig->s; |
| 502 | } |
| 503 | } |
| 504 |
| 505 | int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) { |
| 506 | if (r == NULL || s == NULL) { |
| 507 | return 0; |
| 508 | } |
| 509 | BN_free(sig->r); |
| 510 | BN_free(sig->s); |
| 511 | sig->r = r; |
| 512 | sig->s = s; |
| 513 | return 1; |
| 514 | } |
| 515 |
| 516 | // mod_mul_consttime sets |r| to |a| * |b| modulo |mont->N|, treating |a| and |
| 517 | // |b| as secret. This function internally uses Montgomery reduction, but |
| 518 | // neither inputs nor outputs are in Montgomery form. |
| 519 | static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 520 | const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 521 | bssl::BN_CTXScope scope(ctx); |
| 522 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 523 | // |BN_mod_mul_montgomery| removes a factor of R, so we cancel it with a |
| 524 | // single |BN_to_montgomery| which adds one factor of R. |
| 525 | return tmp != nullptr && // |
| 526 | BN_to_montgomery(tmp, a, mont, ctx) && |
| 527 | BN_mod_mul_montgomery(r, tmp, b, mont, ctx); |
| 528 | } |
| 529 |
| 530 | DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) { |
| 531 | if (!dsa_check_key(dsa)) { |
| 532 | return NULL; |
| 533 | } |
| 534 |
| 535 | if (dsa->priv_key == NULL) { |
| 536 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 537 | return NULL; |
| 538 | } |
| 539 |
| 540 | BIGNUM *kinv = NULL, *r = NULL, *s = NULL; |
| 541 | BIGNUM m; |
| 542 | BIGNUM xr; |
| 543 | BN_CTX *ctx = NULL; |
| 544 | DSA_SIG *ret = NULL; |
| 545 |
| 546 | BN_init(&m); |
| 547 | BN_init(&xr); |
| 548 | s = BN_new(); |
| 549 | { |
| 550 | if (s == NULL) { |
| 551 | goto err; |
| 552 | } |
| 553 | ctx = BN_CTX_new(); |
| 554 | if (ctx == NULL) { |
| 555 | goto err; |
| 556 | } |
| 557 |
| 558 | // Cap iterations so that invalid parameters do not infinite loop. This does |
| 559 | // not impact valid parameters because the probability of requiring even one |
| 560 | // retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, |
| 561 | // so invalid parameters are reachable from most callers handling untrusted |
| 562 | // private keys. (The |dsa_check_key| call above is not sufficient. Checking |
| 563 | // whether arbitrary paremeters form a valid DSA group is expensive.) |
| 564 | static const int kMaxIterations = 32; |
| 565 | int iters = 0; |
| 566 | redo: |
| 567 | if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) { |
| 568 | goto err; |
| 569 | } |
| 570 |
| 571 | if (digest_len > BN_num_bytes(dsa->q)) { |
| 572 | // If the digest length is greater than the size of |dsa->q| use the |
| 573 | // BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-3, 4.2. |
| 574 | // Note the above check that |dsa->q| is a multiple of 8 bits. |
| 575 | digest_len = BN_num_bytes(dsa->q); |
| 576 | } |
| 577 |
| 578 | if (BN_bin2bn(digest, digest_len, &m) == NULL) { |
| 579 | goto err; |
| 580 | } |
| 581 |
| 582 | // |m| is bounded by 2^(num_bits(q)), which is slightly looser than q. This |
| 583 | // violates |bn_mod_add_consttime| and |mod_mul_consttime|'s preconditions. |
| 584 | // (The underlying algorithms could accept looser bounds, but we reduce for |
| 585 | // simplicity.) |
| 586 | size_t q_width = bn_minimal_width(dsa->q); |
| 587 | if (!bn_resize_words(&m, q_width) || !bn_resize_words(&xr, q_width)) { |
| 588 | goto err; |
| 589 | } |
| 590 | bn_reduce_once_in_place(m.d, 0 /* no carry word */, dsa->q->d, |
| 591 | xr.d /* scratch space */, q_width); |
| 592 |
| 593 | // Compute s = inv(k) (m + xr) mod q. Note |dsa->method_mont_q| is |
| 594 | // initialized by |dsa_sign_setup|. |
| 595 | if (!mod_mul_consttime(&xr, dsa->priv_key, r, dsa->method_mont_q, ctx) || |
| 596 | !bn_mod_add_consttime(s, &xr, &m, dsa->q, ctx) || |
| 597 | !mod_mul_consttime(s, s, kinv, dsa->method_mont_q, ctx)) { |
| 598 | goto err; |
| 599 | } |
| 600 |
| 601 | // The signature is computed from the private key, but is public. |
| 602 | bn_declassify(r); |
| 603 | bn_declassify(s); |
| 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 | iters++; |
| 609 | if (iters > kMaxIterations) { |
| 610 | OPENSSL_PUT_ERROR(DSA, DSA_R_TOO_MANY_ITERATIONS); |
| 611 | goto err; |
| 612 | } |
| 613 | goto redo; |
| 614 | } |
| 615 |
| 616 | ret = DSA_SIG_new(); |
| 617 | if (ret == NULL) { |
| 618 | goto err; |
| 619 | } |
| 620 | ret->r = r; |
| 621 | ret->s = s; |
| 622 | } |
| 623 |
| 624 | err: |
| 625 | if (ret == NULL) { |
| 626 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 627 | BN_free(r); |
| 628 | BN_free(s); |
| 629 | } |
| 630 | BN_CTX_free(ctx); |
| 631 | BN_clear_free(&m); |
| 632 | BN_clear_free(&xr); |
| 633 | BN_clear_free(kinv); |
| 634 |
| 635 | return ret; |
| 636 | } |
| 637 |
| 638 | int DSA_do_verify(const uint8_t *digest, size_t digest_len, const DSA_SIG *sig, |
| 639 | const DSA *dsa) { |
| 640 | int valid; |
| 641 | if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) { |
| 642 | return -1; |
| 643 | } |
| 644 | return valid; |
| 645 | } |
| 646 |
| 647 | int DSA_do_check_signature(int *out_valid, const uint8_t *digest, |
| 648 | size_t digest_len, const DSA_SIG *sig, |
| 649 | const DSA *dsa) { |
| 650 | *out_valid = 0; |
| 651 | if (!dsa_check_key(dsa)) { |
| 652 | return 0; |
| 653 | } |
| 654 |
| 655 | if (dsa->pub_key == NULL) { |
| 656 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 657 | return 0; |
| 658 | } |
| 659 |
| 660 | int ret = 0; |
| 661 | BIGNUM u1, u2, t1; |
| 662 | BN_init(&u1); |
| 663 | BN_init(&u2); |
| 664 | BN_init(&t1); |
| 665 | BN_CTX *ctx = BN_CTX_new(); |
| 666 | { |
| 667 | if (ctx == NULL) { |
| 668 | goto err; |
| 669 | } |
| 670 |
| 671 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
| 672 | BN_ucmp(sig->r, dsa->q) >= 0) { |
| 673 | ret = 1; |
| 674 | goto err; |
| 675 | } |
| 676 | if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || |
| 677 | BN_ucmp(sig->s, dsa->q) >= 0) { |
| 678 | ret = 1; |
| 679 | goto err; |
| 680 | } |
| 681 |
| 682 | // Calculate W = inv(S) mod Q |
| 683 | // save W in u2 |
| 684 | if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) { |
| 685 | goto err; |
| 686 | } |
| 687 |
| 688 | // save M in u1 |
| 689 | unsigned q_bits = BN_num_bits(dsa->q); |
| 690 | if (digest_len > (q_bits >> 3)) { |
| 691 | // if the digest length is greater than the size of q use the |
| 692 | // BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 693 | // fips 186-3, 4.2 |
| 694 | digest_len = (q_bits >> 3); |
| 695 | } |
| 696 |
| 697 | if (BN_bin2bn(digest, digest_len, &u1) == NULL) { |
| 698 | goto err; |
| 699 | } |
| 700 |
| 701 | // u1 = M * w mod q |
| 702 | if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) { |
| 703 | goto err; |
| 704 | } |
| 705 |
| 706 | // u2 = r * w mod q |
| 707 | if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) { |
| 708 | goto err; |
| 709 | } |
| 710 |
| 711 | if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 712 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p, |
| 713 | ctx)) { |
| 714 | goto err; |
| 715 | } |
| 716 |
| 717 | if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, |
| 718 | dsa->method_mont_p)) { |
| 719 | goto err; |
| 720 | } |
| 721 |
| 722 | // BN_copy(&u1,&t1); |
| 723 | // let u1 = u1 mod q |
| 724 | if (!BN_mod(&u1, &t1, dsa->q, ctx)) { |
| 725 | goto err; |
| 726 | } |
| 727 |
| 728 | // V is now in u1. If the signature is correct, it will be |
| 729 | // equal to R. |
| 730 | *out_valid = BN_ucmp(&u1, sig->r) == 0; |
| 731 | ret = 1; |
| 732 | } |
| 733 |
| 734 | err: |
| 735 | if (ret != 1) { |
| 736 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 737 | } |
| 738 | BN_CTX_free(ctx); |
| 739 | BN_free(&u1); |
| 740 | BN_free(&u2); |
| 741 | BN_free(&t1); |
| 742 |
| 743 | return ret; |
| 744 | } |
| 745 |
| 746 | int DSA_sign(int type, const uint8_t *digest, size_t digest_len, |
| 747 | uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) { |
| 748 | DSA_SIG *s; |
| 749 |
| 750 | s = DSA_do_sign(digest, digest_len, dsa); |
| 751 | if (s == NULL) { |
| 752 | *out_siglen = 0; |
| 753 | return 0; |
| 754 | } |
| 755 |
| 756 | *out_siglen = i2d_DSA_SIG(s, &out_sig); |
| 757 | DSA_SIG_free(s); |
| 758 | return 1; |
| 759 | } |
| 760 |
| 761 | int DSA_verify(int type, const uint8_t *digest, size_t digest_len, |
| 762 | const uint8_t *sig, size_t sig_len, const DSA *dsa) { |
| 763 | int valid; |
| 764 | if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) { |
| 765 | return -1; |
| 766 | } |
| 767 | return valid; |
| 768 | } |
| 769 |
| 770 | int DSA_check_signature(int *out_valid, const uint8_t *digest, |
| 771 | size_t digest_len, const uint8_t *sig, size_t sig_len, |
| 772 | const DSA *dsa) { |
| 773 | DSA_SIG *s = NULL; |
| 774 | int ret = 0; |
| 775 | uint8_t *der = NULL; |
| 776 |
| 777 | s = DSA_SIG_new(); |
| 778 | { |
| 779 | if (s == NULL) { |
| 780 | goto err; |
| 781 | } |
| 782 |
| 783 | const uint8_t *sigp = sig; |
| 784 | if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) { |
| 785 | goto err; |
| 786 | } |
| 787 |
| 788 | // Ensure that the signature uses DER and doesn't have trailing garbage. |
| 789 | int der_len = i2d_DSA_SIG(s, &der); |
| 790 | if (der_len < 0 || (size_t)der_len != sig_len || |
| 791 | OPENSSL_memcmp(sig, der, sig_len)) { |
| 792 | goto err; |
| 793 | } |
| 794 |
| 795 | ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa); |
| 796 | } |
| 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 | if (dsa->q == NULL) { |
| 820 | return 0; |
| 821 | } |
| 822 |
| 823 | size_t order_len = BN_num_bytes(dsa->q); |
| 824 | // Compute the maximum length of an |order_len| byte integer. Defensively |
| 825 | // assume that the leading 0x00 is included. |
| 826 | size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len; |
| 827 | if (integer_len < order_len) { |
| 828 | return 0; |
| 829 | } |
| 830 | // A DSA signature is two INTEGERs. |
| 831 | size_t value_len = 2 * integer_len; |
| 832 | if (value_len < integer_len) { |
| 833 | return 0; |
| 834 | } |
| 835 | // Add the header. |
| 836 | size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len; |
| 837 | if (ret < value_len) { |
| 838 | return 0; |
| 839 | } |
| 840 | return ret; |
| 841 | } |
| 842 |
| 843 | static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv, |
| 844 | BIGNUM **out_r) { |
| 845 | int ret = 0; |
| 846 | BIGNUM k; |
| 847 | BN_init(&k); |
| 848 | BIGNUM *r = BN_new(); |
| 849 | BIGNUM *kinv = BN_new(); |
| 850 | if (r == NULL || kinv == NULL || |
| 851 | // Get random k |
| 852 | !BN_rand_range_ex(&k, 1, dsa->q) || |
| 853 | !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 854 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p, |
| 855 | ctx) || |
| 856 | !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q, |
| 857 | (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q, |
| 858 | ctx) || |
| 859 | // Compute r = (g^k mod p) mod q |
| 860 | !BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx, |
| 861 | dsa->method_mont_p)) { |
| 862 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 863 | goto err; |
| 864 | } |
| 865 | // Note |BN_mod| below is not constant-time and may leak information about |
| 866 | // |r|. |dsa->p| may be significantly larger than |dsa->q|, so this is not |
| 867 | // easily performed in constant-time with Montgomery reduction. |
| 868 | // |
| 869 | // However, |r| at this point is g^k (mod p). It is almost the value of |r| |
| 870 | // revealed in the signature anyway (g^k (mod p) (mod q)), going from it to |
| 871 | // |k| would require computing a discrete log. |
| 872 | bn_declassify(r); |
| 873 | if (!BN_mod(r, r, dsa->q, ctx) || |
| 874 | // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little |
| 875 | // Theorem. |
| 876 | !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) { |
| 877 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 878 | goto err; |
| 879 | } |
| 880 |
| 881 | BN_clear_free(*out_kinv); |
| 882 | *out_kinv = kinv; |
| 883 | kinv = NULL; |
| 884 |
| 885 | BN_clear_free(*out_r); |
| 886 | *out_r = r; |
| 887 | r = NULL; |
| 888 |
| 889 | ret = 1; |
| 890 |
| 891 | err: |
| 892 | BN_clear_free(&k); |
| 893 | BN_clear_free(r); |
| 894 | BN_clear_free(kinv); |
| 895 | return ret; |
| 896 | } |
| 897 |
| 898 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 899 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
| 900 | return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func); |
| 901 | } |
| 902 |
| 903 | int DSA_set_ex_data(DSA *dsa, int idx, void *arg) { |
| 904 | return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg); |
| 905 | } |
| 906 |
| 907 | void *DSA_get_ex_data(const DSA *dsa, int idx) { |
| 908 | return CRYPTO_get_ex_data(&dsa->ex_data, idx); |
| 909 | } |
| 910 |
| 911 | DH *DSA_dup_DH(const DSA *dsa) { |
| 912 | if (dsa == NULL) { |
| 913 | return NULL; |
| 914 | } |
| 915 |
| 916 | DH *ret = DH_new(); |
| 917 | if (ret == NULL) { |
| 918 | goto err; |
| 919 | } |
| 920 | if (dsa->q != NULL) { |
| 921 | ret->priv_length = BN_num_bits(dsa->q); |
| 922 | if ((ret->q = BN_dup(dsa->q)) == NULL) { |
| 923 | goto err; |
| 924 | } |
| 925 | } |
| 926 | if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) || |
| 927 | (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) || |
| 928 | (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) || |
| 929 | (dsa->priv_key != NULL && |
| 930 | (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) { |
| 931 | goto err; |
| 932 | } |
| 933 |
| 934 | return ret; |
| 935 |
| 936 | err: |
| 937 | DH_free(ret); |
| 938 | return NULL; |
| 939 | } |
| 940 |