]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #ifndef OPENSSL_DH_H | |
11 | # define OPENSSL_DH_H | |
12 | # pragma once | |
13 | ||
14 | # include <openssl/macros.h> | |
15 | # ifndef OPENSSL_NO_DEPRECATED_3_0 | |
16 | # define HEADER_DH_H | |
17 | # endif | |
18 | ||
19 | # include <openssl/opensslconf.h> | |
20 | ||
21 | # ifndef OPENSSL_NO_DH | |
22 | # include <openssl/e_os2.h> | |
23 | # include <openssl/bio.h> | |
24 | # include <openssl/asn1.h> | |
25 | # include <openssl/types.h> | |
26 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 | |
27 | # include <openssl/bn.h> | |
28 | # endif | |
29 | # include <openssl/dherr.h> | |
30 | ||
31 | # ifdef __cplusplus | |
32 | extern "C" { | |
33 | # endif | |
34 | ||
35 | # ifndef OPENSSL_DH_MAX_MODULUS_BITS | |
36 | # define OPENSSL_DH_MAX_MODULUS_BITS 10000 | |
37 | # endif | |
38 | ||
39 | # define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024 | |
40 | ||
41 | # define DH_FLAG_CACHE_MONT_P 0x01 | |
42 | ||
43 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 | |
44 | /* | |
45 | * Does nothing. Previously this switched off constant time behaviour. | |
46 | */ | |
47 | # define DH_FLAG_NO_EXP_CONSTTIME 0x00 | |
48 | # endif | |
49 | ||
50 | /* | |
51 | * If this flag is set the DH method is FIPS compliant and can be used in | |
52 | * FIPS mode. This is set in the validated module method. If an application | |
53 | * sets this flag in its own methods it is its responsibility to ensure the | |
54 | * result is compliant. | |
55 | */ | |
56 | ||
57 | # define DH_FLAG_FIPS_METHOD 0x0400 | |
58 | ||
59 | /* | |
60 | * If this flag is set the operations normally disabled in FIPS mode are | |
61 | * permitted it is then the applications responsibility to ensure that the | |
62 | * usage is compliant. | |
63 | */ | |
64 | ||
65 | # define DH_FLAG_NON_FIPS_ALLOW 0x0400 | |
66 | ||
67 | /* Already defined in ossl_typ.h */ | |
68 | /* typedef struct dh_st DH; */ | |
69 | /* typedef struct dh_method DH_METHOD; */ | |
70 | ||
71 | DECLARE_ASN1_ITEM(DHparams) | |
72 | ||
73 | # define DH_GENERATOR_2 2 | |
74 | # define DH_GENERATOR_3 3 | |
75 | # define DH_GENERATOR_5 5 | |
76 | ||
77 | /* DH_check error codes */ | |
78 | /* | |
79 | * NB: These values must align with the equivalently named macros in | |
80 | * internal/ffc.h. | |
81 | */ | |
82 | # define DH_CHECK_P_NOT_PRIME 0x01 | |
83 | # define DH_CHECK_P_NOT_SAFE_PRIME 0x02 | |
84 | # define DH_UNABLE_TO_CHECK_GENERATOR 0x04 | |
85 | # define DH_NOT_SUITABLE_GENERATOR 0x08 | |
86 | # define DH_CHECK_Q_NOT_PRIME 0x10 | |
87 | # define DH_CHECK_INVALID_Q_VALUE 0x20 | |
88 | # define DH_CHECK_INVALID_J_VALUE 0x40 | |
89 | # define DH_MODULUS_TOO_SMALL 0x80 | |
90 | # define DH_MODULUS_TOO_LARGE 0x100 | |
91 | ||
92 | /* DH_check_pub_key error codes */ | |
93 | # define DH_CHECK_PUBKEY_TOO_SMALL 0x01 | |
94 | # define DH_CHECK_PUBKEY_TOO_LARGE 0x02 | |
95 | # define DH_CHECK_PUBKEY_INVALID 0x04 | |
96 | ||
97 | /* | |
98 | * primes p where (p-1)/2 is prime too are called "safe"; we define this for | |
99 | * backward compatibility: | |
100 | */ | |
101 | # define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME | |
102 | ||
103 | /* DH parameter generation types used by EVP_PKEY_CTX_set_dh_paramgen_type() */ | |
104 | # define DH_PARAMGEN_TYPE_GENERATOR 0 /* Use a generator g */ | |
105 | # define DH_PARAMGEN_TYPE_FIPS_186_2 1 /* Use legacy FIPS186-2 standard */ | |
106 | # define DH_PARAMGEN_TYPE_FIPS_186_4 2 /* Use FIPS186-4 standard */ | |
107 | ||
108 | # define d2i_DHparams_fp(fp,x) \ | |
109 | (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ | |
110 | (char *(*)())d2i_DHparams, \ | |
111 | (fp), \ | |
112 | (unsigned char **)(x)) | |
113 | # define i2d_DHparams_fp(fp,x) \ | |
114 | ASN1_i2d_fp(i2d_DHparams,(fp), (unsigned char *)(x)) | |
115 | # define d2i_DHparams_bio(bp,x) \ | |
116 | ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, bp, x) | |
117 | # define i2d_DHparams_bio(bp,x) \ | |
118 | ASN1_i2d_bio_of(DH,i2d_DHparams,bp,x) | |
119 | ||
120 | # define d2i_DHxparams_fp(fp,x) \ | |
121 | (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ | |
122 | (char *(*)())d2i_DHxparams, \ | |
123 | (fp), \ | |
124 | (unsigned char **)(x)) | |
125 | # define i2d_DHxparams_fp(fp,x) \ | |
126 | ASN1_i2d_fp(i2d_DHxparams,(fp), (unsigned char *)(x)) | |
127 | # define d2i_DHxparams_bio(bp,x) \ | |
128 | ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, bp, x) | |
129 | # define i2d_DHxparams_bio(bp,x) \ | |
130 | ASN1_i2d_bio_of(DH, i2d_DHxparams, bp, x) | |
131 | ||
132 | DECLARE_ASN1_DUP_FUNCTION_name(DH, DHparams) | |
133 | ||
134 | const DH_METHOD *DH_OpenSSL(void); | |
135 | ||
136 | void DH_set_default_method(const DH_METHOD *meth); | |
137 | const DH_METHOD *DH_get_default_method(void); | |
138 | int DH_set_method(DH *dh, const DH_METHOD *meth); | |
139 | DH *DH_new_method(ENGINE *engine); | |
140 | ||
141 | DH *DH_new(void); | |
142 | void DH_free(DH *dh); | |
143 | int DH_up_ref(DH *dh); | |
144 | int DH_bits(const DH *dh); | |
145 | int DH_size(const DH *dh); | |
146 | int DH_security_bits(const DH *dh); | |
147 | #define DH_get_ex_new_index(l, p, newf, dupf, freef) \ | |
148 | CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, l, p, newf, dupf, freef) | |
149 | int DH_set_ex_data(DH *d, int idx, void *arg); | |
150 | void *DH_get_ex_data(DH *d, int idx); | |
151 | ||
152 | /* Deprecated version */ | |
153 | DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator, | |
154 | void (*callback) (int, int, | |
155 | void *), | |
156 | void *cb_arg)) | |
157 | ||
158 | /* New version */ | |
159 | int DH_generate_parameters_ex(DH *dh, int prime_len, int generator, | |
160 | BN_GENCB *cb); | |
161 | ||
162 | int DH_check_params_ex(const DH *dh); | |
163 | int DH_check_ex(const DH *dh); | |
164 | int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key); | |
165 | int DH_check_params(const DH *dh, int *ret); | |
166 | int DH_check(const DH *dh, int *codes); | |
167 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes); | |
168 | int DH_generate_key(DH *dh); | |
169 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
170 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
171 | DECLARE_ASN1_ENCODE_FUNCTIONS_only(DH, DHparams) | |
172 | DECLARE_ASN1_ENCODE_FUNCTIONS_only(DH, DHxparams) | |
173 | # ifndef OPENSSL_NO_STDIO | |
174 | int DHparams_print_fp(FILE *fp, const DH *x); | |
175 | # endif | |
176 | int DHparams_print(BIO *bp, const DH *x); | |
177 | ||
178 | /* RFC 5114 parameters */ | |
179 | DH *DH_get_1024_160(void); | |
180 | DH *DH_get_2048_224(void); | |
181 | DH *DH_get_2048_256(void); | |
182 | ||
183 | /* Named parameters, currently RFC7919 and RFC3526 */ | |
184 | DH *DH_new_by_nid(int nid); | |
185 | int DH_get_nid(DH *dh); | |
186 | ||
187 | # ifndef OPENSSL_NO_CMS | |
188 | /* RFC2631 KDF */ | |
189 | int DH_KDF_X9_42(unsigned char *out, size_t outlen, | |
190 | const unsigned char *Z, size_t Zlen, | |
191 | ASN1_OBJECT *key_oid, | |
192 | const unsigned char *ukm, size_t ukmlen, const EVP_MD *md); | |
193 | # endif | |
194 | ||
195 | void DH_get0_pqg(const DH *dh, | |
196 | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g); | |
197 | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); | |
198 | void DH_get0_key(const DH *dh, | |
199 | const BIGNUM **pub_key, const BIGNUM **priv_key); | |
200 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); | |
201 | const BIGNUM *DH_get0_p(const DH *dh); | |
202 | const BIGNUM *DH_get0_q(const DH *dh); | |
203 | const BIGNUM *DH_get0_g(const DH *dh); | |
204 | const BIGNUM *DH_get0_priv_key(const DH *dh); | |
205 | const BIGNUM *DH_get0_pub_key(const DH *dh); | |
206 | void DH_clear_flags(DH *dh, int flags); | |
207 | int DH_test_flags(const DH *dh, int flags); | |
208 | void DH_set_flags(DH *dh, int flags); | |
209 | ENGINE *DH_get0_engine(DH *d); | |
210 | long DH_get_length(const DH *dh); | |
211 | int DH_set_length(DH *dh, long length); | |
212 | ||
213 | DH_METHOD *DH_meth_new(const char *name, int flags); | |
214 | void DH_meth_free(DH_METHOD *dhm); | |
215 | DH_METHOD *DH_meth_dup(const DH_METHOD *dhm); | |
216 | const char *DH_meth_get0_name(const DH_METHOD *dhm); | |
217 | int DH_meth_set1_name(DH_METHOD *dhm, const char *name); | |
218 | int DH_meth_get_flags(const DH_METHOD *dhm); | |
219 | int DH_meth_set_flags(DH_METHOD *dhm, int flags); | |
220 | void *DH_meth_get0_app_data(const DH_METHOD *dhm); | |
221 | int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data); | |
222 | int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *); | |
223 | int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *)); | |
224 | int (*DH_meth_get_compute_key(const DH_METHOD *dhm)) | |
225 | (unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
226 | int DH_meth_set_compute_key(DH_METHOD *dhm, | |
227 | int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh)); | |
228 | int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm)) | |
229 | (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, | |
230 | BN_CTX *, BN_MONT_CTX *); | |
231 | int DH_meth_set_bn_mod_exp(DH_METHOD *dhm, | |
232 | int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, | |
233 | const BIGNUM *, BN_CTX *, BN_MONT_CTX *)); | |
234 | int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *); | |
235 | int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *)); | |
236 | int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *); | |
237 | int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *)); | |
238 | int (*DH_meth_get_generate_params(const DH_METHOD *dhm)) | |
239 | (DH *, int, int, BN_GENCB *); | |
240 | int DH_meth_set_generate_params(DH_METHOD *dhm, | |
241 | int (*generate_params) (DH *, int, int, BN_GENCB *)); | |
242 | ||
243 | ||
244 | # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \ | |
245 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
246 | EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL) | |
247 | ||
248 | # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \ | |
249 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
250 | EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL) | |
251 | ||
252 | # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \ | |
253 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
254 | EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL) | |
255 | ||
256 | # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \ | |
257 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
258 | EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL) | |
259 | ||
260 | # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \ | |
261 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ | |
262 | EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) | |
263 | ||
264 | # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \ | |
265 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ | |
266 | EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) | |
267 | ||
268 | # define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \ | |
269 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \ | |
270 | EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \ | |
271 | EVP_PKEY_CTRL_DH_NID, nid, NULL) | |
272 | ||
273 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad); | |
274 | ||
275 | # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \ | |
276 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
277 | EVP_PKEY_OP_DERIVE, \ | |
278 | EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL) | |
279 | ||
280 | # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \ | |
281 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
282 | EVP_PKEY_OP_DERIVE, \ | |
283 | EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL) | |
284 | ||
285 | # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \ | |
286 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
287 | EVP_PKEY_OP_DERIVE, \ | |
288 | EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid)) | |
289 | ||
290 | # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \ | |
291 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
292 | EVP_PKEY_OP_DERIVE, \ | |
293 | EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid)) | |
294 | ||
295 | # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \ | |
296 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
297 | EVP_PKEY_OP_DERIVE, \ | |
298 | EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md)) | |
299 | ||
300 | # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \ | |
301 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
302 | EVP_PKEY_OP_DERIVE, \ | |
303 | EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd)) | |
304 | ||
305 | # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \ | |
306 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
307 | EVP_PKEY_OP_DERIVE, \ | |
308 | EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL) | |
309 | ||
310 | # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \ | |
311 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
312 | EVP_PKEY_OP_DERIVE, \ | |
313 | EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen)) | |
314 | ||
315 | # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \ | |
316 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
317 | EVP_PKEY_OP_DERIVE, \ | |
318 | EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p)) | |
319 | ||
320 | # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \ | |
321 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
322 | EVP_PKEY_OP_DERIVE, \ | |
323 | EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p)) | |
324 | ||
325 | # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1) | |
326 | # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2) | |
327 | # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3) | |
328 | # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4) | |
329 | # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5) | |
330 | # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6) | |
331 | # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7) | |
332 | # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8) | |
333 | # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9) | |
334 | # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10) | |
335 | # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11) | |
336 | # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12) | |
337 | # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13) | |
338 | # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14) | |
339 | # define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15) | |
340 | # define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16) | |
341 | ||
342 | /* KDF types */ | |
343 | # define EVP_PKEY_DH_KDF_NONE 1 | |
344 | # ifndef OPENSSL_NO_CMS | |
345 | # define EVP_PKEY_DH_KDF_X9_42 2 | |
346 | # endif | |
347 | ||
348 | ||
349 | # ifdef __cplusplus | |
350 | } | |
351 | # endif | |
352 | # endif | |
353 | #endif |