xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/dh.h (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: dh.h,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Id
38  */
39 
40 #ifndef _HEIM_DH_H
41 #define _HEIM_DH_H 1
42 
43 /* symbol renaming */
44 #define DH hc_DH
45 #define DH_METHOD hc_DH_METHOD
46 #define DH_null_method hc_DH_null_method
47 #define DH_tfm_method hc_DH_tfm_method
48 #define DH_ltm_method hc_DH_ltm_method
49 #define DH_new hc_DH_new
50 #define DH_new_method hc_DH_new_method
51 #define DH_free hc_DH_free
52 #define DH_up_ref hc_DH_up_ref
53 #define DH_size hc_DH_size
54 #define DH_set_default_method hc_DH_set_default_method
55 #define DH_get_default_method hc_DH_get_default_method
56 #define DH_set_method hc_DH_set_method
57 #define DH_get_method hc_DH_get_method
58 #define DH_set_ex_data hc_DH_set_ex_data
59 #define DH_get_ex_data hc_DH_get_ex_data
60 #define DH_generate_parameters_ex hc_DH_generate_parameters_ex
61 #define DH_check_pubkey hc_DH_check_pubkey
62 #define DH_generate_key hc_DH_generate_key
63 #define DH_compute_key hc_DH_compute_key
64 #define	i2d_DHparams hc_i2d_DHparams
65 
66 /*
67  *
68  */
69 
70 typedef struct DH DH;
71 typedef struct DH_METHOD DH_METHOD;
72 
73 #include <hcrypto/bn.h>
74 #include <hcrypto/engine.h>
75 
76 struct DH_METHOD {
77     const char *name;
78     int (*generate_key)(DH *);
79     int (*compute_key)(unsigned char *,const BIGNUM *,DH *);
80     int (*bn_mod_exp)(const DH *, BIGNUM *, const BIGNUM *,
81 		      const BIGNUM *, const BIGNUM *, BN_CTX *,
82 		      BN_MONT_CTX *);
83     int (*init)(DH *);
84     int (*finish)(DH *);
85     int flags;
86     void *app_data;
87     int (*generate_params)(DH *, int, int, BN_GENCB *);
88 };
89 
90 struct DH {
91     int pad;
92     int version;
93     BIGNUM *p;
94     BIGNUM *g;
95     long length;
96     BIGNUM *pub_key;
97     BIGNUM *priv_key;
98     int flags;
99     void *method_mont_p;
100     BIGNUM *q;
101     BIGNUM *j;
102     void *seed;
103     int seedlen;
104     BIGNUM *counter;
105     int references;
106     struct CRYPTO_EX_DATA {
107 	void *sk;
108 	int dummy;
109     } ex_data;
110     const DH_METHOD *meth;
111     ENGINE *engine;
112 };
113 
114 /* DH_check_pubkey return codes in `codes' argument. */
115 #define DH_CHECK_PUBKEY_TOO_SMALL 1
116 #define DH_CHECK_PUBKEY_TOO_LARGE 2
117 
118 /*
119  *
120  */
121 
122 const DH_METHOD *DH_null_method(void);
123 const DH_METHOD *DH_tfm_method(void);
124 const DH_METHOD *DH_ltm_method(void);
125 
126 DH *	DH_new(void);
127 DH *	DH_new_method(ENGINE *);
128 void	DH_free(DH *);
129 int	DH_up_ref(DH *);
130 
131 int	DH_size(const DH *);
132 
133 
134 void	DH_set_default_method(const DH_METHOD *);
135 const DH_METHOD *
136 	DH_get_default_method(void);
137 int	DH_set_method(DH *, const DH_METHOD *);
138 
139 int	DH_set_ex_data(DH *, int, void *);
140 void *	DH_get_ex_data(DH *, int);
141 
142 int	DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
143 int	DH_check_pubkey(const DH *, const BIGNUM *, int *);
144 int	DH_generate_key(DH *);
145 int	DH_compute_key(unsigned char *,const BIGNUM *,DH *);
146 
147 int	i2d_DHparams(DH *, unsigned char **);
148 
149 #endif /* _HEIM_DH_H */
150 
151