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 HEADER_DH_H
17# endif
18
19# include <openssl/opensslconf.h>
20# include <openssl/types.h>
21
22# ifdef __cplusplus
23extern "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
34int EVP_PKEY_CTX_set_dh_paramgen_type(EVP_PKEY_CTX *ctx, int typ);
35int EVP_PKEY_CTX_set_dh_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex);
36int EVP_PKEY_CTX_set_dh_paramgen_seed(EVP_PKEY_CTX *ctx,
37 const unsigned char *seed,
38 size_t seedlen);
39int EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits);
40int EVP_PKEY_CTX_set_dh_paramgen_subprime_len(EVP_PKEY_CTX *ctx, int qlen);
41int EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen);
42int EVP_PKEY_CTX_set_dh_nid(EVP_PKEY_CTX *ctx, int nid);
43int EVP_PKEY_CTX_set_dh_rfc5114(EVP_PKEY_CTX *ctx, int gen);
44int EVP_PKEY_CTX_set_dhx_rfc5114(EVP_PKEY_CTX *ctx, int gen);
45int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad);
46
47int EVP_PKEY_CTX_set_dh_kdf_type(EVP_PKEY_CTX *ctx, int kdf);
48int EVP_PKEY_CTX_get_dh_kdf_type(EVP_PKEY_CTX *ctx);
49int EVP_PKEY_CTX_set0_dh_kdf_oid(EVP_PKEY_CTX *ctx, ASN1_OBJECT *oid);
50int EVP_PKEY_CTX_get0_dh_kdf_oid(EVP_PKEY_CTX *ctx, ASN1_OBJECT **oid);
51int EVP_PKEY_CTX_set_dh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md);
52int EVP_PKEY_CTX_get_dh_kdf_md(EVP_PKEY_CTX *ctx, const EVP_MD **md);
53int EVP_PKEY_CTX_set_dh_kdf_outlen(EVP_PKEY_CTX *ctx, int len);
54int EVP_PKEY_CTX_get_dh_kdf_outlen(EVP_PKEY_CTX *ctx, int *len);
55int EVP_PKEY_CTX_set0_dh_kdf_ukm(EVP_PKEY_CTX *ctx, unsigned char *ukm, int len);
56# ifndef OPENSSL_NO_DEPRECATED_3_0
57OSSL_DEPRECATEDIN_3_0
58int 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
137DECLARE_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 */
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
155# define DH_CHECK_INVALID_J_VALUE 0x40
156# define DH_MODULUS_TOO_SMALL 0x80
157# define DH_MODULUS_TOO_LARGE 0x100
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
194DECLARE_ASN1_DUP_FUNCTION_name_attr(OSSL_DEPRECATEDIN_3_0, DH, DHparams)
195
196OSSL_DEPRECATEDIN_3_0 const DH_METHOD *DH_OpenSSL(void);
197
198OSSL_DEPRECATEDIN_3_0 void DH_set_default_method(const DH_METHOD *meth);
199OSSL_DEPRECATEDIN_3_0 const DH_METHOD *DH_get_default_method(void);
200OSSL_DEPRECATEDIN_3_0 int DH_set_method(DH *dh, const DH_METHOD *meth);
201OSSL_DEPRECATEDIN_3_0 DH *DH_new_method(ENGINE *engine);
202
203OSSL_DEPRECATEDIN_3_0 DH *DH_new(void);
204OSSL_DEPRECATEDIN_3_0 void DH_free(DH *dh);
205OSSL_DEPRECATEDIN_3_0 int DH_up_ref(DH *dh);
206OSSL_DEPRECATEDIN_3_0 int DH_bits(const DH *dh);
207OSSL_DEPRECATEDIN_3_0 int DH_size(const DH *dh);
208OSSL_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
213OSSL_DEPRECATEDIN_3_0 int DH_set_ex_data(DH *d, int idx, void *arg);
214OSSL_DEPRECATEDIN_3_0 void *DH_get_ex_data(const DH *d, int idx);
215
216OSSL_DEPRECATEDIN_3_0 int DH_generate_parameters_ex(DH *dh, int prime_len,
217 int generator,
218 BN_GENCB *cb);
219
220OSSL_DEPRECATEDIN_3_0 int DH_check_params_ex(const DH *dh);
221OSSL_DEPRECATEDIN_3_0 int DH_check_ex(const DH *dh);
222OSSL_DEPRECATEDIN_3_0 int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key);
223OSSL_DEPRECATEDIN_3_0 int DH_check_params(const DH *dh, int *ret);
224OSSL_DEPRECATEDIN_3_0 int DH_check(const DH *dh, int *codes);
225OSSL_DEPRECATEDIN_3_0 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key,
226 int *codes);
227OSSL_DEPRECATEDIN_3_0 int DH_generate_key(DH *dh);
228OSSL_DEPRECATEDIN_3_0 int DH_compute_key(unsigned char *key,
229 const BIGNUM *pub_key, DH *dh);
230OSSL_DEPRECATEDIN_3_0 int DH_compute_key_padded(unsigned char *key,
231 const BIGNUM *pub_key, DH *dh);
232
233DECLARE_ASN1_ENCODE_FUNCTIONS_only_attr(OSSL_DEPRECATEDIN_3_0, DH, DHparams)
234DECLARE_ASN1_ENCODE_FUNCTIONS_only_attr(OSSL_DEPRECATEDIN_3_0, DH, DHxparams)
235
236# ifndef OPENSSL_NO_STDIO
237OSSL_DEPRECATEDIN_3_0 int DHparams_print_fp(FILE *fp, const DH *x);
238# endif
239OSSL_DEPRECATEDIN_3_0 int DHparams_print(BIO *bp, const DH *x);
240
241/* RFC 5114 parameters */
242OSSL_DEPRECATEDIN_3_0 DH *DH_get_1024_160(void);
243OSSL_DEPRECATEDIN_3_0 DH *DH_get_2048_224(void);
244OSSL_DEPRECATEDIN_3_0 DH *DH_get_2048_256(void);
245
246/* Named parameters, currently RFC7919 and RFC3526 */
247OSSL_DEPRECATEDIN_3_0 DH *DH_new_by_nid(int nid);
248OSSL_DEPRECATEDIN_3_0 int DH_get_nid(const DH *dh);
249
250/* RFC2631 KDF */
251OSSL_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
257OSSL_DEPRECATEDIN_3_0 void DH_get0_pqg(const DH *dh, const BIGNUM **p,
258 const BIGNUM **q, const BIGNUM **g);
259OSSL_DEPRECATEDIN_3_0 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
260OSSL_DEPRECATEDIN_3_0 void DH_get0_key(const DH *dh, const BIGNUM **pub_key,
261 const BIGNUM **priv_key);
262OSSL_DEPRECATEDIN_3_0 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
263OSSL_DEPRECATEDIN_3_0 const BIGNUM *DH_get0_p(const DH *dh);
264OSSL_DEPRECATEDIN_3_0 const BIGNUM *DH_get0_q(const DH *dh);
265OSSL_DEPRECATEDIN_3_0 const BIGNUM *DH_get0_g(const DH *dh);
266OSSL_DEPRECATEDIN_3_0 const BIGNUM *DH_get0_priv_key(const DH *dh);
267OSSL_DEPRECATEDIN_3_0 const BIGNUM *DH_get0_pub_key(const DH *dh);
268OSSL_DEPRECATEDIN_3_0 void DH_clear_flags(DH *dh, int flags);
269OSSL_DEPRECATEDIN_3_0 int DH_test_flags(const DH *dh, int flags);
270OSSL_DEPRECATEDIN_3_0 void DH_set_flags(DH *dh, int flags);
271OSSL_DEPRECATEDIN_3_0 ENGINE *DH_get0_engine(DH *d);
272OSSL_DEPRECATEDIN_3_0 long DH_get_length(const DH *dh);
273OSSL_DEPRECATEDIN_3_0 int DH_set_length(DH *dh, long length);
274
275OSSL_DEPRECATEDIN_3_0 DH_METHOD *DH_meth_new(const char *name, int flags);
276OSSL_DEPRECATEDIN_3_0 void DH_meth_free(DH_METHOD *dhm);
277OSSL_DEPRECATEDIN_3_0 DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
278OSSL_DEPRECATEDIN_3_0 const char *DH_meth_get0_name(const DH_METHOD *dhm);
279OSSL_DEPRECATEDIN_3_0 int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
280OSSL_DEPRECATEDIN_3_0 int DH_meth_get_flags(const DH_METHOD *dhm);
281OSSL_DEPRECATEDIN_3_0 int DH_meth_set_flags(DH_METHOD *dhm, int flags);
282OSSL_DEPRECATEDIN_3_0 void *DH_meth_get0_app_data(const DH_METHOD *dhm);
283OSSL_DEPRECATEDIN_3_0 int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
284OSSL_DEPRECATEDIN_3_0 int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
285OSSL_DEPRECATEDIN_3_0 int DH_meth_set_generate_key(DH_METHOD *dhm,
286 int (*generate_key) (DH *));
287OSSL_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);
291OSSL_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));
296OSSL_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 *);
302OSSL_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 *));
308OSSL_DEPRECATEDIN_3_0 int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
309OSSL_DEPRECATEDIN_3_0 int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
310OSSL_DEPRECATEDIN_3_0 int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
311OSSL_DEPRECATEDIN_3_0 int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
312OSSL_DEPRECATEDIN_3_0 int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
313 (DH *, int, int,
314 BN_GENCB *);
315OSSL_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
322OSSL_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