diff options
Diffstat (limited to 'src/lib/libcrypto/dh')
| -rw-r--r-- | src/lib/libcrypto/dh/Makefile.ssl | 136 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh.h | 207 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_asn1.c | 87 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_check.c | 120 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_err.c | 100 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_gen.c | 169 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_key.c | 223 | ||||
| -rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 237 |
8 files changed, 1279 insertions, 0 deletions
diff --git a/src/lib/libcrypto/dh/Makefile.ssl b/src/lib/libcrypto/dh/Makefile.ssl new file mode 100644 index 0000000000..a38a3e85c4 --- /dev/null +++ b/src/lib/libcrypto/dh/Makefile.ssl | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | # | ||
| 2 | # SSLeay/crypto/dh/Makefile | ||
| 3 | # | ||
| 4 | |||
| 5 | DIR= dh | ||
| 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 | MAKE= make -f Makefile.ssl | ||
| 14 | MAKEDEPPROG= makedepend | ||
| 15 | MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) | ||
| 16 | MAKEFILE= Makefile.ssl | ||
| 17 | AR= ar r | ||
| 18 | |||
| 19 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
| 20 | |||
| 21 | GENERAL=Makefile | ||
| 22 | TEST= dhtest.c | ||
| 23 | APPS= | ||
| 24 | |||
| 25 | LIB=$(TOP)/libcrypto.a | ||
| 26 | LIBSRC= dh_asn1.c dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c | ||
| 27 | LIBOBJ= dh_asn1.o dh_gen.o dh_key.o dh_lib.o dh_check.o dh_err.o | ||
| 28 | |||
| 29 | SRC= $(LIBSRC) | ||
| 30 | |||
| 31 | EXHEADER= dh.h | ||
| 32 | HEADER= $(EXHEADER) | ||
| 33 | |||
| 34 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
| 35 | |||
| 36 | top: | ||
| 37 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
| 38 | |||
| 39 | all: lib | ||
| 40 | |||
| 41 | lib: $(LIBOBJ) | ||
| 42 | $(AR) $(LIB) $(LIBOBJ) | ||
| 43 | $(RANLIB) $(LIB) || echo Never mind. | ||
| 44 | @touch lib | ||
| 45 | |||
| 46 | files: | ||
| 47 | $(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO | ||
| 48 | |||
| 49 | links: | ||
| 50 | @sh $(TOP)/util/point.sh Makefile.ssl Makefile | ||
| 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 | @for i in $(EXHEADER) ; \ | ||
| 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) $(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 | dh_asn1.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 83 | dh_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h | ||
| 84 | dh_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 85 | dh_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 86 | dh_asn1.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 87 | dh_asn1.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h | ||
| 88 | dh_asn1.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h | ||
| 89 | dh_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 90 | dh_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 91 | dh_asn1.o: ../../include/openssl/symhacks.h ../cryptlib.h dh_asn1.c | ||
| 92 | dh_check.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 93 | dh_check.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 94 | dh_check.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h | ||
| 95 | dh_check.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
| 96 | dh_check.o: ../../include/openssl/opensslconf.h | ||
| 97 | dh_check.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 98 | dh_check.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 99 | dh_check.o: ../../include/openssl/symhacks.h ../cryptlib.h dh_check.c | ||
| 100 | dh_err.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 101 | dh_err.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 102 | dh_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 103 | dh_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 104 | dh_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 105 | dh_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 106 | dh_err.o: ../../include/openssl/symhacks.h dh_err.c | ||
| 107 | dh_gen.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 108 | dh_gen.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 109 | dh_gen.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h | ||
| 110 | dh_gen.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
| 111 | dh_gen.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
| 112 | dh_gen.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h | ||
| 113 | dh_gen.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 114 | dh_gen.o: ../cryptlib.h dh_gen.c | ||
| 115 | dh_key.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 116 | dh_key.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 117 | dh_key.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 118 | dh_key.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 119 | dh_key.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
| 120 | dh_key.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 121 | dh_key.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 122 | dh_key.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 123 | dh_key.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 124 | dh_key.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
| 125 | dh_key.o: ../cryptlib.h dh_key.c | ||
| 126 | dh_lib.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 127 | dh_lib.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 128 | dh_lib.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h | ||
| 129 | dh_lib.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 130 | dh_lib.o: ../../include/openssl/engine.h ../../include/openssl/err.h | ||
| 131 | dh_lib.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 132 | dh_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 133 | dh_lib.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 134 | dh_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 135 | dh_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h | ||
| 136 | dh_lib.o: ../cryptlib.h dh_lib.c | ||
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h new file mode 100644 index 0000000000..d51dc130f4 --- /dev/null +++ b/src/lib/libcrypto/dh/dh.h | |||
| @@ -0,0 +1,207 @@ | |||
| 1 | /* crypto/dh/dh.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 | #ifndef HEADER_DH_H | ||
| 60 | #define HEADER_DH_H | ||
| 61 | |||
| 62 | #ifdef OPENSSL_NO_DH | ||
| 63 | #error DH is disabled. | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #ifndef OPENSSL_NO_BIO | ||
| 67 | #include <openssl/bio.h> | ||
| 68 | #endif | ||
| 69 | #include <openssl/bn.h> | ||
| 70 | #include <openssl/crypto.h> | ||
| 71 | #include <openssl/ossl_typ.h> | ||
| 72 | |||
| 73 | #define DH_FLAG_CACHE_MONT_P 0x01 | ||
| 74 | |||
| 75 | #ifdef __cplusplus | ||
| 76 | extern "C" { | ||
| 77 | #endif | ||
| 78 | |||
| 79 | typedef struct dh_st DH; | ||
| 80 | |||
| 81 | typedef struct dh_method { | ||
| 82 | const char *name; | ||
| 83 | /* Methods here */ | ||
| 84 | int (*generate_key)(DH *dh); | ||
| 85 | int (*compute_key)(unsigned char *key,const BIGNUM *pub_key,DH *dh); | ||
| 86 | int (*bn_mod_exp)(const DH *dh, BIGNUM *r, const BIGNUM *a, | ||
| 87 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, | ||
| 88 | BN_MONT_CTX *m_ctx); /* Can be null */ | ||
| 89 | |||
| 90 | int (*init)(DH *dh); | ||
| 91 | int (*finish)(DH *dh); | ||
| 92 | int flags; | ||
| 93 | char *app_data; | ||
| 94 | } DH_METHOD; | ||
| 95 | |||
| 96 | struct dh_st | ||
| 97 | { | ||
| 98 | /* This first argument is used to pick up errors when | ||
| 99 | * a DH is passed instead of a EVP_PKEY */ | ||
| 100 | int pad; | ||
| 101 | int version; | ||
| 102 | BIGNUM *p; | ||
| 103 | BIGNUM *g; | ||
| 104 | int length; /* optional */ | ||
| 105 | BIGNUM *pub_key; /* g^x */ | ||
| 106 | BIGNUM *priv_key; /* x */ | ||
| 107 | |||
| 108 | int flags; | ||
| 109 | char *method_mont_p; | ||
| 110 | /* Place holders if we want to do X9.42 DH */ | ||
| 111 | BIGNUM *q; | ||
| 112 | BIGNUM *j; | ||
| 113 | unsigned char *seed; | ||
| 114 | int seedlen; | ||
| 115 | BIGNUM *counter; | ||
| 116 | |||
| 117 | int references; | ||
| 118 | CRYPTO_EX_DATA ex_data; | ||
| 119 | const DH_METHOD *meth; | ||
| 120 | ENGINE *engine; | ||
| 121 | }; | ||
| 122 | |||
| 123 | #define DH_GENERATOR_2 2 | ||
| 124 | /* #define DH_GENERATOR_3 3 */ | ||
| 125 | #define DH_GENERATOR_5 5 | ||
| 126 | |||
| 127 | /* DH_check error codes */ | ||
| 128 | #define DH_CHECK_P_NOT_PRIME 0x01 | ||
| 129 | #define DH_CHECK_P_NOT_SAFE_PRIME 0x02 | ||
| 130 | #define DH_UNABLE_TO_CHECK_GENERATOR 0x04 | ||
| 131 | #define DH_NOT_SUITABLE_GENERATOR 0x08 | ||
| 132 | |||
| 133 | /* primes p where (p-1)/2 is prime too are called "safe"; we define | ||
| 134 | this for backward compatibility: */ | ||
| 135 | #define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME | ||
| 136 | |||
| 137 | #define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \ | ||
| 138 | (char *(*)())d2i_DHparams,(char *)(x)) | ||
| 139 | #define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ | ||
| 140 | (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x)) | ||
| 141 | #define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \ | ||
| 142 | (unsigned char *)(x)) | ||
| 143 | #define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \ | ||
| 144 | (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x)) | ||
| 145 | #ifdef __cplusplus | ||
| 146 | #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio((int (*)())i2d_DHparams,(bp), \ | ||
| 147 | (unsigned char *)(x)) | ||
| 148 | #else | ||
| 149 | #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \ | ||
| 150 | (unsigned char *)(x)) | ||
| 151 | #endif | ||
| 152 | |||
| 153 | const DH_METHOD *DH_OpenSSL(void); | ||
| 154 | |||
| 155 | void DH_set_default_method(const DH_METHOD *meth); | ||
| 156 | const DH_METHOD *DH_get_default_method(void); | ||
| 157 | int DH_set_method(DH *dh, const DH_METHOD *meth); | ||
| 158 | DH *DH_new_method(ENGINE *engine); | ||
| 159 | |||
| 160 | DH * DH_new(void); | ||
| 161 | void DH_free(DH *dh); | ||
| 162 | int DH_up_ref(DH *dh); | ||
| 163 | int DH_size(const DH *dh); | ||
| 164 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 165 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 166 | int DH_set_ex_data(DH *d, int idx, void *arg); | ||
| 167 | void *DH_get_ex_data(DH *d, int idx); | ||
| 168 | DH * DH_generate_parameters(int prime_len,int generator, | ||
| 169 | void (*callback)(int,int,void *),void *cb_arg); | ||
| 170 | int DH_check(const DH *dh,int *codes); | ||
| 171 | int DH_generate_key(DH *dh); | ||
| 172 | int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); | ||
| 173 | DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); | ||
| 174 | int i2d_DHparams(const DH *a,unsigned char **pp); | ||
| 175 | #ifndef OPENSSL_NO_FP_API | ||
| 176 | int DHparams_print_fp(FILE *fp, const DH *x); | ||
| 177 | #endif | ||
| 178 | #ifndef OPENSSL_NO_BIO | ||
| 179 | int DHparams_print(BIO *bp, const DH *x); | ||
| 180 | #else | ||
| 181 | int DHparams_print(char *bp, const DH *x); | ||
| 182 | #endif | ||
| 183 | |||
| 184 | /* BEGIN ERROR CODES */ | ||
| 185 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 186 | * made after this point may be overwritten when the script is next run. | ||
| 187 | */ | ||
| 188 | void ERR_load_DH_strings(void); | ||
| 189 | |||
| 190 | /* Error codes for the DH functions. */ | ||
| 191 | |||
| 192 | /* Function codes. */ | ||
| 193 | #define DH_F_DHPARAMS_PRINT 100 | ||
| 194 | #define DH_F_DHPARAMS_PRINT_FP 101 | ||
| 195 | #define DH_F_DH_COMPUTE_KEY 102 | ||
| 196 | #define DH_F_DH_GENERATE_KEY 103 | ||
| 197 | #define DH_F_DH_GENERATE_PARAMETERS 104 | ||
| 198 | #define DH_F_DH_NEW_METHOD 105 | ||
| 199 | |||
| 200 | /* Reason codes. */ | ||
| 201 | #define DH_R_BAD_GENERATOR 101 | ||
| 202 | #define DH_R_NO_PRIVATE_VALUE 100 | ||
| 203 | |||
| 204 | #ifdef __cplusplus | ||
| 205 | } | ||
| 206 | #endif | ||
| 207 | #endif | ||
diff --git a/src/lib/libcrypto/dh/dh_asn1.c b/src/lib/libcrypto/dh/dh_asn1.c new file mode 100644 index 0000000000..769b5b68c5 --- /dev/null +++ b/src/lib/libcrypto/dh/dh_asn1.c | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /* dh_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/bn.h> | ||
| 62 | #include <openssl/dh.h> | ||
| 63 | #include <openssl/objects.h> | ||
| 64 | #include <openssl/asn1t.h> | ||
| 65 | |||
| 66 | /* Override the default free and new methods */ | ||
| 67 | static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 68 | { | ||
| 69 | if(operation == ASN1_OP_NEW_PRE) { | ||
| 70 | *pval = (ASN1_VALUE *)DH_new(); | ||
| 71 | if(*pval) return 2; | ||
| 72 | return 0; | ||
| 73 | } else if(operation == ASN1_OP_FREE_PRE) { | ||
| 74 | DH_free((DH *)*pval); | ||
| 75 | *pval = NULL; | ||
| 76 | return 2; | ||
| 77 | } | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | ASN1_SEQUENCE_cb(DHparams, dh_cb) = { | ||
| 82 | ASN1_SIMPLE(DH, p, BIGNUM), | ||
| 83 | ASN1_SIMPLE(DH, g, BIGNUM), | ||
| 84 | ASN1_OPT(DH, length, ZLONG), | ||
| 85 | } ASN1_SEQUENCE_END_cb(DH, DHparams) | ||
| 86 | |||
| 87 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DH, DHparams, DHparams) | ||
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c new file mode 100644 index 0000000000..f0373f7d68 --- /dev/null +++ b/src/lib/libcrypto/dh/dh_check.c | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | /* crypto/dh/dh_check.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 "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/dh.h> | ||
| 63 | |||
| 64 | /* Check that p is a safe prime and | ||
| 65 | * if g is 2, 3 or 5, check that is is a suitable generator | ||
| 66 | * where | ||
| 67 | * for 2, p mod 24 == 11 | ||
| 68 | * for 3, p mod 12 == 5 | ||
| 69 | * for 5, p mod 10 == 3 or 7 | ||
| 70 | * should hold. | ||
| 71 | */ | ||
| 72 | |||
| 73 | int DH_check(const DH *dh, int *ret) | ||
| 74 | { | ||
| 75 | int ok=0; | ||
| 76 | BN_CTX *ctx=NULL; | ||
| 77 | BN_ULONG l; | ||
| 78 | BIGNUM *q=NULL; | ||
| 79 | |||
| 80 | *ret=0; | ||
| 81 | ctx=BN_CTX_new(); | ||
| 82 | if (ctx == NULL) goto err; | ||
| 83 | q=BN_new(); | ||
| 84 | if (q == NULL) goto err; | ||
| 85 | |||
| 86 | if (BN_is_word(dh->g,DH_GENERATOR_2)) | ||
| 87 | { | ||
| 88 | l=BN_mod_word(dh->p,24); | ||
| 89 | if (l != 11) *ret|=DH_NOT_SUITABLE_GENERATOR; | ||
| 90 | } | ||
| 91 | #if 0 | ||
| 92 | else if (BN_is_word(dh->g,DH_GENERATOR_3)) | ||
| 93 | { | ||
| 94 | l=BN_mod_word(dh->p,12); | ||
| 95 | if (l != 5) *ret|=DH_NOT_SUITABLE_GENERATOR; | ||
| 96 | } | ||
| 97 | #endif | ||
| 98 | else if (BN_is_word(dh->g,DH_GENERATOR_5)) | ||
| 99 | { | ||
| 100 | l=BN_mod_word(dh->p,10); | ||
| 101 | if ((l != 3) && (l != 7)) | ||
| 102 | *ret|=DH_NOT_SUITABLE_GENERATOR; | ||
| 103 | } | ||
| 104 | else | ||
| 105 | *ret|=DH_UNABLE_TO_CHECK_GENERATOR; | ||
| 106 | |||
| 107 | if (!BN_is_prime(dh->p,BN_prime_checks,NULL,ctx,NULL)) | ||
| 108 | *ret|=DH_CHECK_P_NOT_PRIME; | ||
| 109 | else | ||
| 110 | { | ||
| 111 | if (!BN_rshift1(q,dh->p)) goto err; | ||
| 112 | if (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL)) | ||
| 113 | *ret|=DH_CHECK_P_NOT_SAFE_PRIME; | ||
| 114 | } | ||
| 115 | ok=1; | ||
| 116 | err: | ||
| 117 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 118 | if (q != NULL) BN_free(q); | ||
| 119 | return(ok); | ||
| 120 | } | ||
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c new file mode 100644 index 0000000000..d837950aec --- /dev/null +++ b/src/lib/libcrypto/dh/dh_err.c | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | /* crypto/dh/dh_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2002 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/dh.h> | ||
| 64 | |||
| 65 | /* BEGIN ERROR CODES */ | ||
| 66 | #ifndef OPENSSL_NO_ERR | ||
| 67 | static ERR_STRING_DATA DH_str_functs[]= | ||
| 68 | { | ||
| 69 | {ERR_PACK(0,DH_F_DHPARAMS_PRINT,0), "DHparams_print"}, | ||
| 70 | {ERR_PACK(0,DH_F_DHPARAMS_PRINT_FP,0), "DHparams_print_fp"}, | ||
| 71 | {ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0), "DH_compute_key"}, | ||
| 72 | {ERR_PACK(0,DH_F_DH_GENERATE_KEY,0), "DH_generate_key"}, | ||
| 73 | {ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0), "DH_generate_parameters"}, | ||
| 74 | {ERR_PACK(0,DH_F_DH_NEW_METHOD,0), "DH_new_method"}, | ||
| 75 | {0,NULL} | ||
| 76 | }; | ||
| 77 | |||
| 78 | static ERR_STRING_DATA DH_str_reasons[]= | ||
| 79 | { | ||
| 80 | {DH_R_BAD_GENERATOR ,"bad generator"}, | ||
| 81 | {DH_R_NO_PRIVATE_VALUE ,"no private value"}, | ||
| 82 | {0,NULL} | ||
| 83 | }; | ||
| 84 | |||
| 85 | #endif | ||
| 86 | |||
| 87 | void ERR_load_DH_strings(void) | ||
| 88 | { | ||
| 89 | static int init=1; | ||
| 90 | |||
| 91 | if (init) | ||
| 92 | { | ||
| 93 | init=0; | ||
| 94 | #ifndef OPENSSL_NO_ERR | ||
| 95 | ERR_load_strings(ERR_LIB_DH,DH_str_functs); | ||
| 96 | ERR_load_strings(ERR_LIB_DH,DH_str_reasons); | ||
| 97 | #endif | ||
| 98 | |||
| 99 | } | ||
| 100 | } | ||
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c new file mode 100644 index 0000000000..06f78b35ab --- /dev/null +++ b/src/lib/libcrypto/dh/dh_gen.c | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | /* crypto/dh/dh_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 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/dh.h> | ||
| 63 | |||
| 64 | /* We generate DH parameters as follows | ||
| 65 | * find a prime q which is prime_len/2 bits long. | ||
| 66 | * p=(2*q)+1 or (p-1)/2 = q | ||
| 67 | * For this case, g is a generator if | ||
| 68 | * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1. | ||
| 69 | * Since the factors of p-1 are q and 2, we just need to check | ||
| 70 | * g^2 mod p != 1 and g^q mod p != 1. | ||
| 71 | * | ||
| 72 | * Having said all that, | ||
| 73 | * there is another special case method for the generators 2, 3 and 5. | ||
| 74 | * for 2, p mod 24 == 11 | ||
| 75 | * for 3, p mod 12 == 5 <<<<< does not work for safe primes. | ||
| 76 | * for 5, p mod 10 == 3 or 7 | ||
| 77 | * | ||
| 78 | * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the | ||
| 79 | * special generators and for answering some of my questions. | ||
| 80 | * | ||
| 81 | * I've implemented the second simple method :-). | ||
| 82 | * Since DH should be using a safe prime (both p and q are prime), | ||
| 83 | * this generator function can take a very very long time to run. | ||
| 84 | */ | ||
| 85 | /* Actually there is no reason to insist that 'generator' be a generator. | ||
| 86 | * It's just as OK (and in some sense better) to use a generator of the | ||
| 87 | * order-q subgroup. | ||
| 88 | */ | ||
| 89 | DH *DH_generate_parameters(int prime_len, int generator, | ||
| 90 | void (*callback)(int,int,void *), void *cb_arg) | ||
| 91 | { | ||
| 92 | BIGNUM *p=NULL,*t1,*t2; | ||
| 93 | DH *ret=NULL; | ||
| 94 | int g,ok= -1; | ||
| 95 | BN_CTX *ctx=NULL; | ||
| 96 | |||
| 97 | ret=DH_new(); | ||
| 98 | if (ret == NULL) goto err; | ||
| 99 | ctx=BN_CTX_new(); | ||
| 100 | if (ctx == NULL) goto err; | ||
| 101 | BN_CTX_start(ctx); | ||
| 102 | t1 = BN_CTX_get(ctx); | ||
| 103 | t2 = BN_CTX_get(ctx); | ||
| 104 | if (t1 == NULL || t2 == NULL) goto err; | ||
| 105 | |||
| 106 | if (generator <= 1) | ||
| 107 | { | ||
| 108 | DHerr(DH_F_DH_GENERATE_PARAMETERS, DH_R_BAD_GENERATOR); | ||
| 109 | goto err; | ||
| 110 | } | ||
| 111 | if (generator == DH_GENERATOR_2) | ||
| 112 | { | ||
| 113 | if (!BN_set_word(t1,24)) goto err; | ||
| 114 | if (!BN_set_word(t2,11)) goto err; | ||
| 115 | g=2; | ||
| 116 | } | ||
| 117 | #if 0 /* does not work for safe primes */ | ||
| 118 | else if (generator == DH_GENERATOR_3) | ||
| 119 | { | ||
| 120 | if (!BN_set_word(t1,12)) goto err; | ||
| 121 | if (!BN_set_word(t2,5)) goto err; | ||
| 122 | g=3; | ||
| 123 | } | ||
| 124 | #endif | ||
| 125 | else if (generator == DH_GENERATOR_5) | ||
| 126 | { | ||
| 127 | if (!BN_set_word(t1,10)) goto err; | ||
| 128 | if (!BN_set_word(t2,3)) goto err; | ||
| 129 | /* BN_set_word(t3,7); just have to miss | ||
| 130 | * out on these ones :-( */ | ||
| 131 | g=5; | ||
| 132 | } | ||
| 133 | else | ||
| 134 | { | ||
| 135 | /* in the general case, don't worry if 'generator' is a | ||
| 136 | * generator or not: since we are using safe primes, | ||
| 137 | * it will generate either an order-q or an order-2q group, | ||
| 138 | * which both is OK */ | ||
| 139 | if (!BN_set_word(t1,2)) goto err; | ||
| 140 | if (!BN_set_word(t2,1)) goto err; | ||
| 141 | g=generator; | ||
| 142 | } | ||
| 143 | |||
| 144 | p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg); | ||
| 145 | if (p == NULL) goto err; | ||
| 146 | if (callback != NULL) callback(3,0,cb_arg); | ||
| 147 | ret->p=p; | ||
| 148 | ret->g=BN_new(); | ||
| 149 | if (!BN_set_word(ret->g,g)) goto err; | ||
| 150 | ok=1; | ||
| 151 | err: | ||
| 152 | if (ok == -1) | ||
| 153 | { | ||
| 154 | DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB); | ||
| 155 | ok=0; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (ctx != NULL) | ||
| 159 | { | ||
| 160 | BN_CTX_end(ctx); | ||
| 161 | BN_CTX_free(ctx); | ||
| 162 | } | ||
| 163 | if (!ok && (ret != NULL)) | ||
| 164 | { | ||
| 165 | DH_free(ret); | ||
| 166 | ret=NULL; | ||
| 167 | } | ||
| 168 | return(ret); | ||
| 169 | } | ||
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c new file mode 100644 index 0000000000..1a0efca2c4 --- /dev/null +++ b/src/lib/libcrypto/dh/dh_key.c | |||
| @@ -0,0 +1,223 @@ | |||
| 1 | /* crypto/dh/dh_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 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rand.h> | ||
| 63 | #include <openssl/dh.h> | ||
| 64 | #include <openssl/engine.h> | ||
| 65 | |||
| 66 | static int generate_key(DH *dh); | ||
| 67 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | ||
| 68 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | ||
| 69 | const BIGNUM *a, const BIGNUM *p, | ||
| 70 | const BIGNUM *m, BN_CTX *ctx, | ||
| 71 | BN_MONT_CTX *m_ctx); | ||
| 72 | static int dh_init(DH *dh); | ||
| 73 | static int dh_finish(DH *dh); | ||
| 74 | |||
| 75 | int DH_generate_key(DH *dh) | ||
| 76 | { | ||
| 77 | return dh->meth->generate_key(dh); | ||
| 78 | } | ||
| 79 | |||
| 80 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | ||
| 81 | { | ||
| 82 | return dh->meth->compute_key(key, pub_key, dh); | ||
| 83 | } | ||
| 84 | |||
| 85 | static DH_METHOD dh_ossl = { | ||
| 86 | "OpenSSL DH Method", | ||
| 87 | generate_key, | ||
| 88 | compute_key, | ||
| 89 | dh_bn_mod_exp, | ||
| 90 | dh_init, | ||
| 91 | dh_finish, | ||
| 92 | 0, | ||
| 93 | NULL | ||
| 94 | }; | ||
| 95 | |||
| 96 | const DH_METHOD *DH_OpenSSL(void) | ||
| 97 | { | ||
| 98 | return &dh_ossl; | ||
| 99 | } | ||
| 100 | |||
| 101 | static int generate_key(DH *dh) | ||
| 102 | { | ||
| 103 | int ok=0; | ||
| 104 | int generate_new_key=0; | ||
| 105 | unsigned l; | ||
| 106 | BN_CTX *ctx; | ||
| 107 | BN_MONT_CTX *mont; | ||
| 108 | BIGNUM *pub_key=NULL,*priv_key=NULL; | ||
| 109 | |||
| 110 | ctx = BN_CTX_new(); | ||
| 111 | if (ctx == NULL) goto err; | ||
| 112 | |||
| 113 | if (dh->priv_key == NULL) | ||
| 114 | { | ||
| 115 | priv_key=BN_new(); | ||
| 116 | if (priv_key == NULL) goto err; | ||
| 117 | generate_new_key=1; | ||
| 118 | } | ||
| 119 | else | ||
| 120 | priv_key=dh->priv_key; | ||
| 121 | |||
| 122 | if (dh->pub_key == NULL) | ||
| 123 | { | ||
| 124 | pub_key=BN_new(); | ||
| 125 | if (pub_key == NULL) goto err; | ||
| 126 | } | ||
| 127 | else | ||
| 128 | pub_key=dh->pub_key; | ||
| 129 | |||
| 130 | if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P)) | ||
| 131 | { | ||
| 132 | if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) | ||
| 133 | if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p, | ||
| 134 | dh->p,ctx)) goto err; | ||
| 135 | } | ||
| 136 | mont=(BN_MONT_CTX *)dh->method_mont_p; | ||
| 137 | |||
| 138 | if (generate_new_key) | ||
| 139 | { | ||
| 140 | l = dh->length ? dh->length : BN_num_bits(dh->p)-1; /* secret exponent length */ | ||
| 141 | if (!BN_rand(priv_key, l, 0, 0)) goto err; | ||
| 142 | } | ||
| 143 | if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key,dh->p,ctx,mont)) | ||
| 144 | goto err; | ||
| 145 | |||
| 146 | dh->pub_key=pub_key; | ||
| 147 | dh->priv_key=priv_key; | ||
| 148 | ok=1; | ||
| 149 | err: | ||
| 150 | if (ok != 1) | ||
| 151 | DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB); | ||
| 152 | |||
| 153 | if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key); | ||
| 154 | if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key); | ||
| 155 | BN_CTX_free(ctx); | ||
| 156 | return(ok); | ||
| 157 | } | ||
| 158 | |||
| 159 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | ||
| 160 | { | ||
| 161 | BN_CTX *ctx; | ||
| 162 | BN_MONT_CTX *mont; | ||
| 163 | BIGNUM *tmp; | ||
| 164 | int ret= -1; | ||
| 165 | |||
| 166 | ctx = BN_CTX_new(); | ||
| 167 | if (ctx == NULL) goto err; | ||
| 168 | BN_CTX_start(ctx); | ||
| 169 | tmp = BN_CTX_get(ctx); | ||
| 170 | |||
| 171 | if (dh->priv_key == NULL) | ||
| 172 | { | ||
| 173 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); | ||
| 174 | goto err; | ||
| 175 | } | ||
| 176 | if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P)) | ||
| 177 | { | ||
| 178 | if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) | ||
| 179 | if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p, | ||
| 180 | dh->p,ctx)) goto err; | ||
| 181 | } | ||
| 182 | |||
| 183 | mont=(BN_MONT_CTX *)dh->method_mont_p; | ||
| 184 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) | ||
| 185 | { | ||
| 186 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); | ||
| 187 | goto err; | ||
| 188 | } | ||
| 189 | |||
| 190 | ret=BN_bn2bin(tmp,key); | ||
| 191 | err: | ||
| 192 | BN_CTX_end(ctx); | ||
| 193 | BN_CTX_free(ctx); | ||
| 194 | return(ret); | ||
| 195 | } | ||
| 196 | |||
| 197 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | ||
| 198 | const BIGNUM *a, const BIGNUM *p, | ||
| 199 | const BIGNUM *m, BN_CTX *ctx, | ||
| 200 | BN_MONT_CTX *m_ctx) | ||
| 201 | { | ||
| 202 | if (a->top == 1) | ||
| 203 | { | ||
| 204 | BN_ULONG A = a->d[0]; | ||
| 205 | return BN_mod_exp_mont_word(r,A,p,m,ctx,m_ctx); | ||
| 206 | } | ||
| 207 | else | ||
| 208 | return BN_mod_exp_mont(r,a,p,m,ctx,m_ctx); | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | static int dh_init(DH *dh) | ||
| 213 | { | ||
| 214 | dh->flags |= DH_FLAG_CACHE_MONT_P; | ||
| 215 | return(1); | ||
| 216 | } | ||
| 217 | |||
| 218 | static int dh_finish(DH *dh) | ||
| 219 | { | ||
| 220 | if(dh->method_mont_p) | ||
| 221 | BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p); | ||
| 222 | return(1); | ||
| 223 | } | ||
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c new file mode 100644 index 0000000000..ba5fd41057 --- /dev/null +++ b/src/lib/libcrypto/dh/dh_lib.c | |||
| @@ -0,0 +1,237 @@ | |||
| 1 | /* crypto/dh/dh_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 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/dh.h> | ||
| 63 | #include <openssl/engine.h> | ||
| 64 | |||
| 65 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; | ||
| 66 | |||
| 67 | static const DH_METHOD *default_DH_method = NULL; | ||
| 68 | |||
| 69 | void DH_set_default_method(const DH_METHOD *meth) | ||
| 70 | { | ||
| 71 | default_DH_method = meth; | ||
| 72 | } | ||
| 73 | |||
| 74 | const DH_METHOD *DH_get_default_method(void) | ||
| 75 | { | ||
| 76 | if(!default_DH_method) | ||
| 77 | default_DH_method = DH_OpenSSL(); | ||
| 78 | return default_DH_method; | ||
| 79 | } | ||
| 80 | |||
| 81 | int DH_set_method(DH *dh, const DH_METHOD *meth) | ||
| 82 | { | ||
| 83 | /* NB: The caller is specifically setting a method, so it's not up to us | ||
| 84 | * to deal with which ENGINE it comes from. */ | ||
| 85 | const DH_METHOD *mtmp; | ||
| 86 | mtmp = dh->meth; | ||
| 87 | if (mtmp->finish) mtmp->finish(dh); | ||
| 88 | if (dh->engine) | ||
| 89 | { | ||
| 90 | ENGINE_finish(dh->engine); | ||
| 91 | dh->engine = NULL; | ||
| 92 | } | ||
| 93 | dh->meth = meth; | ||
| 94 | if (meth->init) meth->init(dh); | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | DH *DH_new(void) | ||
| 99 | { | ||
| 100 | return DH_new_method(NULL); | ||
| 101 | } | ||
| 102 | |||
| 103 | DH *DH_new_method(ENGINE *engine) | ||
| 104 | { | ||
| 105 | DH *ret; | ||
| 106 | |||
| 107 | ret=(DH *)OPENSSL_malloc(sizeof(DH)); | ||
| 108 | if (ret == NULL) | ||
| 109 | { | ||
| 110 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); | ||
| 111 | return(NULL); | ||
| 112 | } | ||
| 113 | |||
| 114 | ret->meth = DH_get_default_method(); | ||
| 115 | if (engine) | ||
| 116 | { | ||
| 117 | if (!ENGINE_init(engine)) | ||
| 118 | { | ||
| 119 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); | ||
| 120 | OPENSSL_free(ret); | ||
| 121 | return NULL; | ||
| 122 | } | ||
| 123 | ret->engine = engine; | ||
| 124 | } | ||
| 125 | else | ||
| 126 | ret->engine = ENGINE_get_default_DH(); | ||
| 127 | if(ret->engine) | ||
| 128 | { | ||
| 129 | ret->meth = ENGINE_get_DH(ret->engine); | ||
| 130 | if(!ret->meth) | ||
| 131 | { | ||
| 132 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB); | ||
| 133 | ENGINE_finish(ret->engine); | ||
| 134 | OPENSSL_free(ret); | ||
| 135 | return NULL; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | ret->pad=0; | ||
| 140 | ret->version=0; | ||
| 141 | ret->p=NULL; | ||
| 142 | ret->g=NULL; | ||
| 143 | ret->length=0; | ||
| 144 | ret->pub_key=NULL; | ||
| 145 | ret->priv_key=NULL; | ||
| 146 | ret->q=NULL; | ||
| 147 | ret->j=NULL; | ||
| 148 | ret->seed = NULL; | ||
| 149 | ret->seedlen = 0; | ||
| 150 | ret->counter = NULL; | ||
| 151 | ret->method_mont_p=NULL; | ||
| 152 | ret->references = 1; | ||
| 153 | ret->flags=ret->meth->flags; | ||
| 154 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | ||
| 155 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
| 156 | { | ||
| 157 | if (ret->engine) | ||
| 158 | ENGINE_finish(ret->engine); | ||
| 159 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | ||
| 160 | OPENSSL_free(ret); | ||
| 161 | ret=NULL; | ||
| 162 | } | ||
| 163 | return(ret); | ||
| 164 | } | ||
| 165 | |||
| 166 | void DH_free(DH *r) | ||
| 167 | { | ||
| 168 | int i; | ||
| 169 | if(r == NULL) return; | ||
| 170 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); | ||
| 171 | #ifdef REF_PRINT | ||
| 172 | REF_PRINT("DH",r); | ||
| 173 | #endif | ||
| 174 | if (i > 0) return; | ||
| 175 | #ifdef REF_CHECK | ||
| 176 | if (i < 0) | ||
| 177 | { | ||
| 178 | fprintf(stderr,"DH_free, bad reference count\n"); | ||
| 179 | abort(); | ||
| 180 | } | ||
| 181 | #endif | ||
| 182 | |||
| 183 | if (r->meth->finish) | ||
| 184 | r->meth->finish(r); | ||
| 185 | if (r->engine) | ||
| 186 | ENGINE_finish(r->engine); | ||
| 187 | |||
| 188 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); | ||
| 189 | |||
| 190 | if (r->p != NULL) BN_clear_free(r->p); | ||
| 191 | if (r->g != NULL) BN_clear_free(r->g); | ||
| 192 | if (r->q != NULL) BN_clear_free(r->q); | ||
| 193 | if (r->j != NULL) BN_clear_free(r->j); | ||
| 194 | if (r->seed) OPENSSL_free(r->seed); | ||
| 195 | if (r->counter != NULL) BN_clear_free(r->counter); | ||
| 196 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | ||
| 197 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | ||
| 198 | OPENSSL_free(r); | ||
| 199 | } | ||
| 200 | |||
| 201 | int DH_up_ref(DH *r) | ||
| 202 | { | ||
| 203 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); | ||
| 204 | #ifdef REF_PRINT | ||
| 205 | REF_PRINT("DH",r); | ||
| 206 | #endif | ||
| 207 | #ifdef REF_CHECK | ||
| 208 | if (i < 2) | ||
| 209 | { | ||
| 210 | fprintf(stderr, "DH_up, bad reference count\n"); | ||
| 211 | abort(); | ||
| 212 | } | ||
| 213 | #endif | ||
| 214 | return ((i > 1) ? 1 : 0); | ||
| 215 | } | ||
| 216 | |||
| 217 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 218 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 219 | { | ||
| 220 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, | ||
| 221 | new_func, dup_func, free_func); | ||
| 222 | } | ||
| 223 | |||
| 224 | int DH_set_ex_data(DH *d, int idx, void *arg) | ||
| 225 | { | ||
| 226 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | ||
| 227 | } | ||
| 228 | |||
| 229 | void *DH_get_ex_data(DH *d, int idx) | ||
| 230 | { | ||
| 231 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | ||
| 232 | } | ||
| 233 | |||
| 234 | int DH_size(const DH *dh) | ||
| 235 | { | ||
| 236 | return(BN_num_bytes(dh->p)); | ||
| 237 | } | ||
