1 /* $OpenBSD: dsa_ossl.c,v 1.51 2023/03/27 10:25:02 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ 60 61 #include <stdio.h> 62 63 #include <openssl/asn1.h> 64 #include <openssl/bn.h> 65 #include <openssl/dsa.h> 66 #include <openssl/err.h> 67 #include <openssl/sha.h> 68 69 #include "bn_local.h" 70 #include "dsa_local.h" 71 72 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); 73 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, 74 BIGNUM **rp); 75 static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, 76 DSA *dsa); 77 static int dsa_init(DSA *dsa); 78 static int dsa_finish(DSA *dsa); 79 80 static DSA_METHOD openssl_dsa_meth = { 81 .name = "OpenSSL DSA method", 82 .dsa_do_sign = dsa_do_sign, 83 .dsa_sign_setup = dsa_sign_setup, 84 .dsa_do_verify = dsa_do_verify, 85 .init = dsa_init, 86 .finish = dsa_finish, 87 }; 88 89 const DSA_METHOD * 90 DSA_OpenSSL(void) 91 { 92 return &openssl_dsa_meth; 93 } 94 95 /* 96 * Since DSA parameters are entirely arbitrary and checking them to be 97 * consistent is very expensive, we cannot do so on every sign operation. 98 * Instead, cap the number of retries so we do not loop indefinitely if 99 * the generator of the multiplicative group happens to be nilpotent. 100 * The probability of needing a retry with valid parameters is negligible, 101 * so trying 32 times is amply enough. 102 */ 103 #define DSA_MAX_SIGN_ITERATIONS 32 104 105 static DSA_SIG * 106 dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) 107 { 108 BIGNUM *b = NULL, *bm = NULL, *bxr = NULL, *binv = NULL, *m = NULL; 109 BIGNUM *kinv = NULL, *r = NULL, *s = NULL; 110 BN_CTX *ctx = NULL; 111 int reason = ERR_R_BN_LIB; 112 DSA_SIG *ret = NULL; 113 int attempts = 0; 114 int noredo = 0; 115 116 if (!dsa_check_key(dsa)) { 117 reason = DSA_R_INVALID_PARAMETERS; 118 goto err; 119 } 120 121 if ((s = BN_new()) == NULL) 122 goto err; 123 124 if ((ctx = BN_CTX_new()) == NULL) 125 goto err; 126 127 BN_CTX_start(ctx); 128 129 if ((b = BN_CTX_get(ctx)) == NULL) 130 goto err; 131 if ((binv = BN_CTX_get(ctx)) == NULL) 132 goto err; 133 if ((bm = BN_CTX_get(ctx)) == NULL) 134 goto err; 135 if ((bxr = BN_CTX_get(ctx)) == NULL) 136 goto err; 137 if ((m = BN_CTX_get(ctx)) == NULL) 138 goto err; 139 140 /* 141 * If the digest length is greater than N (the bit length of q), the 142 * leftmost N bits of the digest shall be used, see FIPS 186-3, 4.2. 143 * In this case the digest length is given in bytes. 144 */ 145 if (dlen > BN_num_bytes(dsa->q)) 146 dlen = BN_num_bytes(dsa->q); 147 if (BN_bin2bn(dgst, dlen, m) == NULL) 148 goto err; 149 150 redo: 151 if (dsa->kinv == NULL || dsa->r == NULL) { 152 if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) 153 goto err; 154 } else { 155 kinv = dsa->kinv; 156 dsa->kinv = NULL; 157 r = dsa->r; 158 dsa->r = NULL; 159 noredo = 1; 160 } 161 162 /* 163 * Compute: 164 * 165 * s = inv(k)(m + xr) mod q 166 * 167 * In order to reduce the possibility of a side-channel attack, the 168 * following is calculated using a blinding value: 169 * 170 * s = inv(b)(bm + bxr)inv(k) mod q 171 * 172 * Where b is a random value in the range [1, q). 173 */ 174 if (!bn_rand_interval(b, BN_value_one(), dsa->q)) 175 goto err; 176 if (BN_mod_inverse_ct(binv, b, dsa->q, ctx) == NULL) 177 goto err; 178 179 if (!BN_mod_mul(bxr, b, dsa->priv_key, dsa->q, ctx)) /* bx */ 180 goto err; 181 if (!BN_mod_mul(bxr, bxr, r, dsa->q, ctx)) /* bxr */ 182 goto err; 183 if (!BN_mod_mul(bm, b, m, dsa->q, ctx)) /* bm */ 184 goto err; 185 if (!BN_mod_add(s, bxr, bm, dsa->q, ctx)) /* s = bm + bxr */ 186 goto err; 187 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) /* s = b(m + xr)k^-1 */ 188 goto err; 189 if (!BN_mod_mul(s, s, binv, dsa->q, ctx)) /* s = (m + xr)k^-1 */ 190 goto err; 191 192 /* 193 * Redo if r or s is zero as required by FIPS 186-3: this is very 194 * unlikely. 195 */ 196 if (BN_is_zero(r) || BN_is_zero(s)) { 197 if (noredo) { 198 reason = DSA_R_NEED_NEW_SETUP_VALUES; 199 goto err; 200 } 201 if (++attempts > DSA_MAX_SIGN_ITERATIONS) { 202 reason = DSA_R_INVALID_PARAMETERS; 203 goto err; 204 } 205 goto redo; 206 } 207 208 if ((ret = DSA_SIG_new()) == NULL) { 209 reason = ERR_R_MALLOC_FAILURE; 210 goto err; 211 } 212 ret->r = r; 213 ret->s = s; 214 215 err: 216 if (!ret) { 217 DSAerror(reason); 218 BN_free(r); 219 BN_free(s); 220 } 221 BN_CTX_end(ctx); 222 BN_CTX_free(ctx); 223 BN_free(kinv); 224 225 return ret; 226 } 227 228 static int 229 dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 230 { 231 BIGNUM *k = NULL, *l = NULL, *m = NULL, *kinv = NULL, *r = NULL; 232 BN_CTX *ctx = NULL; 233 int q_bits; 234 int ret = 0; 235 236 if (!dsa_check_key(dsa)) 237 goto err; 238 239 if ((r = BN_new()) == NULL) 240 goto err; 241 242 if ((ctx = ctx_in) == NULL) 243 ctx = BN_CTX_new(); 244 if (ctx == NULL) 245 goto err; 246 247 BN_CTX_start(ctx); 248 249 if ((k = BN_CTX_get(ctx)) == NULL) 250 goto err; 251 if ((l = BN_CTX_get(ctx)) == NULL) 252 goto err; 253 if ((m = BN_CTX_get(ctx)) == NULL) 254 goto err; 255 256 /* Preallocate space */ 257 q_bits = BN_num_bits(dsa->q); 258 if (!BN_set_bit(k, q_bits) || 259 !BN_set_bit(l, q_bits) || 260 !BN_set_bit(m, q_bits)) 261 goto err; 262 263 if (!bn_rand_interval(k, BN_value_one(), dsa->q)) 264 goto err; 265 266 BN_set_flags(k, BN_FLG_CONSTTIME); 267 268 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { 269 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, 270 CRYPTO_LOCK_DSA, dsa->p, ctx)) 271 goto err; 272 } 273 274 /* Compute r = (g^k mod p) mod q */ 275 276 /* 277 * We do not want timing information to leak the length of k, 278 * so we compute G^k using an equivalent exponent of fixed 279 * bit-length. 280 * 281 * We unconditionally perform both of these additions to prevent a 282 * small timing information leakage. We then choose the sum that is 283 * one bit longer than the modulus. 284 * 285 * TODO: revisit the bn_copy aiming for a memory access agnostic 286 * conditional copy. 287 */ 288 289 if (!BN_add(l, k, dsa->q) || 290 !BN_add(m, l, dsa->q) || 291 !bn_copy(k, BN_num_bits(l) > q_bits ? l : m)) 292 goto err; 293 294 if (dsa->meth->bn_mod_exp != NULL) { 295 if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx, 296 dsa->method_mont_p)) 297 goto err; 298 } else { 299 if (!BN_mod_exp_mont_ct(r, dsa->g, k, dsa->p, ctx, 300 dsa->method_mont_p)) 301 goto err; 302 } 303 304 if (!BN_mod_ct(r, r, dsa->q, ctx)) 305 goto err; 306 307 /* Compute part of 's = inv(k) (m + xr) mod q' */ 308 if ((kinv = BN_mod_inverse_ct(NULL, k, dsa->q, ctx)) == NULL) 309 goto err; 310 311 BN_free(*kinvp); 312 *kinvp = kinv; 313 kinv = NULL; 314 315 BN_free(*rp); 316 *rp = r; 317 318 ret = 1; 319 320 err: 321 if (!ret) { 322 DSAerror(ERR_R_BN_LIB); 323 BN_free(r); 324 } 325 BN_CTX_end(ctx); 326 if (ctx != ctx_in) 327 BN_CTX_free(ctx); 328 329 return ret; 330 } 331 332 static int 333 dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) 334 { 335 BIGNUM *u1 = NULL, *u2 = NULL, *t1 = NULL; 336 BN_CTX *ctx = NULL; 337 BN_MONT_CTX *mont = NULL; 338 int qbits; 339 int ret = -1; 340 341 if (!dsa_check_key(dsa)) 342 goto err; 343 344 if ((ctx = BN_CTX_new()) == NULL) 345 goto err; 346 347 BN_CTX_start(ctx); 348 349 if ((u1 = BN_CTX_get(ctx)) == NULL) 350 goto err; 351 if ((u2 = BN_CTX_get(ctx)) == NULL) 352 goto err; 353 if ((t1 = BN_CTX_get(ctx)) == NULL) 354 goto err; 355 356 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || 357 BN_ucmp(sig->r, dsa->q) >= 0) { 358 ret = 0; 359 goto err; 360 } 361 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || 362 BN_ucmp(sig->s, dsa->q) >= 0) { 363 ret = 0; 364 goto err; 365 } 366 367 /* Calculate w = inv(s) mod q, saving w in u2. */ 368 if ((BN_mod_inverse_ct(u2, sig->s, dsa->q, ctx)) == NULL) 369 goto err; 370 371 /* 372 * If the digest length is greater than the size of q use the 373 * BN_num_bits(dsa->q) leftmost bits of the digest, see FIPS 186-4, 4.2. 374 */ 375 qbits = BN_num_bits(dsa->q); 376 if (dgst_len > (qbits >> 3)) 377 dgst_len = (qbits >> 3); 378 379 /* Save m in u1. */ 380 if (BN_bin2bn(dgst, dgst_len, u1) == NULL) 381 goto err; 382 383 /* u1 = m * w mod q */ 384 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx)) 385 goto err; 386 387 /* u2 = r * w mod q */ 388 if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx)) 389 goto err; 390 391 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { 392 mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p, 393 CRYPTO_LOCK_DSA, dsa->p, ctx); 394 if (!mont) 395 goto err; 396 } 397 398 if (dsa->meth->dsa_mod_exp != NULL) { 399 if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, 400 u2, dsa->p, ctx, mont)) 401 goto err; 402 } else { 403 if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, 404 dsa->p, ctx, mont)) 405 goto err; 406 } 407 408 /* let u1 = u1 mod q */ 409 if (!BN_mod_ct(u1, t1, dsa->q, ctx)) 410 goto err; 411 412 /* v is in u1 - if the signature is correct, it will be equal to r. */ 413 ret = BN_ucmp(u1, sig->r) == 0; 414 415 err: 416 if (ret < 0) 417 DSAerror(ERR_R_BN_LIB); 418 BN_CTX_end(ctx); 419 BN_CTX_free(ctx); 420 421 return ret; 422 } 423 424 static int 425 dsa_init(DSA *dsa) 426 { 427 dsa->flags |= DSA_FLAG_CACHE_MONT_P; 428 return 1; 429 } 430 431 static int 432 dsa_finish(DSA *dsa) 433 { 434 BN_MONT_CTX_free(dsa->method_mont_p); 435 return 1; 436 } 437 438 DSA_SIG * 439 DSA_SIG_new(void) 440 { 441 return calloc(1, sizeof(DSA_SIG)); 442 } 443 444 void 445 DSA_SIG_free(DSA_SIG *sig) 446 { 447 if (sig == NULL) 448 return; 449 450 BN_free(sig->r); 451 BN_free(sig->s); 452 free(sig); 453 } 454 455 int 456 DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 457 { 458 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp); 459 } 460 461 DSA_SIG * 462 DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) 463 { 464 return dsa->meth->dsa_do_sign(dgst, dlen, dsa); 465 } 466 467 int 468 DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) 469 { 470 return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); 471 } 472