1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 * The DSS routines are based on patches supplied by
58 * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
60 #include <openssl/dsa.h>
64 #include <openssl/bn.h>
65 #include <openssl/dh.h>
66 #include <openssl/digest.h>
67 #include <openssl/engine.h>
68 #include <openssl/err.h>
69 #include <openssl/ex_data.h>
70 #include <openssl/mem.h>
71 #include <openssl/rand.h>
72 #include <openssl/sha.h>
73 #include <openssl/thread.h>
75 #include "../fipsmodule/bn/internal.h"
76 #include "../internal.h"
79 #define OPENSSL_DSA_MAX_MODULUS_BITS 10000
81 // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
83 #define DSS_prime_checks 50
85 static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
88 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
91 DSA *dsa = OPENSSL_malloc(sizeof(DSA));
93 OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
97 OPENSSL_memset(dsa, 0, sizeof(DSA));
101 CRYPTO_MUTEX_init(&dsa->method_mont_lock);
102 CRYPTO_new_ex_data(&dsa->ex_data);
107 void DSA_free(DSA *dsa) {
112 if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
116 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
118 BN_clear_free(dsa->p);
119 BN_clear_free(dsa->q);
120 BN_clear_free(dsa->g);
121 BN_clear_free(dsa->pub_key);
122 BN_clear_free(dsa->priv_key);
123 BN_MONT_CTX_free(dsa->method_mont_p);
124 BN_MONT_CTX_free(dsa->method_mont_q);
125 CRYPTO_MUTEX_cleanup(&dsa->method_mont_lock);
129 int DSA_up_ref(DSA *dsa) {
130 CRYPTO_refcount_inc(&dsa->references);
134 void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
135 const BIGNUM **out_priv_key) {
136 if (out_pub_key != NULL) {
137 *out_pub_key = dsa->pub_key;
139 if (out_priv_key != NULL) {
140 *out_priv_key = dsa->priv_key;
144 void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, const BIGNUM **out_q,
145 const BIGNUM **out_g) {
157 int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key) {
158 if (dsa->pub_key == NULL && pub_key == NULL) {
162 if (pub_key != NULL) {
163 BN_free(dsa->pub_key);
164 dsa->pub_key = pub_key;
166 if (priv_key != NULL) {
167 BN_free(dsa->priv_key);
168 dsa->priv_key = priv_key;
174 int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
175 if ((dsa->p == NULL && p == NULL) ||
176 (dsa->q == NULL && q == NULL) ||
177 (dsa->g == NULL && g == NULL)) {
197 int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
198 size_t seed_len, int *out_counter,
199 unsigned long *out_h, BN_GENCB *cb) {
201 unsigned char seed[SHA256_DIGEST_LENGTH];
202 unsigned char md[SHA256_DIGEST_LENGTH];
203 unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
204 BIGNUM *r0, *W, *X, *c, *test;
205 BIGNUM *g = NULL, *q = NULL, *p = NULL;
206 BN_MONT_CTX *mont = NULL;
216 evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
217 qsize = EVP_MD_size(evpmd);
223 bits = (bits + 63) / 64 * 64;
225 if (seed_in != NULL) {
226 if (seed_len < (size_t)qsize) {
229 if (seed_len > (size_t)qsize) {
230 // Only consume as much seed as is expected.
233 OPENSSL_memcpy(seed, seed_in, seed_len);
242 r0 = BN_CTX_get(ctx);
249 test = BN_CTX_get(ctx);
251 if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
259 if (!BN_GENCB_call(cb, 0, m++)) {
263 int use_random_seed = (seed_in == NULL);
264 if (use_random_seed) {
265 if (!RAND_bytes(seed, qsize)) {
269 // If we come back through, use random seed next time.
272 OPENSSL_memcpy(buf, seed, qsize);
273 OPENSSL_memcpy(buf2, seed, qsize);
274 // precompute "SEED + 1" for step 7:
275 for (i = qsize - 1; i < qsize; i--) {
283 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
284 !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
287 for (i = 0; i < qsize; i++) {
293 md[qsize - 1] |= 0x01;
294 if (!BN_bin2bn(md, qsize, q)) {
299 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
307 // do a callback call
311 if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
319 n = (bits - 1) / 160;
322 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
328 // now 'buf' contains "SEED + offset - 1"
329 for (k = 0; k <= n; k++) {
330 // obtain "SEED + offset + k" by incrementing:
331 for (i = qsize - 1; i < qsize; i--) {
338 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
343 if (!BN_bin2bn(md, qsize, r0) ||
344 !BN_lshift(r0, r0, (qsize << 3) * k) ||
351 if (!BN_mask_bits(W, bits - 1) ||
353 !BN_add(X, X, test)) {
358 if (!BN_lshift1(r0, q) ||
359 !BN_mod(c, X, r0, ctx) ||
360 !BN_sub(r0, c, BN_value_one()) ||
366 if (BN_cmp(p, test) >= 0) {
368 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
370 goto end; // found it
379 // "offset = offset + n + 1"
382 if (counter >= 4096) {
388 if (!BN_GENCB_call(cb, 2, 1)) {
392 // We now need to generate g
394 if (!BN_sub(test, p, BN_value_one()) ||
395 !BN_div(r0, NULL, test, q, ctx)) {
399 mont = BN_MONT_CTX_new_for_modulus(p, ctx);
401 !BN_set_word(test, h)) {
407 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
413 if (!BN_add(test, test, BN_value_one())) {
419 if (!BN_GENCB_call(cb, 3, 1)) {
433 if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
437 if (out_counter != NULL) {
438 *out_counter = counter;
450 BN_MONT_CTX_free(mont);
455 DSA *DSAparams_dup(const DSA *dsa) {
456 DSA *ret = DSA_new();
460 ret->p = BN_dup(dsa->p);
461 ret->q = BN_dup(dsa->q);
462 ret->g = BN_dup(dsa->g);
463 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
470 int DSA_generate_key(DSA *dsa) {
473 BIGNUM *pub_key = NULL, *priv_key = NULL;
480 priv_key = dsa->priv_key;
481 if (priv_key == NULL) {
483 if (priv_key == NULL) {
488 if (!BN_rand_range_ex(priv_key, 1, dsa->q)) {
492 pub_key = dsa->pub_key;
493 if (pub_key == NULL) {
495 if (pub_key == NULL) {
500 if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, &dsa->method_mont_lock,
502 !BN_mod_exp_mont_consttime(pub_key, dsa->g, priv_key, dsa->p, ctx,
503 dsa->method_mont_p)) {
507 dsa->priv_key = priv_key;
508 dsa->pub_key = pub_key;
512 if (dsa->pub_key == NULL) {
515 if (dsa->priv_key == NULL) {
523 DSA_SIG *DSA_SIG_new(void) {
525 sig = OPENSSL_malloc(sizeof(DSA_SIG));
534 void DSA_SIG_free(DSA_SIG *sig) {
544 DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
545 BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
549 int reason = ERR_R_BN_LIB;
555 if (!dsa->p || !dsa->q || !dsa->g) {
556 reason = DSA_R_MISSING_PARAMETERS;
570 if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
574 if (digest_len > BN_num_bytes(dsa->q)) {
575 // if the digest length is greater than the size of q use the
576 // BN_num_bits(dsa->q) leftmost bits of the digest, see
578 digest_len = BN_num_bytes(dsa->q);
581 if (BN_bin2bn(digest, digest_len, &m) == NULL) {
585 // Compute s = inv(k) (m + xr) mod q
586 if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
589 if (!BN_add(s, &xr, &m)) {
590 goto err; // s = m + xr
592 if (BN_cmp(s, dsa->q) > 0) {
593 if (!BN_sub(s, s, dsa->q)) {
597 if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
601 // Redo if r or s is zero as required by FIPS 186-3: this is
603 if (BN_is_zero(r) || BN_is_zero(s)) {
615 OPENSSL_PUT_ERROR(DSA, reason);
627 int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
630 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
636 int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
637 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
645 if (!dsa->p || !dsa->q || !dsa->g) {
646 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
650 i = BN_num_bits(dsa->q);
651 // fips 186-3 allows only different sizes for q
652 if (i != 160 && i != 224 && i != 256) {
653 OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
657 if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
658 OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
671 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
672 BN_ucmp(sig->r, dsa->q) >= 0) {
676 if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
677 BN_ucmp(sig->s, dsa->q) >= 0) {
682 // Calculate W = inv(S) mod Q
684 if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
689 if (digest_len > (i >> 3)) {
690 // if the digest length is greater than the size of q use the
691 // BN_num_bits(dsa->q) leftmost bits of the digest, see
693 digest_len = (i >> 3);
696 if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
701 if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
706 if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
710 if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
711 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
716 if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
717 dsa->method_mont_p)) {
723 if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
727 // V is now in u1. If the signature is correct, it will be
729 *out_valid = BN_ucmp(&u1, sig->r) == 0;
734 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
744 int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
745 uint8_t *out_sig, unsigned int *out_siglen, const DSA *dsa) {
748 s = DSA_do_sign(digest, digest_len, dsa);
754 *out_siglen = i2d_DSA_SIG(s, &out_sig);
759 int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
760 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
762 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
768 int DSA_check_signature(int *out_valid, const uint8_t *digest,
769 size_t digest_len, const uint8_t *sig, size_t sig_len,
780 const uint8_t *sigp = sig;
781 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
785 // Ensure that the signature uses DER and doesn't have trailing garbage.
786 int der_len = i2d_DSA_SIG(s, &der);
787 if (der_len < 0 || (size_t)der_len != sig_len ||
788 OPENSSL_memcmp(sig, der, sig_len)) {
792 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
800 // der_len_len returns the number of bytes needed to represent a length of |len|
802 static size_t der_len_len(size_t len) {
814 int DSA_size(const DSA *dsa) {
815 size_t order_len = BN_num_bytes(dsa->q);
816 // Compute the maximum length of an |order_len| byte integer. Defensively
817 // assume that the leading 0x00 is included.
818 size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
819 if (integer_len < order_len) {
822 // A DSA signature is two INTEGERs.
823 size_t value_len = 2 * integer_len;
824 if (value_len < integer_len) {
828 size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
829 if (ret < value_len) {
835 static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
838 BIGNUM k, *kinv = NULL, *r = NULL;
841 if (!dsa->p || !dsa->q || !dsa->g) {
842 OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
858 if (r == NULL || kinv == NULL ||
860 !BN_rand_range_ex(&k, 1, dsa->q) ||
861 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
862 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->p,
864 !BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_q,
865 (CRYPTO_MUTEX *)&dsa->method_mont_lock, dsa->q,
867 // Compute r = (g^k mod p) mod q
868 !BN_mod_exp_mont_consttime(r, dsa->g, &k, dsa->p, ctx,
869 dsa->method_mont_p) ||
870 !BN_mod(r, r, dsa->q, ctx) ||
871 // Compute part of 's = inv(k) (m + xr) mod q' using Fermat's Little
873 !bn_mod_inverse_prime(kinv, &k, dsa->q, ctx, dsa->method_mont_q)) {
877 BN_clear_free(*out_kinv);
880 BN_clear_free(*out_r);
886 OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
892 if (ctx_in == NULL) {
900 int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
901 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
903 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
910 int DSA_set_ex_data(DSA *dsa, int idx, void *arg) {
911 return CRYPTO_set_ex_data(&dsa->ex_data, idx, arg);
914 void *DSA_get_ex_data(const DSA *dsa, int idx) {
915 return CRYPTO_get_ex_data(&dsa->ex_data, idx);
918 DH *DSA_dup_DH(const DSA *dsa) {
927 if (dsa->q != NULL) {
928 ret->priv_length = BN_num_bits(dsa->q);
929 if ((ret->q = BN_dup(dsa->q)) == NULL) {
933 if ((dsa->p != NULL && (ret->p = BN_dup(dsa->p)) == NULL) ||
934 (dsa->g != NULL && (ret->g = BN_dup(dsa->g)) == NULL) ||
935 (dsa->pub_key != NULL && (ret->pub_key = BN_dup(dsa->pub_key)) == NULL) ||
936 (dsa->priv_key != NULL &&
937 (ret->priv_key = BN_dup(dsa->priv_key)) == NULL)) {