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