xref: /onnv-gate/usr/src/common/openssl/crypto/dh/dh.h (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/dh/dh.h */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #ifndef HEADER_DH_H
600Sstevel@tonic-gate #define HEADER_DH_H
610Sstevel@tonic-gate 
62*2139Sjp161948 #include <openssl/e_os2.h>
63*2139Sjp161948 
640Sstevel@tonic-gate #ifdef OPENSSL_NO_DH
650Sstevel@tonic-gate #error DH is disabled.
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
690Sstevel@tonic-gate #include <openssl/bio.h>
700Sstevel@tonic-gate #endif
71*2139Sjp161948 #include <openssl/ossl_typ.h>
72*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
730Sstevel@tonic-gate #include <openssl/bn.h>
74*2139Sjp161948 #endif
750Sstevel@tonic-gate 
76*2139Sjp161948 #define DH_FLAG_CACHE_MONT_P     0x01
77*2139Sjp161948 #define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH
78*2139Sjp161948                                        * implementation now uses constant time
79*2139Sjp161948                                        * modular exponentiation for secret exponents
80*2139Sjp161948                                        * by default. This flag causes the
81*2139Sjp161948                                        * faster variable sliding window method to
82*2139Sjp161948                                        * be used for all exponents.
83*2139Sjp161948                                        */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifdef  __cplusplus
860Sstevel@tonic-gate extern "C" {
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 
89*2139Sjp161948 /* Already defined in ossl_typ.h */
90*2139Sjp161948 /* typedef struct dh_st DH; */
91*2139Sjp161948 /* typedef struct dh_method DH_METHOD; */
920Sstevel@tonic-gate 
93*2139Sjp161948 struct dh_method
94*2139Sjp161948 	{
950Sstevel@tonic-gate 	const char *name;
960Sstevel@tonic-gate 	/* Methods here */
970Sstevel@tonic-gate 	int (*generate_key)(DH *dh);
980Sstevel@tonic-gate 	int (*compute_key)(unsigned char *key,const BIGNUM *pub_key,DH *dh);
990Sstevel@tonic-gate 	int (*bn_mod_exp)(const DH *dh, BIGNUM *r, const BIGNUM *a,
1000Sstevel@tonic-gate 				const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1010Sstevel@tonic-gate 				BN_MONT_CTX *m_ctx); /* Can be null */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	int (*init)(DH *dh);
1040Sstevel@tonic-gate 	int (*finish)(DH *dh);
1050Sstevel@tonic-gate 	int flags;
1060Sstevel@tonic-gate 	char *app_data;
107*2139Sjp161948 	/* If this is non-NULL, it will be used to generate parameters */
108*2139Sjp161948 	int (*generate_params)(DH *dh, int prime_len, int generator, BN_GENCB *cb);
109*2139Sjp161948 	};
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate struct dh_st
1120Sstevel@tonic-gate 	{
1130Sstevel@tonic-gate 	/* This first argument is used to pick up errors when
1140Sstevel@tonic-gate 	 * a DH is passed instead of a EVP_PKEY */
1150Sstevel@tonic-gate 	int pad;
1160Sstevel@tonic-gate 	int version;
1170Sstevel@tonic-gate 	BIGNUM *p;
1180Sstevel@tonic-gate 	BIGNUM *g;
1190Sstevel@tonic-gate 	long length; /* optional */
1200Sstevel@tonic-gate 	BIGNUM *pub_key;	/* g^x */
1210Sstevel@tonic-gate 	BIGNUM *priv_key;	/* x */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	int flags;
124*2139Sjp161948 	BN_MONT_CTX *method_mont_p;
1250Sstevel@tonic-gate 	/* Place holders if we want to do X9.42 DH */
1260Sstevel@tonic-gate 	BIGNUM *q;
1270Sstevel@tonic-gate 	BIGNUM *j;
1280Sstevel@tonic-gate 	unsigned char *seed;
1290Sstevel@tonic-gate 	int seedlen;
1300Sstevel@tonic-gate 	BIGNUM *counter;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	int references;
1330Sstevel@tonic-gate 	CRYPTO_EX_DATA ex_data;
1340Sstevel@tonic-gate 	const DH_METHOD *meth;
1350Sstevel@tonic-gate 	ENGINE *engine;
1360Sstevel@tonic-gate 	};
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define DH_GENERATOR_2		2
1390Sstevel@tonic-gate /* #define DH_GENERATOR_3	3 */
1400Sstevel@tonic-gate #define DH_GENERATOR_5		5
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /* DH_check error codes */
1430Sstevel@tonic-gate #define DH_CHECK_P_NOT_PRIME		0x01
1440Sstevel@tonic-gate #define DH_CHECK_P_NOT_SAFE_PRIME	0x02
1450Sstevel@tonic-gate #define DH_UNABLE_TO_CHECK_GENERATOR	0x04
1460Sstevel@tonic-gate #define DH_NOT_SUITABLE_GENERATOR	0x08
1470Sstevel@tonic-gate 
148*2139Sjp161948 /* DH_check_pub_key error codes */
149*2139Sjp161948 #define DH_CHECK_PUBKEY_TOO_SMALL	0x01
150*2139Sjp161948 #define DH_CHECK_PUBKEY_TOO_LARGE	0x02
151*2139Sjp161948 
1520Sstevel@tonic-gate /* primes p where (p-1)/2 is prime too are called "safe"; we define
1530Sstevel@tonic-gate    this for backward compatibility: */
1540Sstevel@tonic-gate #define DH_CHECK_P_NOT_STRONG_PRIME	DH_CHECK_P_NOT_SAFE_PRIME
1550Sstevel@tonic-gate 
156*2139Sjp161948 #define DHparams_dup(x) ASN1_dup_of_const(DH,i2d_DHparams,d2i_DHparams,x)
1570Sstevel@tonic-gate #define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
1580Sstevel@tonic-gate 		(char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
1590Sstevel@tonic-gate #define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
1600Sstevel@tonic-gate 		(unsigned char *)(x))
161*2139Sjp161948 #define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
162*2139Sjp161948 #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate const DH_METHOD *DH_OpenSSL(void);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate void DH_set_default_method(const DH_METHOD *meth);
1670Sstevel@tonic-gate const DH_METHOD *DH_get_default_method(void);
1680Sstevel@tonic-gate int DH_set_method(DH *dh, const DH_METHOD *meth);
1690Sstevel@tonic-gate DH *DH_new_method(ENGINE *engine);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate DH *	DH_new(void);
1720Sstevel@tonic-gate void	DH_free(DH *dh);
1730Sstevel@tonic-gate int	DH_up_ref(DH *dh);
1740Sstevel@tonic-gate int	DH_size(const DH *dh);
1750Sstevel@tonic-gate int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1760Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
1770Sstevel@tonic-gate int DH_set_ex_data(DH *d, int idx, void *arg);
1780Sstevel@tonic-gate void *DH_get_ex_data(DH *d, int idx);
179*2139Sjp161948 
180*2139Sjp161948 /* Deprecated version */
181*2139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
1820Sstevel@tonic-gate DH *	DH_generate_parameters(int prime_len,int generator,
1830Sstevel@tonic-gate 		void (*callback)(int,int,void *),void *cb_arg);
184*2139Sjp161948 #endif /* !defined(OPENSSL_NO_DEPRECATED) */
185*2139Sjp161948 
186*2139Sjp161948 /* New version */
187*2139Sjp161948 int	DH_generate_parameters_ex(DH *dh, int prime_len,int generator, BN_GENCB *cb);
188*2139Sjp161948 
1890Sstevel@tonic-gate int	DH_check(const DH *dh,int *codes);
190*2139Sjp161948 int	DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes);
1910Sstevel@tonic-gate int	DH_generate_key(DH *dh);
1920Sstevel@tonic-gate int	DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
1930Sstevel@tonic-gate DH *	d2i_DHparams(DH **a,const unsigned char **pp, long length);
1940Sstevel@tonic-gate int	i2d_DHparams(const DH *a,unsigned char **pp);
1950Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
1960Sstevel@tonic-gate int	DHparams_print_fp(FILE *fp, const DH *x);
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
1990Sstevel@tonic-gate int	DHparams_print(BIO *bp, const DH *x);
2000Sstevel@tonic-gate #else
2010Sstevel@tonic-gate int	DHparams_print(char *bp, const DH *x);
2020Sstevel@tonic-gate #endif
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /* BEGIN ERROR CODES */
2050Sstevel@tonic-gate /* The following lines are auto generated by the script mkerr.pl. Any changes
2060Sstevel@tonic-gate  * made after this point may be overwritten when the script is next run.
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate void ERR_load_DH_strings(void);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /* Error codes for the DH functions. */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /* Function codes. */
213*2139Sjp161948 #define DH_F_COMPUTE_KEY				 102
2140Sstevel@tonic-gate #define DH_F_DHPARAMS_PRINT				 100
2150Sstevel@tonic-gate #define DH_F_DHPARAMS_PRINT_FP				 101
216*2139Sjp161948 #define DH_F_DH_BUILTIN_GENPARAMS			 106
2170Sstevel@tonic-gate #define DH_F_DH_NEW_METHOD				 105
218*2139Sjp161948 #define DH_F_GENERATE_KEY				 103
219*2139Sjp161948 #define DH_F_GENERATE_PARAMETERS			 104
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /* Reason codes. */
2220Sstevel@tonic-gate #define DH_R_BAD_GENERATOR				 101
2230Sstevel@tonic-gate #define DH_R_NO_PRIVATE_VALUE				 100
224*2139Sjp161948 #define DH_R_INVALID_PUBKEY				 102
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #ifdef  __cplusplus
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate #endif
2290Sstevel@tonic-gate #endif
230