2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
38 RCSID("$Id: dh.c 18618 2006-10-19 17:31:51Z lha $");
53 return DH_new_method(NULL);
57 DH_new_method(ENGINE *engine)
61 dh = calloc(1, sizeof(*dh));
68 ENGINE_up_ref(engine);
71 dh->engine = ENGINE_get_default_DH();
75 dh->meth = ENGINE_get_DH(dh->engine);
76 if (dh->meth == NULL) {
77 ENGINE_finish(engine);
84 dh->meth = DH_get_default_method();
86 (*dh->meth->init)(dh);
94 if (dh->references <= 0)
97 if (--dh->references > 0)
100 (*dh->meth->finish)(dh);
103 ENGINE_finish(dh->engine);
105 #define free_if(f) if (f) { BN_free(f); }
108 free_if(dh->pub_key);
109 free_if(dh->priv_key);
112 free_if(dh->counter);
115 memset(dh, 0, sizeof(*dh));
122 return ++dh->references;
126 DH_size(const DH *dh)
128 return BN_num_bytes(dh->p);
132 DH_set_ex_data(DH *dh, int idx, void *data)
134 dh->ex_data.sk = data;
139 DH_get_ex_data(DH *dh, int idx)
141 return dh->ex_data.sk;
145 DH_generate_parameters_ex(DH *dh, int prime_len, int generator, BN_GENCB *cb)
147 if (dh->meth->generate_params)
148 return dh->meth->generate_params(dh, prime_len, generator, cb);
155 * pub_key > 1 and pub_key < p - 1
157 * to avoid small subgroups attack.
161 DH_check_pubkey(const DH *dh, const BIGNUM *pub_key, int *codes)
163 BIGNUM *bn = NULL, *sum = NULL;
172 if (!BN_set_word(bn, 1))
175 if (BN_cmp(bn, pub_key) >= 0)
176 *codes |= DH_CHECK_PUBKEY_TOO_SMALL;
182 BN_uadd(sum, pub_key, bn);
184 if (BN_cmp(sum, dh->p) >= 0)
185 *codes |= DH_CHECK_PUBKEY_TOO_LARGE;
198 DH_generate_key(DH *dh)
200 return dh->meth->generate_key(dh);
204 DH_compute_key(unsigned char *shared_key,
205 const BIGNUM *peer_pub_key, DH *dh)
209 if (!DH_check_pubkey(dh, peer_pub_key, &codes) || codes != 0)
212 return dh->meth->compute_key(shared_key, peer_pub_key, dh);
216 DH_set_method(DH *dh, const DH_METHOD *method)
218 (*dh->meth->finish)(dh);
220 ENGINE_finish(dh->engine);
224 (*dh->meth->init)(dh);
233 dh_null_generate_key(DH *dh)
239 dh_null_compute_key(unsigned char *shared,const BIGNUM *pub, DH *dh)
251 dh_null_finish(DH *dh)
257 dh_null_generate_params(DH *dh, int prime_num, int len, BN_GENCB *cb)
262 static const DH_METHOD dh_null_method = {
264 dh_null_generate_key,
271 dh_null_generate_params
274 extern const DH_METHOD hc_dh_imath_method;
275 static const DH_METHOD *dh_default_method = &hc_dh_imath_method;
280 return &dh_null_method;
284 DH_set_default_method(const DH_METHOD *meth)
286 dh_default_method = meth;
290 DH_get_default_method(void)
292 return dh_default_method;