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 module deimos.openssl.dh;
60 
61 import deimos.openssl._d_util;
62 
63 import deimos.openssl.evp; // Needed for EVP_PKEY_ALG_CTRL.
64 
65 public import deimos.openssl.e_os2;
66 
67 version (OPENSSL_NO_DH) {
68   static assert(false, "DH is disabled.");
69 }
70 
71 version(OPENSSL_NO_BIO) {} else {
72 public import deimos.openssl.bio;
73 }
74 public import deimos.openssl.ossl_typ;
75 version(OPENSSL_NO_DEPRECATED) {} else {
76 public import deimos.openssl.bn;
77 }
78 
79 // #ifndef OPENSSL_DH_MAX_MODULUS_BITS
80 enum OPENSSL_DH_MAX_MODULUS_BITS = 10000;
81 // #endif
82 
83 enum DH_FLAG_CACHE_MONT_P = 0x01;
84 enum DH_FLAG_NO_EXP_CONSTTIME = 0x02; /* new with 0.9.7h; the built-in DH
85                                        * implementation now uses constant time
86                                        * modular exponentiation for secret exponents
87                                        * by default. This flag causes the
88                                        * faster variable sliding window method to
89                                        * be used for all exponents.
90                                        */
91 
92 /* If this flag is set the DH method is FIPS compliant and can be used
93  * in FIPS mode. This is set in the validated module method. If an
94  * application sets this flag in its own methods it is its reposibility
95  * to ensure the result is compliant.
96  */
97 
98 enum DH_FLAG_FIPS_METHOD = 0x0400;
99 
100 /* If this flag is set the operations normally disabled in FIPS mode are
101  * permitted it is then the applications responsibility to ensure that the
102  * usage is compliant.
103  */
104 
105 enum DH_FLAG_NON_FIPS_ALLOW = 0x0400;
106 
107 extern (C):
108 nothrow:
109 
110 /* Already defined in ossl_typ.h */
111 /* typedef dh_st DH; */
112 /* typedef dh_method DH_METHOD; */
113 
114 struct dh_method
115 	{
116 	const(char)* name;
117 	/* Methods here */
118 	ExternC!(int function(DH* dh)) generate_key;
119 	ExternC!(int function(ubyte* key,const(BIGNUM)* pub_key,DH* dh)) compute_key;
120 	ExternC!(int function(const(DH)* dh, BIGNUM* r, const(BIGNUM)* a,
121 				const(BIGNUM)* p, const(BIGNUM)* m, BN_CTX* ctx,
122 				BN_MONT_CTX* m_ctx)) bn_mod_exp; /* Can be null */
123 
124 	ExternC!(int function(DH* dh)) init_;
125 	ExternC!(int function(DH* dh)) finish;
126 	int flags;
127 	char* app_data;
128 	/* If this is non-NULL, it will be used to generate parameters */
129 	ExternC!(int function(DH* dh, int prime_len, int generator, BN_GENCB* cb)) generate_params;
130 	};
131 
132 struct dh_st
133 	{
134 	/* This first argument is used to pick up errors when
135 	 * a DH is passed instead of a EVP_PKEY */
136 	int pad;
137 	int version_;
138 	BIGNUM* p;
139 	BIGNUM* g;
140 	c_long length; /* optional */
141 	BIGNUM* pub_key;	/* g^x */
142 	BIGNUM* priv_key;	/* x */
143 
144 	int flags;
145 	BN_MONT_CTX* method_mont_p;
146 	/* Place holders if we want to do X9.42 DH */
147 	BIGNUM* q;
148 	BIGNUM* j;
149 	ubyte* seed;
150 	int seedlen;
151 	BIGNUM* counter;
152 
153 	int references;
154 	CRYPTO_EX_DATA ex_data;
155 	const(DH_METHOD)* meth;
156 	ENGINE* engine;
157 	};
158 
159 enum DH_GENERATOR_2 = 2;
160 /* enum DH_GENERATOR_3 = 3; */
161 enum DH_GENERATOR_5 = 5;
162 
163 /* DH_check error codes */
164 enum DH_CHECK_P_NOT_PRIME = 0x01;
165 enum DH_CHECK_P_NOT_SAFE_PRIME = 0x02;
166 enum DH_UNABLE_TO_CHECK_GENERATOR = 0x04;
167 enum DH_NOT_SUITABLE_GENERATOR = 0x08;
168 
169 /* DH_check_pub_key error codes */
170 enum DH_CHECK_PUBKEY_TOO_SMALL = 0x01;
171 enum DH_CHECK_PUBKEY_TOO_LARGE = 0x02;
172 
173 /* primes p where (p-1)/2 is prime too are called "safe"; we define
174    this for backward compatibility: */
175 alias DH_CHECK_P_NOT_SAFE_PRIME DH_CHECK_P_NOT_STRONG_PRIME;
176 
177 auto d2i_DHparams_fp()(FILE* fp, void** x) {
178 	return cast(DH*)ASN1_d2i_fp(cast(ExternC!(void* function()))&DH_new,
179 		cast(d2i_of_void*)&d2i_DHparams,fp,x);
180 }
181 auto i2d_DHparams_fp()(FILE* fp, void* x) { return ASN1_i2d_fp(cast(d2i_of_void*)&i2d_DHparams,fp, x); }
182 auto d2i_DHparams_bio()(BIO* bp, void** x) { return ASN1_d2i_bio_of!DH(&DH_new,&d2i_DHparams,bp,x); }
183 auto i2d_DHparams_bio()(BIO* bp, void** x) { return ASN1_i2d_bio_of_const!DH(&i2d_DHparams,bp,x); }
184 
185 DH* DHparams_dup(DH*);
186 
187 const(DH_METHOD)* DH_OpenSSL();
188 
189 void DH_set_default_method(const(DH_METHOD)* meth);
190 const(DH_METHOD)* DH_get_default_method();
191 int DH_set_method(DH* dh, const(DH_METHOD)* meth);
192 DH* DH_new_method(ENGINE* engine);
193 
194 DH* 	DH_new();
195 void	DH_free(DH* dh);
196 int	DH_up_ref(DH* dh);
197 int	DH_size(const(DH)* dh);
198 int DH_get_ex_new_index(c_long argl, void* argp, CRYPTO_EX_new* new_func,
199 	     CRYPTO_EX_dup* dup_func, CRYPTO_EX_free* free_func);
200 int DH_set_ex_data(DH* d, int idx, void* arg);
201 void* DH_get_ex_data(DH* d, int idx);
202 
203 /* Deprecated version */
204 version(OPENSSL_NO_DEPRECATED) {} else {
205 DH* 	DH_generate_parameters(int prime_len,int generator,
206 		ExternC!(void function(int,int,void*)) callback,void* cb_arg);
207 } /* !defined(OPENSSL_NO_DEPRECATED) */
208 
209 /* New version */
210 int	DH_generate_parameters_ex(DH* dh, int prime_len,int generator, BN_GENCB* cb);
211 
212 int	DH_check(const(DH)* dh,int* codes);
213 int	DH_check_pub_key(const(DH)* dh,const(BIGNUM)* pub_key, int* codes);
214 int	DH_generate_key(DH* dh);
215 int	DH_compute_key(ubyte* key,const(BIGNUM)* pub_key,DH* dh);
216 DH* 	d2i_DHparams(DH** a,const(ubyte)** pp, c_long length);
217 int	i2d_DHparams(const(DH)* a,ubyte** pp);
218 version(OPENSSL_NO_FP_API) {} else {
219 int	DHparams_print_fp(FILE* fp, const(DH)* x);
220 }
221 version(OPENSSL_NO_BIO) {
222 int	DHparams_print(char* bp, const(DH)* x);
223 } else {
224 int	DHparams_print(BIO* bp, const(DH)* x);
225 }
226 
227 auto EVP_PKEY_CTX_set_dh_paramgen_prime_len()(EVP_PKEY_CTX* ctx, int len) {
228 	return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
229 			EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, null);
230 }
231 
232 auto EVP_PKEY_CTX_set_dh_paramgen_generator()(EVP_PKEY_CTX* ctx, int gen) {
233 	return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
234 			EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, null);
235 }
236 
237 enum EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN = (EVP_PKEY_ALG_CTRL + 1);
238 enum EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR = (EVP_PKEY_ALG_CTRL + 2);
239 
240 
241 /* BEGIN ERROR CODES */
242 /* The following lines are auto generated by the script mkerr.pl. Any changes
243  * made after this point may be overwritten when the script is next run.
244  */
245 void ERR_load_DH_strings();
246 
247 /* Error codes for the DH functions. */
248 
249 /* Function codes. */
250 enum DH_F_COMPUTE_KEY = 102;
251 enum DH_F_DHPARAMS_PRINT_FP = 101;
252 enum DH_F_DH_BUILTIN_GENPARAMS = 106;
253 enum DH_F_DH_COMPUTE_KEY = 114;
254 enum DH_F_DH_GENERATE_KEY = 115;
255 enum DH_F_DH_GENERATE_PARAMETERS_EX = 116;
256 enum DH_F_DH_NEW_METHOD = 105;
257 enum DH_F_DH_PARAM_DECODE = 107;
258 enum DH_F_DH_PRIV_DECODE = 110;
259 enum DH_F_DH_PRIV_ENCODE = 111;
260 enum DH_F_DH_PUB_DECODE = 108;
261 enum DH_F_DH_PUB_ENCODE = 109;
262 enum DH_F_DO_DH_PRINT = 100;
263 enum DH_F_GENERATE_KEY = 103;
264 enum DH_F_GENERATE_PARAMETERS = 104;
265 enum DH_F_PKEY_DH_DERIVE = 112;
266 enum DH_F_PKEY_DH_KEYGEN = 113;
267 
268 /* Reason codes. */
269 enum DH_R_BAD_GENERATOR = 101;
270 enum DH_R_BN_DECODE_ERROR = 109;
271 enum DH_R_BN_ERROR = 106;
272 enum DH_R_DECODE_ERROR = 104;
273 enum DH_R_INVALID_PUBKEY = 102;
274 enum DH_R_KEYS_NOT_SET = 108;
275 enum DH_R_KEY_SIZE_TOO_SMALL = 110;
276 enum DH_R_MODULUS_TOO_LARGE = 103;
277 enum DH_R_NON_FIPS_METHOD = 111;
278 enum DH_R_NO_PARAMETERS_SET = 107;
279 enum DH_R_NO_PRIVATE_VALUE = 100;
280 enum DH_R_PARAMETER_ENCODING_ERROR = 105;