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