]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/openssl/dh.h
Add pad support
[thirdparty/openssl.git] / include / openssl / dh.h
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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 HEADER_DH_H
11 # define HEADER_DH_H
12
13 # include <openssl/opensslconf.h>
14
15 # ifndef OPENSSL_NO_DH
16 # include <openssl/e_os2.h>
17 # include <openssl/bio.h>
18 # include <openssl/asn1.h>
19 # include <openssl/ossl_typ.h>
20 # if OPENSSL_API_COMPAT < 0x10100000L
21 # include <openssl/bn.h>
22 # endif
23 # include <openssl/dherr.h>
24
25 # ifdef __cplusplus
26 extern "C" {
27 # endif
28
29 # ifndef OPENSSL_DH_MAX_MODULUS_BITS
30 # define OPENSSL_DH_MAX_MODULUS_BITS 10000
31 # endif
32
33 # define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024
34
35 # define DH_FLAG_CACHE_MONT_P 0x01
36
37 # if OPENSSL_API_COMPAT < 0x10100000L
38 /*
39 * Does nothing. Previously this switched off constant time behaviour.
40 */
41 # define DH_FLAG_NO_EXP_CONSTTIME 0x00
42 # endif
43
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
47 * sets this flag in its own methods it is its responsibility to ensure the
48 * result is compliant.
49 */
50
51 # define DH_FLAG_FIPS_METHOD 0x0400
52
53 /*
54 * If this flag is set the operations normally disabled in FIPS mode are
55 * permitted it is then the applications responsibility to ensure that the
56 * usage is compliant.
57 */
58
59 # define DH_FLAG_NON_FIPS_ALLOW 0x0400
60
61 /* Already defined in ossl_typ.h */
62 /* typedef struct dh_st DH; */
63 /* typedef struct dh_method DH_METHOD; */
64
65 DECLARE_ASN1_ITEM(DHparams)
66
67 # define DH_GENERATOR_2 2
68 /* #define DH_GENERATOR_3 3 */
69 # define DH_GENERATOR_5 5
70
71 /* DH_check error codes */
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
79
80 /* DH_check_pub_key error codes */
81 # define DH_CHECK_PUBKEY_TOO_SMALL 0x01
82 # define DH_CHECK_PUBKEY_TOO_LARGE 0x02
83 # define DH_CHECK_PUBKEY_INVALID 0x04
84
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
90
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) \
101 ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
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) \
113 ASN1_i2d_bio_of_const(DH, i2d_DHxparams, bp, x)
114
115 DH *DHparams_dup(DH *);
116
117 const DH_METHOD *DH_OpenSSL(void);
118
119 void DH_set_default_method(const DH_METHOD *meth);
120 const DH_METHOD *DH_get_default_method(void);
121 int DH_set_method(DH *dh, const DH_METHOD *meth);
122 DH *DH_new_method(ENGINE *engine);
123
124 DH *DH_new(void);
125 void DH_free(DH *dh);
126 int DH_up_ref(DH *dh);
127 int DH_bits(const DH *dh);
128 int DH_size(const DH *dh);
129 int DH_security_bits(const DH *dh);
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)
132 int DH_set_ex_data(DH *d, int idx, void *arg);
133 void *DH_get_ex_data(DH *d, int idx);
134
135 /* Deprecated version */
136 DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator,
137 void (*callback) (int, int,
138 void *),
139 void *cb_arg))
140
141 /* New version */
142 int DH_generate_parameters_ex(DH *dh, int prime_len, int generator,
143 BN_GENCB *cb);
144
145 int DH_check_params(const DH *dh, int *ret);
146 int DH_check(const DH *dh, int *codes);
147 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes);
148 int DH_generate_key(DH *dh);
149 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
150 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);
151 DH *d2i_DHparams(DH **a, const unsigned char **pp, long length);
152 int i2d_DHparams(const DH *a, unsigned char **pp);
153 DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length);
154 int i2d_DHxparams(const DH *a, unsigned char **pp);
155 # ifndef OPENSSL_NO_STDIO
156 int DHparams_print_fp(FILE *fp, const DH *x);
157 # endif
158 int DHparams_print(BIO *bp, const DH *x);
159
160 /* RFC 5114 parameters */
161 DH *DH_get_1024_160(void);
162 DH *DH_get_2048_224(void);
163 DH *DH_get_2048_256(void);
164
165 /* Named parameters, currently RFC7919 */
166 DH *DH_new_by_nid(int nid);
167 int DH_get_nid(const DH *dh);
168
169 # ifndef OPENSSL_NO_CMS
170 /* RFC2631 KDF */
171 int DH_KDF_X9_42(unsigned char *out, size_t outlen,
172 const unsigned char *Z, size_t Zlen,
173 ASN1_OBJECT *key_oid,
174 const unsigned char *ukm, size_t ukmlen, const EVP_MD *md);
175 # endif
176
177 void DH_get0_pqg(const DH *dh,
178 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
179 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
180 void DH_get0_key(const DH *dh,
181 const BIGNUM **pub_key, const BIGNUM **priv_key);
182 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
183 void DH_clear_flags(DH *dh, int flags);
184 int DH_test_flags(const DH *dh, int flags);
185 void DH_set_flags(DH *dh, int flags);
186 ENGINE *DH_get0_engine(DH *d);
187 long DH_get_length(const DH *dh);
188 int DH_set_length(DH *dh, long length);
189
190 DH_METHOD *DH_meth_new(const char *name, int flags);
191 void DH_meth_free(DH_METHOD *dhm);
192 DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
193 const char *DH_meth_get0_name(const DH_METHOD *dhm);
194 int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
195 int DH_meth_get_flags(DH_METHOD *dhm);
196 int DH_meth_set_flags(DH_METHOD *dhm, int flags);
197 void *DH_meth_get0_app_data(const DH_METHOD *dhm);
198 int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
199 int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
200 int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *));
201 int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
202 (unsigned char *key, const BIGNUM *pub_key, DH *dh);
203 int DH_meth_set_compute_key(DH_METHOD *dhm,
204 int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh));
205 int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
206 (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
207 BN_CTX *, BN_MONT_CTX *);
208 int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
209 int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
210 const BIGNUM *, BN_CTX *, BN_MONT_CTX *));
211 int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
212 int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
213 int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
214 int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
215 int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
216 (DH *, int, int, BN_GENCB *);
217 int DH_meth_set_generate_params(DH_METHOD *dhm,
218 int (*generate_params) (DH *, int, int, BN_GENCB *));
219
220
221 # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
222 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
223 EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
224
225 # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
226 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
227 EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
228
229 # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
230 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
231 EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
232
233 # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
234 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
235 EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
236
237 # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \
238 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
239 EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
240
241 # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \
242 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
243 EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
244
245 # define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \
246 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \
247 EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \
248 EVP_PKEY_CTRL_DH_NID, nid, NULL)
249
250 # define EVP_PKEY_CTX_set_dh_pad(ctx, pad) \
251 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, \
252 EVP_PKEY_CTRL_DH_PAD, pad, NULL)
253
254 # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
255 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
256 EVP_PKEY_OP_DERIVE, \
257 EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
258
259 # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
260 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
261 EVP_PKEY_OP_DERIVE, \
262 EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
263
264 # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
265 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
266 EVP_PKEY_OP_DERIVE, \
267 EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid))
268
269 # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
270 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
271 EVP_PKEY_OP_DERIVE, \
272 EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid))
273
274 # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
275 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
276 EVP_PKEY_OP_DERIVE, \
277 EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md))
278
279 # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
280 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
281 EVP_PKEY_OP_DERIVE, \
282 EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd))
283
284 # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
285 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
286 EVP_PKEY_OP_DERIVE, \
287 EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
288
289 # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
290 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
291 EVP_PKEY_OP_DERIVE, \
292 EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen))
293
294 # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
295 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
296 EVP_PKEY_OP_DERIVE, \
297 EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p))
298
299 # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
300 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
301 EVP_PKEY_OP_DERIVE, \
302 EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p))
303
304 # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1)
305 # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2)
306 # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3)
307 # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4)
308 # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5)
309 # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6)
310 # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7)
311 # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8)
312 # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9)
313 # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10)
314 # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11)
315 # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12)
316 # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13)
317 # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14)
318 # define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15)
319 # define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
320
321 /* KDF types */
322 # define EVP_PKEY_DH_KDF_NONE 1
323 # ifndef OPENSSL_NO_CMS
324 # define EVP_PKEY_DH_KDF_X9_42 2
325 # endif
326
327 int ERR_load_DH_strings(void);
328
329 # ifdef __cplusplus
330 }
331 # endif
332 # endif
333 #endif