diff options
Diffstat (limited to 'src/lib/libcrypto/dsa')
-rw-r--r-- | src/lib/libcrypto/dsa/Makefile | 173 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/README | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa.h | 272 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_asn1.c | 140 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_err.c | 114 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 305 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_key.c | 121 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_lib.c | 308 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ossl.c | 393 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_sign.c | 106 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_vrf.c | 103 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsagen.c | 111 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsatest.c | 251 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/fips186a.txt | 122 |
14 files changed, 2523 insertions, 0 deletions
diff --git a/src/lib/libcrypto/dsa/Makefile b/src/lib/libcrypto/dsa/Makefile new file mode 100644 index 0000000000..4f10278039 --- /dev/null +++ b/src/lib/libcrypto/dsa/Makefile | |||
@@ -0,0 +1,173 @@ | |||
1 | # | ||
2 | # OpenSSL/crypto/dsa/Makefile | ||
3 | # | ||
4 | |||
5 | DIR= dsa | ||
6 | TOP= ../.. | ||
7 | CC= cc | ||
8 | INCLUDES= -I.. -I$(TOP) -I../../include | ||
9 | CFLAG=-g | ||
10 | INSTALL_PREFIX= | ||
11 | OPENSSLDIR= /usr/local/ssl | ||
12 | INSTALLTOP=/usr/local/ssl | ||
13 | MAKEDEPPROG= makedepend | ||
14 | MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) | ||
15 | MAKEFILE= Makefile | ||
16 | AR= ar r | ||
17 | |||
18 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
19 | |||
20 | GENERAL=Makefile | ||
21 | TEST=dsatest.c | ||
22 | APPS= | ||
23 | |||
24 | LIB=$(TOP)/libcrypto.a | ||
25 | LIBSRC= dsa_gen.c dsa_key.c dsa_lib.c dsa_asn1.c dsa_vrf.c dsa_sign.c \ | ||
26 | dsa_err.c dsa_ossl.c | ||
27 | LIBOBJ= dsa_gen.o dsa_key.o dsa_lib.o dsa_asn1.o dsa_vrf.o dsa_sign.o \ | ||
28 | dsa_err.o dsa_ossl.o | ||
29 | |||
30 | SRC= $(LIBSRC) | ||
31 | |||
32 | EXHEADER= dsa.h | ||
33 | HEADER= $(EXHEADER) | ||
34 | |||
35 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
36 | |||
37 | top: | ||
38 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
39 | |||
40 | all: lib | ||
41 | |||
42 | lib: $(LIBOBJ) | ||
43 | $(AR) $(LIB) $(LIBOBJ) | ||
44 | $(RANLIB) $(LIB) || echo Never mind. | ||
45 | @touch lib | ||
46 | |||
47 | files: | ||
48 | $(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO | ||
49 | |||
50 | links: | ||
51 | @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER) | ||
52 | @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST) | ||
53 | @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS) | ||
54 | |||
55 | install: | ||
56 | @headerlist="$(EXHEADER)"; for i in $$headerlist ; \ | ||
57 | do \ | ||
58 | (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ | ||
59 | chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ | ||
60 | done; | ||
61 | |||
62 | tags: | ||
63 | ctags $(SRC) | ||
64 | |||
65 | tests: | ||
66 | |||
67 | lint: | ||
68 | lint -DLINT $(INCLUDES) $(SRC)>fluff | ||
69 | |||
70 | depend: | ||
71 | $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC) | ||
72 | |||
73 | dclean: | ||
74 | $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new | ||
75 | mv -f Makefile.new $(MAKEFILE) | ||
76 | |||
77 | clean: | ||
78 | rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff | ||
79 | |||
80 | # DO NOT DELETE THIS LINE -- make depend depends on it. | ||
81 | |||
82 | dsa_asn1.o: ../../e_os.h ../../include/openssl/asn1.h | ||
83 | dsa_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h | ||
84 | dsa_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
85 | dsa_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
86 | dsa_asn1.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
87 | dsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
88 | dsa_asn1.o: ../../include/openssl/opensslconf.h | ||
89 | dsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
90 | dsa_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
91 | dsa_asn1.o: ../../include/openssl/symhacks.h ../cryptlib.h dsa_asn1.c | ||
92 | dsa_err.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
93 | dsa_err.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
94 | dsa_err.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
95 | dsa_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
96 | dsa_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
97 | dsa_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h | ||
98 | dsa_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
99 | dsa_err.o: dsa_err.c | ||
100 | dsa_gen.o: ../../e_os.h ../../include/openssl/aes.h | ||
101 | dsa_gen.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
102 | dsa_gen.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
103 | dsa_gen.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
104 | dsa_gen.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
105 | dsa_gen.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
106 | dsa_gen.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
107 | dsa_gen.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
108 | dsa_gen.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
109 | dsa_gen.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
110 | dsa_gen.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
111 | dsa_gen.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
112 | dsa_gen.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
113 | dsa_gen.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h | ||
114 | dsa_gen.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h | ||
115 | dsa_gen.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h | ||
116 | dsa_gen.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
117 | dsa_gen.o: ../../include/openssl/sha.h ../../include/openssl/stack.h | ||
118 | dsa_gen.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
119 | dsa_gen.o: ../../include/openssl/ui_compat.h ../cryptlib.h dsa_gen.c | ||
120 | dsa_key.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
121 | dsa_key.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
122 | dsa_key.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
123 | dsa_key.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
124 | dsa_key.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
125 | dsa_key.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
126 | dsa_key.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h | ||
127 | dsa_key.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
128 | dsa_key.o: ../cryptlib.h dsa_key.c | ||
129 | dsa_lib.o: ../../e_os.h ../../include/openssl/asn1.h | ||
130 | dsa_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
131 | dsa_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
132 | dsa_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
133 | dsa_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h | ||
134 | dsa_lib.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
135 | dsa_lib.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
136 | dsa_lib.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h | ||
137 | dsa_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
138 | dsa_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
139 | dsa_lib.o: ../../include/openssl/ui.h ../cryptlib.h dsa_lib.c | ||
140 | dsa_ossl.o: ../../e_os.h ../../include/openssl/asn1.h | ||
141 | dsa_ossl.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
142 | dsa_ossl.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
143 | dsa_ossl.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
144 | dsa_ossl.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
145 | dsa_ossl.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
146 | dsa_ossl.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
147 | dsa_ossl.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h | ||
148 | dsa_ossl.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
149 | dsa_ossl.o: ../cryptlib.h dsa_ossl.c | ||
150 | dsa_sign.o: ../../e_os.h ../../include/openssl/asn1.h | ||
151 | dsa_sign.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
152 | dsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
153 | dsa_sign.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
154 | dsa_sign.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h | ||
155 | dsa_sign.o: ../../include/openssl/err.h ../../include/openssl/fips.h | ||
156 | dsa_sign.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
157 | dsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
158 | dsa_sign.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
159 | dsa_sign.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
160 | dsa_sign.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
161 | dsa_sign.o: ../cryptlib.h dsa_sign.c | ||
162 | dsa_vrf.o: ../../e_os.h ../../include/openssl/asn1.h | ||
163 | dsa_vrf.o: ../../include/openssl/asn1_mac.h ../../include/openssl/bio.h | ||
164 | dsa_vrf.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
165 | dsa_vrf.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
166 | dsa_vrf.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
167 | dsa_vrf.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
168 | dsa_vrf.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h | ||
169 | dsa_vrf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
170 | dsa_vrf.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h | ||
171 | dsa_vrf.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
172 | dsa_vrf.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
173 | dsa_vrf.o: ../../include/openssl/ui.h ../cryptlib.h dsa_vrf.c | ||
diff --git a/src/lib/libcrypto/dsa/README b/src/lib/libcrypto/dsa/README new file mode 100644 index 0000000000..6a7e9c170a --- /dev/null +++ b/src/lib/libcrypto/dsa/README | |||
@@ -0,0 +1,4 @@ | |||
1 | The stuff in here is based on patches supplied to me by | ||
2 | Steven Schoch <schoch@sheba.arc.nasa.gov> to do DSS. | ||
3 | I have since modified a them a little but a debt of gratitude | ||
4 | is due for doing the initial work. | ||
diff --git a/src/lib/libcrypto/dsa/dsa.h b/src/lib/libcrypto/dsa/dsa.h new file mode 100644 index 0000000000..aa0669eb7a --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa.h | |||
@@ -0,0 +1,272 @@ | |||
1 | /* crypto/dsa/dsa.h */ | ||
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 | /* | ||
60 | * The DSS routines are based on patches supplied by | ||
61 | * Steven Schoch <schoch@sheba.arc.nasa.gov>. He basically did the | ||
62 | * work and I have just tweaked them a little to fit into my | ||
63 | * stylistic vision for SSLeay :-) */ | ||
64 | |||
65 | #ifndef HEADER_DSA_H | ||
66 | #define HEADER_DSA_H | ||
67 | |||
68 | #ifdef OPENSSL_NO_DSA | ||
69 | #error DSA is disabled. | ||
70 | #endif | ||
71 | |||
72 | #ifndef OPENSSL_NO_BIO | ||
73 | #include <openssl/bio.h> | ||
74 | #endif | ||
75 | #include <openssl/bn.h> | ||
76 | #include <openssl/crypto.h> | ||
77 | #include <openssl/ossl_typ.h> | ||
78 | #ifndef OPENSSL_NO_DH | ||
79 | # include <openssl/dh.h> | ||
80 | #endif | ||
81 | |||
82 | #define OPENSSL_DSA_MAX_MODULUS_BITS 3072 | ||
83 | |||
84 | #define DSA_FLAG_CACHE_MONT_P 0x01 | ||
85 | #define DSA_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DSA | ||
86 | * implementation now uses constant time | ||
87 | * modular exponentiation for secret exponents | ||
88 | * by default. This flag causes the | ||
89 | * faster variable sliding window method to | ||
90 | * be used for all exponents. | ||
91 | */ | ||
92 | |||
93 | /* If this flag is set external DSA_METHOD callbacks are allowed in FIPS mode | ||
94 | * it is then the applications responsibility to ensure the external method | ||
95 | * is compliant. | ||
96 | */ | ||
97 | |||
98 | #define DSA_FLAG_FIPS_EXTERNAL_METHOD_ALLOW 0x04 | ||
99 | |||
100 | #if defined(OPENSSL_FIPS) | ||
101 | #define FIPS_DSA_SIZE_T int | ||
102 | #endif | ||
103 | |||
104 | #ifdef __cplusplus | ||
105 | extern "C" { | ||
106 | #endif | ||
107 | |||
108 | typedef struct dsa_st DSA; | ||
109 | |||
110 | typedef struct DSA_SIG_st | ||
111 | { | ||
112 | BIGNUM *r; | ||
113 | BIGNUM *s; | ||
114 | } DSA_SIG; | ||
115 | |||
116 | typedef struct dsa_method { | ||
117 | const char *name; | ||
118 | DSA_SIG * (*dsa_do_sign)(const unsigned char *dgst, int dlen, DSA *dsa); | ||
119 | int (*dsa_sign_setup)(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, | ||
120 | BIGNUM **rp); | ||
121 | int (*dsa_do_verify)(const unsigned char *dgst, int dgst_len, | ||
122 | DSA_SIG *sig, DSA *dsa); | ||
123 | int (*dsa_mod_exp)(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, | ||
124 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, | ||
125 | BN_MONT_CTX *in_mont); | ||
126 | int (*bn_mod_exp)(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
127 | const BIGNUM *m, BN_CTX *ctx, | ||
128 | BN_MONT_CTX *m_ctx); /* Can be null */ | ||
129 | int (*init)(DSA *dsa); | ||
130 | int (*finish)(DSA *dsa); | ||
131 | int flags; | ||
132 | char *app_data; | ||
133 | } DSA_METHOD; | ||
134 | |||
135 | struct dsa_st | ||
136 | { | ||
137 | /* This first variable is used to pick up errors where | ||
138 | * a DSA is passed instead of of a EVP_PKEY */ | ||
139 | int pad; | ||
140 | long version; | ||
141 | int write_params; | ||
142 | BIGNUM *p; | ||
143 | BIGNUM *q; /* == 20 */ | ||
144 | BIGNUM *g; | ||
145 | |||
146 | BIGNUM *pub_key; /* y public key */ | ||
147 | BIGNUM *priv_key; /* x private key */ | ||
148 | |||
149 | BIGNUM *kinv; /* Signing pre-calc */ | ||
150 | BIGNUM *r; /* Signing pre-calc */ | ||
151 | |||
152 | int flags; | ||
153 | /* Normally used to cache montgomery values */ | ||
154 | char *method_mont_p; | ||
155 | int references; | ||
156 | CRYPTO_EX_DATA ex_data; | ||
157 | const DSA_METHOD *meth; | ||
158 | /* functional reference if 'meth' is ENGINE-provided */ | ||
159 | ENGINE *engine; | ||
160 | }; | ||
161 | |||
162 | #define DSAparams_dup(x) (DSA *)ASN1_dup((int (*)())i2d_DSAparams, \ | ||
163 | (char *(*)())d2i_DSAparams,(char *)(x)) | ||
164 | #define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \ | ||
165 | (char *(*)())d2i_DSAparams,(fp),(unsigned char **)(x)) | ||
166 | #define i2d_DSAparams_fp(fp,x) ASN1_i2d_fp(i2d_DSAparams,(fp), \ | ||
167 | (unsigned char *)(x)) | ||
168 | #define d2i_DSAparams_bio(bp,x) (DSA *)ASN1_d2i_bio((char *(*)())DSA_new, \ | ||
169 | (char *(*)())d2i_DSAparams,(bp),(unsigned char **)(x)) | ||
170 | #define i2d_DSAparams_bio(bp,x) ASN1_i2d_bio(i2d_DSAparams,(bp), \ | ||
171 | (unsigned char *)(x)) | ||
172 | |||
173 | |||
174 | DSA_SIG * DSA_SIG_new(void); | ||
175 | void DSA_SIG_free(DSA_SIG *a); | ||
176 | int i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp); | ||
177 | DSA_SIG * d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length); | ||
178 | |||
179 | DSA_SIG * DSA_do_sign(const unsigned char *dgst,int dlen,DSA *dsa); | ||
180 | int DSA_do_verify(const unsigned char *dgst,int dgst_len, | ||
181 | DSA_SIG *sig,DSA *dsa); | ||
182 | |||
183 | const DSA_METHOD *DSA_OpenSSL(void); | ||
184 | |||
185 | void DSA_set_default_method(const DSA_METHOD *); | ||
186 | const DSA_METHOD *DSA_get_default_method(void); | ||
187 | int DSA_set_method(DSA *dsa, const DSA_METHOD *); | ||
188 | |||
189 | DSA * DSA_new(void); | ||
190 | DSA * DSA_new_method(ENGINE *engine); | ||
191 | void DSA_free (DSA *r); | ||
192 | /* "up" the DSA object's reference count */ | ||
193 | int DSA_up_ref(DSA *r); | ||
194 | int DSA_size(const DSA *); | ||
195 | /* next 4 return -1 on error */ | ||
196 | int DSA_sign_setup( DSA *dsa,BN_CTX *ctx_in,BIGNUM **kinvp,BIGNUM **rp); | ||
197 | int DSA_sign(int type,const unsigned char *dgst,int dlen, | ||
198 | unsigned char *sig, unsigned int *siglen, DSA *dsa); | ||
199 | int DSA_verify(int type,const unsigned char *dgst,int dgst_len, | ||
200 | const unsigned char *sigbuf, int siglen, DSA *dsa); | ||
201 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
202 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
203 | int DSA_set_ex_data(DSA *d, int idx, void *arg); | ||
204 | void *DSA_get_ex_data(DSA *d, int idx); | ||
205 | |||
206 | DSA * d2i_DSAPublicKey(DSA **a, const unsigned char **pp, long length); | ||
207 | DSA * d2i_DSAPrivateKey(DSA **a, const unsigned char **pp, long length); | ||
208 | DSA * d2i_DSAparams(DSA **a, const unsigned char **pp, long length); | ||
209 | DSA * DSA_generate_parameters(int bits, | ||
210 | unsigned char *seed,int seed_len, | ||
211 | int *counter_ret, unsigned long *h_ret,void | ||
212 | (*callback)(int, int, void *),void *cb_arg); | ||
213 | int DSA_generate_key(DSA *a); | ||
214 | int i2d_DSAPublicKey(const DSA *a, unsigned char **pp); | ||
215 | int i2d_DSAPrivateKey(const DSA *a, unsigned char **pp); | ||
216 | int i2d_DSAparams(const DSA *a,unsigned char **pp); | ||
217 | |||
218 | #ifndef OPENSSL_NO_BIO | ||
219 | int DSAparams_print(BIO *bp, const DSA *x); | ||
220 | int DSA_print(BIO *bp, const DSA *x, int off); | ||
221 | #endif | ||
222 | #ifndef OPENSSL_NO_FP_API | ||
223 | int DSAparams_print_fp(FILE *fp, const DSA *x); | ||
224 | int DSA_print_fp(FILE *bp, const DSA *x, int off); | ||
225 | #endif | ||
226 | |||
227 | #define DSS_prime_checks 50 | ||
228 | /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: | ||
229 | * 50 rounds of Rabin-Miller */ | ||
230 | #define DSA_is_prime(n, callback, cb_arg) \ | ||
231 | BN_is_prime(n, DSS_prime_checks, callback, NULL, cb_arg) | ||
232 | |||
233 | #ifndef OPENSSL_NO_DH | ||
234 | /* Convert DSA structure (key or just parameters) into DH structure | ||
235 | * (be careful to avoid small subgroup attacks when using this!) */ | ||
236 | DH *DSA_dup_DH(const DSA *r); | ||
237 | #endif | ||
238 | |||
239 | /* BEGIN ERROR CODES */ | ||
240 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
241 | * made after this point may be overwritten when the script is next run. | ||
242 | */ | ||
243 | void ERR_load_DSA_strings(void); | ||
244 | |||
245 | /* Error codes for the DSA functions. */ | ||
246 | |||
247 | /* Function codes. */ | ||
248 | #define DSA_F_D2I_DSA_SIG 110 | ||
249 | #define DSA_F_DSAPARAMS_PRINT 100 | ||
250 | #define DSA_F_DSAPARAMS_PRINT_FP 101 | ||
251 | #define DSA_F_DSA_DO_SIGN 112 | ||
252 | #define DSA_F_DSA_DO_VERIFY 113 | ||
253 | #define DSA_F_DSA_NEW_METHOD 103 | ||
254 | #define DSA_F_DSA_PRINT 104 | ||
255 | #define DSA_F_DSA_PRINT_FP 105 | ||
256 | #define DSA_F_DSA_SIGN 106 | ||
257 | #define DSA_F_DSA_SIGN_SETUP 107 | ||
258 | #define DSA_F_DSA_SIG_NEW 109 | ||
259 | #define DSA_F_DSA_VERIFY 108 | ||
260 | #define DSA_F_I2D_DSA_SIG 111 | ||
261 | #define DSA_F_SIG_CB 114 | ||
262 | |||
263 | /* Reason codes. */ | ||
264 | #define DSA_R_BAD_Q_VALUE 102 | ||
265 | #define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 100 | ||
266 | #define DSA_R_MISSING_PARAMETERS 101 | ||
267 | #define DSA_R_MODULUS_TOO_LARGE 103 | ||
268 | |||
269 | #ifdef __cplusplus | ||
270 | } | ||
271 | #endif | ||
272 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_asn1.c b/src/lib/libcrypto/dsa/dsa_asn1.c new file mode 100644 index 0000000000..23fce555aa --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_asn1.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* dsa_asn1.c */ | ||
2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
3 | * project 2000. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in | ||
17 | * the documentation and/or other materials provided with the | ||
18 | * distribution. | ||
19 | * | ||
20 | * 3. All advertising materials mentioning features or use of this | ||
21 | * software must display the following acknowledgment: | ||
22 | * "This product includes software developed by the OpenSSL Project | ||
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
24 | * | ||
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
26 | * endorse or promote products derived from this software without | ||
27 | * prior written permission. For written permission, please contact | ||
28 | * licensing@OpenSSL.org. | ||
29 | * | ||
30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
31 | * nor may "OpenSSL" appear in their names without prior written | ||
32 | * permission of the OpenSSL Project. | ||
33 | * | ||
34 | * 6. Redistributions of any form whatsoever must retain the following | ||
35 | * acknowledgment: | ||
36 | * "This product includes software developed by the OpenSSL Project | ||
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * ==================================================================== | ||
52 | * | ||
53 | * This product includes cryptographic software written by Eric Young | ||
54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
55 | * Hudson (tjh@cryptsoft.com). | ||
56 | * | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include "cryptlib.h" | ||
61 | #include <openssl/dsa.h> | ||
62 | #include <openssl/asn1.h> | ||
63 | #include <openssl/asn1t.h> | ||
64 | |||
65 | /* Override the default new methods */ | ||
66 | static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
67 | { | ||
68 | if(operation == ASN1_OP_NEW_PRE) { | ||
69 | DSA_SIG *sig; | ||
70 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); | ||
71 | sig->r = NULL; | ||
72 | sig->s = NULL; | ||
73 | *pval = (ASN1_VALUE *)sig; | ||
74 | if(sig) return 2; | ||
75 | DSAerr(DSA_F_SIG_CB, ERR_R_MALLOC_FAILURE); | ||
76 | return 0; | ||
77 | } | ||
78 | return 1; | ||
79 | } | ||
80 | |||
81 | ASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = { | ||
82 | ASN1_SIMPLE(DSA_SIG, r, CBIGNUM), | ||
83 | ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) | ||
84 | } ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG) | ||
85 | |||
86 | IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG) | ||
87 | |||
88 | /* Override the default free and new methods */ | ||
89 | static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
90 | { | ||
91 | if(operation == ASN1_OP_NEW_PRE) { | ||
92 | *pval = (ASN1_VALUE *)DSA_new(); | ||
93 | if(*pval) return 2; | ||
94 | return 0; | ||
95 | } else if(operation == ASN1_OP_FREE_PRE) { | ||
96 | DSA_free((DSA *)*pval); | ||
97 | *pval = NULL; | ||
98 | return 2; | ||
99 | } | ||
100 | return 1; | ||
101 | } | ||
102 | |||
103 | ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = { | ||
104 | ASN1_SIMPLE(DSA, version, LONG), | ||
105 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
106 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
107 | ASN1_SIMPLE(DSA, g, BIGNUM), | ||
108 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
109 | ASN1_SIMPLE(DSA, priv_key, BIGNUM) | ||
110 | } ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey) | ||
111 | |||
112 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey) | ||
113 | |||
114 | ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = { | ||
115 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
116 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
117 | ASN1_SIMPLE(DSA, g, BIGNUM), | ||
118 | } ASN1_SEQUENCE_END_cb(DSA, DSAparams) | ||
119 | |||
120 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams) | ||
121 | |||
122 | /* DSA public key is a bit trickier... its effectively a CHOICE type | ||
123 | * decided by a field called write_params which can either write out | ||
124 | * just the public key as an INTEGER or the parameters and public key | ||
125 | * in a SEQUENCE | ||
126 | */ | ||
127 | |||
128 | ASN1_SEQUENCE(dsa_pub_internal) = { | ||
129 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
130 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
131 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
132 | ASN1_SIMPLE(DSA, g, BIGNUM) | ||
133 | } ASN1_SEQUENCE_END_name(DSA, dsa_pub_internal) | ||
134 | |||
135 | ASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = { | ||
136 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
137 | ASN1_EX_COMBINE(0, 0, dsa_pub_internal) | ||
138 | } ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params) | ||
139 | |||
140 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey) | ||
diff --git a/src/lib/libcrypto/dsa/dsa_err.c b/src/lib/libcrypto/dsa/dsa_err.c new file mode 100644 index 0000000000..d7fac69154 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_err.c | |||
@@ -0,0 +1,114 @@ | |||
1 | /* crypto/dsa/dsa_err.c */ | ||
2 | /* ==================================================================== | ||
3 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in | ||
14 | * the documentation and/or other materials provided with the | ||
15 | * distribution. | ||
16 | * | ||
17 | * 3. All advertising materials mentioning features or use of this | ||
18 | * software must display the following acknowledgment: | ||
19 | * "This product includes software developed by the OpenSSL Project | ||
20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
21 | * | ||
22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
23 | * endorse or promote products derived from this software without | ||
24 | * prior written permission. For written permission, please contact | ||
25 | * openssl-core@OpenSSL.org. | ||
26 | * | ||
27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
28 | * nor may "OpenSSL" appear in their names without prior written | ||
29 | * permission of the OpenSSL Project. | ||
30 | * | ||
31 | * 6. Redistributions of any form whatsoever must retain the following | ||
32 | * acknowledgment: | ||
33 | * "This product includes software developed by the OpenSSL Project | ||
34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
35 | * | ||
36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
48 | * ==================================================================== | ||
49 | * | ||
50 | * This product includes cryptographic software written by Eric Young | ||
51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
52 | * Hudson (tjh@cryptsoft.com). | ||
53 | * | ||
54 | */ | ||
55 | |||
56 | /* NOTE: this file was auto generated by the mkerr.pl script: any changes | ||
57 | * made to it will be overwritten when the script next updates this file, | ||
58 | * only reason strings will be preserved. | ||
59 | */ | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <openssl/err.h> | ||
63 | #include <openssl/dsa.h> | ||
64 | |||
65 | /* BEGIN ERROR CODES */ | ||
66 | #ifndef OPENSSL_NO_ERR | ||
67 | |||
68 | #define ERR_FUNC(func) ERR_PACK(ERR_LIB_DSA,func,0) | ||
69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_DSA,0,reason) | ||
70 | |||
71 | static ERR_STRING_DATA DSA_str_functs[]= | ||
72 | { | ||
73 | {ERR_FUNC(DSA_F_D2I_DSA_SIG), "d2i_DSA_SIG"}, | ||
74 | {ERR_FUNC(DSA_F_DSAPARAMS_PRINT), "DSAparams_print"}, | ||
75 | {ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"}, | ||
76 | {ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"}, | ||
77 | {ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"}, | ||
78 | {ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"}, | ||
79 | {ERR_FUNC(DSA_F_DSA_PRINT), "DSA_print"}, | ||
80 | {ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"}, | ||
81 | {ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"}, | ||
82 | {ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"}, | ||
83 | {ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"}, | ||
84 | {ERR_FUNC(DSA_F_DSA_VERIFY), "DSA_verify"}, | ||
85 | {ERR_FUNC(DSA_F_I2D_DSA_SIG), "i2d_DSA_SIG"}, | ||
86 | {ERR_FUNC(DSA_F_SIG_CB), "SIG_CB"}, | ||
87 | {0,NULL} | ||
88 | }; | ||
89 | |||
90 | static ERR_STRING_DATA DSA_str_reasons[]= | ||
91 | { | ||
92 | {ERR_REASON(DSA_R_BAD_Q_VALUE) ,"bad q value"}, | ||
93 | {ERR_REASON(DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"}, | ||
94 | {ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"}, | ||
95 | {ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, | ||
96 | {0,NULL} | ||
97 | }; | ||
98 | |||
99 | #endif | ||
100 | |||
101 | void ERR_load_DSA_strings(void) | ||
102 | { | ||
103 | static int init=1; | ||
104 | |||
105 | if (init) | ||
106 | { | ||
107 | init=0; | ||
108 | #ifndef OPENSSL_NO_ERR | ||
109 | ERR_load_strings(0,DSA_str_functs); | ||
110 | ERR_load_strings(0,DSA_str_reasons); | ||
111 | #endif | ||
112 | |||
113 | } | ||
114 | } | ||
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c new file mode 100644 index 0000000000..e40afeea51 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
@@ -0,0 +1,305 @@ | |||
1 | /* crypto/dsa/dsa_gen.c */ | ||
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 | #undef GENUINE_DSA | ||
60 | |||
61 | #ifdef GENUINE_DSA | ||
62 | /* Parameter generation follows the original release of FIPS PUB 186, | ||
63 | * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180) */ | ||
64 | #define HASH EVP_sha() | ||
65 | #else | ||
66 | /* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186, | ||
67 | * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in | ||
68 | * FIPS PUB 180-1) */ | ||
69 | #define HASH EVP_sha1() | ||
70 | #endif | ||
71 | |||
72 | #ifndef OPENSSL_NO_SHA | ||
73 | |||
74 | #include <stdio.h> | ||
75 | #include <time.h> | ||
76 | #include "cryptlib.h" | ||
77 | #include <openssl/evp.h> | ||
78 | #include <openssl/bn.h> | ||
79 | #include <openssl/dsa.h> | ||
80 | #include <openssl/rand.h> | ||
81 | #include <openssl/sha.h> | ||
82 | |||
83 | #ifndef OPENSSL_FIPS | ||
84 | DSA *DSA_generate_parameters(int bits, | ||
85 | unsigned char *seed_in, int seed_len, | ||
86 | int *counter_ret, unsigned long *h_ret, | ||
87 | void (*callback)(int, int, void *), | ||
88 | void *cb_arg) | ||
89 | { | ||
90 | int ok=0; | ||
91 | unsigned char seed[SHA_DIGEST_LENGTH]; | ||
92 | unsigned char md[SHA_DIGEST_LENGTH]; | ||
93 | unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; | ||
94 | BIGNUM *r0,*W,*X,*c,*test; | ||
95 | BIGNUM *g=NULL,*q=NULL,*p=NULL; | ||
96 | BN_MONT_CTX *mont=NULL; | ||
97 | int k,n=0,i,b,m=0; | ||
98 | int counter=0; | ||
99 | int r=0; | ||
100 | BN_CTX *ctx=NULL,*ctx2=NULL,*ctx3=NULL; | ||
101 | unsigned int h=2; | ||
102 | DSA *ret=NULL; | ||
103 | |||
104 | if (bits < 512) bits=512; | ||
105 | bits=(bits+63)/64*64; | ||
106 | |||
107 | if (seed_len < 20) | ||
108 | seed_in = NULL; /* seed buffer too small -- ignore */ | ||
109 | if (seed_len > 20) | ||
110 | seed_len = 20; /* App. 2.2 of FIPS PUB 186 allows larger SEED, | ||
111 | * but our internal buffers are restricted to 160 bits*/ | ||
112 | if ((seed_in != NULL) && (seed_len == 20)) | ||
113 | memcpy(seed,seed_in,seed_len); | ||
114 | |||
115 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
116 | if ((ctx2=BN_CTX_new()) == NULL) goto err; | ||
117 | if ((ctx3=BN_CTX_new()) == NULL) goto err; | ||
118 | if ((ret=DSA_new()) == NULL) goto err; | ||
119 | |||
120 | if ((mont=BN_MONT_CTX_new()) == NULL) goto err; | ||
121 | |||
122 | BN_CTX_start(ctx2); | ||
123 | r0 = BN_CTX_get(ctx2); | ||
124 | g = BN_CTX_get(ctx2); | ||
125 | W = BN_CTX_get(ctx2); | ||
126 | q = BN_CTX_get(ctx2); | ||
127 | X = BN_CTX_get(ctx2); | ||
128 | c = BN_CTX_get(ctx2); | ||
129 | p = BN_CTX_get(ctx2); | ||
130 | test = BN_CTX_get(ctx2); | ||
131 | if (test == NULL) goto err; | ||
132 | |||
133 | if (!BN_lshift(test,BN_value_one(),bits-1)) goto err; | ||
134 | |||
135 | for (;;) | ||
136 | { | ||
137 | for (;;) /* find q */ | ||
138 | { | ||
139 | int seed_is_random; | ||
140 | |||
141 | /* step 1 */ | ||
142 | if (callback != NULL) callback(0,m++,cb_arg); | ||
143 | |||
144 | if (!seed_len) | ||
145 | { | ||
146 | RAND_pseudo_bytes(seed,SHA_DIGEST_LENGTH); | ||
147 | seed_is_random = 1; | ||
148 | } | ||
149 | else | ||
150 | { | ||
151 | seed_is_random = 0; | ||
152 | seed_len=0; /* use random seed if 'seed_in' turns out to be bad*/ | ||
153 | } | ||
154 | memcpy(buf,seed,SHA_DIGEST_LENGTH); | ||
155 | memcpy(buf2,seed,SHA_DIGEST_LENGTH); | ||
156 | /* precompute "SEED + 1" for step 7: */ | ||
157 | for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) | ||
158 | { | ||
159 | buf[i]++; | ||
160 | if (buf[i] != 0) break; | ||
161 | } | ||
162 | |||
163 | /* step 2 */ | ||
164 | EVP_Digest(seed,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); | ||
165 | EVP_Digest(buf,SHA_DIGEST_LENGTH,buf2,NULL,HASH, NULL); | ||
166 | for (i=0; i<SHA_DIGEST_LENGTH; i++) | ||
167 | md[i]^=buf2[i]; | ||
168 | |||
169 | /* step 3 */ | ||
170 | md[0]|=0x80; | ||
171 | md[SHA_DIGEST_LENGTH-1]|=0x01; | ||
172 | if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) goto err; | ||
173 | |||
174 | /* step 4 */ | ||
175 | r = BN_is_prime_fasttest(q, DSS_prime_checks, callback, ctx3, cb_arg, seed_is_random); | ||
176 | if (r > 0) | ||
177 | break; | ||
178 | if (r != 0) | ||
179 | goto err; | ||
180 | |||
181 | /* do a callback call */ | ||
182 | /* step 5 */ | ||
183 | } | ||
184 | |||
185 | if (callback != NULL) callback(2,0,cb_arg); | ||
186 | if (callback != NULL) callback(3,0,cb_arg); | ||
187 | |||
188 | /* step 6 */ | ||
189 | counter=0; | ||
190 | /* "offset = 2" */ | ||
191 | |||
192 | n=(bits-1)/160; | ||
193 | b=(bits-1)-n*160; | ||
194 | |||
195 | for (;;) | ||
196 | { | ||
197 | if (callback != NULL && counter != 0) | ||
198 | callback(0,counter,cb_arg); | ||
199 | |||
200 | /* step 7 */ | ||
201 | if (!BN_zero(W)) goto err; | ||
202 | /* now 'buf' contains "SEED + offset - 1" */ | ||
203 | for (k=0; k<=n; k++) | ||
204 | { | ||
205 | /* obtain "SEED + offset + k" by incrementing: */ | ||
206 | for (i=SHA_DIGEST_LENGTH-1; i >= 0; i--) | ||
207 | { | ||
208 | buf[i]++; | ||
209 | if (buf[i] != 0) break; | ||
210 | } | ||
211 | |||
212 | EVP_Digest(buf,SHA_DIGEST_LENGTH,md,NULL,HASH, NULL); | ||
213 | |||
214 | /* step 8 */ | ||
215 | if (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) | ||
216 | goto err; | ||
217 | if (!BN_lshift(r0,r0,160*k)) goto err; | ||
218 | if (!BN_add(W,W,r0)) goto err; | ||
219 | } | ||
220 | |||
221 | /* more of step 8 */ | ||
222 | if (!BN_mask_bits(W,bits-1)) goto err; | ||
223 | if (!BN_copy(X,W)) goto err; | ||
224 | if (!BN_add(X,X,test)) goto err; | ||
225 | |||
226 | /* step 9 */ | ||
227 | if (!BN_lshift1(r0,q)) goto err; | ||
228 | if (!BN_mod(c,X,r0,ctx)) goto err; | ||
229 | if (!BN_sub(r0,c,BN_value_one())) goto err; | ||
230 | if (!BN_sub(p,X,r0)) goto err; | ||
231 | |||
232 | /* step 10 */ | ||
233 | if (BN_cmp(p,test) >= 0) | ||
234 | { | ||
235 | /* step 11 */ | ||
236 | r = BN_is_prime_fasttest(p, DSS_prime_checks, callback, ctx3, cb_arg, 1); | ||
237 | if (r > 0) | ||
238 | goto end; /* found it */ | ||
239 | if (r != 0) | ||
240 | goto err; | ||
241 | } | ||
242 | |||
243 | /* step 13 */ | ||
244 | counter++; | ||
245 | /* "offset = offset + n + 1" */ | ||
246 | |||
247 | /* step 14 */ | ||
248 | if (counter >= 4096) break; | ||
249 | } | ||
250 | } | ||
251 | end: | ||
252 | if (callback != NULL) callback(2,1,cb_arg); | ||
253 | |||
254 | /* We now need to generate g */ | ||
255 | /* Set r0=(p-1)/q */ | ||
256 | if (!BN_sub(test,p,BN_value_one())) goto err; | ||
257 | if (!BN_div(r0,NULL,test,q,ctx)) goto err; | ||
258 | |||
259 | if (!BN_set_word(test,h)) goto err; | ||
260 | if (!BN_MONT_CTX_set(mont,p,ctx)) goto err; | ||
261 | |||
262 | for (;;) | ||
263 | { | ||
264 | /* g=test^r0%p */ | ||
265 | if (!BN_mod_exp_mont(g,test,r0,p,ctx,mont)) goto err; | ||
266 | if (!BN_is_one(g)) break; | ||
267 | if (!BN_add(test,test,BN_value_one())) goto err; | ||
268 | h++; | ||
269 | } | ||
270 | |||
271 | if (callback != NULL) callback(3,1,cb_arg); | ||
272 | |||
273 | ok=1; | ||
274 | err: | ||
275 | if (!ok) | ||
276 | { | ||
277 | if (ret != NULL) DSA_free(ret); | ||
278 | } | ||
279 | else | ||
280 | { | ||
281 | ret->p=BN_dup(p); | ||
282 | ret->q=BN_dup(q); | ||
283 | ret->g=BN_dup(g); | ||
284 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) | ||
285 | { | ||
286 | ok=0; | ||
287 | goto err; | ||
288 | } | ||
289 | if ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20); | ||
290 | if (counter_ret != NULL) *counter_ret=counter; | ||
291 | if (h_ret != NULL) *h_ret=h; | ||
292 | } | ||
293 | if (ctx != NULL) BN_CTX_free(ctx); | ||
294 | if (ctx2 != NULL) | ||
295 | { | ||
296 | BN_CTX_end(ctx2); | ||
297 | BN_CTX_free(ctx2); | ||
298 | } | ||
299 | if (ctx3 != NULL) BN_CTX_free(ctx3); | ||
300 | if (mont != NULL) BN_MONT_CTX_free(mont); | ||
301 | return(ok?ret:NULL); | ||
302 | } | ||
303 | #endif /* ndef OPENSSL_FIPS */ | ||
304 | #endif /* ndef OPENSSL_NO_SHA */ | ||
305 | |||
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c new file mode 100644 index 0000000000..980b6dc2d3 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_key.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* crypto/dsa/dsa_key.c */ | ||
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 | #ifndef OPENSSL_NO_SHA | ||
60 | #include <stdio.h> | ||
61 | #include <time.h> | ||
62 | #include "cryptlib.h" | ||
63 | #include <openssl/bn.h> | ||
64 | #include <openssl/dsa.h> | ||
65 | #include <openssl/rand.h> | ||
66 | |||
67 | #ifndef OPENSSL_FIPS | ||
68 | int DSA_generate_key(DSA *dsa) | ||
69 | { | ||
70 | int ok=0; | ||
71 | BN_CTX *ctx=NULL; | ||
72 | BIGNUM *pub_key=NULL,*priv_key=NULL; | ||
73 | |||
74 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
75 | |||
76 | if (dsa->priv_key == NULL) | ||
77 | { | ||
78 | if ((priv_key=BN_new()) == NULL) goto err; | ||
79 | } | ||
80 | else | ||
81 | priv_key=dsa->priv_key; | ||
82 | |||
83 | do | ||
84 | if (!BN_rand_range(priv_key,dsa->q)) goto err; | ||
85 | while (BN_is_zero(priv_key)); | ||
86 | |||
87 | if (dsa->pub_key == NULL) | ||
88 | { | ||
89 | if ((pub_key=BN_new()) == NULL) goto err; | ||
90 | } | ||
91 | else | ||
92 | pub_key=dsa->pub_key; | ||
93 | |||
94 | { | ||
95 | BIGNUM local_prk; | ||
96 | BIGNUM *prk; | ||
97 | |||
98 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
99 | { | ||
100 | BN_init(&local_prk); | ||
101 | prk = &local_prk; | ||
102 | BN_with_flags(prk, priv_key, BN_FLG_EXP_CONSTTIME); | ||
103 | } | ||
104 | else | ||
105 | prk = priv_key; | ||
106 | |||
107 | if (!BN_mod_exp(pub_key,dsa->g,prk,dsa->p,ctx)) goto err; | ||
108 | } | ||
109 | |||
110 | dsa->priv_key=priv_key; | ||
111 | dsa->pub_key=pub_key; | ||
112 | ok=1; | ||
113 | |||
114 | err: | ||
115 | if ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key); | ||
116 | if ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key); | ||
117 | if (ctx != NULL) BN_CTX_free(ctx); | ||
118 | return(ok); | ||
119 | } | ||
120 | #endif | ||
121 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c new file mode 100644 index 0000000000..4171af24c6 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_lib.c | |||
@@ -0,0 +1,308 @@ | |||
1 | /* crypto/dsa/dsa_lib.c */ | ||
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 | #include "cryptlib.h" | ||
63 | #include <openssl/bn.h> | ||
64 | #include <openssl/dsa.h> | ||
65 | #include <openssl/asn1.h> | ||
66 | #ifndef OPENSSL_NO_ENGINE | ||
67 | #include <openssl/engine.h> | ||
68 | #endif | ||
69 | |||
70 | const char *DSA_version="DSA" OPENSSL_VERSION_PTEXT; | ||
71 | |||
72 | static const DSA_METHOD *default_DSA_method = NULL; | ||
73 | |||
74 | void DSA_set_default_method(const DSA_METHOD *meth) | ||
75 | { | ||
76 | default_DSA_method = meth; | ||
77 | } | ||
78 | |||
79 | const DSA_METHOD *DSA_get_default_method(void) | ||
80 | { | ||
81 | if(!default_DSA_method) | ||
82 | default_DSA_method = DSA_OpenSSL(); | ||
83 | return default_DSA_method; | ||
84 | } | ||
85 | |||
86 | DSA *DSA_new(void) | ||
87 | { | ||
88 | return DSA_new_method(NULL); | ||
89 | } | ||
90 | |||
91 | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) | ||
92 | { | ||
93 | /* NB: The caller is specifically setting a method, so it's not up to us | ||
94 | * to deal with which ENGINE it comes from. */ | ||
95 | const DSA_METHOD *mtmp; | ||
96 | mtmp = dsa->meth; | ||
97 | if (mtmp->finish) mtmp->finish(dsa); | ||
98 | #ifndef OPENSSL_NO_ENGINE | ||
99 | if (dsa->engine) | ||
100 | { | ||
101 | ENGINE_finish(dsa->engine); | ||
102 | dsa->engine = NULL; | ||
103 | } | ||
104 | #endif | ||
105 | dsa->meth = meth; | ||
106 | if (meth->init) meth->init(dsa); | ||
107 | return 1; | ||
108 | } | ||
109 | |||
110 | DSA *DSA_new_method(ENGINE *engine) | ||
111 | { | ||
112 | DSA *ret; | ||
113 | |||
114 | ret=(DSA *)OPENSSL_malloc(sizeof(DSA)); | ||
115 | if (ret == NULL) | ||
116 | { | ||
117 | DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); | ||
118 | return(NULL); | ||
119 | } | ||
120 | ret->meth = DSA_get_default_method(); | ||
121 | #ifndef OPENSSL_NO_ENGINE | ||
122 | if (engine) | ||
123 | { | ||
124 | if (!ENGINE_init(engine)) | ||
125 | { | ||
126 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | ||
127 | OPENSSL_free(ret); | ||
128 | return NULL; | ||
129 | } | ||
130 | ret->engine = engine; | ||
131 | } | ||
132 | else | ||
133 | ret->engine = ENGINE_get_default_DSA(); | ||
134 | if(ret->engine) | ||
135 | { | ||
136 | ret->meth = ENGINE_get_DSA(ret->engine); | ||
137 | if(!ret->meth) | ||
138 | { | ||
139 | DSAerr(DSA_F_DSA_NEW_METHOD, | ||
140 | ERR_R_ENGINE_LIB); | ||
141 | ENGINE_finish(ret->engine); | ||
142 | OPENSSL_free(ret); | ||
143 | return NULL; | ||
144 | } | ||
145 | } | ||
146 | #endif | ||
147 | |||
148 | ret->pad=0; | ||
149 | ret->version=0; | ||
150 | ret->write_params=1; | ||
151 | ret->p=NULL; | ||
152 | ret->q=NULL; | ||
153 | ret->g=NULL; | ||
154 | |||
155 | ret->pub_key=NULL; | ||
156 | ret->priv_key=NULL; | ||
157 | |||
158 | ret->kinv=NULL; | ||
159 | ret->r=NULL; | ||
160 | ret->method_mont_p=NULL; | ||
161 | |||
162 | ret->references=1; | ||
163 | ret->flags=ret->meth->flags; | ||
164 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
165 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
166 | { | ||
167 | #ifndef OPENSSL_NO_ENGINE | ||
168 | if (ret->engine) | ||
169 | ENGINE_finish(ret->engine); | ||
170 | #endif | ||
171 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
172 | OPENSSL_free(ret); | ||
173 | ret=NULL; | ||
174 | } | ||
175 | |||
176 | return(ret); | ||
177 | } | ||
178 | |||
179 | void DSA_free(DSA *r) | ||
180 | { | ||
181 | int i; | ||
182 | |||
183 | if (r == NULL) return; | ||
184 | |||
185 | i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); | ||
186 | #ifdef REF_PRINT | ||
187 | REF_PRINT("DSA",r); | ||
188 | #endif | ||
189 | if (i > 0) return; | ||
190 | #ifdef REF_CHECK | ||
191 | if (i < 0) | ||
192 | { | ||
193 | fprintf(stderr,"DSA_free, bad reference count\n"); | ||
194 | abort(); | ||
195 | } | ||
196 | #endif | ||
197 | |||
198 | if(r->meth->finish) | ||
199 | r->meth->finish(r); | ||
200 | #ifndef OPENSSL_NO_ENGINE | ||
201 | if(r->engine) | ||
202 | ENGINE_finish(r->engine); | ||
203 | #endif | ||
204 | |||
205 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); | ||
206 | |||
207 | if (r->p != NULL) BN_clear_free(r->p); | ||
208 | if (r->q != NULL) BN_clear_free(r->q); | ||
209 | if (r->g != NULL) BN_clear_free(r->g); | ||
210 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | ||
211 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | ||
212 | if (r->kinv != NULL) BN_clear_free(r->kinv); | ||
213 | if (r->r != NULL) BN_clear_free(r->r); | ||
214 | OPENSSL_free(r); | ||
215 | } | ||
216 | |||
217 | int DSA_up_ref(DSA *r) | ||
218 | { | ||
219 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); | ||
220 | #ifdef REF_PRINT | ||
221 | REF_PRINT("DSA",r); | ||
222 | #endif | ||
223 | #ifdef REF_CHECK | ||
224 | if (i < 2) | ||
225 | { | ||
226 | fprintf(stderr, "DSA_up_ref, bad reference count\n"); | ||
227 | abort(); | ||
228 | } | ||
229 | #endif | ||
230 | return ((i > 1) ? 1 : 0); | ||
231 | } | ||
232 | |||
233 | int DSA_size(const DSA *r) | ||
234 | { | ||
235 | int ret,i; | ||
236 | ASN1_INTEGER bs; | ||
237 | unsigned char buf[4]; /* 4 bytes looks really small. | ||
238 | However, i2d_ASN1_INTEGER() will not look | ||
239 | beyond the first byte, as long as the second | ||
240 | parameter is NULL. */ | ||
241 | |||
242 | i=BN_num_bits(r->q); | ||
243 | bs.length=(i+7)/8; | ||
244 | bs.data=buf; | ||
245 | bs.type=V_ASN1_INTEGER; | ||
246 | /* If the top bit is set the asn1 encoding is 1 larger. */ | ||
247 | buf[0]=0xff; | ||
248 | |||
249 | i=i2d_ASN1_INTEGER(&bs,NULL); | ||
250 | i+=i; /* r and s */ | ||
251 | ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); | ||
252 | return(ret); | ||
253 | } | ||
254 | |||
255 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
256 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
257 | { | ||
258 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, | ||
259 | new_func, dup_func, free_func); | ||
260 | } | ||
261 | |||
262 | int DSA_set_ex_data(DSA *d, int idx, void *arg) | ||
263 | { | ||
264 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | ||
265 | } | ||
266 | |||
267 | void *DSA_get_ex_data(DSA *d, int idx) | ||
268 | { | ||
269 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | ||
270 | } | ||
271 | |||
272 | #ifndef OPENSSL_NO_DH | ||
273 | DH *DSA_dup_DH(const DSA *r) | ||
274 | { | ||
275 | /* DSA has p, q, g, optional pub_key, optional priv_key. | ||
276 | * DH has p, optional length, g, optional pub_key, optional priv_key. | ||
277 | */ | ||
278 | |||
279 | DH *ret = NULL; | ||
280 | |||
281 | if (r == NULL) | ||
282 | goto err; | ||
283 | ret = DH_new(); | ||
284 | if (ret == NULL) | ||
285 | goto err; | ||
286 | if (r->p != NULL) | ||
287 | if ((ret->p = BN_dup(r->p)) == NULL) | ||
288 | goto err; | ||
289 | if (r->q != NULL) | ||
290 | ret->length = BN_num_bits(r->q); | ||
291 | if (r->g != NULL) | ||
292 | if ((ret->g = BN_dup(r->g)) == NULL) | ||
293 | goto err; | ||
294 | if (r->pub_key != NULL) | ||
295 | if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) | ||
296 | goto err; | ||
297 | if (r->priv_key != NULL) | ||
298 | if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) | ||
299 | goto err; | ||
300 | |||
301 | return ret; | ||
302 | |||
303 | err: | ||
304 | if (ret != NULL) | ||
305 | DH_free(ret); | ||
306 | return NULL; | ||
307 | } | ||
308 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c new file mode 100644 index 0000000000..5de5fc7e91 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_ossl.c | |||
@@ -0,0 +1,393 @@ | |||
1 | /* crypto/dsa/dsa_ossl.c */ | ||
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 | #include "cryptlib.h" | ||
63 | #include <openssl/bn.h> | ||
64 | #include <openssl/dsa.h> | ||
65 | #include <openssl/rand.h> | ||
66 | #include <openssl/asn1.h> | ||
67 | |||
68 | #ifndef OPENSSL_FIPS | ||
69 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); | ||
70 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp); | ||
71 | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
72 | DSA *dsa); | ||
73 | static int dsa_init(DSA *dsa); | ||
74 | static int dsa_finish(DSA *dsa); | ||
75 | static int dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, | ||
76 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, | ||
77 | BN_MONT_CTX *in_mont); | ||
78 | static int dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
79 | const BIGNUM *m, BN_CTX *ctx, | ||
80 | BN_MONT_CTX *m_ctx); | ||
81 | |||
82 | static DSA_METHOD openssl_dsa_meth = { | ||
83 | "OpenSSL DSA method", | ||
84 | dsa_do_sign, | ||
85 | dsa_sign_setup, | ||
86 | dsa_do_verify, | ||
87 | dsa_mod_exp, | ||
88 | dsa_bn_mod_exp, | ||
89 | dsa_init, | ||
90 | dsa_finish, | ||
91 | 0, | ||
92 | NULL | ||
93 | }; | ||
94 | |||
95 | const DSA_METHOD *DSA_OpenSSL(void) | ||
96 | { | ||
97 | return &openssl_dsa_meth; | ||
98 | } | ||
99 | |||
100 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | ||
101 | { | ||
102 | BIGNUM *kinv=NULL,*r=NULL,*s=NULL; | ||
103 | BIGNUM m; | ||
104 | BIGNUM xr; | ||
105 | BN_CTX *ctx=NULL; | ||
106 | int i,reason=ERR_R_BN_LIB; | ||
107 | DSA_SIG *ret=NULL; | ||
108 | |||
109 | BN_init(&m); | ||
110 | BN_init(&xr); | ||
111 | |||
112 | if (!dsa->p || !dsa->q || !dsa->g) | ||
113 | { | ||
114 | reason=DSA_R_MISSING_PARAMETERS; | ||
115 | goto err; | ||
116 | } | ||
117 | |||
118 | s=BN_new(); | ||
119 | if (s == NULL) goto err; | ||
120 | |||
121 | i=BN_num_bytes(dsa->q); /* should be 20 */ | ||
122 | if ((dlen > i) || (dlen > 50)) | ||
123 | { | ||
124 | reason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE; | ||
125 | goto err; | ||
126 | } | ||
127 | |||
128 | ctx=BN_CTX_new(); | ||
129 | if (ctx == NULL) goto err; | ||
130 | |||
131 | if ((dsa->kinv == NULL) || (dsa->r == NULL)) | ||
132 | { | ||
133 | if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err; | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | kinv=dsa->kinv; | ||
138 | dsa->kinv=NULL; | ||
139 | r=dsa->r; | ||
140 | dsa->r=NULL; | ||
141 | } | ||
142 | |||
143 | if (BN_bin2bn(dgst,dlen,&m) == NULL) goto err; | ||
144 | |||
145 | /* Compute s = inv(k) (m + xr) mod q */ | ||
146 | if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */ | ||
147 | if (!BN_add(s, &xr, &m)) goto err; /* s = m + xr */ | ||
148 | if (BN_cmp(s,dsa->q) > 0) | ||
149 | BN_sub(s,s,dsa->q); | ||
150 | if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err; | ||
151 | |||
152 | ret=DSA_SIG_new(); | ||
153 | if (ret == NULL) goto err; | ||
154 | ret->r = r; | ||
155 | ret->s = s; | ||
156 | |||
157 | err: | ||
158 | if (!ret) | ||
159 | { | ||
160 | DSAerr(DSA_F_DSA_DO_SIGN,reason); | ||
161 | BN_free(r); | ||
162 | BN_free(s); | ||
163 | } | ||
164 | if (ctx != NULL) BN_CTX_free(ctx); | ||
165 | BN_clear_free(&m); | ||
166 | BN_clear_free(&xr); | ||
167 | if (kinv != NULL) /* dsa->kinv is NULL now if we used it */ | ||
168 | BN_clear_free(kinv); | ||
169 | return(ret); | ||
170 | } | ||
171 | |||
172 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | ||
173 | { | ||
174 | BN_CTX *ctx; | ||
175 | BIGNUM k,kq,*K,*kinv=NULL,*r=NULL; | ||
176 | int ret=0; | ||
177 | |||
178 | if (!dsa->p || !dsa->q || !dsa->g) | ||
179 | { | ||
180 | DSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS); | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | BN_init(&k); | ||
185 | BN_init(&kq); | ||
186 | |||
187 | if (ctx_in == NULL) | ||
188 | { | ||
189 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
190 | } | ||
191 | else | ||
192 | ctx=ctx_in; | ||
193 | |||
194 | if ((r=BN_new()) == NULL) goto err; | ||
195 | |||
196 | /* Get random k */ | ||
197 | do | ||
198 | if (!BN_rand_range(&k, dsa->q)) goto err; | ||
199 | while (BN_is_zero(&k)); | ||
200 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
201 | { | ||
202 | BN_set_flags(&k, BN_FLG_EXP_CONSTTIME); | ||
203 | } | ||
204 | |||
205 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) | ||
206 | { | ||
207 | if (!BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p, | ||
208 | CRYPTO_LOCK_DSA, | ||
209 | dsa->p, ctx)) | ||
210 | goto err; | ||
211 | } | ||
212 | |||
213 | /* Compute r = (g^k mod p) mod q */ | ||
214 | |||
215 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
216 | { | ||
217 | if (!BN_copy(&kq, &k)) goto err; | ||
218 | |||
219 | /* We do not want timing information to leak the length of k, | ||
220 | * so we compute g^k using an equivalent exponent of fixed length. | ||
221 | * | ||
222 | * (This is a kludge that we need because the BN_mod_exp_mont() | ||
223 | * does not let us specify the desired timing behaviour.) */ | ||
224 | |||
225 | if (!BN_add(&kq, &kq, dsa->q)) goto err; | ||
226 | if (BN_num_bits(&kq) <= BN_num_bits(dsa->q)) | ||
227 | { | ||
228 | if (!BN_add(&kq, &kq, dsa->q)) goto err; | ||
229 | } | ||
230 | |||
231 | K = &kq; | ||
232 | } | ||
233 | else | ||
234 | { | ||
235 | K = &k; | ||
236 | } | ||
237 | if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,K,dsa->p,ctx, | ||
238 | (BN_MONT_CTX *)dsa->method_mont_p)) goto err; | ||
239 | if (!BN_mod(r,r,dsa->q,ctx)) goto err; | ||
240 | |||
241 | /* Compute part of 's = inv(k) (m + xr) mod q' */ | ||
242 | if ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err; | ||
243 | |||
244 | if (*kinvp != NULL) BN_clear_free(*kinvp); | ||
245 | *kinvp=kinv; | ||
246 | kinv=NULL; | ||
247 | if (*rp != NULL) BN_clear_free(*rp); | ||
248 | *rp=r; | ||
249 | ret=1; | ||
250 | err: | ||
251 | if (!ret) | ||
252 | { | ||
253 | DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB); | ||
254 | if (kinv != NULL) BN_clear_free(kinv); | ||
255 | if (r != NULL) BN_clear_free(r); | ||
256 | } | ||
257 | if (ctx_in == NULL) BN_CTX_free(ctx); | ||
258 | if (kinv != NULL) BN_clear_free(kinv); | ||
259 | BN_clear_free(&k); | ||
260 | BN_clear_free(&kq); | ||
261 | return(ret); | ||
262 | } | ||
263 | |||
264 | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
265 | DSA *dsa) | ||
266 | { | ||
267 | BN_CTX *ctx; | ||
268 | BIGNUM u1,u2,t1; | ||
269 | BN_MONT_CTX *mont=NULL; | ||
270 | int ret = -1; | ||
271 | if (!dsa->p || !dsa->q || !dsa->g) | ||
272 | { | ||
273 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS); | ||
274 | return -1; | ||
275 | } | ||
276 | |||
277 | if (BN_num_bits(dsa->q) != 160) | ||
278 | { | ||
279 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE); | ||
280 | return -1; | ||
281 | } | ||
282 | |||
283 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) | ||
284 | { | ||
285 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE); | ||
286 | return -1; | ||
287 | } | ||
288 | |||
289 | BN_init(&u1); | ||
290 | BN_init(&u2); | ||
291 | BN_init(&t1); | ||
292 | |||
293 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
294 | |||
295 | if (BN_is_zero(sig->r) || sig->r->neg || BN_ucmp(sig->r, dsa->q) >= 0) | ||
296 | { | ||
297 | ret = 0; | ||
298 | goto err; | ||
299 | } | ||
300 | if (BN_is_zero(sig->s) || sig->s->neg || BN_ucmp(sig->s, dsa->q) >= 0) | ||
301 | { | ||
302 | ret = 0; | ||
303 | goto err; | ||
304 | } | ||
305 | |||
306 | /* Calculate W = inv(S) mod Q | ||
307 | * save W in u2 */ | ||
308 | if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err; | ||
309 | |||
310 | /* save M in u1 */ | ||
311 | if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err; | ||
312 | |||
313 | /* u1 = M * w mod q */ | ||
314 | if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err; | ||
315 | |||
316 | /* u2 = r * w mod q */ | ||
317 | if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err; | ||
318 | |||
319 | |||
320 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) | ||
321 | { | ||
322 | mont = BN_MONT_CTX_set_locked( | ||
323 | (BN_MONT_CTX **)&dsa->method_mont_p, | ||
324 | CRYPTO_LOCK_DSA, dsa->p, ctx); | ||
325 | if (!mont) | ||
326 | goto err; | ||
327 | } | ||
328 | |||
329 | #if 0 | ||
330 | { | ||
331 | BIGNUM t2; | ||
332 | |||
333 | BN_init(&t2); | ||
334 | /* v = ( g^u1 * y^u2 mod p ) mod q */ | ||
335 | /* let t1 = g ^ u1 mod p */ | ||
336 | if (!BN_mod_exp_mont(&t1,dsa->g,&u1,dsa->p,ctx,mont)) goto err; | ||
337 | /* let t2 = y ^ u2 mod p */ | ||
338 | if (!BN_mod_exp_mont(&t2,dsa->pub_key,&u2,dsa->p,ctx,mont)) goto err; | ||
339 | /* let u1 = t1 * t2 mod p */ | ||
340 | if (!BN_mod_mul(&u1,&t1,&t2,dsa->p,ctx)) goto err_bn; | ||
341 | BN_free(&t2); | ||
342 | } | ||
343 | /* let u1 = u1 mod q */ | ||
344 | if (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err; | ||
345 | #else | ||
346 | { | ||
347 | if (!dsa->meth->dsa_mod_exp(dsa, &t1,dsa->g,&u1,dsa->pub_key,&u2, | ||
348 | dsa->p,ctx,mont)) goto err; | ||
349 | /* BN_copy(&u1,&t1); */ | ||
350 | /* let u1 = u1 mod q */ | ||
351 | if (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err; | ||
352 | } | ||
353 | #endif | ||
354 | /* V is now in u1. If the signature is correct, it will be | ||
355 | * equal to R. */ | ||
356 | ret=(BN_ucmp(&u1, sig->r) == 0); | ||
357 | |||
358 | err: | ||
359 | if (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB); | ||
360 | if (ctx != NULL) BN_CTX_free(ctx); | ||
361 | BN_free(&u1); | ||
362 | BN_free(&u2); | ||
363 | BN_free(&t1); | ||
364 | return(ret); | ||
365 | } | ||
366 | |||
367 | static int dsa_init(DSA *dsa) | ||
368 | { | ||
369 | dsa->flags|=DSA_FLAG_CACHE_MONT_P; | ||
370 | return(1); | ||
371 | } | ||
372 | |||
373 | static int dsa_finish(DSA *dsa) | ||
374 | { | ||
375 | if(dsa->method_mont_p) | ||
376 | BN_MONT_CTX_free((BN_MONT_CTX *)dsa->method_mont_p); | ||
377 | return(1); | ||
378 | } | ||
379 | |||
380 | static int dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, | ||
381 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, | ||
382 | BN_MONT_CTX *in_mont) | ||
383 | { | ||
384 | return BN_mod_exp2_mont(rr, a1, p1, a2, p2, m, ctx, in_mont); | ||
385 | } | ||
386 | |||
387 | static int dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
388 | const BIGNUM *m, BN_CTX *ctx, | ||
389 | BN_MONT_CTX *m_ctx) | ||
390 | { | ||
391 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); | ||
392 | } | ||
393 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_sign.c b/src/lib/libcrypto/dsa/dsa_sign.c new file mode 100644 index 0000000000..37c65efb20 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_sign.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* crypto/dsa/dsa_sign.c */ | ||
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 | #include "cryptlib.h" | ||
63 | #include <openssl/bn.h> | ||
64 | #include <openssl/dsa.h> | ||
65 | #include <openssl/rand.h> | ||
66 | #include <openssl/asn1.h> | ||
67 | #ifndef OPENSSL_NO_ENGINE | ||
68 | #include <openssl/engine.h> | ||
69 | #endif | ||
70 | #include <openssl/fips.h> | ||
71 | |||
72 | DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | ||
73 | { | ||
74 | #ifdef OPENSSL_FIPS | ||
75 | if(FIPS_mode() && !(dsa->flags & DSA_FLAG_FIPS_EXTERNAL_METHOD_ALLOW) | ||
76 | && !FIPS_dsa_check(dsa)) | ||
77 | return NULL; | ||
78 | #endif | ||
79 | return dsa->meth->dsa_do_sign(dgst, dlen, dsa); | ||
80 | } | ||
81 | |||
82 | int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, | ||
83 | unsigned int *siglen, DSA *dsa) | ||
84 | { | ||
85 | DSA_SIG *s; | ||
86 | s=DSA_do_sign(dgst,dlen,dsa); | ||
87 | if (s == NULL) | ||
88 | { | ||
89 | *siglen=0; | ||
90 | return(0); | ||
91 | } | ||
92 | *siglen=i2d_DSA_SIG(s,&sig); | ||
93 | DSA_SIG_free(s); | ||
94 | return(1); | ||
95 | } | ||
96 | |||
97 | int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | ||
98 | { | ||
99 | #ifdef OPENSSL_FIPS | ||
100 | if(FIPS_mode() && !(dsa->flags & DSA_FLAG_FIPS_EXTERNAL_METHOD_ALLOW) | ||
101 | && !FIPS_dsa_check(dsa)) | ||
102 | return 0; | ||
103 | #endif | ||
104 | return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp); | ||
105 | } | ||
106 | |||
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c new file mode 100644 index 0000000000..c9784bed48 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_vrf.c | |||
@@ -0,0 +1,103 @@ | |||
1 | /* crypto/dsa/dsa_vrf.c */ | ||
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 | #include "cryptlib.h" | ||
63 | #include <openssl/bn.h> | ||
64 | #include <openssl/dsa.h> | ||
65 | #include <openssl/rand.h> | ||
66 | #include <openssl/asn1.h> | ||
67 | #include <openssl/asn1_mac.h> | ||
68 | #ifndef OPENSSL_NO_ENGINE | ||
69 | #include <openssl/engine.h> | ||
70 | #endif | ||
71 | #include <openssl/fips.h> | ||
72 | |||
73 | int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
74 | DSA *dsa) | ||
75 | { | ||
76 | #ifdef OPENSSL_FIPS | ||
77 | if(FIPS_mode() && !(dsa->flags & DSA_FLAG_FIPS_EXTERNAL_METHOD_ALLOW) | ||
78 | && !FIPS_dsa_check(dsa)) | ||
79 | return -1; | ||
80 | #endif | ||
81 | return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); | ||
82 | } | ||
83 | |||
84 | /* data has already been hashed (probably with SHA or SHA-1). */ | ||
85 | /* returns | ||
86 | * 1: correct signature | ||
87 | * 0: incorrect signature | ||
88 | * -1: error | ||
89 | */ | ||
90 | int DSA_verify(int type, const unsigned char *dgst, int dgst_len, | ||
91 | const unsigned char *sigbuf, int siglen, DSA *dsa) | ||
92 | { | ||
93 | DSA_SIG *s; | ||
94 | int ret=-1; | ||
95 | |||
96 | s = DSA_SIG_new(); | ||
97 | if (s == NULL) return(ret); | ||
98 | if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err; | ||
99 | ret=DSA_do_verify(dgst,dgst_len,s,dsa); | ||
100 | err: | ||
101 | DSA_SIG_free(s); | ||
102 | return(ret); | ||
103 | } | ||
diff --git a/src/lib/libcrypto/dsa/dsagen.c b/src/lib/libcrypto/dsa/dsagen.c new file mode 100644 index 0000000000..1b6a1cca0f --- /dev/null +++ b/src/lib/libcrypto/dsa/dsagen.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* crypto/dsa/dsagen.c */ | ||
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 | #include <stdio.h> | ||
60 | #include <openssl/dsa.h> | ||
61 | |||
62 | #define TEST | ||
63 | #define GENUINE_DSA | ||
64 | |||
65 | #ifdef GENUINE_DSA | ||
66 | #define LAST_VALUE 0xbd | ||
67 | #else | ||
68 | #define LAST_VALUE 0xd3 | ||
69 | #endif | ||
70 | |||
71 | #ifdef TEST | ||
72 | unsigned char seed[20]={ | ||
73 | 0xd5,0x01,0x4e,0x4b, | ||
74 | 0x60,0xef,0x2b,0xa8, | ||
75 | 0xb6,0x21,0x1b,0x40, | ||
76 | 0x62,0xba,0x32,0x24, | ||
77 | 0xe0,0x42,0x7d,LAST_VALUE}; | ||
78 | #endif | ||
79 | |||
80 | int cb(int p, int n) | ||
81 | { | ||
82 | char c='*'; | ||
83 | |||
84 | if (p == 0) c='.'; | ||
85 | if (p == 1) c='+'; | ||
86 | if (p == 2) c='*'; | ||
87 | if (p == 3) c='\n'; | ||
88 | printf("%c",c); | ||
89 | fflush(stdout); | ||
90 | } | ||
91 | |||
92 | main() | ||
93 | { | ||
94 | int i; | ||
95 | BIGNUM *n; | ||
96 | BN_CTX *ctx; | ||
97 | unsigned char seed_buf[20]; | ||
98 | DSA *dsa; | ||
99 | int counter,h; | ||
100 | BIO *bio_err=NULL; | ||
101 | |||
102 | if (bio_err == NULL) | ||
103 | bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); | ||
104 | |||
105 | memcpy(seed_buf,seed,20); | ||
106 | dsa=DSA_generate_parameters(1024,seed,20,&counter,&h,cb,bio_err); | ||
107 | |||
108 | if (dsa == NULL) | ||
109 | DSA_print(bio_err,dsa,0); | ||
110 | } | ||
111 | |||
diff --git a/src/lib/libcrypto/dsa/dsatest.c b/src/lib/libcrypto/dsa/dsatest.c new file mode 100644 index 0000000000..55a3756aff --- /dev/null +++ b/src/lib/libcrypto/dsa/dsatest.c | |||
@@ -0,0 +1,251 @@ | |||
1 | /* crypto/dsa/dsatest.c */ | ||
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 | #include <stdio.h> | ||
60 | #include <stdlib.h> | ||
61 | #include <string.h> | ||
62 | #include <sys/types.h> | ||
63 | #include <sys/stat.h> | ||
64 | |||
65 | #include "../e_os.h" | ||
66 | |||
67 | #include <openssl/crypto.h> | ||
68 | #include <openssl/rand.h> | ||
69 | #include <openssl/bio.h> | ||
70 | #include <openssl/err.h> | ||
71 | |||
72 | #ifdef OPENSSL_NO_DSA | ||
73 | int main(int argc, char *argv[]) | ||
74 | { | ||
75 | printf("No DSA support\n"); | ||
76 | return(0); | ||
77 | } | ||
78 | #else | ||
79 | #include <openssl/dsa.h> | ||
80 | |||
81 | #ifdef OPENSSL_SYS_WIN16 | ||
82 | #define MS_CALLBACK _far _loadds | ||
83 | #else | ||
84 | #define MS_CALLBACK | ||
85 | #endif | ||
86 | |||
87 | static void MS_CALLBACK dsa_cb(int p, int n, void *arg); | ||
88 | |||
89 | /* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to | ||
90 | * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */ | ||
91 | static unsigned char seed[20]={ | ||
92 | 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40, | ||
93 | 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3, | ||
94 | }; | ||
95 | |||
96 | static unsigned char out_p[]={ | ||
97 | 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa, | ||
98 | 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb, | ||
99 | 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7, | ||
100 | 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5, | ||
101 | 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf, | ||
102 | 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac, | ||
103 | 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2, | ||
104 | 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91, | ||
105 | }; | ||
106 | |||
107 | static unsigned char out_q[]={ | ||
108 | 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee, | ||
109 | 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e, | ||
110 | 0xda,0xce,0x91,0x5f, | ||
111 | }; | ||
112 | |||
113 | static unsigned char out_g[]={ | ||
114 | 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13, | ||
115 | 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00, | ||
116 | 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb, | ||
117 | 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e, | ||
118 | 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf, | ||
119 | 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c, | ||
120 | 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c, | ||
121 | 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02, | ||
122 | }; | ||
123 | |||
124 | static const unsigned char str1[]="12345678901234567890"; | ||
125 | |||
126 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; | ||
127 | |||
128 | static BIO *bio_err=NULL; | ||
129 | |||
130 | int main(int argc, char **argv) | ||
131 | { | ||
132 | DSA *dsa=NULL; | ||
133 | int counter,ret=0,i,j; | ||
134 | unsigned char buf[256]; | ||
135 | unsigned long h; | ||
136 | unsigned char sig[256]; | ||
137 | unsigned int siglen; | ||
138 | |||
139 | if (bio_err == NULL) | ||
140 | bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); | ||
141 | |||
142 | CRYPTO_malloc_debug_init(); | ||
143 | CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); | ||
144 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | ||
145 | |||
146 | ERR_load_crypto_strings(); | ||
147 | RAND_seed(rnd_seed, sizeof rnd_seed); | ||
148 | |||
149 | BIO_printf(bio_err,"test generation of DSA parameters\n"); | ||
150 | |||
151 | dsa=DSA_generate_parameters(512,seed,20,&counter,&h,dsa_cb,bio_err); | ||
152 | |||
153 | BIO_printf(bio_err,"seed\n"); | ||
154 | for (i=0; i<20; i+=4) | ||
155 | { | ||
156 | BIO_printf(bio_err,"%02X%02X%02X%02X ", | ||
157 | seed[i],seed[i+1],seed[i+2],seed[i+3]); | ||
158 | } | ||
159 | BIO_printf(bio_err,"\ncounter=%d h=%d\n",counter,h); | ||
160 | |||
161 | if (dsa == NULL) goto end; | ||
162 | DSA_print(bio_err,dsa,0); | ||
163 | if (counter != 105) | ||
164 | { | ||
165 | BIO_printf(bio_err,"counter should be 105\n"); | ||
166 | goto end; | ||
167 | } | ||
168 | if (h != 2) | ||
169 | { | ||
170 | BIO_printf(bio_err,"h should be 2\n"); | ||
171 | goto end; | ||
172 | } | ||
173 | |||
174 | i=BN_bn2bin(dsa->q,buf); | ||
175 | j=sizeof(out_q); | ||
176 | if ((i != j) || (memcmp(buf,out_q,i) != 0)) | ||
177 | { | ||
178 | BIO_printf(bio_err,"q value is wrong\n"); | ||
179 | goto end; | ||
180 | } | ||
181 | |||
182 | i=BN_bn2bin(dsa->p,buf); | ||
183 | j=sizeof(out_p); | ||
184 | if ((i != j) || (memcmp(buf,out_p,i) != 0)) | ||
185 | { | ||
186 | BIO_printf(bio_err,"p value is wrong\n"); | ||
187 | goto end; | ||
188 | } | ||
189 | |||
190 | i=BN_bn2bin(dsa->g,buf); | ||
191 | j=sizeof(out_g); | ||
192 | if ((i != j) || (memcmp(buf,out_g,i) != 0)) | ||
193 | { | ||
194 | BIO_printf(bio_err,"g value is wrong\n"); | ||
195 | goto end; | ||
196 | } | ||
197 | |||
198 | dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME; | ||
199 | DSA_generate_key(dsa); | ||
200 | DSA_sign(0, str1, 20, sig, &siglen, dsa); | ||
201 | if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) | ||
202 | ret=1; | ||
203 | |||
204 | dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME; | ||
205 | DSA_generate_key(dsa); | ||
206 | DSA_sign(0, str1, 20, sig, &siglen, dsa); | ||
207 | if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1) | ||
208 | ret=1; | ||
209 | |||
210 | end: | ||
211 | if (!ret) | ||
212 | ERR_print_errors(bio_err); | ||
213 | if (dsa != NULL) DSA_free(dsa); | ||
214 | CRYPTO_cleanup_all_ex_data(); | ||
215 | ERR_remove_state(0); | ||
216 | ERR_free_strings(); | ||
217 | CRYPTO_mem_leaks(bio_err); | ||
218 | if (bio_err != NULL) | ||
219 | { | ||
220 | BIO_free(bio_err); | ||
221 | bio_err = NULL; | ||
222 | } | ||
223 | EXIT(!ret); | ||
224 | return(0); | ||
225 | } | ||
226 | |||
227 | static int cb_exit(int ec) | ||
228 | { | ||
229 | EXIT(ec); | ||
230 | return(0); /* To keep some compilers quiet */ | ||
231 | } | ||
232 | |||
233 | static void MS_CALLBACK dsa_cb(int p, int n, void *arg) | ||
234 | { | ||
235 | char c='*'; | ||
236 | static int ok=0,num=0; | ||
237 | |||
238 | if (p == 0) { c='.'; num++; }; | ||
239 | if (p == 1) c='+'; | ||
240 | if (p == 2) { c='*'; ok++; } | ||
241 | if (p == 3) c='\n'; | ||
242 | BIO_write(arg,&c,1); | ||
243 | (void)BIO_flush(arg); | ||
244 | |||
245 | if (!ok && (p == 0) && (num > 1)) | ||
246 | { | ||
247 | BIO_printf((BIO *)arg,"error in dsatest\n"); | ||
248 | cb_exit(1); | ||
249 | } | ||
250 | } | ||
251 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/fips186a.txt b/src/lib/libcrypto/dsa/fips186a.txt new file mode 100644 index 0000000000..3a2e0a0d51 --- /dev/null +++ b/src/lib/libcrypto/dsa/fips186a.txt | |||
@@ -0,0 +1,122 @@ | |||
1 | The origional FIPE 180 used SHA-0 (FIPS 180) for its appendix 5 | ||
2 | examples. This is an updated version that uses SHA-1 (FIPS 180-1) | ||
3 | supplied to me by Wei Dai | ||
4 | -- | ||
5 | APPENDIX 5. EXAMPLE OF THE DSA | ||
6 | |||
7 | |||
8 | This appendix is for informational purposes only and is not required to meet | ||
9 | the standard. | ||
10 | |||
11 | Let L = 512 (size of p). The values in this example are expressed in | ||
12 | hexadecimal notation. The p and q given here were generated by the prime | ||
13 | generation standard described in appendix 2 using the 160-bit SEED: | ||
14 | |||
15 | d5014e4b 60ef2ba8 b6211b40 62ba3224 e0427dd3 | ||
16 | |||
17 | With this SEED, the algorithm found p and q when the counter was at 105. | ||
18 | |||
19 | x was generated by the algorithm described in appendix 3, section 3.1, using | ||
20 | the SHA to construct G (as in appendix 3, section 3.3) and a 160-bit XSEED: | ||
21 | |||
22 | XSEED = | ||
23 | |||
24 | bd029bbe 7f51960b cf9edb2b 61f06f0f eb5a38b6 | ||
25 | |||
26 | t = | ||
27 | 67452301 EFCDAB89 98BADCFE 10325476 C3D2E1F0 | ||
28 | |||
29 | x = G(t,XSEED) mod q | ||
30 | |||
31 | k was generated by the algorithm described in appendix 3, section 3.2, using | ||
32 | the SHA to construct G (as in appendix 3, section 3.3) and a 160-bit KSEED: | ||
33 | |||
34 | KSEED = | ||
35 | |||
36 | 687a66d9 0648f993 867e121f 4ddf9ddb 01205584 | ||
37 | |||
38 | t = | ||
39 | EFCDAB89 98BADCFE 10325476 C3D2E1F0 67452301 | ||
40 | |||
41 | k = G(t,KSEED) mod q | ||
42 | |||
43 | Finally: | ||
44 | |||
45 | h = 2 | ||
46 | |||
47 | p = | ||
48 | 8df2a494 492276aa 3d25759b b06869cb eac0d83a fb8d0cf7 | ||
49 | cbb8324f 0d7882e5 d0762fc5 b7210eaf c2e9adac 32ab7aac | ||
50 | 49693dfb f83724c2 ec0736ee 31c80291 | ||
51 | |||
52 | |||
53 | q = | ||
54 | c773218c 737ec8ee 993b4f2d ed30f48e dace915f | ||
55 | |||
56 | |||
57 | g = | ||
58 | 626d0278 39ea0a13 413163a5 5b4cb500 299d5522 956cefcb | ||
59 | 3bff10f3 99ce2c2e 71cb9de5 fa24babf 58e5b795 21925c9c | ||
60 | c42e9f6f 464b088c c572af53 e6d78802 | ||
61 | |||
62 | |||
63 | x = | ||
64 | 2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614 | ||
65 | |||
66 | |||
67 | k = | ||
68 | 358dad57 1462710f 50e254cf 1a376b2b deaadfbf | ||
69 | |||
70 | |||
71 | kinv = | ||
72 | |||
73 | 0d516729 8202e49b 4116ac10 4fc3f415 ae52f917 | ||
74 | |||
75 | M = ASCII form of "abc" (See FIPS PUB 180-1, Appendix A) | ||
76 | |||
77 | SHA(M) = | ||
78 | |||
79 | a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d | ||
80 | |||
81 | |||
82 | y = | ||
83 | |||
84 | 19131871 d75b1612 a819f29d 78d1b0d7 346f7aa7 7bb62a85 | ||
85 | 9bfd6c56 75da9d21 2d3a36ef 1672ef66 0b8c7c25 5cc0ec74 | ||
86 | 858fba33 f44c0669 9630a76b 030ee333 | ||
87 | |||
88 | |||
89 | r = | ||
90 | 8bac1ab6 6410435c b7181f95 b16ab97c 92b341c0 | ||
91 | |||
92 | s = | ||
93 | 41e2345f 1f56df24 58f426d1 55b4ba2d b6dcd8c8 | ||
94 | |||
95 | |||
96 | w = | ||
97 | 9df4ece5 826be95f ed406d41 b43edc0b 1c18841b | ||
98 | |||
99 | |||
100 | u1 = | ||
101 | bf655bd0 46f0b35e c791b004 804afcbb 8ef7d69d | ||
102 | |||
103 | |||
104 | u2 = | ||
105 | 821a9263 12e97ade abcc8d08 2b527897 8a2df4b0 | ||
106 | |||
107 | |||
108 | gu1 mod p = | ||
109 | |||
110 | 51b1bf86 7888e5f3 af6fb476 9dd016bc fe667a65 aafc2753 | ||
111 | 9063bd3d 2b138b4c e02cc0c0 2ec62bb6 7306c63e 4db95bbf | ||
112 | 6f96662a 1987a21b e4ec1071 010b6069 | ||
113 | |||
114 | |||
115 | yu2 mod p = | ||
116 | |||
117 | 8b510071 2957e950 50d6b8fd 376a668e 4b0d633c 1e46e665 | ||
118 | 5c611a72 e2b28483 be52c74d 4b30de61 a668966e dc307a67 | ||
119 | c19441f4 22bf3c34 08aeba1f 0a4dbec7 | ||
120 | |||
121 | v = | ||
122 | 8bac1ab6 6410435c b7181f95 b16ab97c 92b341c0 | ||