1 /* $OpenBSD: dsa_ossl.c,v 1.47 2023/01/11 04:39:42 jsing 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 static DSA_SIG * 96 dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) 97 { 98 BIGNUM *b = NULL, *bm = NULL, *bxr = NULL, *binv = NULL, *m = NULL; 99 BIGNUM *kinv = NULL, *r = NULL, *s = NULL; 100 BN_CTX *ctx = NULL; 101 int reason = ERR_R_BN_LIB; 102 DSA_SIG *ret = NULL; 103 int noredo = 0; 104 105 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { 106 reason = DSA_R_MISSING_PARAMETERS; 107 goto err; 108 } 109 110 if ((s = BN_new()) == NULL) 111 goto err; 112 113 if ((ctx = BN_CTX_new()) == NULL) 114 goto err; 115 116 BN_CTX_start(ctx); 117 118 if ((b = BN_CTX_get(ctx)) == NULL) 119 goto err; 120 if ((binv = BN_CTX_get(ctx)) == NULL) 121 goto err; 122 if ((bm = BN_CTX_get(ctx)) == NULL) 123 goto err; 124 if ((bxr = BN_CTX_get(ctx)) == NULL) 125 goto err; 126 if ((m = BN_CTX_get(ctx)) == NULL) 127 goto err; 128 129 /* 130 * If the digest length is greater than N (the bit length of q), the 131 * leftmost N bits of the digest shall be used, see FIPS 186-3, 4.2. 132 * In this case the digest length is given in bytes. 133 */ 134 if (dlen > BN_num_bytes(dsa->q)) 135 dlen = BN_num_bytes(dsa->q); 136 if (BN_bin2bn(dgst, dlen, m) == NULL) 137 goto err; 138 139 redo: 140 if (dsa->kinv == NULL || dsa->r == NULL) { 141 if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) 142 goto err; 143 } else { 144 kinv = dsa->kinv; 145 dsa->kinv = NULL; 146 r = dsa->r; 147 dsa->r = NULL; 148 noredo = 1; 149 } 150 151 /* 152 * Compute: 153 * 154 * s = inv(k)(m + xr) mod q 155 * 156 * In order to reduce the possibility of a side-channel attack, the 157 * following is calculated using a blinding value: 158 * 159 * s = inv(b)(bm + bxr)inv(k) mod q 160 * 161 * Where b is a random value in the range [1, q). 162 */ 163 if (!bn_rand_interval(b, BN_value_one(), dsa->q)) 164 goto err; 165 if (BN_mod_inverse_ct(binv, b, dsa->q, ctx) == NULL) 166 goto err; 167 168 if (!BN_mod_mul(bxr, b, dsa->priv_key, dsa->q, ctx)) /* bx */ 169 goto err; 170 if (!BN_mod_mul(bxr, bxr, r, dsa->q, ctx)) /* bxr */ 171 goto err; 172 if (!BN_mod_mul(bm, b, m, dsa->q, ctx)) /* bm */ 173 goto err; 174 if (!BN_mod_add(s, bxr, bm, dsa->q, ctx)) /* s = bm + bxr */ 175 goto err; 176 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) /* s = b(m + xr)k^-1 */ 177 goto err; 178 if (!BN_mod_mul(s, s, binv, dsa->q, ctx)) /* s = (m + xr)k^-1 */ 179 goto err; 180 181 /* 182 * Redo if r or s is zero as required by FIPS 186-3: this is very 183 * unlikely. 184 */ 185 if (BN_is_zero(r) || BN_is_zero(s)) { 186 if (noredo) { 187 reason = DSA_R_NEED_NEW_SETUP_VALUES; 188 goto err; 189 } 190 goto redo; 191 } 192 193 if ((ret = DSA_SIG_new()) == NULL) { 194 reason = ERR_R_MALLOC_FAILURE; 195 goto err; 196 } 197 ret->r = r; 198 ret->s = s; 199 200 err: 201 if (!ret) { 202 DSAerror(reason); 203 BN_free(r); 204 BN_free(s); 205 } 206 BN_CTX_end(ctx); 207 BN_CTX_free(ctx); 208 BN_free(kinv); 209 210 return ret; 211 } 212 213 static int 214 dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 215 { 216 BIGNUM *k = NULL, *l = NULL, *m = NULL, *kinv = NULL, *r = NULL; 217 BN_CTX *ctx = NULL; 218 int q_bits; 219 int ret = 0; 220 221 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { 222 DSAerror(DSA_R_MISSING_PARAMETERS); 223 return 0; 224 } 225 226 if ((r = BN_new()) == NULL) 227 goto err; 228 229 if ((ctx = ctx_in) == NULL) 230 ctx = BN_CTX_new(); 231 if (ctx == NULL) 232 goto err; 233 234 BN_CTX_start(ctx); 235 236 if ((k = BN_CTX_get(ctx)) == NULL) 237 goto err; 238 if ((l = BN_CTX_get(ctx)) == NULL) 239 goto err; 240 if ((m = BN_CTX_get(ctx)) == NULL) 241 goto err; 242 243 /* Preallocate space */ 244 q_bits = BN_num_bits(dsa->q); 245 if (!BN_set_bit(k, q_bits) || 246 !BN_set_bit(l, q_bits) || 247 !BN_set_bit(m, q_bits)) 248 goto err; 249 250 if (!bn_rand_interval(k, BN_value_one(), dsa->q)) 251 goto err; 252 253 BN_set_flags(k, BN_FLG_CONSTTIME); 254 255 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { 256 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, 257 CRYPTO_LOCK_DSA, dsa->p, ctx)) 258 goto err; 259 } 260 261 /* Compute r = (g^k mod p) mod q */ 262 263 /* 264 * We do not want timing information to leak the length of k, 265 * so we compute G^k using an equivalent exponent of fixed 266 * bit-length. 267 * 268 * We unconditionally perform both of these additions to prevent a 269 * small timing information leakage. We then choose the sum that is 270 * one bit longer than the modulus. 271 * 272 * TODO: revisit the BN_copy aiming for a memory access agnostic 273 * conditional copy. 274 */ 275 276 if (!BN_add(l, k, dsa->q) || 277 !BN_add(m, l, dsa->q) || 278 !BN_copy(k, BN_num_bits(l) > q_bits ? l : m)) 279 goto err; 280 281 if (dsa->meth->bn_mod_exp != NULL) { 282 if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx, 283 dsa->method_mont_p)) 284 goto err; 285 } else { 286 if (!BN_mod_exp_mont_ct(r, dsa->g, k, dsa->p, ctx, 287 dsa->method_mont_p)) 288 goto err; 289 } 290 291 if (!BN_mod_ct(r, r, dsa->q, ctx)) 292 goto err; 293 294 /* Compute part of 's = inv(k) (m + xr) mod q' */ 295 if ((kinv = BN_mod_inverse_ct(NULL, k, dsa->q, ctx)) == NULL) 296 goto err; 297 298 BN_free(*kinvp); 299 *kinvp = kinv; 300 kinv = NULL; 301 302 BN_free(*rp); 303 *rp = r; 304 305 ret = 1; 306 307 err: 308 if (!ret) { 309 DSAerror(ERR_R_BN_LIB); 310 BN_free(r); 311 } 312 BN_CTX_end(ctx); 313 if (ctx != ctx_in) 314 BN_CTX_free(ctx); 315 316 return ret; 317 } 318 319 static int 320 dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa) 321 { 322 BIGNUM *u1 = NULL, *u2 = NULL, *t1 = NULL; 323 BN_CTX *ctx = NULL; 324 BN_MONT_CTX *mont = NULL; 325 int qbits; 326 int ret = -1; 327 328 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { 329 DSAerror(DSA_R_MISSING_PARAMETERS); 330 return -1; 331 } 332 333 /* FIPS 186-3 allows only three different sizes for q. */ 334 qbits = BN_num_bits(dsa->q); 335 if (qbits != 160 && qbits != 224 && qbits != 256) { 336 DSAerror(DSA_R_BAD_Q_VALUE); 337 return -1; 338 } 339 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) { 340 DSAerror(DSA_R_MODULUS_TOO_LARGE); 341 return -1; 342 } 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-3, 4.2. 374 */ 375 if (dgst_len > (qbits >> 3)) 376 dgst_len = (qbits >> 3); 377 378 /* Save m in u1. */ 379 if (BN_bin2bn(dgst, dgst_len, u1) == NULL) 380 goto err; 381 382 /* u1 = m * w mod q */ 383 if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx)) 384 goto err; 385 386 /* u2 = r * w mod q */ 387 if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx)) 388 goto err; 389 390 if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { 391 mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p, 392 CRYPTO_LOCK_DSA, dsa->p, ctx); 393 if (!mont) 394 goto err; 395 } 396 397 if (dsa->meth->dsa_mod_exp != NULL) { 398 if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, 399 u2, dsa->p, ctx, mont)) 400 goto err; 401 } else { 402 if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, 403 dsa->p, ctx, mont)) 404 goto err; 405 } 406 407 /* let u1 = u1 mod q */ 408 if (!BN_mod_ct(u1, t1, dsa->q, ctx)) 409 goto err; 410 411 /* v is in u1 - if the signature is correct, it will be equal to r. */ 412 ret = BN_ucmp(u1, sig->r) == 0; 413 414 err: 415 if (ret < 0) 416 DSAerror(ERR_R_BN_LIB); 417 BN_CTX_end(ctx); 418 BN_CTX_free(ctx); 419 420 return ret; 421 } 422 423 static int 424 dsa_init(DSA *dsa) 425 { 426 dsa->flags |= DSA_FLAG_CACHE_MONT_P; 427 return 1; 428 } 429 430 static int 431 dsa_finish(DSA *dsa) 432 { 433 BN_MONT_CTX_free(dsa->method_mont_p); 434 return 1; 435 } 436