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