| 
									
										
										
										
											2016-05-18 02:51:26 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-04-23 20:55:52 +08:00
										 |  |  |  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:36:26 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:51:26 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 05:23:39 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * DSA low level APIs are deprecated for public use, but still ok for | 
					
						
							|  |  |  |  * internal use. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include "internal/deprecated.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | #include "crypto/bn.h"
 | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | #include <openssl/bn.h>
 | 
					
						
							| 
									
										
										
										
											2007-02-03 22:41:12 +08:00
										 |  |  | #include <openssl/sha.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "dsa_local.h"
 | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | #include <openssl/asn1.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, | 
					
						
							|  |  |  |                                     BIGNUM **rp); | 
					
						
							|  |  |  | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, | 
					
						
							|  |  |  |                           BIGNUM **rp, const unsigned char *dgst, int dlen); | 
					
						
							|  |  |  | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, | 
					
						
							|  |  |  |                          DSA_SIG *sig, DSA *dsa); | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | static int dsa_init(DSA *dsa); | 
					
						
							|  |  |  | static int dsa_finish(DSA *dsa); | 
					
						
							| 
									
										
										
										
											2018-10-29 04:50:51 +08:00
										 |  |  | static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q, | 
					
						
							|  |  |  |                                       BN_CTX *ctx); | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static DSA_METHOD openssl_dsa_meth = { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     "OpenSSL DSA method", | 
					
						
							|  |  |  |     dsa_do_sign, | 
					
						
							|  |  |  |     dsa_sign_setup_no_digest, | 
					
						
							|  |  |  |     dsa_do_verify, | 
					
						
							|  |  |  |     NULL,                       /* dsa_mod_exp, */ | 
					
						
							|  |  |  |     NULL,                       /* dsa_bn_mod_exp, */ | 
					
						
							|  |  |  |     dsa_init, | 
					
						
							|  |  |  |     dsa_finish, | 
					
						
							|  |  |  |     DSA_FLAG_FIPS_METHOD, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  |     NULL, | 
					
						
							|  |  |  |     NULL | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-08 00:07:42 +08:00
										 |  |  | static const DSA_METHOD *default_DSA_method = &openssl_dsa_meth; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 04:34:56 +08:00
										 |  |  | #ifndef FIPS_MODULE
 | 
					
						
							| 
									
										
										
										
											2017-04-08 00:07:42 +08:00
										 |  |  | void DSA_set_default_method(const DSA_METHOD *meth) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     default_DSA_method = meth; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-14 04:34:56 +08:00
										 |  |  | #endif /* FIPS_MODULE */
 | 
					
						
							| 
									
										
										
										
											2017-04-08 00:07:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const DSA_METHOD *DSA_get_default_method(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return default_DSA_method; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-11-07 21:54:39 +08:00
										 |  |  | const DSA_METHOD *DSA_OpenSSL(void) | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     return &openssl_dsa_meth; | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Fix external symbols related to dsa keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dsa_check_pairwise, dsa_check_params, dsa_check_priv_key, dsa_check_pub_key, dsa_check_pub_key_partial,
dsa_do_sign_int, dsa_ffc_params_fromdata,
dsa_generate_ffc_parameters, dsa_generate_public_key,
dsa_get0_params, dsa_key_fromdata, dsa_new_with_ctx, dsa_pkey_method, dsa_sign_int
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
											
										 
											2021-02-18 14:30:37 +08:00
										 |  |  | DSA_SIG *ossl_dsa_do_sign_int(const unsigned char *dgst, int dlen, DSA *dsa) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     BIGNUM *kinv = NULL; | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |     BIGNUM *m, *blind, *blindm, *tmp; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     BN_CTX *ctx = NULL; | 
					
						
							|  |  |  |     int reason = ERR_R_BN_LIB; | 
					
						
							|  |  |  |     DSA_SIG *ret = NULL; | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     int rv = 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (dsa->params.p == NULL | 
					
						
							|  |  |  |         || dsa->params.q == NULL | 
					
						
							|  |  |  |         || dsa->params.g == NULL) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         reason = DSA_R_MISSING_PARAMETERS; | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-26 17:03:12 +08:00
										 |  |  |     if (dsa->priv_key == NULL) { | 
					
						
							|  |  |  |         reason = DSA_R_MISSING_PRIVATE_KEY; | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     ret = DSA_SIG_new(); | 
					
						
							|  |  |  |     if (ret == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2016-07-20 01:57:15 +08:00
										 |  |  |     ret->r = BN_new(); | 
					
						
							|  |  |  |     ret->s = BN_new(); | 
					
						
							|  |  |  |     if (ret->r == NULL || ret->s == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 11:03:46 +08:00
										 |  |  |     ctx = BN_CTX_new_ex(dsa->libctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |     m = BN_CTX_get(ctx); | 
					
						
							|  |  |  |     blind = BN_CTX_get(ctx); | 
					
						
							|  |  |  |     blindm = BN_CTX_get(ctx); | 
					
						
							|  |  |  |     tmp = BN_CTX_get(ctx); | 
					
						
							|  |  |  |     if (tmp == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |  redo: | 
					
						
							| 
									
										
										
										
											2016-06-10 05:09:48 +08:00
										 |  |  |     if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen)) | 
					
						
							| 
									
										
										
										
											2016-03-08 01:31:00 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (dlen > BN_num_bytes(dsa->params.q)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * if the digest length is greater than the size of q use the | 
					
						
							|  |  |  |          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3, | 
					
						
							|  |  |  |          * 4.2 | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         dlen = BN_num_bytes(dsa->params.q); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (BN_bin2bn(dgst, dlen, m) == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * The normal signature calculation is: | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *   s := k^-1 * (m + r * priv_key) mod q | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * We will blind this to protect against side channel attacks | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Generate a blinding value */ | 
					
						
							|  |  |  |     do { | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         if (!BN_priv_rand_ex(blind, BN_num_bits(dsa->params.q) - 1, | 
					
						
							| 
									
										
										
										
											2020-01-12 09:32:12 +08:00
										 |  |  |                              BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |     } while (BN_is_zero(blind)); | 
					
						
							|  |  |  |     BN_set_flags(blind, BN_FLG_CONSTTIME); | 
					
						
							|  |  |  |     BN_set_flags(blindm, BN_FLG_CONSTTIME); | 
					
						
							|  |  |  |     BN_set_flags(tmp, BN_FLG_CONSTTIME); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* tmp := blind * priv_key * r mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(tmp, tmp, ret->r, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* blindm := blind * m mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(blindm, blind, m, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* s : = (blind * priv_key * r) + (blind * m) mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->params.q)) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* s := s * k^-1 mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |     /* s:= s * blind^-1 mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (BN_mod_inverse(blind, blind, dsa->params.q, ctx) == NULL) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(ret->s, ret->s, blind, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2018-06-19 22:07:02 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Redo if r or s is zero as required by FIPS 186-3: this is very | 
					
						
							|  |  |  |      * unlikely. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-06-10 05:09:48 +08:00
										 |  |  |     if (BN_is_zero(ret->r) || BN_is_zero(ret->s)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto redo; | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     rv = 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     if (rv == 0) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, reason); | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |         DSA_SIG_free(ret); | 
					
						
							|  |  |  |         ret = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_CTX_free(ctx); | 
					
						
							|  |  |  |     BN_clear_free(kinv); | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 09:32:12 +08:00
										 |  |  | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
											
												Fix external symbols related to dsa keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dsa_check_pairwise, dsa_check_params, dsa_check_priv_key, dsa_check_pub_key, dsa_check_pub_key_partial,
dsa_do_sign_int, dsa_ffc_params_fromdata,
dsa_generate_ffc_parameters, dsa_generate_public_key,
dsa_get0_params, dsa_key_fromdata, dsa_new_with_ctx, dsa_pkey_method, dsa_sign_int
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
											
										 
											2021-02-18 14:30:37 +08:00
										 |  |  |     return ossl_dsa_do_sign_int(dgst, dlen, dsa); | 
					
						
							| 
									
										
										
										
											2020-01-12 09:32:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 01:05:43 +08:00
										 |  |  | static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                                     BIGNUM **kinvp, BIGNUM **rp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0); | 
					
						
							| 
									
										
										
										
											2013-07-15 19:42:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 01:05:43 +08:00
										 |  |  | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                           BIGNUM **kinvp, BIGNUM **rp, | 
					
						
							|  |  |  |                           const unsigned char *dgst, int dlen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BN_CTX *ctx = NULL; | 
					
						
							| 
									
										
										
										
											2016-06-07 16:32:23 +08:00
										 |  |  |     BIGNUM *k, *kinv = NULL, *r = *rp; | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     BIGNUM *l; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     int q_bits, q_words; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!dsa->params.p || !dsa->params.q || !dsa->params.g) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 22:56:08 +08:00
										 |  |  |     /* Reject obviously invalid parameters */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (BN_is_zero(dsa->params.p) | 
					
						
							|  |  |  |         || BN_is_zero(dsa->params.q) | 
					
						
							|  |  |  |         || BN_is_zero(dsa->params.g)) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_PARAMETERS); | 
					
						
							| 
									
										
										
										
											2019-05-03 22:56:08 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-26 17:03:12 +08:00
										 |  |  |     if (dsa->priv_key == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PRIVATE_KEY); | 
					
						
							| 
									
										
										
										
											2019-07-26 17:03:12 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-03 22:56:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     k = BN_new(); | 
					
						
							| 
									
										
										
										
											2017-11-01 04:58:13 +08:00
										 |  |  |     l = BN_new(); | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     if (k == NULL || l == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ctx_in == NULL) { | 
					
						
							| 
									
										
										
										
											2020-01-12 09:32:12 +08:00
										 |  |  |         /* if you don't pass in ctx_in you get a default libctx */ | 
					
						
							|  |  |  |         if ((ctx = BN_CTX_new_ex(NULL)) == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } else | 
					
						
							|  |  |  |         ctx = ctx_in; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 04:58:13 +08:00
										 |  |  |     /* Preallocate space */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     q_bits = BN_num_bits(dsa->params.q); | 
					
						
							|  |  |  |     q_words = bn_get_top(dsa->params.q); | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     if (!bn_wexpand(k, q_words + 2) | 
					
						
							|  |  |  |         || !bn_wexpand(l, q_words + 2)) | 
					
						
							| 
									
										
										
										
											2017-11-01 04:58:13 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* Get random k */ | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         if (dgst != NULL) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * We calculate k from SHA512(private_key + H(message) + random). | 
					
						
							|  |  |  |              * This protects the private key from a weak PRNG. | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |             if (!BN_generate_dsa_nonce(k, dsa->params.q, dsa->priv_key, dgst, | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |                                        dlen, ctx)) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         } else if (!BN_priv_rand_range_ex(k, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } while (BN_is_zero(k)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-07 21:02:01 +08:00
										 |  |  |     BN_set_flags(k, BN_FLG_CONSTTIME); | 
					
						
							| 
									
										
										
										
											2018-11-01 06:44:11 +08:00
										 |  |  |     BN_set_flags(l, BN_FLG_CONSTTIME); | 
					
						
							| 
									
										
										
										
											2016-06-07 21:02:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { | 
					
						
							|  |  |  |         if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |                                     dsa->lock, dsa->params.p, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Compute r = (g^k mod p) mod q */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 17:55:11 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * We do not want timing information to leak the length of k, so we | 
					
						
							| 
									
										
										
										
											2017-11-01 04:58:13 +08:00
										 |  |  |      * compute G^k using an equivalent scalar of fixed bit-length. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * We unconditionally perform both of these additions to prevent a | 
					
						
							|  |  |  |      * small timing information leakage.  We then choose the sum that is | 
					
						
							|  |  |  |      * one bit longer than the modulus. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |      * There are some concerns about the efficacy of doing this.  More | 
					
						
							| 
									
										
										
										
											2019-07-02 16:04:04 +08:00
										 |  |  |      * specifically refer to the discussion starting with: | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |      *     https://github.com/openssl/openssl/pull/7486#discussion_r228323705
 | 
					
						
							|  |  |  |      * The fix is to rework BN so these gymnastics aren't required. | 
					
						
							| 
									
										
										
										
											2016-05-26 17:55:11 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_add(l, k, dsa->params.q) | 
					
						
							|  |  |  |         || !BN_add(k, l, dsa->params.q)) | 
					
						
							| 
									
										
										
										
											2016-05-26 17:55:11 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:45:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |     if ((dsa)->meth->bn_mod_exp != NULL) { | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |             if (!dsa->meth->bn_mod_exp(dsa, r, dsa->params.g, k, dsa->params.p, | 
					
						
							|  |  |  |                                        ctx, dsa->method_mont_p)) | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |                 goto err; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |             if (!BN_mod_exp_mont(r, dsa->params.g, k, dsa->params.p, ctx, | 
					
						
							|  |  |  |                                  dsa->method_mont_p)) | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |                 goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod(r, r, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-24 05:42:46 +08:00
										 |  |  |     /* Compute part of 's = inv(k) (m + xr) mod q' */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if ((kinv = dsa_mod_inverse_fermat(k, dsa->params.q, ctx)) == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_clear_free(*kinvp); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     *kinvp = kinv; | 
					
						
							|  |  |  |     kinv = NULL; | 
					
						
							|  |  |  |     ret = 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     if (!ret) | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, ERR_R_BN_LIB); | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     if (ctx != ctx_in) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         BN_CTX_free(ctx); | 
					
						
							|  |  |  |     BN_clear_free(k); | 
					
						
							| 
									
										
										
										
											2017-11-01 04:58:13 +08:00
										 |  |  |     BN_clear_free(l); | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, | 
					
						
							|  |  |  |                          DSA_SIG *sig, DSA *dsa) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BN_CTX *ctx; | 
					
						
							|  |  |  |     BIGNUM *u1, *u2, *t1; | 
					
						
							|  |  |  |     BN_MONT_CTX *mont = NULL; | 
					
						
							| 
									
										
										
										
											2016-06-10 05:09:48 +08:00
										 |  |  |     const BIGNUM *r, *s; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     int ret = -1, i; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:28:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (dsa->params.p == NULL | 
					
						
							|  |  |  |         || dsa->params.q == NULL | 
					
						
							|  |  |  |         || dsa->params.g == NULL) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     i = BN_num_bits(dsa->params.q); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* fips 186-3 allows only different sizes for q */ | 
					
						
							|  |  |  |     if (i != 160 && i != 224 && i != 256) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_BAD_Q_VALUE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (BN_num_bits(dsa->params.p) > OPENSSL_DSA_MAX_MODULUS_BITS) { | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, DSA_R_MODULUS_TOO_LARGE); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     u1 = BN_new(); | 
					
						
							|  |  |  |     u2 = BN_new(); | 
					
						
							|  |  |  |     t1 = BN_new(); | 
					
						
							| 
									
										
										
										
											2020-01-12 09:32:12 +08:00
										 |  |  |     ctx = BN_CTX_new_ex(NULL); /* verify does not need a libctx */ | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |     if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-10 05:09:48 +08:00
										 |  |  |     DSA_SIG_get0(sig, &r, &s); | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (BN_is_zero(r) || BN_is_negative(r) || | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         BN_ucmp(r, dsa->params.q) >= 0) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         ret = 0; | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     if (BN_is_zero(s) || BN_is_negative(s) || | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         BN_ucmp(s, dsa->params.q) >= 0) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         ret = 0; | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Calculate W = inv(S) mod Q save W in u2 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if ((BN_mod_inverse(u2, s, dsa->params.q, ctx)) == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* save M in u1 */ | 
					
						
							|  |  |  |     if (dgst_len > (i >> 3)) | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * if the digest length is greater than the size of q use the | 
					
						
							|  |  |  |          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3, | 
					
						
							|  |  |  |          * 4.2 | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         dgst_len = (i >> 3); | 
					
						
							|  |  |  |     if (BN_bin2bn(dgst, dgst_len, u1) == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* u1 = M * w mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(u1, u1, u2, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* u2 = r * w mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod_mul(u2, r, u2, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { | 
					
						
							|  |  |  |         mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p, | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |                                       dsa->lock, dsa->params.p, ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         if (!mont) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |     if (dsa->meth->dsa_mod_exp != NULL) { | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->params.g, u1, dsa->pub_key, u2, | 
					
						
							|  |  |  |                                     dsa->params.p, ctx, mont)) | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |         if (!BN_mod_exp2_mont(t1, dsa->params.g, u1, dsa->pub_key, u2, | 
					
						
							|  |  |  |                               dsa->params.p, ctx, mont)) | 
					
						
							| 
									
										
										
										
											2016-05-26 17:06:27 +08:00
										 |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     /* let u1 = u1 mod q */ | 
					
						
							| 
									
										
										
										
											2020-01-24 12:09:33 +08:00
										 |  |  |     if (!BN_mod(u1, t1, dsa->params.q, ctx)) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * V is now in u1.  If the signature is correct, it will be equal to R. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-03-04 10:39:50 +08:00
										 |  |  |     ret = (BN_ucmp(u1, r) == 0); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |  err: | 
					
						
							|  |  |  |     if (ret < 0) | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |         ERR_raise(ERR_LIB_DSA, ERR_R_BN_LIB); | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_CTX_free(ctx); | 
					
						
							|  |  |  |     BN_free(u1); | 
					
						
							|  |  |  |     BN_free(u2); | 
					
						
							|  |  |  |     BN_free(t1); | 
					
						
							| 
									
										
										
										
											2017-10-17 22:04:09 +08:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int dsa_init(DSA *dsa) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     dsa->flags |= DSA_FLAG_CACHE_MONT_P; | 
					
						
							| 
									
										
											  
											
												ffc: add _ossl to exported but internal functions
The functions updated are:
    ffc_generate_private_key, ffc_named_group_from_uid,
    ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
    ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
    ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
    ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
    ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
    ffc_params_flags_to_name, ffc_params_fromdata,
    ffc_params_get0_pqg, ffc_params_get_validate_params,
    ffc_params_init, ffc_params_print, ffc_params_set0_j,
    ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
    ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
    ffc_params_set_validate_params, ffc_params_simple_validate,
    ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
    ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
    and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
											
										 
											2020-09-30 13:07:24 +08:00
										 |  |  |     ossl_ffc_params_init(&dsa->params); | 
					
						
							| 
									
										
										
										
											2020-02-06 20:28:36 +08:00
										 |  |  |     dsa->dirty_cnt++; | 
					
						
							| 
									
										
										
										
											2017-10-09 19:05:58 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int dsa_finish(DSA *dsa) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-01 09:37:06 +08:00
										 |  |  |     BN_MONT_CTX_free(dsa->method_mont_p); | 
					
						
							| 
									
										
										
										
											2017-10-09 19:05:58 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1999-08-23 01:57:38 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-29 04:50:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Compute the inverse of k modulo q. | 
					
						
							|  |  |  |  * Since q is prime, Fermat's Little Theorem applies, which reduces this to | 
					
						
							|  |  |  |  * mod-exp operation.  Both the exponent and modulus are public information | 
					
						
							|  |  |  |  * so a mod-exp that doesn't leak the base is sufficient.  A newly allocated | 
					
						
							|  |  |  |  * BIGNUM is returned which the caller must free. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q, | 
					
						
							|  |  |  |                                       BN_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BIGNUM *res = NULL; | 
					
						
							|  |  |  |     BIGNUM *r, *e; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((r = BN_new()) == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BN_CTX_start(ctx); | 
					
						
							|  |  |  |     if ((e = BN_CTX_get(ctx)) != NULL | 
					
						
							|  |  |  |             && BN_set_word(r, 2) | 
					
						
							|  |  |  |             && BN_sub(e, q, r) | 
					
						
							|  |  |  |             && BN_mod_exp_mont(r, k, e, q, ctx, NULL)) | 
					
						
							|  |  |  |         res = r; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         BN_free(r); | 
					
						
							|  |  |  |     BN_CTX_end(ctx); | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } |