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