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