Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 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 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 62 | #include <string.h> |
| 63 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include <openssl/asn1.h> |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 65 | #include <openssl/bn.h> |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 66 | #include <openssl/dh.h> |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 67 | #include <openssl/digest.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | #include <openssl/engine.h> |
| 69 | #include <openssl/err.h> |
| 70 | #include <openssl/ex_data.h> |
| 71 | #include <openssl/mem.h> |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 72 | #include <openssl/rand.h> |
| 73 | #include <openssl/sha.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 74 | #include <openssl/thread.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 75 | |
| 76 | #include "internal.h" |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 77 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 79 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 80 | #define OPENSSL_DSA_MAX_MODULUS_BITS 10000 |
| 81 | |
| 82 | /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of |
| 83 | * Rabin-Miller */ |
| 84 | #define DSS_prime_checks 50 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 86 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; |
| 87 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 88 | DSA *DSA_new(void) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA)); |
| 90 | if (dsa == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 91 | OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | memset(dsa, 0, sizeof(DSA)); |
| 96 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | dsa->write_params = 1; |
| 98 | dsa->references = 1; |
| 99 | |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 100 | CRYPTO_MUTEX_init(&dsa->method_mont_p_lock); |
| 101 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 102 | if (!CRYPTO_new_ex_data(&g_ex_data_class, dsa, &dsa->ex_data)) { |
David Benjamin | 79680ff | 2015-10-23 18:46:16 -0400 | [diff] [blame] | 103 | CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | OPENSSL_free(dsa); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | return dsa; |
| 109 | } |
| 110 | |
| 111 | void DSA_free(DSA *dsa) { |
| 112 | if (dsa == NULL) { |
| 113 | return; |
| 114 | } |
| 115 | |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 116 | if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 120 | CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 122 | BN_clear_free(dsa->p); |
| 123 | BN_clear_free(dsa->q); |
| 124 | BN_clear_free(dsa->g); |
| 125 | BN_clear_free(dsa->pub_key); |
| 126 | BN_clear_free(dsa->priv_key); |
| 127 | BN_clear_free(dsa->kinv); |
| 128 | BN_clear_free(dsa->r); |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 129 | BN_MONT_CTX_free(dsa->method_mont_p); |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 130 | CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | OPENSSL_free(dsa); |
| 132 | } |
| 133 | |
| 134 | int DSA_up_ref(DSA *dsa) { |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 135 | CRYPTO_refcount_inc(&dsa->references); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in, |
| 140 | size_t seed_len, int *out_counter, |
| 141 | unsigned long *out_h, BN_GENCB *cb) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 142 | int ok = 0; |
| 143 | unsigned char seed[SHA256_DIGEST_LENGTH]; |
| 144 | unsigned char md[SHA256_DIGEST_LENGTH]; |
| 145 | unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH]; |
| 146 | BIGNUM *r0, *W, *X, *c, *test; |
| 147 | BIGNUM *g = NULL, *q = NULL, *p = NULL; |
| 148 | BN_MONT_CTX *mont = NULL; |
| 149 | int k, n = 0, m = 0; |
| 150 | unsigned i; |
| 151 | int counter = 0; |
| 152 | int r = 0; |
| 153 | BN_CTX *ctx = NULL; |
| 154 | unsigned int h = 2; |
| 155 | unsigned qsize; |
| 156 | const EVP_MD *evpmd; |
| 157 | |
| 158 | evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1(); |
| 159 | qsize = EVP_MD_size(evpmd); |
| 160 | |
| 161 | if (bits < 512) { |
| 162 | bits = 512; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 164 | |
| 165 | bits = (bits + 63) / 64 * 64; |
| 166 | |
| 167 | if (seed_in != NULL) { |
| 168 | if (seed_len < (size_t)qsize) { |
| 169 | return 0; |
| 170 | } |
| 171 | if (seed_len > (size_t)qsize) { |
| 172 | /* Only consume as much seed as is expected. */ |
| 173 | seed_len = qsize; |
| 174 | } |
| 175 | memcpy(seed, seed_in, seed_len); |
| 176 | } |
| 177 | |
| 178 | ctx = BN_CTX_new(); |
| 179 | if (ctx == NULL) { |
| 180 | goto err; |
| 181 | } |
| 182 | BN_CTX_start(ctx); |
| 183 | |
| 184 | mont = BN_MONT_CTX_new(); |
| 185 | if (mont == NULL) { |
| 186 | goto err; |
| 187 | } |
| 188 | |
| 189 | r0 = BN_CTX_get(ctx); |
| 190 | g = BN_CTX_get(ctx); |
| 191 | W = BN_CTX_get(ctx); |
| 192 | q = BN_CTX_get(ctx); |
| 193 | X = BN_CTX_get(ctx); |
| 194 | c = BN_CTX_get(ctx); |
| 195 | p = BN_CTX_get(ctx); |
| 196 | test = BN_CTX_get(ctx); |
| 197 | |
| 198 | if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) { |
| 199 | goto err; |
| 200 | } |
| 201 | |
| 202 | for (;;) { |
| 203 | /* Find q. */ |
| 204 | for (;;) { |
| 205 | /* step 1 */ |
| 206 | if (!BN_GENCB_call(cb, 0, m++)) { |
| 207 | goto err; |
| 208 | } |
| 209 | |
| 210 | int use_random_seed = (seed_in == NULL); |
| 211 | if (use_random_seed) { |
| 212 | if (!RAND_bytes(seed, qsize)) { |
| 213 | goto err; |
| 214 | } |
| 215 | } else { |
| 216 | /* If we come back through, use random seed next time. */ |
| 217 | seed_in = NULL; |
| 218 | } |
| 219 | memcpy(buf, seed, qsize); |
| 220 | memcpy(buf2, seed, qsize); |
| 221 | /* precompute "SEED + 1" for step 7: */ |
| 222 | for (i = qsize - 1; i < qsize; i--) { |
| 223 | buf[i]++; |
| 224 | if (buf[i] != 0) { |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* step 2 */ |
| 230 | if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) || |
| 231 | !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) { |
| 232 | goto err; |
| 233 | } |
| 234 | for (i = 0; i < qsize; i++) { |
| 235 | md[i] ^= buf2[i]; |
| 236 | } |
| 237 | |
| 238 | /* step 3 */ |
| 239 | md[0] |= 0x80; |
| 240 | md[qsize - 1] |= 0x01; |
| 241 | if (!BN_bin2bn(md, qsize, q)) { |
| 242 | goto err; |
| 243 | } |
| 244 | |
| 245 | /* step 4 */ |
| 246 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb); |
| 247 | if (r > 0) { |
| 248 | break; |
| 249 | } |
| 250 | if (r != 0) { |
| 251 | goto err; |
| 252 | } |
| 253 | |
| 254 | /* do a callback call */ |
| 255 | /* step 5 */ |
| 256 | } |
| 257 | |
| 258 | if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) { |
| 259 | goto err; |
| 260 | } |
| 261 | |
| 262 | /* step 6 */ |
| 263 | counter = 0; |
| 264 | /* "offset = 2" */ |
| 265 | |
| 266 | n = (bits - 1) / 160; |
| 267 | |
| 268 | for (;;) { |
| 269 | if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) { |
| 270 | goto err; |
| 271 | } |
| 272 | |
| 273 | /* step 7 */ |
| 274 | BN_zero(W); |
| 275 | /* now 'buf' contains "SEED + offset - 1" */ |
| 276 | for (k = 0; k <= n; k++) { |
| 277 | /* obtain "SEED + offset + k" by incrementing: */ |
| 278 | for (i = qsize - 1; i < qsize; i--) { |
| 279 | buf[i]++; |
| 280 | if (buf[i] != 0) { |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) { |
| 286 | goto err; |
| 287 | } |
| 288 | |
| 289 | /* step 8 */ |
| 290 | if (!BN_bin2bn(md, qsize, r0) || |
| 291 | !BN_lshift(r0, r0, (qsize << 3) * k) || |
| 292 | !BN_add(W, W, r0)) { |
| 293 | goto err; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* more of step 8 */ |
| 298 | if (!BN_mask_bits(W, bits - 1) || |
| 299 | !BN_copy(X, W) || |
| 300 | !BN_add(X, X, test)) { |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | /* step 9 */ |
| 305 | if (!BN_lshift1(r0, q) || |
| 306 | !BN_mod(c, X, r0, ctx) || |
| 307 | !BN_sub(r0, c, BN_value_one()) || |
| 308 | !BN_sub(p, X, r0)) { |
| 309 | goto err; |
| 310 | } |
| 311 | |
| 312 | /* step 10 */ |
| 313 | if (BN_cmp(p, test) >= 0) { |
| 314 | /* step 11 */ |
| 315 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb); |
| 316 | if (r > 0) { |
| 317 | goto end; /* found it */ |
| 318 | } |
| 319 | if (r != 0) { |
| 320 | goto err; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* step 13 */ |
| 325 | counter++; |
| 326 | /* "offset = offset + n + 1" */ |
| 327 | |
| 328 | /* step 14 */ |
| 329 | if (counter >= 4096) { |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | end: |
| 335 | if (!BN_GENCB_call(cb, 2, 1)) { |
| 336 | goto err; |
| 337 | } |
| 338 | |
| 339 | /* We now need to generate g */ |
| 340 | /* Set r0=(p-1)/q */ |
| 341 | if (!BN_sub(test, p, BN_value_one()) || |
| 342 | !BN_div(r0, NULL, test, q, ctx)) { |
| 343 | goto err; |
| 344 | } |
| 345 | |
| 346 | if (!BN_set_word(test, h) || |
| 347 | !BN_MONT_CTX_set(mont, p, ctx)) { |
| 348 | goto err; |
| 349 | } |
| 350 | |
| 351 | for (;;) { |
| 352 | /* g=test^r0%p */ |
| 353 | if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) { |
| 354 | goto err; |
| 355 | } |
| 356 | if (!BN_is_one(g)) { |
| 357 | break; |
| 358 | } |
| 359 | if (!BN_add(test, test, BN_value_one())) { |
| 360 | goto err; |
| 361 | } |
| 362 | h++; |
| 363 | } |
| 364 | |
| 365 | if (!BN_GENCB_call(cb, 3, 1)) { |
| 366 | goto err; |
| 367 | } |
| 368 | |
| 369 | ok = 1; |
| 370 | |
| 371 | err: |
| 372 | if (ok) { |
| 373 | BN_free(dsa->p); |
| 374 | BN_free(dsa->q); |
| 375 | BN_free(dsa->g); |
| 376 | dsa->p = BN_dup(p); |
| 377 | dsa->q = BN_dup(q); |
| 378 | dsa->g = BN_dup(g); |
| 379 | if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
| 380 | ok = 0; |
| 381 | goto err; |
| 382 | } |
| 383 | if (out_counter != NULL) { |
| 384 | *out_counter = counter; |
| 385 | } |
| 386 | if (out_h != NULL) { |
| 387 | *out_h = h; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (ctx) { |
| 392 | BN_CTX_end(ctx); |
| 393 | BN_CTX_free(ctx); |
| 394 | } |
| 395 | |
| 396 | BN_MONT_CTX_free(mont); |
| 397 | |
| 398 | return ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | int DSA_generate_key(DSA *dsa) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 402 | int ok = 0; |
| 403 | BN_CTX *ctx = NULL; |
| 404 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
| 405 | BIGNUM prk; |
| 406 | |
| 407 | ctx = BN_CTX_new(); |
| 408 | if (ctx == NULL) { |
| 409 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 411 | |
| 412 | priv_key = dsa->priv_key; |
| 413 | if (priv_key == NULL) { |
| 414 | priv_key = BN_new(); |
| 415 | if (priv_key == NULL) { |
| 416 | goto err; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | do { |
| 421 | if (!BN_rand_range(priv_key, dsa->q)) { |
| 422 | goto err; |
| 423 | } |
| 424 | } while (BN_is_zero(priv_key)); |
| 425 | |
| 426 | pub_key = dsa->pub_key; |
| 427 | if (pub_key == NULL) { |
| 428 | pub_key = BN_new(); |
| 429 | if (pub_key == NULL) { |
| 430 | goto err; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | BN_init(&prk); |
| 435 | BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME); |
| 436 | |
| 437 | if (!BN_mod_exp(pub_key, dsa->g, &prk, dsa->p, ctx)) { |
| 438 | goto err; |
| 439 | } |
| 440 | |
| 441 | dsa->priv_key = priv_key; |
| 442 | dsa->pub_key = pub_key; |
| 443 | ok = 1; |
| 444 | |
| 445 | err: |
| 446 | if (dsa->pub_key == NULL) { |
| 447 | BN_free(pub_key); |
| 448 | } |
| 449 | if (dsa->priv_key == NULL) { |
| 450 | BN_free(priv_key); |
| 451 | } |
| 452 | BN_CTX_free(ctx); |
| 453 | |
| 454 | return ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | DSA_SIG *DSA_SIG_new(void) { |
| 458 | DSA_SIG *sig; |
| 459 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); |
| 460 | if (!sig) { |
| 461 | return NULL; |
| 462 | } |
| 463 | sig->r = NULL; |
| 464 | sig->s = NULL; |
| 465 | return sig; |
| 466 | } |
| 467 | |
| 468 | void DSA_SIG_free(DSA_SIG *sig) { |
| 469 | if (!sig) { |
| 470 | return; |
| 471 | } |
| 472 | |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 473 | BN_free(sig->r); |
| 474 | BN_free(sig->s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | OPENSSL_free(sig); |
| 476 | } |
| 477 | |
| 478 | DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 479 | BIGNUM *kinv = NULL, *r = NULL, *s = NULL; |
| 480 | BIGNUM m; |
| 481 | BIGNUM xr; |
| 482 | BN_CTX *ctx = NULL; |
| 483 | int reason = ERR_R_BN_LIB; |
| 484 | DSA_SIG *ret = NULL; |
| 485 | int noredo = 0; |
| 486 | |
| 487 | BN_init(&m); |
| 488 | BN_init(&xr); |
| 489 | |
| 490 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 491 | reason = DSA_R_MISSING_PARAMETERS; |
| 492 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 494 | |
| 495 | s = BN_new(); |
| 496 | if (s == NULL) { |
| 497 | goto err; |
| 498 | } |
| 499 | ctx = BN_CTX_new(); |
| 500 | if (ctx == NULL) { |
| 501 | goto err; |
| 502 | } |
| 503 | |
| 504 | redo: |
| 505 | if (dsa->kinv == NULL || dsa->r == NULL) { |
| 506 | if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) { |
| 507 | goto err; |
| 508 | } |
| 509 | } else { |
| 510 | kinv = dsa->kinv; |
| 511 | dsa->kinv = NULL; |
| 512 | r = dsa->r; |
| 513 | dsa->r = NULL; |
| 514 | noredo = 1; |
| 515 | } |
| 516 | |
| 517 | if (digest_len > BN_num_bytes(dsa->q)) { |
| 518 | /* if the digest length is greater than the size of q use the |
| 519 | * BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 520 | * fips 186-3, 4.2 */ |
| 521 | digest_len = BN_num_bytes(dsa->q); |
| 522 | } |
| 523 | |
| 524 | if (BN_bin2bn(digest, digest_len, &m) == NULL) { |
| 525 | goto err; |
| 526 | } |
| 527 | |
| 528 | /* Compute s = inv(k) (m + xr) mod q */ |
| 529 | if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) { |
| 530 | goto err; /* s = xr */ |
| 531 | } |
| 532 | if (!BN_add(s, &xr, &m)) { |
| 533 | goto err; /* s = m + xr */ |
| 534 | } |
| 535 | if (BN_cmp(s, dsa->q) > 0) { |
| 536 | if (!BN_sub(s, s, dsa->q)) { |
| 537 | goto err; |
| 538 | } |
| 539 | } |
| 540 | if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) { |
| 541 | goto err; |
| 542 | } |
| 543 | |
| 544 | ret = DSA_SIG_new(); |
| 545 | if (ret == NULL) { |
| 546 | goto err; |
| 547 | } |
| 548 | /* Redo if r or s is zero as required by FIPS 186-3: this is |
| 549 | * very unlikely. */ |
| 550 | if (BN_is_zero(r) || BN_is_zero(s)) { |
| 551 | if (noredo) { |
| 552 | reason = DSA_R_NEED_NEW_SETUP_VALUES; |
| 553 | goto err; |
| 554 | } |
| 555 | goto redo; |
| 556 | } |
| 557 | ret->r = r; |
| 558 | ret->s = s; |
| 559 | |
| 560 | err: |
| 561 | if (!ret) { |
| 562 | OPENSSL_PUT_ERROR(DSA, reason); |
| 563 | BN_free(r); |
| 564 | BN_free(s); |
| 565 | } |
| 566 | BN_CTX_free(ctx); |
| 567 | BN_clear_free(&m); |
| 568 | BN_clear_free(&xr); |
| 569 | BN_clear_free(kinv); |
| 570 | |
| 571 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig, |
| 575 | const DSA *dsa) { |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 576 | int valid; |
| 577 | if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 579 | } |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 580 | return valid; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | int DSA_do_check_signature(int *out_valid, const uint8_t *digest, |
| 584 | size_t digest_len, DSA_SIG *sig, const DSA *dsa) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 585 | BN_CTX *ctx; |
| 586 | BIGNUM u1, u2, t1; |
| 587 | BN_MONT_CTX *mont = NULL; |
| 588 | int ret = 0; |
| 589 | unsigned i; |
| 590 | |
| 591 | *out_valid = 0; |
| 592 | |
| 593 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 594 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 595 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 596 | } |
| 597 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 598 | i = BN_num_bits(dsa->q); |
| 599 | /* fips 186-3 allows only different sizes for q */ |
| 600 | if (i != 160 && i != 224 && i != 256) { |
| 601 | OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { |
| 606 | OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | BN_init(&u1); |
| 611 | BN_init(&u2); |
| 612 | BN_init(&t1); |
| 613 | |
| 614 | ctx = BN_CTX_new(); |
| 615 | if (ctx == NULL) { |
| 616 | goto err; |
| 617 | } |
| 618 | |
| 619 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
| 620 | BN_ucmp(sig->r, dsa->q) >= 0) { |
| 621 | ret = 1; |
| 622 | goto err; |
| 623 | } |
| 624 | if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || |
| 625 | BN_ucmp(sig->s, dsa->q) >= 0) { |
| 626 | ret = 1; |
| 627 | goto err; |
| 628 | } |
| 629 | |
| 630 | /* Calculate W = inv(S) mod Q |
| 631 | * save W in u2 */ |
| 632 | if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) { |
| 633 | goto err; |
| 634 | } |
| 635 | |
| 636 | /* save M in u1 */ |
| 637 | if (digest_len > (i >> 3)) { |
| 638 | /* if the digest length is greater than the size of q use the |
| 639 | * BN_num_bits(dsa->q) leftmost bits of the digest, see |
| 640 | * fips 186-3, 4.2 */ |
| 641 | digest_len = (i >> 3); |
| 642 | } |
| 643 | |
| 644 | if (BN_bin2bn(digest, digest_len, &u1) == NULL) { |
| 645 | goto err; |
| 646 | } |
| 647 | |
| 648 | /* u1 = M * w mod q */ |
| 649 | if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) { |
| 650 | goto err; |
| 651 | } |
| 652 | |
| 653 | /* u2 = r * w mod q */ |
| 654 | if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) { |
| 655 | goto err; |
| 656 | } |
| 657 | |
| 658 | mont = BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 659 | (CRYPTO_MUTEX *)&dsa->method_mont_p_lock, |
| 660 | dsa->p, ctx); |
| 661 | if (!mont) { |
| 662 | goto err; |
| 663 | } |
| 664 | |
| 665 | if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, |
| 666 | mont)) { |
| 667 | goto err; |
| 668 | } |
| 669 | |
| 670 | /* BN_copy(&u1,&t1); */ |
| 671 | /* let u1 = u1 mod q */ |
| 672 | if (!BN_mod(&u1, &t1, dsa->q, ctx)) { |
| 673 | goto err; |
| 674 | } |
| 675 | |
| 676 | /* V is now in u1. If the signature is correct, it will be |
| 677 | * equal to R. */ |
| 678 | *out_valid = BN_ucmp(&u1, sig->r) == 0; |
| 679 | ret = 1; |
| 680 | |
| 681 | err: |
| 682 | if (ret != 1) { |
| 683 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 684 | } |
| 685 | BN_CTX_free(ctx); |
| 686 | BN_free(&u1); |
| 687 | BN_free(&u2); |
| 688 | BN_free(&t1); |
| 689 | |
| 690 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | int DSA_sign(int type, const uint8_t *digest, size_t digest_len, |
| 694 | uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) { |
| 695 | DSA_SIG *s; |
| 696 | |
| 697 | s = DSA_do_sign(digest, digest_len, dsa); |
| 698 | if (s == NULL) { |
| 699 | *out_siglen = 0; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | *out_siglen = i2d_DSA_SIG(s, &out_sig); |
| 704 | DSA_SIG_free(s); |
| 705 | return 1; |
| 706 | } |
| 707 | |
| 708 | int DSA_verify(int type, const uint8_t *digest, size_t digest_len, |
| 709 | const uint8_t *sig, size_t sig_len, const DSA *dsa) { |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 710 | int valid; |
| 711 | if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) { |
| 712 | return -1; |
| 713 | } |
| 714 | return valid; |
| 715 | } |
| 716 | |
| 717 | int DSA_check_signature(int *out_valid, const uint8_t *digest, |
| 718 | size_t digest_len, const uint8_t *sig, size_t sig_len, |
| 719 | const DSA *dsa) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 720 | DSA_SIG *s = NULL; |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 721 | int ret = 0; |
Adam Langley | ca9a538 | 2015-01-08 12:26:55 -0800 | [diff] [blame] | 722 | uint8_t *der = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | |
| 724 | s = DSA_SIG_new(); |
| 725 | if (s == NULL) { |
| 726 | goto err; |
| 727 | } |
| 728 | |
Adam Langley | ca9a538 | 2015-01-08 12:26:55 -0800 | [diff] [blame] | 729 | const uint8_t *sigp = sig; |
| 730 | if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) { |
| 731 | goto err; |
| 732 | } |
| 733 | |
| 734 | /* Ensure that the signature uses DER and doesn't have trailing garbage. */ |
| 735 | int der_len = i2d_DSA_SIG(s, &der); |
| 736 | if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 737 | goto err; |
| 738 | } |
| 739 | |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 740 | ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 741 | |
| 742 | err: |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 743 | OPENSSL_free(der); |
| 744 | DSA_SIG_free(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 745 | return ret; |
| 746 | } |
| 747 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | int DSA_size(const DSA *dsa) { |
| 749 | int ret, i; |
| 750 | ASN1_INTEGER bs; |
| 751 | unsigned char buf[4]; /* 4 bytes looks really small. |
| 752 | However, i2d_ASN1_INTEGER() will not look |
| 753 | beyond the first byte, as long as the second |
| 754 | parameter is NULL. */ |
| 755 | |
| 756 | i = BN_num_bits(dsa->q); |
| 757 | bs.length = (i + 7) / 8; |
| 758 | bs.data = buf; |
| 759 | bs.type = V_ASN1_INTEGER; |
| 760 | /* If the top bit is set the asn1 encoding is 1 larger. */ |
| 761 | buf[0] = 0xff; |
| 762 | |
| 763 | i = i2d_ASN1_INTEGER(&bs, NULL); |
| 764 | i += i; /* r and s */ |
| 765 | ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); |
| 766 | return ret; |
| 767 | } |
| 768 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 769 | int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | BIGNUM **out_r) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 771 | BN_CTX *ctx; |
| 772 | BIGNUM k, kq, *K, *kinv = NULL, *r = NULL; |
| 773 | int ret = 0; |
| 774 | |
| 775 | if (!dsa->p || !dsa->q || !dsa->g) { |
| 776 | OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS); |
| 777 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 778 | } |
| 779 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame^] | 780 | BN_init(&k); |
| 781 | BN_init(&kq); |
| 782 | |
| 783 | ctx = ctx_in; |
| 784 | if (ctx == NULL) { |
| 785 | ctx = BN_CTX_new(); |
| 786 | if (ctx == NULL) { |
| 787 | goto err; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | r = BN_new(); |
| 792 | if (r == NULL) { |
| 793 | goto err; |
| 794 | } |
| 795 | |
| 796 | /* Get random k */ |
| 797 | do { |
| 798 | if (!BN_rand_range(&k, dsa->q)) { |
| 799 | goto err; |
| 800 | } |
| 801 | } while (BN_is_zero(&k)); |
| 802 | |
| 803 | BN_set_flags(&k, BN_FLG_CONSTTIME); |
| 804 | |
| 805 | if (BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, |
| 806 | (CRYPTO_MUTEX *)&dsa->method_mont_p_lock, dsa->p, |
| 807 | ctx) == NULL) { |
| 808 | goto err; |
| 809 | } |
| 810 | |
| 811 | /* Compute r = (g^k mod p) mod q */ |
| 812 | if (!BN_copy(&kq, &k)) { |
| 813 | goto err; |
| 814 | } |
| 815 | |
| 816 | /* We do not want timing information to leak the length of k, |
| 817 | * so we compute g^k using an equivalent exponent of fixed length. |
| 818 | * |
| 819 | * (This is a kludge that we need because the BN_mod_exp_mont() |
| 820 | * does not let us specify the desired timing behaviour.) */ |
| 821 | |
| 822 | if (!BN_add(&kq, &kq, dsa->q)) { |
| 823 | goto err; |
| 824 | } |
| 825 | if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) { |
| 826 | goto err; |
| 827 | } |
| 828 | |
| 829 | K = &kq; |
| 830 | |
| 831 | if (!BN_mod_exp_mont(r, dsa->g, K, dsa->p, ctx, dsa->method_mont_p)) { |
| 832 | goto err; |
| 833 | } |
| 834 | if (!BN_mod(r, r, dsa->q, ctx)) { |
| 835 | goto err; |
| 836 | } |
| 837 | |
| 838 | /* Compute part of 's = inv(k) (m + xr) mod q' */ |
| 839 | kinv = BN_mod_inverse(NULL, &k, dsa->q, ctx); |
| 840 | if (kinv == NULL) { |
| 841 | goto err; |
| 842 | } |
| 843 | |
| 844 | BN_clear_free(*out_kinv); |
| 845 | *out_kinv = kinv; |
| 846 | kinv = NULL; |
| 847 | BN_clear_free(*out_r); |
| 848 | *out_r = r; |
| 849 | ret = 1; |
| 850 | |
| 851 | err: |
| 852 | if (!ret) { |
| 853 | OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB); |
| 854 | if (r != NULL) { |
| 855 | BN_clear_free(r); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | if (ctx_in == NULL) { |
| 860 | BN_CTX_free(ctx); |
| 861 | } |
| 862 | BN_clear_free(&k); |
| 863 | BN_clear_free(&kq); |
| 864 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 868 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 869 | int index; |
| 870 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func, |
| 871 | dup_func, free_func)) { |
| 872 | return -1; |
| 873 | } |
| 874 | return index; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | int DSA_set_ex_data(DSA *d, int idx, void *arg) { |
| 878 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
| 879 | } |
| 880 | |
| 881 | void *DSA_get_ex_data(const DSA *d, int idx) { |
| 882 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
| 883 | } |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 884 | |
| 885 | DH *DSA_dup_DH(const DSA *r) { |
| 886 | DH *ret = NULL; |
| 887 | |
| 888 | if (r == NULL) { |
| 889 | goto err; |
| 890 | } |
| 891 | ret = DH_new(); |
| 892 | if (ret == NULL) { |
| 893 | goto err; |
| 894 | } |
| 895 | if (r->q != NULL) { |
| 896 | ret->priv_length = BN_num_bits(r->q); |
| 897 | if ((ret->q = BN_dup(r->q)) == NULL) { |
| 898 | goto err; |
| 899 | } |
| 900 | } |
| 901 | if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) || |
| 902 | (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) || |
| 903 | (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) || |
| 904 | (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) { |
| 905 | goto err; |
| 906 | } |
| 907 | |
| 908 | return ret; |
| 909 | |
| 910 | err: |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 911 | DH_free(ret); |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 912 | return NULL; |
| 913 | } |