xref: /onnv-gate/usr/src/common/openssl/crypto/dsa/dsa.h (revision 2864:0fad37eee08f)
10Sstevel@tonic-gate /* crypto/dsa/dsa.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 /*
600Sstevel@tonic-gate  * The DSS routines are based on patches supplied by
610Sstevel@tonic-gate  * Steven Schoch <schoch@sheba.arc.nasa.gov>.  He basically did the
620Sstevel@tonic-gate  * work and I have just tweaked them a little to fit into my
630Sstevel@tonic-gate  * stylistic vision for SSLeay :-) */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifndef HEADER_DSA_H
660Sstevel@tonic-gate #define HEADER_DSA_H
670Sstevel@tonic-gate 
682139Sjp161948 #include <openssl/e_os2.h>
692139Sjp161948 
700Sstevel@tonic-gate #ifdef OPENSSL_NO_DSA
710Sstevel@tonic-gate #error DSA is disabled.
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
750Sstevel@tonic-gate #include <openssl/bio.h>
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate #include <openssl/crypto.h>
780Sstevel@tonic-gate #include <openssl/ossl_typ.h>
792139Sjp161948 
802139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
812139Sjp161948 #include <openssl/bn.h>
820Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
830Sstevel@tonic-gate # include <openssl/dh.h>
840Sstevel@tonic-gate #endif
852139Sjp161948 #endif
860Sstevel@tonic-gate 
87*2864Sjp161948 #ifndef OPENSSL_DSA_MAX_MODULUS_BITS
88*2864Sjp161948 # define OPENSSL_DSA_MAX_MODULUS_BITS  10000
89*2864Sjp161948 #endif
90*2864Sjp161948 
910Sstevel@tonic-gate #define DSA_FLAG_CACHE_MONT_P	0x01
922139Sjp161948 #define DSA_FLAG_NO_EXP_CONSTTIME       0x02 /* new with 0.9.7h; the built-in DSA
932139Sjp161948                                               * implementation now uses constant time
942139Sjp161948                                               * modular exponentiation for secret exponents
952139Sjp161948                                               * by default. This flag causes the
962139Sjp161948                                               * faster variable sliding window method to
972139Sjp161948                                               * be used for all exponents.
982139Sjp161948                                               */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #ifdef  __cplusplus
1010Sstevel@tonic-gate extern "C" {
1020Sstevel@tonic-gate #endif
1030Sstevel@tonic-gate 
1042139Sjp161948 /* Already defined in ossl_typ.h */
1052139Sjp161948 /* typedef struct dsa_st DSA; */
1062139Sjp161948 /* typedef struct dsa_method DSA_METHOD; */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate typedef struct DSA_SIG_st
1090Sstevel@tonic-gate 	{
1100Sstevel@tonic-gate 	BIGNUM *r;
1110Sstevel@tonic-gate 	BIGNUM *s;
1120Sstevel@tonic-gate 	} DSA_SIG;
1130Sstevel@tonic-gate 
1142139Sjp161948 struct dsa_method
1152139Sjp161948 	{
1160Sstevel@tonic-gate 	const char *name;
1170Sstevel@tonic-gate 	DSA_SIG * (*dsa_do_sign)(const unsigned char *dgst, int dlen, DSA *dsa);
1180Sstevel@tonic-gate 	int (*dsa_sign_setup)(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
1190Sstevel@tonic-gate 								BIGNUM **rp);
1200Sstevel@tonic-gate 	int (*dsa_do_verify)(const unsigned char *dgst, int dgst_len,
1210Sstevel@tonic-gate 							DSA_SIG *sig, DSA *dsa);
1220Sstevel@tonic-gate 	int (*dsa_mod_exp)(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1,
1230Sstevel@tonic-gate 			BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx,
1240Sstevel@tonic-gate 			BN_MONT_CTX *in_mont);
1250Sstevel@tonic-gate 	int (*bn_mod_exp)(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
1260Sstevel@tonic-gate 				const BIGNUM *m, BN_CTX *ctx,
1270Sstevel@tonic-gate 				BN_MONT_CTX *m_ctx); /* Can be null */
1280Sstevel@tonic-gate 	int (*init)(DSA *dsa);
1290Sstevel@tonic-gate 	int (*finish)(DSA *dsa);
1300Sstevel@tonic-gate 	int flags;
1310Sstevel@tonic-gate 	char *app_data;
1322139Sjp161948 	/* If this is non-NULL, it is used to generate DSA parameters */
1332139Sjp161948 	int (*dsa_paramgen)(DSA *dsa, int bits,
1342139Sjp161948 			unsigned char *seed, int seed_len,
1352139Sjp161948 			int *counter_ret, unsigned long *h_ret,
1362139Sjp161948 			BN_GENCB *cb);
1372139Sjp161948 	/* If this is non-NULL, it is used to generate DSA keys */
1382139Sjp161948 	int (*dsa_keygen)(DSA *dsa);
1392139Sjp161948 	};
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate struct dsa_st
1420Sstevel@tonic-gate 	{
1430Sstevel@tonic-gate 	/* This first variable is used to pick up errors where
1440Sstevel@tonic-gate 	 * a DSA is passed instead of of a EVP_PKEY */
1450Sstevel@tonic-gate 	int pad;
1460Sstevel@tonic-gate 	long version;
1470Sstevel@tonic-gate 	int write_params;
1480Sstevel@tonic-gate 	BIGNUM *p;
1490Sstevel@tonic-gate 	BIGNUM *q;	/* == 20 */
1500Sstevel@tonic-gate 	BIGNUM *g;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	BIGNUM *pub_key;  /* y public key */
1530Sstevel@tonic-gate 	BIGNUM *priv_key; /* x private key */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	BIGNUM *kinv;	/* Signing pre-calc */
1560Sstevel@tonic-gate 	BIGNUM *r;	/* Signing pre-calc */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	int flags;
1590Sstevel@tonic-gate 	/* Normally used to cache montgomery values */
1602139Sjp161948 	BN_MONT_CTX *method_mont_p;
1610Sstevel@tonic-gate 	int references;
1620Sstevel@tonic-gate 	CRYPTO_EX_DATA ex_data;
1630Sstevel@tonic-gate 	const DSA_METHOD *meth;
1640Sstevel@tonic-gate 	/* functional reference if 'meth' is ENGINE-provided */
1650Sstevel@tonic-gate 	ENGINE *engine;
1660Sstevel@tonic-gate 	};
1670Sstevel@tonic-gate 
1682139Sjp161948 #define DSAparams_dup(x) ASN1_dup_of_const(DSA,i2d_DSAparams,d2i_DSAparams,x)
1690Sstevel@tonic-gate #define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \
1700Sstevel@tonic-gate 		(char *(*)())d2i_DSAparams,(fp),(unsigned char **)(x))
1710Sstevel@tonic-gate #define i2d_DSAparams_fp(fp,x) ASN1_i2d_fp(i2d_DSAparams,(fp), \
1720Sstevel@tonic-gate 		(unsigned char *)(x))
1732139Sjp161948 #define d2i_DSAparams_bio(bp,x) ASN1_d2i_bio_of(DSA,DSA_new,d2i_DSAparams,bp,x)
1742139Sjp161948 #define i2d_DSAparams_bio(bp,x) ASN1_i2d_bio_of_const(DSA,i2d_DSAparams,bp,x)
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate DSA_SIG * DSA_SIG_new(void);
1780Sstevel@tonic-gate void	DSA_SIG_free(DSA_SIG *a);
1790Sstevel@tonic-gate int	i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp);
1800Sstevel@tonic-gate DSA_SIG * d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate DSA_SIG * DSA_do_sign(const unsigned char *dgst,int dlen,DSA *dsa);
1830Sstevel@tonic-gate int	DSA_do_verify(const unsigned char *dgst,int dgst_len,
1840Sstevel@tonic-gate 		      DSA_SIG *sig,DSA *dsa);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate const DSA_METHOD *DSA_OpenSSL(void);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate void	DSA_set_default_method(const DSA_METHOD *);
1890Sstevel@tonic-gate const DSA_METHOD *DSA_get_default_method(void);
1900Sstevel@tonic-gate int	DSA_set_method(DSA *dsa, const DSA_METHOD *);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate DSA *	DSA_new(void);
1930Sstevel@tonic-gate DSA *	DSA_new_method(ENGINE *engine);
1940Sstevel@tonic-gate void	DSA_free (DSA *r);
1950Sstevel@tonic-gate /* "up" the DSA object's reference count */
1960Sstevel@tonic-gate int	DSA_up_ref(DSA *r);
1970Sstevel@tonic-gate int	DSA_size(const DSA *);
1980Sstevel@tonic-gate 	/* next 4 return -1 on error */
1990Sstevel@tonic-gate int	DSA_sign_setup( DSA *dsa,BN_CTX *ctx_in,BIGNUM **kinvp,BIGNUM **rp);
2000Sstevel@tonic-gate int	DSA_sign(int type,const unsigned char *dgst,int dlen,
2010Sstevel@tonic-gate 		unsigned char *sig, unsigned int *siglen, DSA *dsa);
2020Sstevel@tonic-gate int	DSA_verify(int type,const unsigned char *dgst,int dgst_len,
2030Sstevel@tonic-gate 		const unsigned char *sigbuf, int siglen, DSA *dsa);
2040Sstevel@tonic-gate int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2050Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
2060Sstevel@tonic-gate int DSA_set_ex_data(DSA *d, int idx, void *arg);
2070Sstevel@tonic-gate void *DSA_get_ex_data(DSA *d, int idx);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate DSA *	d2i_DSAPublicKey(DSA **a, const unsigned char **pp, long length);
2100Sstevel@tonic-gate DSA *	d2i_DSAPrivateKey(DSA **a, const unsigned char **pp, long length);
2110Sstevel@tonic-gate DSA * 	d2i_DSAparams(DSA **a, const unsigned char **pp, long length);
2122139Sjp161948 
2132139Sjp161948 /* Deprecated version */
2142139Sjp161948 #ifndef OPENSSL_NO_DEPRECATED
2150Sstevel@tonic-gate DSA *	DSA_generate_parameters(int bits,
2160Sstevel@tonic-gate 		unsigned char *seed,int seed_len,
2170Sstevel@tonic-gate 		int *counter_ret, unsigned long *h_ret,void
2180Sstevel@tonic-gate 		(*callback)(int, int, void *),void *cb_arg);
2192139Sjp161948 #endif /* !defined(OPENSSL_NO_DEPRECATED) */
2202139Sjp161948 
2212139Sjp161948 /* New version */
2222139Sjp161948 int	DSA_generate_parameters_ex(DSA *dsa, int bits,
2232139Sjp161948 		unsigned char *seed,int seed_len,
2242139Sjp161948 		int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
2252139Sjp161948 
2260Sstevel@tonic-gate int	DSA_generate_key(DSA *a);
2270Sstevel@tonic-gate int	i2d_DSAPublicKey(const DSA *a, unsigned char **pp);
2280Sstevel@tonic-gate int 	i2d_DSAPrivateKey(const DSA *a, unsigned char **pp);
2290Sstevel@tonic-gate int	i2d_DSAparams(const DSA *a,unsigned char **pp);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate #ifndef OPENSSL_NO_BIO
2320Sstevel@tonic-gate int	DSAparams_print(BIO *bp, const DSA *x);
2330Sstevel@tonic-gate int	DSA_print(BIO *bp, const DSA *x, int off);
2340Sstevel@tonic-gate #endif
2350Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
2360Sstevel@tonic-gate int	DSAparams_print_fp(FILE *fp, const DSA *x);
2370Sstevel@tonic-gate int	DSA_print_fp(FILE *bp, const DSA *x, int off);
2380Sstevel@tonic-gate #endif
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate #define DSS_prime_checks 50
2410Sstevel@tonic-gate /* Primality test according to FIPS PUB 186[-1], Appendix 2.1:
2420Sstevel@tonic-gate  * 50 rounds of Rabin-Miller */
2430Sstevel@tonic-gate #define DSA_is_prime(n, callback, cb_arg) \
2440Sstevel@tonic-gate 	BN_is_prime(n, DSS_prime_checks, callback, NULL, cb_arg)
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
2470Sstevel@tonic-gate /* Convert DSA structure (key or just parameters) into DH structure
2480Sstevel@tonic-gate  * (be careful to avoid small subgroup attacks when using this!) */
2490Sstevel@tonic-gate DH *DSA_dup_DH(const DSA *r);
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /* BEGIN ERROR CODES */
2530Sstevel@tonic-gate /* The following lines are auto generated by the script mkerr.pl. Any changes
2540Sstevel@tonic-gate  * made after this point may be overwritten when the script is next run.
2550Sstevel@tonic-gate  */
2560Sstevel@tonic-gate void ERR_load_DSA_strings(void);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /* Error codes for the DSA functions. */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /* Function codes. */
2610Sstevel@tonic-gate #define DSA_F_D2I_DSA_SIG				 110
2620Sstevel@tonic-gate #define DSA_F_DSAPARAMS_PRINT				 100
2630Sstevel@tonic-gate #define DSA_F_DSAPARAMS_PRINT_FP			 101
2640Sstevel@tonic-gate #define DSA_F_DSA_DO_SIGN				 112
2650Sstevel@tonic-gate #define DSA_F_DSA_DO_VERIFY				 113
2660Sstevel@tonic-gate #define DSA_F_DSA_NEW_METHOD				 103
2670Sstevel@tonic-gate #define DSA_F_DSA_PRINT					 104
2680Sstevel@tonic-gate #define DSA_F_DSA_PRINT_FP				 105
2690Sstevel@tonic-gate #define DSA_F_DSA_SIGN					 106
2700Sstevel@tonic-gate #define DSA_F_DSA_SIGN_SETUP				 107
2710Sstevel@tonic-gate #define DSA_F_DSA_SIG_NEW				 109
2720Sstevel@tonic-gate #define DSA_F_DSA_VERIFY				 108
2730Sstevel@tonic-gate #define DSA_F_I2D_DSA_SIG				 111
2740Sstevel@tonic-gate #define DSA_F_SIG_CB					 114
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /* Reason codes. */
2770Sstevel@tonic-gate #define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE		 100
2780Sstevel@tonic-gate #define DSA_R_MISSING_PARAMETERS			 101
279*2864Sjp161948 #define DSA_R_BAD_Q_VALUE				 102
280*2864Sjp161948 #define DSA_R_MODULUS_TOO_LARGE				 103
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate #ifdef  __cplusplus
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate #endif
2850Sstevel@tonic-gate #endif
286