]>
Commit | Line | Data |
---|---|---|
21dcbebc RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
21dcbebc RS |
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 | |
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> |
98186eb4 | 20 | # if OPENSSL_API_COMPAT < 0x10100000L |
0f113f3e MC |
21 | # include <openssl/bn.h> |
22 | # endif | |
5e3225cc | 23 | |
3c27208f RS |
24 | # ifdef __cplusplus |
25 | extern "C" { | |
26 | # endif | |
27 | ||
0f113f3e MC |
28 | # ifndef OPENSSL_DH_MAX_MODULUS_BITS |
29 | # define OPENSSL_DH_MAX_MODULUS_BITS 10000 | |
30 | # endif | |
83c3410b | 31 | |
0f113f3e MC |
32 | # define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024 |
33 | ||
34 | # define DH_FLAG_CACHE_MONT_P 0x01 | |
0f113f3e | 35 | # define DH_FLAG_NO_EXP_CONSTTIME 0x02 |
dfeab068 | 36 | |
0f113f3e MC |
37 | /* |
38 | * If this flag is set the DH method is FIPS compliant and can be used in | |
39 | * FIPS mode. This is set in the validated module method. If an application | |
8483a003 | 40 | * sets this flag in its own methods it is its responsibility to ensure the |
0f113f3e | 41 | * result is compliant. |
4960411e DSH |
42 | */ |
43 | ||
0f113f3e | 44 | # define DH_FLAG_FIPS_METHOD 0x0400 |
4960411e | 45 | |
0f113f3e MC |
46 | /* |
47 | * If this flag is set the operations normally disabled in FIPS mode are | |
4960411e DSH |
48 | * permitted it is then the applications responsibility to ensure that the |
49 | * usage is compliant. | |
50 | */ | |
51 | ||
0f113f3e | 52 | # define DH_FLAG_NON_FIPS_ALLOW 0x0400 |
4960411e | 53 | |
3a87a9b9 GT |
54 | /* Already defined in ossl_typ.h */ |
55 | /* typedef struct dh_st DH; */ | |
56 | /* typedef struct dh_method DH_METHOD; */ | |
13066cee | 57 | |
599eccfc RS |
58 | DECLARE_ASN1_ITEM(DHparams) |
59 | ||
0f113f3e MC |
60 | # define DH_GENERATOR_2 2 |
61 | /* #define DH_GENERATOR_3 3 */ | |
62 | # define DH_GENERATOR_5 5 | |
d02b48c6 RE |
63 | |
64 | /* DH_check error codes */ | |
0f113f3e MC |
65 | # define DH_CHECK_P_NOT_PRIME 0x01 |
66 | # define DH_CHECK_P_NOT_SAFE_PRIME 0x02 | |
67 | # define DH_UNABLE_TO_CHECK_GENERATOR 0x04 | |
68 | # define DH_NOT_SUITABLE_GENERATOR 0x08 | |
69 | # define DH_CHECK_Q_NOT_PRIME 0x10 | |
70 | # define DH_CHECK_INVALID_Q_VALUE 0x20 | |
71 | # define DH_CHECK_INVALID_J_VALUE 0x40 | |
d02b48c6 | 72 | |
bf3d6c0c | 73 | /* DH_check_pub_key error codes */ |
0f113f3e MC |
74 | # define DH_CHECK_PUBKEY_TOO_SMALL 0x01 |
75 | # define DH_CHECK_PUBKEY_TOO_LARGE 0x02 | |
cb389fe8 | 76 | # define DH_CHECK_PUBKEY_INVALID 0x04 |
bf3d6c0c | 77 | |
0f113f3e MC |
78 | /* |
79 | * primes p where (p-1)/2 is prime too are called "safe"; we define this for | |
80 | * backward compatibility: | |
81 | */ | |
82 | # define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME | |
4486d0cd | 83 | |
0f113f3e MC |
84 | # define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ |
85 | (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x)) | |
86 | # define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \ | |
87 | (unsigned char *)(x)) | |
88 | # define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x) | |
89 | # define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x) | |
d02b48c6 | 90 | |
f4274da1 DSH |
91 | DH *DHparams_dup(DH *); |
92 | ||
f971ccb2 | 93 | const DH_METHOD *DH_OpenSSL(void); |
13066cee | 94 | |
cb78486d GT |
95 | void DH_set_default_method(const DH_METHOD *meth); |
96 | const DH_METHOD *DH_get_default_method(void); | |
97 | int DH_set_method(DH *dh, const DH_METHOD *meth); | |
98 | DH *DH_new_method(ENGINE *engine); | |
13066cee | 99 | |
0f113f3e MC |
100 | DH *DH_new(void); |
101 | void DH_free(DH *dh); | |
102 | int DH_up_ref(DH *dh); | |
26c79d56 | 103 | int DH_bits(const DH *dh); |
0f113f3e MC |
104 | int DH_size(const DH *dh); |
105 | int DH_security_bits(const DH *dh); | |
e6390aca RS |
106 | #define DH_get_ex_new_index(l, p, newf, dupf, freef) \ |
107 | CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, l, p, newf, dupf, freef) | |
dd9d233e DSH |
108 | int DH_set_ex_data(DH *d, int idx, void *arg); |
109 | void *DH_get_ex_data(DH *d, int idx); | |
e9224c71 GT |
110 | |
111 | /* Deprecated version */ | |
98186eb4 | 112 | DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator, |
0f113f3e MC |
113 | void (*callback) (int, int, |
114 | void *), | |
98186eb4 | 115 | void *cb_arg)) |
e9224c71 GT |
116 | |
117 | /* New version */ | |
0f113f3e MC |
118 | int DH_generate_parameters_ex(DH *dh, int prime_len, int generator, |
119 | BN_GENCB *cb); | |
120 | ||
121 | int DH_check(const DH *dh, int *codes); | |
122 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes); | |
123 | int DH_generate_key(DH *dh); | |
124 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
125 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
126 | DH *d2i_DHparams(DH **a, const unsigned char **pp, long length); | |
127 | int i2d_DHparams(const DH *a, unsigned char **pp); | |
128 | DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length); | |
129 | int i2d_DHxparams(const DH *a, unsigned char **pp); | |
130 | # ifndef OPENSSL_NO_STDIO | |
131 | int DHparams_print_fp(FILE *fp, const DH *x); | |
132 | # endif | |
0f113f3e | 133 | int DHparams_print(BIO *bp, const DH *x); |
d02b48c6 | 134 | |
20bee968 DSH |
135 | /* RFC 5114 parameters */ |
136 | DH *DH_get_1024_160(void); | |
137 | DH *DH_get_2048_224(void); | |
138 | DH *DH_get_2048_256(void); | |
139 | ||
e968561d | 140 | # ifndef OPENSSL_NO_CMS |
dc1ce3bc | 141 | /* RFC2631 KDF */ |
0f113f3e MC |
142 | int DH_KDF_X9_42(unsigned char *out, size_t outlen, |
143 | const unsigned char *Z, size_t Zlen, | |
144 | ASN1_OBJECT *key_oid, | |
145 | const unsigned char *ukm, size_t ukmlen, const EVP_MD *md); | |
e968561d | 146 | # endif |
0f113f3e | 147 | |
0aeddcfa MC |
148 | void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g); |
149 | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); | |
150 | void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key); | |
151 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); | |
152 | void DH_clear_flags(DH *dh, int flags); | |
153 | int DH_test_flags(const DH *dh, int flags); | |
154 | void DH_set_flags(DH *dh, int flags); | |
155 | ENGINE *DH_get0_engine(DH *d); | |
156 | long DH_get_length(const DH *dh); | |
157 | int DH_set_length(DH *dh, long length); | |
158 | ||
17e01abb MC |
159 | DH_METHOD *DH_meth_new(const char *name, int flags); |
160 | void DH_meth_free(DH_METHOD *dhm); | |
161 | DH_METHOD *DH_meth_dup(const DH_METHOD *dhm); | |
162 | const char *DH_meth_get0_name(const DH_METHOD *dhm); | |
163 | int DH_meth_set1_name(DH_METHOD *dhm, const char *name); | |
164 | int DH_meth_get_flags(DH_METHOD *dhm); | |
165 | int DH_meth_set_flags(DH_METHOD *dhm, int flags); | |
166 | void *DH_meth_get0_app_data(const DH_METHOD *dhm); | |
167 | int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data); | |
168 | int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *); | |
169 | int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *)); | |
170 | int (*DH_meth_get_compute_key(const DH_METHOD *dhm)) | |
171 | (unsigned char *key, const BIGNUM *pub_key, DH *dh); | |
172 | int DH_meth_set_compute_key(DH_METHOD *dhm, | |
173 | int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh)); | |
174 | int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm)) | |
175 | (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, | |
176 | BN_CTX *, BN_MONT_CTX *); | |
177 | int DH_meth_set_bn_mod_exp(DH_METHOD *dhm, | |
178 | int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, | |
179 | const BIGNUM *, BN_CTX *, BN_MONT_CTX *)); | |
180 | int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *); | |
181 | int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *)); | |
182 | int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *); | |
183 | int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *)); | |
184 | int (*DH_meth_get_generate_params(const DH_METHOD *dhm)) | |
185 | (DH *, int, int, BN_GENCB *); | |
186 | int DH_meth_set_generate_params(DH_METHOD *dhm, | |
187 | int (*generate_params) (DH *, int, int, BN_GENCB *)); | |
188 | ||
0aeddcfa | 189 | |
0f113f3e MC |
190 | # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \ |
191 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
192 | EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL) | |
193 | ||
194 | # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \ | |
195 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
196 | EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL) | |
197 | ||
198 | # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \ | |
199 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
200 | EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL) | |
201 | ||
202 | # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \ | |
203 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \ | |
204 | EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL) | |
205 | ||
206 | # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \ | |
207 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ | |
208 | EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) | |
209 | ||
210 | # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \ | |
211 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \ | |
212 | EVP_PKEY_CTRL_DH_RFC5114, gen, NULL) | |
213 | ||
214 | # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \ | |
215 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
216 | EVP_PKEY_OP_DERIVE, \ | |
217 | EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL) | |
218 | ||
219 | # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \ | |
220 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
221 | EVP_PKEY_OP_DERIVE, \ | |
222 | EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL) | |
223 | ||
224 | # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \ | |
225 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
226 | EVP_PKEY_OP_DERIVE, \ | |
227 | EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)oid) | |
228 | ||
229 | # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \ | |
230 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
231 | EVP_PKEY_OP_DERIVE, \ | |
232 | EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)poid) | |
233 | ||
234 | # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \ | |
235 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
236 | EVP_PKEY_OP_DERIVE, \ | |
237 | EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)md) | |
238 | ||
239 | # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \ | |
240 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
241 | EVP_PKEY_OP_DERIVE, \ | |
242 | EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)pmd) | |
243 | ||
244 | # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \ | |
245 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
246 | EVP_PKEY_OP_DERIVE, \ | |
247 | EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL) | |
248 | ||
249 | # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \ | |
250 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
251 | EVP_PKEY_OP_DERIVE, \ | |
252 | EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)plen) | |
253 | ||
254 | # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \ | |
255 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
256 | EVP_PKEY_OP_DERIVE, \ | |
257 | EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)p) | |
258 | ||
259 | # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \ | |
260 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \ | |
261 | EVP_PKEY_OP_DERIVE, \ | |
262 | EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)p) | |
263 | ||
264 | # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1) | |
265 | # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2) | |
266 | # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3) | |
267 | # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4) | |
268 | # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5) | |
269 | # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6) | |
270 | # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7) | |
271 | # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8) | |
272 | # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9) | |
273 | # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10) | |
274 | # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11) | |
275 | # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12) | |
276 | # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13) | |
277 | # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14) | |
bd59f2b9 DSH |
278 | |
279 | /* KDF types */ | |
0f113f3e | 280 | # define EVP_PKEY_DH_KDF_NONE 1 |
e968561d | 281 | # ifndef OPENSSL_NO_CMS |
0f113f3e | 282 | # define EVP_PKEY_DH_KDF_X9_42 2 |
e968561d | 283 | # endif |
3ba0885a | 284 | |
d02b48c6 | 285 | /* BEGIN ERROR CODES */ |
0f113f3e MC |
286 | /* |
287 | * The following lines are auto generated by the script mkerr.pl. Any changes | |
6d311938 DSH |
288 | * made after this point may be overwritten when the script is next run. |
289 | */ | |
b476df64 | 290 | void ERR_load_DH_strings(void); |
6d311938 | 291 | |
d02b48c6 RE |
292 | /* Error codes for the DH functions. */ |
293 | ||
294 | /* Function codes. */ | |
0f113f3e MC |
295 | # define DH_F_COMPUTE_KEY 102 |
296 | # define DH_F_DHPARAMS_PRINT_FP 101 | |
297 | # define DH_F_DH_BUILTIN_GENPARAMS 106 | |
298 | # define DH_F_DH_CMS_DECRYPT 114 | |
299 | # define DH_F_DH_CMS_SET_PEERKEY 115 | |
300 | # define DH_F_DH_CMS_SET_SHARED_INFO 116 | |
569d0646 MC |
301 | # define DH_F_DH_METH_DUP 117 |
302 | # define DH_F_DH_METH_NEW 118 | |
303 | # define DH_F_DH_METH_SET1_NAME 119 | |
0f113f3e MC |
304 | # define DH_F_DH_NEW_METHOD 105 |
305 | # define DH_F_DH_PARAM_DECODE 107 | |
306 | # define DH_F_DH_PRIV_DECODE 110 | |
307 | # define DH_F_DH_PRIV_ENCODE 111 | |
308 | # define DH_F_DH_PUB_DECODE 108 | |
309 | # define DH_F_DH_PUB_ENCODE 109 | |
310 | # define DH_F_DO_DH_PRINT 100 | |
311 | # define DH_F_GENERATE_KEY 103 | |
312 | # define DH_F_GENERATE_PARAMETERS 104 | |
313 | # define DH_F_PKEY_DH_DERIVE 112 | |
314 | # define DH_F_PKEY_DH_KEYGEN 113 | |
d02b48c6 RE |
315 | |
316 | /* Reason codes. */ | |
0f113f3e MC |
317 | # define DH_R_BAD_GENERATOR 101 |
318 | # define DH_R_BN_DECODE_ERROR 109 | |
319 | # define DH_R_BN_ERROR 106 | |
320 | # define DH_R_DECODE_ERROR 104 | |
321 | # define DH_R_INVALID_PUBKEY 102 | |
322 | # define DH_R_KDF_PARAMETER_ERROR 112 | |
323 | # define DH_R_KEYS_NOT_SET 108 | |
324 | # define DH_R_KEY_SIZE_TOO_SMALL 110 | |
325 | # define DH_R_MODULUS_TOO_LARGE 103 | |
326 | # define DH_R_NO_PARAMETERS_SET 107 | |
327 | # define DH_R_NO_PRIVATE_VALUE 100 | |
328 | # define DH_R_PARAMETER_ENCODING_ERROR 105 | |
329 | # define DH_R_PEER_KEY_ERROR 111 | |
330 | # define DH_R_SHARED_INFO_ERROR 113 | |
6d311938 | 331 | |
3c27208f | 332 | # ifdef __cplusplus |
d02b48c6 | 333 | } |
3c27208f RS |
334 | # endif |
335 | # endif | |
336 | ||
d02b48c6 | 337 | #endif |