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