xref: /openbsd-src/lib/libcrypto/dh/dh.h (revision 515aa502ddfbfa9d16fb6d4368077c2ccf35cf3d)
1*515aa502Stb /* $OpenBSD: dh.h,v 1.38 2025/01/25 17:59:44 tb Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
595b37fcf3Sryker #ifndef HEADER_DH_H
605b37fcf3Sryker #define HEADER_DH_H
615b37fcf3Sryker 
6220175b85Sjsing #include <openssl/opensslconf.h>
634fcf65c5Sdjm 
64da347917Sbeck #ifndef OPENSSL_NO_BIO
65c109e398Sbeck #include <openssl/bio.h>
66c109e398Sbeck #endif
67da347917Sbeck #include <openssl/ossl_typ.h>
684fcf65c5Sdjm #include <openssl/bn.h>
69913ec974Sbeck 
704fcf65c5Sdjm #ifndef OPENSSL_DH_MAX_MODULUS_BITS
71ee1f122aSpvalchev # define OPENSSL_DH_MAX_MODULUS_BITS	10000
724fcf65c5Sdjm #endif
73ee1f122aSpvalchev 
74913ec974Sbeck #define DH_FLAG_CACHE_MONT_P     0x01
75913ec974Sbeck 
765cdd308eSdjm /* If this flag is set the DH method is FIPS compliant and can be used
775cdd308eSdjm  * in FIPS mode. This is set in the validated module method. If an
785cdd308eSdjm  * application sets this flag in its own methods it is its reposibility
795cdd308eSdjm  * to ensure the result is compliant.
805cdd308eSdjm  */
815cdd308eSdjm 
825cdd308eSdjm #define DH_FLAG_FIPS_METHOD			0x0400
835cdd308eSdjm 
845cdd308eSdjm /* If this flag is set the operations normally disabled in FIPS mode are
855cdd308eSdjm  * permitted it is then the applications responsibility to ensure that the
865cdd308eSdjm  * usage is compliant.
875cdd308eSdjm  */
885cdd308eSdjm 
895cdd308eSdjm #define DH_FLAG_NON_FIPS_ALLOW			0x0400
905cdd308eSdjm 
91c109e398Sbeck #ifdef  __cplusplus
92c109e398Sbeck extern "C" {
93c109e398Sbeck #endif
94c109e398Sbeck 
955b37fcf3Sryker #define DH_GENERATOR_2		2
965b37fcf3Sryker /* #define DH_GENERATOR_3	3 */
975b37fcf3Sryker #define DH_GENERATOR_5		5
985b37fcf3Sryker 
995b37fcf3Sryker /* DH_check error codes */
1005b37fcf3Sryker #define DH_CHECK_P_NOT_PRIME		0x01
101ba5406e9Sbeck #define DH_CHECK_P_NOT_SAFE_PRIME	0x02
1025b37fcf3Sryker #define DH_UNABLE_TO_CHECK_GENERATOR	0x04
1035b37fcf3Sryker #define DH_NOT_SUITABLE_GENERATOR	0x08
104c44f9f1fStb #define DH_CHECK_Q_NOT_PRIME		0x10
105c44f9f1fStb #define DH_CHECK_INVALID_Q_VALUE	0x20
106c44f9f1fStb #define DH_CHECK_INVALID_J_VALUE	0x40
1075b37fcf3Sryker 
1083f6aedb7Sdjm /* DH_check_pub_key error codes */
1093f6aedb7Sdjm #define DH_CHECK_PUBKEY_TOO_SMALL	0x01
1103f6aedb7Sdjm #define DH_CHECK_PUBKEY_TOO_LARGE	0x02
111c44f9f1fStb #define DH_CHECK_PUBKEY_INVALID		0x04
1123f6aedb7Sdjm 
113ba5406e9Sbeck /* primes p where (p-1)/2 is prime too are called "safe"; we define
114ba5406e9Sbeck    this for backward compatibility: */
115ba5406e9Sbeck #define DH_CHECK_P_NOT_STRONG_PRIME	DH_CHECK_P_NOT_SAFE_PRIME
116ba5406e9Sbeck 
1172ca51753Sjsing DH *d2i_DHparams_bio(BIO *bp, DH **a);
1182ca51753Sjsing int i2d_DHparams_bio(BIO *bp, DH *a);
1192ca51753Sjsing DH *d2i_DHparams_fp(FILE *fp, DH **a);
1202ca51753Sjsing int i2d_DHparams_fp(FILE *fp, DH *a);
1215b37fcf3Sryker 
1220a5d6edeSdjm DH *DHparams_dup(DH *);
123ba5406e9Sbeck 
1240a5d6edeSdjm const DH_METHOD *DH_OpenSSL(void);
125e6841c1dSdjm 
126da347917Sbeck void DH_set_default_method(const DH_METHOD *meth);
127da347917Sbeck const DH_METHOD *DH_get_default_method(void);
128da347917Sbeck int DH_set_method(DH *dh, const DH_METHOD *meth);
129da347917Sbeck DH *DH_new_method(ENGINE *engine);
130ba5406e9Sbeck 
1315b37fcf3Sryker DH *	DH_new(void);
1325b37fcf3Sryker void	DH_free(DH *dh);
133da347917Sbeck int	DH_up_ref(DH *dh);
134da347917Sbeck int	DH_size(const DH *dh);
135a108d6e6Stb int	DH_bits(const DH *dh);
136ba5406e9Sbeck int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
137ba5406e9Sbeck 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
138ba5406e9Sbeck int DH_set_ex_data(DH *d, int idx, void *arg);
139ba5406e9Sbeck void *DH_get_ex_data(DH *d, int idx);
140fee21fefStb int DH_security_bits(const DH *dh);
1414fcf65c5Sdjm 
142dfbc7a43Sjsing ENGINE *DH_get0_engine(DH *d);
143f08abba0Stb void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q,
144f08abba0Stb     const BIGNUM **g);
1458216844aStb int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
146f08abba0Stb void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
1471cdc303dStb int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
148d742c600Stb const BIGNUM *DH_get0_p(const DH *dh);
149d742c600Stb const BIGNUM *DH_get0_q(const DH *dh);
150d742c600Stb const BIGNUM *DH_get0_g(const DH *dh);
151d742c600Stb const BIGNUM *DH_get0_priv_key(const DH *dh);
152d742c600Stb const BIGNUM *DH_get0_pub_key(const DH *dh);
1535d59ae97Stb void DH_clear_flags(DH *dh, int flags);
1545d59ae97Stb int DH_test_flags(const DH *dh, int flags);
1555d59ae97Stb void DH_set_flags(DH *dh, int flags);
1567c7e4506Stb long DH_get_length(const DH *dh);
15757addcd0Stb int DH_set_length(DH *dh, long length);
158f08abba0Stb 
159e4c559e8Stb /*
160e4c559e8Stb  * Wrapped in OPENSSL_NO_DEPRECATED in 0.9.8, added to rust-openssl in 2020,
161e4c559e8Stb  * for "advanced DH support".
162e4c559e8Stb  */
1635b37fcf3Sryker DH *	DH_generate_parameters(int prime_len,int generator,
164913ec974Sbeck 		void (*callback)(int,int,void *),void *cb_arg);
1654fcf65c5Sdjm 
1664fcf65c5Sdjm /* New version */
1674fcf65c5Sdjm int	DH_generate_parameters_ex(DH *dh, int prime_len,int generator, BN_GENCB *cb);
1684fcf65c5Sdjm 
169da347917Sbeck int	DH_check(const DH *dh,int *codes);
1703f6aedb7Sdjm int	DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes);
1715b37fcf3Sryker int	DH_generate_key(DH *dh);
172da347917Sbeck int	DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
173da347917Sbeck DH *	d2i_DHparams(DH **a,const unsigned char **pp, long length);
174da347917Sbeck int	i2d_DHparams(const DH *a,unsigned char **pp);
175da347917Sbeck int	DHparams_print_fp(FILE *fp, const DH *x);
176da347917Sbeck #ifndef OPENSSL_NO_BIO
177da347917Sbeck int	DHparams_print(BIO *bp, const DH *x);
1785b37fcf3Sryker #else
179da347917Sbeck int	DHparams_print(char *bp, const DH *x);
1805b37fcf3Sryker #endif
1815b37fcf3Sryker 
1820a5d6edeSdjm #define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
1830a5d6edeSdjm 	EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
1840a5d6edeSdjm 			EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
1850a5d6edeSdjm 
1860a5d6edeSdjm #define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
1870a5d6edeSdjm 	EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
1880a5d6edeSdjm 			EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
1890a5d6edeSdjm 
1900a5d6edeSdjm #define	EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN	(EVP_PKEY_ALG_CTRL + 1)
1910a5d6edeSdjm #define	EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR	(EVP_PKEY_ALG_CTRL + 2)
1920a5d6edeSdjm 
1930a5d6edeSdjm 
194da347917Sbeck void ERR_load_DH_strings(void);
195913ec974Sbeck 
1965b37fcf3Sryker /* Error codes for the DH functions. */
1975b37fcf3Sryker 
1985b37fcf3Sryker /* Function codes. */
1994fcf65c5Sdjm #define DH_F_COMPUTE_KEY				 102
2005b37fcf3Sryker #define DH_F_DHPARAMS_PRINT_FP				 101
2014fcf65c5Sdjm #define DH_F_DH_BUILTIN_GENPARAMS			 106
2025cdd308eSdjm #define DH_F_DH_COMPUTE_KEY				 114
2035cdd308eSdjm #define DH_F_DH_GENERATE_KEY				 115
2045cdd308eSdjm #define DH_F_DH_GENERATE_PARAMETERS_EX			 116
205da347917Sbeck #define DH_F_DH_NEW_METHOD				 105
2060a5d6edeSdjm #define DH_F_DH_PARAM_DECODE				 107
2070a5d6edeSdjm #define DH_F_DH_PRIV_DECODE				 110
2080a5d6edeSdjm #define DH_F_DH_PRIV_ENCODE				 111
2090a5d6edeSdjm #define DH_F_DH_PUB_DECODE				 108
2100a5d6edeSdjm #define DH_F_DH_PUB_ENCODE				 109
2110a5d6edeSdjm #define DH_F_DO_DH_PRINT				 100
2124fcf65c5Sdjm #define DH_F_GENERATE_KEY				 103
2134fcf65c5Sdjm #define DH_F_GENERATE_PARAMETERS			 104
2140a5d6edeSdjm #define DH_F_PKEY_DH_DERIVE				 112
2150a5d6edeSdjm #define DH_F_PKEY_DH_KEYGEN				 113
2165b37fcf3Sryker 
2175b37fcf3Sryker /* Reason codes. */
218da347917Sbeck #define DH_R_BAD_GENERATOR				 101
2190a5d6edeSdjm #define DH_R_BN_DECODE_ERROR				 109
2200a5d6edeSdjm #define DH_R_BN_ERROR					 106
2210a5d6edeSdjm #define DH_R_DECODE_ERROR				 104
2224fcf65c5Sdjm #define DH_R_INVALID_PUBKEY				 102
2230a5d6edeSdjm #define DH_R_KEYS_NOT_SET				 108
2245cdd308eSdjm #define DH_R_KEY_SIZE_TOO_SMALL				 110
2254fcf65c5Sdjm #define DH_R_MODULUS_TOO_LARGE				 103
2265cdd308eSdjm #define DH_R_NON_FIPS_METHOD				 111
2270a5d6edeSdjm #define DH_R_NO_PARAMETERS_SET				 107
2285b37fcf3Sryker #define DH_R_NO_PRIVATE_VALUE				 100
2290a5d6edeSdjm #define DH_R_PARAMETER_ENCODING_ERROR			 105
230ef053c5dStb #define DH_R_CHECK_INVALID_J_VALUE			 115
231ef053c5dStb #define DH_R_CHECK_INVALID_Q_VALUE			 116
232ef053c5dStb #define DH_R_CHECK_PUBKEY_INVALID			 122
233ef053c5dStb #define DH_R_CHECK_PUBKEY_TOO_LARGE			 123
234ef053c5dStb #define DH_R_CHECK_PUBKEY_TOO_SMALL			 124
235ef053c5dStb #define DH_R_CHECK_P_NOT_PRIME				 117
236ef053c5dStb #define DH_R_CHECK_P_NOT_SAFE_PRIME			 118
237ef053c5dStb #define DH_R_CHECK_Q_NOT_PRIME				 119
238ef053c5dStb #define DH_R_MISSING_PUBKEY				 125
239ef053c5dStb #define DH_R_NOT_SUITABLE_GENERATOR			 120
240ef053c5dStb #define DH_R_UNABLE_TO_CHECK_GENERATOR			 121
2415b37fcf3Sryker 
2425b37fcf3Sryker #ifdef  __cplusplus
2435b37fcf3Sryker }
2445b37fcf3Sryker #endif
2455b37fcf3Sryker #endif
246