1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * Copyright (c) 2006 Kungliga Tekniska Högskolan
26 * (Royal Institute of Technology, Stockholm, Sweden).
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * 1. Redistributions of source code must retain the above copyright
34 *    notice, this list of conditions and the following disclaimer.
35 *
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 *
40 * 3. Neither the name of the Institute nor the names of its contributors
41 *    may be used to endorse or promote products derived from this software
42 *    without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#ifndef _OSSL_DH_H_
58#define _OSSL_DH_H_			1
59
60/* symbol renaming */
61#define DH_null_method			ossl_DH_null_method
62#define DH_imath_method			ossl_DH_imath_method
63#define DH_cdsa_method			ossl_DH_cdsa_method
64#define DH_tfm_method			ossl_DH_tfm_method
65#define DH_ltm_method			ossl_DH_ltm_method
66#define DH_sf_method			ossl_DH_sf_method
67#define DH_new				ossl_DH_new
68#define DH_new_method			ossl_DH_new_method
69#define DH_free				ossl_DH_free
70#define DH_up_ref			ossl_DH_up_ref
71#define DH_size				ossl_DH_size
72#define DH_set_default_method		ossl_DH_set_default_method
73#define DH_get_default_method		ossl_DH_get_default_method
74#define DH_set_method			ossl_DH_set_method
75#define DH_get_method			ossl_DH_get_method
76#define DH_set_ex_data			ossl_DH_set_ex_data
77#define DH_get_ex_data			ossl_DH_get_ex_data
78#define DH_generate_parameters_ex	ossl_DH_generate_parameters_ex
79#define DH_check_pubkey			ossl_DH_check_pubkey
80#define DH_generate_key			ossl_DH_generate_key
81#define DH_compute_key			ossl_DH_compute_key
82#define i2d_DHparams			ossl_i2d_DHparams
83
84/*
85 *
86 */
87
88typedef struct DH		DH;
89typedef struct DH_METHOD	DH_METHOD;
90
91#include "ossl-bn.h"
92#include "ossl-engine.h"
93
94struct DH_METHOD {
95	const char *	name;
96	int		(*generate_key)(DH *);
97	int		(*compute_key)(unsigned char *, const BIGNUM *, DH *);
98	int		(*bn_mod_exp)(const DH *, BIGNUM *, const BIGNUM *,
99			    const BIGNUM *, const BIGNUM *, BN_CTX *
100			    /* , BN_MONT_CTX * */);
101	int		(*init)(DH *);
102	int		(*finish)(DH *);
103	int		flags;
104	void *		app_data;
105	int		(*generate_params)(DH *, int, int, BN_GENCB *);
106};
107
108struct DH {
109	int			pad;
110	int			version;
111	BIGNUM *		p;
112	BIGNUM *		g;
113	long			length;
114	BIGNUM *		pub_key;
115	BIGNUM *		priv_key;
116	int			flags;
117	void *			method_mont_p;
118	BIGNUM *		q;
119	BIGNUM *		j;
120	void *			seed;
121	int			seedlen;
122	BIGNUM *		counter;
123	int			references;
124	struct CRYPTO_EX_DATA {
125		void *	sk;
126		int	dummy;
127	}
128	ex_data;
129	const DH_METHOD *	meth;
130	ENGINE *		engine;
131};
132
133/* DH_check_pubkey return codes in `codes' argument. */
134#define DH_CHECK_PUBKEY_TOO_SMALL	1
135#define DH_CHECK_PUBKEY_TOO_LARGE	2
136
137#define DH_GENERATOR_5			5
138
139/* DH flags */
140#define DH_FLAG_NO_EXP_CONSTTIME	0x02
141
142/*
143 *
144 */
145
146const DH_METHOD *DH_null_method(void);
147const DH_METHOD *DH_tfm_method(void);
148const DH_METHOD *DH_ltm_method(void);
149const DH_METHOD *DH_imath_method(void);
150const DH_METHOD *DH_cdsa_method(void);
151const DH_METHOD *DH_sf_method(void);
152
153DH *DH_new(void);
154DH *DH_new_method(ENGINE *);
155void DH_free(DH *);
156int DH_up_ref(DH *);
157
158int DH_size(const DH *);
159
160
161void DH_set_default_method(const DH_METHOD *);
162const DH_METHOD *
163DH_get_default_method(void);
164int DH_set_method(DH *, const DH_METHOD *);
165
166int DH_set_ex_data(DH *, int, void *);
167void *DH_get_ex_data(DH *, int);
168
169int DH_generate_parameters_ex(DH *, int, int, BN_GENCB *);
170int DH_check_pubkey(const DH *, const BIGNUM *, int *);
171int DH_generate_key(DH *);
172int DH_compute_key(unsigned char *, const BIGNUM *, DH *);
173
174int i2d_DHparams(DH *, unsigned char **);
175
176#endif /* _OSSL_DH_H_ */
177