summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dh')
-rw-r--r--src/lib/libcrypto/dh/dh.h213
-rw-r--r--src/lib/libcrypto/dh/dh_check.c120
-rw-r--r--src/lib/libcrypto/dh/dh_err.c99
-rw-r--r--src/lib/libcrypto/dh/dh_gen.c153
-rw-r--r--src/lib/libcrypto/dh/dh_key.c216
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c237
6 files changed, 0 insertions, 1038 deletions
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h
deleted file mode 100644
index 7a8d9f88c2..0000000000
--- a/src/lib/libcrypto/dh/dh.h
+++ /dev/null
@@ -1,213 +0,0 @@
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 NO_DH
63#error DH is disabled.
64#endif
65
66#ifndef NO_BIO
67#include <openssl/bio.h>
68#endif
69#include <openssl/bn.h>
70#include <openssl/crypto.h>
71
72#define DH_FLAG_CACHE_MONT_P 0x01
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78typedef struct dh_st DH;
79
80typedef struct dh_method {
81 const char *name;
82 /* Methods here */
83 int (*generate_key)(DH *dh);
84 int (*compute_key)(unsigned char *key,BIGNUM *pub_key,DH *dh);
85 int (*bn_mod_exp)(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
86 const BIGNUM *m, BN_CTX *ctx,
87 BN_MONT_CTX *m_ctx); /* Can be null */
88
89 int (*init)(DH *dh);
90 int (*finish)(DH *dh);
91 int flags;
92 char *app_data;
93} DH_METHOD;
94
95struct dh_st
96 {
97 /* This first argument is used to pick up errors when
98 * a DH is passed instead of a EVP_PKEY */
99 int pad;
100 int version;
101 BIGNUM *p;
102 BIGNUM *g;
103 int length; /* optional */
104 BIGNUM *pub_key; /* g^x */
105 BIGNUM *priv_key; /* x */
106
107 int flags;
108 char *method_mont_p;
109 /* Place holders if we want to do X9.42 DH */
110 BIGNUM *q;
111 BIGNUM *j;
112 unsigned char *seed;
113 int seedlen;
114 BIGNUM *counter;
115
116 int references;
117 CRYPTO_EX_DATA ex_data;
118#if 0
119 DH_METHOD *meth;
120#else
121 struct engine_st *engine;
122#endif
123 };
124
125#define DH_GENERATOR_2 2
126/* #define DH_GENERATOR_3 3 */
127#define DH_GENERATOR_5 5
128
129/* DH_check error codes */
130#define DH_CHECK_P_NOT_PRIME 0x01
131#define DH_CHECK_P_NOT_SAFE_PRIME 0x02
132#define DH_UNABLE_TO_CHECK_GENERATOR 0x04
133#define DH_NOT_SUITABLE_GENERATOR 0x08
134
135/* primes p where (p-1)/2 is prime too are called "safe"; we define
136 this for backward compatibility: */
137#define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME
138
139#define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \
140 (char *(*)())d2i_DHparams,(char *)(x))
141#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
142 (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
143#define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
144 (unsigned char *)(x))
145#define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \
146 (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x))
147#ifdef __cplusplus
148#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio((int (*)())i2d_DHparams,(bp), \
149 (unsigned char *)(x))
150#else
151#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \
152 (unsigned char *)(x))
153#endif
154
155DH_METHOD *DH_OpenSSL(void);
156
157void DH_set_default_openssl_method(DH_METHOD *meth);
158DH_METHOD *DH_get_default_openssl_method(void);
159#if 0
160DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth);
161DH *DH_new_method(DH_METHOD *meth);
162#else
163int DH_set_method(DH *dh, struct engine_st *engine);
164DH *DH_new_method(struct engine_st *engine);
165#endif
166
167DH * DH_new(void);
168void DH_free(DH *dh);
169int DH_size(DH *dh);
170int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
171 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
172int DH_set_ex_data(DH *d, int idx, void *arg);
173void *DH_get_ex_data(DH *d, int idx);
174DH * DH_generate_parameters(int prime_len,int generator,
175 void (*callback)(int,int,void *),void *cb_arg);
176int DH_check(DH *dh,int *codes);
177int DH_generate_key(DH *dh);
178int DH_compute_key(unsigned char *key,BIGNUM *pub_key,DH *dh);
179DH * d2i_DHparams(DH **a,unsigned char **pp, long length);
180int i2d_DHparams(DH *a,unsigned char **pp);
181#ifndef NO_FP_API
182int DHparams_print_fp(FILE *fp, DH *x);
183#endif
184#ifndef NO_BIO
185int DHparams_print(BIO *bp, DH *x);
186#else
187int DHparams_print(char *bp, DH *x);
188#endif
189void ERR_load_DH_strings(void );
190
191/* BEGIN ERROR CODES */
192/* The following lines are auto generated by the script mkerr.pl. Any changes
193 * made after this point may be overwritten when the script is next run.
194 */
195
196/* Error codes for the DH functions. */
197
198/* Function codes. */
199#define DH_F_DHPARAMS_PRINT 100
200#define DH_F_DHPARAMS_PRINT_FP 101
201#define DH_F_DH_COMPUTE_KEY 102
202#define DH_F_DH_GENERATE_KEY 103
203#define DH_F_DH_GENERATE_PARAMETERS 104
204#define DH_F_DH_NEW 105
205
206/* Reason codes. */
207#define DH_R_NO_PRIVATE_VALUE 100
208
209#ifdef __cplusplus
210}
211#endif
212#endif
213
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
deleted file mode 100644
index 7e5cfd8bfc..0000000000
--- a/src/lib/libcrypto/dh/dh_check.c
+++ /dev/null
@@ -1,120 +0,0 @@
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
73int DH_check(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;
116err:
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
deleted file mode 100644
index ff2d1684c2..0000000000
--- a/src/lib/libcrypto/dh/dh_err.c
+++ /dev/null
@@ -1,99 +0,0 @@
1/* crypto/dh/dh_err.c */
2/* ====================================================================
3 * Copyright (c) 1999 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 NO_ERR
67static 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,0), "DH_new"},
75{0,NULL}
76 };
77
78static ERR_STRING_DATA DH_str_reasons[]=
79 {
80{DH_R_NO_PRIVATE_VALUE ,"no private value"},
81{0,NULL}
82 };
83
84#endif
85
86void ERR_load_DH_strings(void)
87 {
88 static int init=1;
89
90 if (init)
91 {
92 init=0;
93#ifndef NO_ERR
94 ERR_load_strings(ERR_LIB_DH,DH_str_functs);
95 ERR_load_strings(ERR_LIB_DH,DH_str_reasons);
96#endif
97
98 }
99 }
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c
deleted file mode 100644
index 7a6a38fbb4..0000000000
--- a/src/lib/libcrypto/dh/dh_gen.c
+++ /dev/null
@@ -1,153 +0,0 @@
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
86DH *DH_generate_parameters(int prime_len, int generator,
87 void (*callback)(int,int,void *), void *cb_arg)
88 {
89 BIGNUM *p=NULL,*t1,*t2;
90 DH *ret=NULL;
91 int g,ok= -1;
92 BN_CTX *ctx=NULL;
93
94 ret=DH_new();
95 if (ret == NULL) goto err;
96 ctx=BN_CTX_new();
97 if (ctx == NULL) goto err;
98 BN_CTX_start(ctx);
99 t1 = BN_CTX_get(ctx);
100 t2 = BN_CTX_get(ctx);
101 if (t1 == NULL || t2 == NULL) goto err;
102
103 if (generator == DH_GENERATOR_2)
104 {
105 BN_set_word(t1,24);
106 BN_set_word(t2,11);
107 g=2;
108 }
109#ifdef undef /* does not work for safe primes */
110 else if (generator == DH_GENERATOR_3)
111 {
112 BN_set_word(t1,12);
113 BN_set_word(t2,5);
114 g=3;
115 }
116#endif
117 else if (generator == DH_GENERATOR_5)
118 {
119 BN_set_word(t1,10);
120 BN_set_word(t2,3);
121 /* BN_set_word(t3,7); just have to miss
122 * out on these ones :-( */
123 g=5;
124 }
125 else
126 g=generator;
127
128 p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg);
129 if (p == NULL) goto err;
130 if (callback != NULL) callback(3,0,cb_arg);
131 ret->p=p;
132 ret->g=BN_new();
133 if (!BN_set_word(ret->g,g)) goto err;
134 ok=1;
135err:
136 if (ok == -1)
137 {
138 DHerr(DH_F_DH_GENERATE_PARAMETERS,ERR_R_BN_LIB);
139 ok=0;
140 }
141
142 if (ctx != NULL)
143 {
144 BN_CTX_end(ctx);
145 BN_CTX_free(ctx);
146 }
147 if (!ok && (ret != NULL))
148 {
149 DH_free(ret);
150 ret=NULL;
151 }
152 return(ret);
153 }
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
deleted file mode 100644
index 22b087b778..0000000000
--- a/src/lib/libcrypto/dh/dh_key.c
+++ /dev/null
@@ -1,216 +0,0 @@
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
66static int generate_key(DH *dh);
67static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh);
68static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
69 const BIGNUM *m, BN_CTX *ctx,
70 BN_MONT_CTX *m_ctx);
71static int dh_init(DH *dh);
72static int dh_finish(DH *dh);
73
74int DH_generate_key(DH *dh)
75 {
76 return ENGINE_get_DH(dh->engine)->generate_key(dh);
77 }
78
79int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)
80 {
81 return ENGINE_get_DH(dh->engine)->compute_key(key, pub_key, dh);
82 }
83
84static DH_METHOD dh_ossl = {
85"OpenSSL DH Method",
86generate_key,
87compute_key,
88dh_bn_mod_exp,
89dh_init,
90dh_finish,
910,
92NULL
93};
94
95DH_METHOD *DH_OpenSSL(void)
96{
97 return &dh_ossl;
98}
99
100static int generate_key(DH *dh)
101 {
102 int ok=0;
103 BN_CTX ctx;
104 BN_MONT_CTX *mont;
105 BIGNUM *pub_key=NULL,*priv_key=NULL;
106
107 BN_CTX_init(&ctx);
108
109 if (dh->priv_key == NULL)
110 {
111 priv_key=BN_new();
112 if (priv_key == NULL) goto err;
113 do
114 if (!BN_rand_range(priv_key, dh->p)) goto err;
115 while (BN_is_zero(priv_key));
116 }
117 else
118 priv_key=dh->priv_key;
119
120 if (dh->pub_key == NULL)
121 {
122 pub_key=BN_new();
123 if (pub_key == NULL) goto err;
124 }
125 else
126 pub_key=dh->pub_key;
127
128 if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
129 {
130 if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
131 if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
132 dh->p,&ctx)) goto err;
133 }
134 mont=(BN_MONT_CTX *)dh->method_mont_p;
135
136 if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, pub_key, dh->g,
137 priv_key,dh->p,&ctx,mont))
138 goto err;
139
140 dh->pub_key=pub_key;
141 dh->priv_key=priv_key;
142 ok=1;
143err:
144 if (ok != 1)
145 DHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);
146
147 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
148 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
149 BN_CTX_free(&ctx);
150 return(ok);
151 }
152
153static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)
154 {
155 BN_CTX ctx;
156 BN_MONT_CTX *mont;
157 BIGNUM *tmp;
158 int ret= -1;
159
160 BN_CTX_init(&ctx);
161 BN_CTX_start(&ctx);
162 tmp = BN_CTX_get(&ctx);
163
164 if (dh->priv_key == NULL)
165 {
166 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
167 goto err;
168 }
169 if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
170 {
171 if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
172 if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
173 dh->p,&ctx)) goto err;
174 }
175
176 mont=(BN_MONT_CTX *)dh->method_mont_p;
177 if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, tmp, pub_key,
178 dh->priv_key,dh->p,&ctx,mont))
179 {
180 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
181 goto err;
182 }
183
184 ret=BN_bn2bin(tmp,key);
185err:
186 BN_CTX_end(&ctx);
187 BN_CTX_free(&ctx);
188 return(ret);
189 }
190
191static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
192 const BIGNUM *m, BN_CTX *ctx,
193 BN_MONT_CTX *m_ctx)
194 {
195 if (a->top == 1)
196 {
197 BN_ULONG A = a->d[0];
198 return BN_mod_exp_mont_word(r,A,p,m,ctx,m_ctx);
199 }
200 else
201 return BN_mod_exp_mont(r,a,p,m,ctx,m_ctx);
202 }
203
204
205static int dh_init(DH *dh)
206 {
207 dh->flags |= DH_FLAG_CACHE_MONT_P;
208 return(1);
209 }
210
211static int dh_finish(DH *dh)
212 {
213 if(dh->method_mont_p)
214 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p);
215 return(1);
216 }
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
deleted file mode 100644
index 96f118c153..0000000000
--- a/src/lib/libcrypto/dh/dh_lib.c
+++ /dev/null
@@ -1,237 +0,0 @@
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
65const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
66
67static DH_METHOD *default_DH_method;
68static int dh_meth_num = 0;
69static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL;
70
71void DH_set_default_openssl_method(DH_METHOD *meth)
72{
73 ENGINE *e;
74 /* We'll need to notify the "openssl" ENGINE of this
75 * change too. We won't bother locking things down at
76 * our end as there was never any locking in these
77 * functions! */
78 if(default_DH_method != meth)
79 {
80 default_DH_method = meth;
81 e = ENGINE_by_id("openssl");
82 if(e)
83 {
84 ENGINE_set_DH(e, meth);
85 ENGINE_free(e);
86 }
87 }
88}
89
90DH_METHOD *DH_get_default_openssl_method(void)
91{
92 if(!default_DH_method) default_DH_method = DH_OpenSSL();
93 return default_DH_method;
94}
95
96#if 0
97DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth)
98{
99 DH_METHOD *mtmp;
100 mtmp = dh->meth;
101 if (mtmp->finish) mtmp->finish(dh);
102 dh->meth = meth;
103 if (meth->init) meth->init(dh);
104 return mtmp;
105}
106#else
107int DH_set_method(DH *dh, ENGINE *engine)
108{
109 ENGINE *mtmp;
110 DH_METHOD *meth;
111 mtmp = dh->engine;
112 meth = ENGINE_get_DH(mtmp);
113 if (!ENGINE_init(engine))
114 return 0;
115 if (meth->finish) meth->finish(dh);
116 dh->engine= engine;
117 meth = ENGINE_get_DH(engine);
118 if (meth->init) meth->init(dh);
119 /* SHOULD ERROR CHECK THIS!!! */
120 ENGINE_finish(mtmp);
121 return 1;
122}
123#endif
124
125DH *DH_new(void)
126{
127 return DH_new_method(NULL);
128}
129
130#if 0
131DH *DH_new_method(DH_METHOD *meth)
132#else
133DH *DH_new_method(ENGINE *engine)
134#endif
135 {
136 DH_METHOD *meth;
137 DH *ret;
138 ret=(DH *)OPENSSL_malloc(sizeof(DH));
139
140 if (ret == NULL)
141 {
142 DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE);
143 return(NULL);
144 }
145 if(engine)
146 ret->engine = engine;
147 else
148 {
149 if((ret->engine=ENGINE_get_default_DH()) == NULL)
150 {
151 OPENSSL_free(ret);
152 return NULL;
153 }
154 }
155 meth = ENGINE_get_DH(ret->engine);
156 ret->pad=0;
157 ret->version=0;
158 ret->p=NULL;
159 ret->g=NULL;
160 ret->length=0;
161 ret->pub_key=NULL;
162 ret->priv_key=NULL;
163 ret->q=NULL;
164 ret->j=NULL;
165 ret->seed = NULL;
166 ret->seedlen = 0;
167 ret->counter = NULL;
168 ret->method_mont_p=NULL;
169 ret->references = 1;
170 ret->flags=meth->flags;
171 CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data);
172 if ((meth->init != NULL) && !meth->init(ret))
173 {
174 CRYPTO_free_ex_data(dh_meth,ret,&ret->ex_data);
175 OPENSSL_free(ret);
176 ret=NULL;
177 }
178 return(ret);
179 }
180
181void DH_free(DH *r)
182 {
183 DH_METHOD *meth;
184 int i;
185 if(r == NULL) return;
186 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
187#ifdef REF_PRINT
188 REF_PRINT("DH",r);
189#endif
190 if (i > 0) return;
191#ifdef REF_CHECK
192 if (i < 0)
193 {
194 fprintf(stderr,"DH_free, bad reference count\n");
195 abort();
196 }
197#endif
198
199 meth = ENGINE_get_DH(r->engine);
200 if(meth->finish) meth->finish(r);
201 ENGINE_finish(r->engine);
202
203 CRYPTO_free_ex_data(dh_meth, r, &r->ex_data);
204
205 if (r->p != NULL) BN_clear_free(r->p);
206 if (r->g != NULL) BN_clear_free(r->g);
207 if (r->q != NULL) BN_clear_free(r->q);
208 if (r->j != NULL) BN_clear_free(r->j);
209 if (r->seed) OPENSSL_free(r->seed);
210 if (r->counter != NULL) BN_clear_free(r->counter);
211 if (r->pub_key != NULL) BN_clear_free(r->pub_key);
212 if (r->priv_key != NULL) BN_clear_free(r->priv_key);
213 OPENSSL_free(r);
214 }
215
216int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
217 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
218 {
219 dh_meth_num++;
220 return(CRYPTO_get_ex_new_index(dh_meth_num-1,
221 &dh_meth,argl,argp,new_func,dup_func,free_func));
222 }
223
224int DH_set_ex_data(DH *d, int idx, void *arg)
225 {
226 return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
227 }
228
229void *DH_get_ex_data(DH *d, int idx)
230 {
231 return(CRYPTO_get_ex_data(&d->ex_data,idx));
232 }
233
234int DH_size(DH *dh)
235 {
236 return(BN_num_bytes(dh->p));
237 }