xref: /onnv-gate/usr/src/common/openssl/crypto/dh/dh.h (revision 2864:0fad37eee08f)
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 
622139Sjp161948 #include <openssl/e_os2.h>
632139Sjp161948 
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
712139Sjp161948 #include <openssl/ossl_typ.h>
722139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
730Sstevel@tonic-gate #include <openssl/bn.h>
742139Sjp161948 #endif
75*2864Sjp161948 
76*2864Sjp161948 #ifndef OPENSSL_DH_MAX_MODULUS_BITS
77*2864Sjp161948 # define OPENSSL_DH_MAX_MODULUS_BITS   10000
78*2864Sjp161948 #endif
790Sstevel@tonic-gate 
802139Sjp161948 #define DH_FLAG_CACHE_MONT_P     0x01
812139Sjp161948 #define DH_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DH
822139Sjp161948                                        * implementation now uses constant time
832139Sjp161948                                        * modular exponentiation for secret exponents
842139Sjp161948                                        * by default. This flag causes the
852139Sjp161948                                        * faster variable sliding window method to
862139Sjp161948                                        * be used for all exponents.
872139Sjp161948                                        */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #ifdef  __cplusplus
900Sstevel@tonic-gate extern "C" {
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
932139Sjp161948 /* Already defined in ossl_typ.h */
942139Sjp161948 /* typedef struct dh_st DH; */
952139Sjp161948 /* typedef struct dh_method DH_METHOD; */
960Sstevel@tonic-gate 
972139Sjp161948 struct dh_method
982139Sjp161948 	{
990Sstevel@tonic-gate 	const char *name;
1000Sstevel@tonic-gate 	/* Methods here */
1010Sstevel@tonic-gate 	int (*generate_key)(DH *dh);
1020Sstevel@tonic-gate 	int (*compute_key)(unsigned char *key,const BIGNUM *pub_key,DH *dh);
1030Sstevel@tonic-gate 	int (*bn_mod_exp)(const DH *dh, BIGNUM *r, const BIGNUM *a,
1040Sstevel@tonic-gate 				const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
1050Sstevel@tonic-gate 				BN_MONT_CTX *m_ctx); /* Can be null */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	int (*init)(DH *dh);
1080Sstevel@tonic-gate 	int (*finish)(DH *dh);
1090Sstevel@tonic-gate 	int flags;
1100Sstevel@tonic-gate 	char *app_data;
1112139Sjp161948 	/* If this is non-NULL, it will be used to generate parameters */
1122139Sjp161948 	int (*generate_params)(DH *dh, int prime_len, int generator, BN_GENCB *cb);
1132139Sjp161948 	};
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate struct dh_st
1160Sstevel@tonic-gate 	{
1170Sstevel@tonic-gate 	/* This first argument is used to pick up errors when
1180Sstevel@tonic-gate 	 * a DH is passed instead of a EVP_PKEY */
1190Sstevel@tonic-gate 	int pad;
1200Sstevel@tonic-gate 	int version;
1210Sstevel@tonic-gate 	BIGNUM *p;
1220Sstevel@tonic-gate 	BIGNUM *g;
1230Sstevel@tonic-gate 	long length; /* optional */
1240Sstevel@tonic-gate 	BIGNUM *pub_key;	/* g^x */
1250Sstevel@tonic-gate 	BIGNUM *priv_key;	/* x */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	int flags;
1282139Sjp161948 	BN_MONT_CTX *method_mont_p;
1290Sstevel@tonic-gate 	/* Place holders if we want to do X9.42 DH */
1300Sstevel@tonic-gate 	BIGNUM *q;
1310Sstevel@tonic-gate 	BIGNUM *j;
1320Sstevel@tonic-gate 	unsigned char *seed;
1330Sstevel@tonic-gate 	int seedlen;
1340Sstevel@tonic-gate 	BIGNUM *counter;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	int references;
1370Sstevel@tonic-gate 	CRYPTO_EX_DATA ex_data;
1380Sstevel@tonic-gate 	const DH_METHOD *meth;
1390Sstevel@tonic-gate 	ENGINE *engine;
1400Sstevel@tonic-gate 	};
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #define DH_GENERATOR_2		2
1430Sstevel@tonic-gate /* #define DH_GENERATOR_3	3 */
1440Sstevel@tonic-gate #define DH_GENERATOR_5		5
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* DH_check error codes */
1470Sstevel@tonic-gate #define DH_CHECK_P_NOT_PRIME		0x01
1480Sstevel@tonic-gate #define DH_CHECK_P_NOT_SAFE_PRIME	0x02
1490Sstevel@tonic-gate #define DH_UNABLE_TO_CHECK_GENERATOR	0x04
1500Sstevel@tonic-gate #define DH_NOT_SUITABLE_GENERATOR	0x08
1510Sstevel@tonic-gate 
1522139Sjp161948 /* DH_check_pub_key error codes */
1532139Sjp161948 #define DH_CHECK_PUBKEY_TOO_SMALL	0x01
1542139Sjp161948 #define DH_CHECK_PUBKEY_TOO_LARGE	0x02
1552139Sjp161948 
1560Sstevel@tonic-gate /* primes p where (p-1)/2 is prime too are called "safe"; we define
1570Sstevel@tonic-gate    this for backward compatibility: */
1580Sstevel@tonic-gate #define DH_CHECK_P_NOT_STRONG_PRIME	DH_CHECK_P_NOT_SAFE_PRIME
1590Sstevel@tonic-gate 
1602139Sjp161948 #define DHparams_dup(x) ASN1_dup_of_const(DH,i2d_DHparams,d2i_DHparams,x)
1610Sstevel@tonic-gate #define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
1620Sstevel@tonic-gate 		(char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
1630Sstevel@tonic-gate #define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
1640Sstevel@tonic-gate 		(unsigned char *)(x))
1652139Sjp161948 #define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
1662139Sjp161948 #define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate const DH_METHOD *DH_OpenSSL(void);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate void DH_set_default_method(const DH_METHOD *meth);
1710Sstevel@tonic-gate const DH_METHOD *DH_get_default_method(void);
1720Sstevel@tonic-gate int DH_set_method(DH *dh, const DH_METHOD *meth);
1730Sstevel@tonic-gate DH *DH_new_method(ENGINE *engine);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate DH *	DH_new(void);
1760Sstevel@tonic-gate void	DH_free(DH *dh);
1770Sstevel@tonic-gate int	DH_up_ref(DH *dh);
1780Sstevel@tonic-gate int	DH_size(const DH *dh);
1790Sstevel@tonic-gate int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1800Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
1810Sstevel@tonic-gate int DH_set_ex_data(DH *d, int idx, void *arg);
1820Sstevel@tonic-gate void *DH_get_ex_data(DH *d, int idx);
1832139Sjp161948 
1842139Sjp161948 /* Deprecated version */
1852139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
1860Sstevel@tonic-gate DH *	DH_generate_parameters(int prime_len,int generator,
1870Sstevel@tonic-gate 		void (*callback)(int,int,void *),void *cb_arg);
1882139Sjp161948 #endif /* !defined(OPENSSL_NO_DEPRECATED) */
1892139Sjp161948 
1902139Sjp161948 /* New version */
1912139Sjp161948 int	DH_generate_parameters_ex(DH *dh, int prime_len,int generator, BN_GENCB *cb);
1922139Sjp161948 
1930Sstevel@tonic-gate int	DH_check(const DH *dh,int *codes);
1942139Sjp161948 int	DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes);
1950Sstevel@tonic-gate int	DH_generate_key(DH *dh);
1960Sstevel@tonic-gate int	DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
1970Sstevel@tonic-gate DH *	d2i_DHparams(DH **a,const unsigned char **pp, long length);
1980Sstevel@tonic-gate int	i2d_DHparams(const DH *a,unsigned char **pp);
1990Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
2000Sstevel@tonic-gate int	DHparams_print_fp(FILE *fp, const DH *x);
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
2030Sstevel@tonic-gate int	DHparams_print(BIO *bp, const DH *x);
2040Sstevel@tonic-gate #else
2050Sstevel@tonic-gate int	DHparams_print(char *bp, const DH *x);
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* BEGIN ERROR CODES */
2090Sstevel@tonic-gate /* The following lines are auto generated by the script mkerr.pl. Any changes
2100Sstevel@tonic-gate  * made after this point may be overwritten when the script is next run.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate void ERR_load_DH_strings(void);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /* Error codes for the DH functions. */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /* Function codes. */
2172139Sjp161948 #define DH_F_COMPUTE_KEY				 102
2180Sstevel@tonic-gate #define DH_F_DHPARAMS_PRINT				 100
2190Sstevel@tonic-gate #define DH_F_DHPARAMS_PRINT_FP				 101
2202139Sjp161948 #define DH_F_DH_BUILTIN_GENPARAMS			 106
2210Sstevel@tonic-gate #define DH_F_DH_NEW_METHOD				 105
2222139Sjp161948 #define DH_F_GENERATE_KEY				 103
2232139Sjp161948 #define DH_F_GENERATE_PARAMETERS			 104
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* Reason codes. */
2260Sstevel@tonic-gate #define DH_R_BAD_GENERATOR				 101
2270Sstevel@tonic-gate #define DH_R_NO_PRIVATE_VALUE				 100
2282139Sjp161948 #define DH_R_INVALID_PUBKEY				 102
229*2864Sjp161948 #define DH_R_MODULUS_TOO_LARGE				 103
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate #ifdef  __cplusplus
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate #endif
2340Sstevel@tonic-gate #endif
235