Include <com_err.h>
[heimdal.git] / lib / hcrypto / dh.c
blobd42ac34fd22533bdaa14c6791fe3d259a843782d
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <dh.h>
42 #include <roken.h>
44 /**
45 * @page page_dh DH - Diffie-Hellman key exchange
47 * Diffie-Hellman key exchange is a protocol that allows two parties
48 * to establish a shared secret key.
50 * Include and example how to use DH_new() and friends here.
52 * See the library functions here: @ref hcrypto_dh
55 /**
56 * Create a new DH object using DH_new_method(NULL), see DH_new_method().
58 * @return a newly allocated DH object.
60 * @ingroup hcrypto_dh
63 DH *
64 DH_new(void)
66 return DH_new_method(NULL);
69 /**
70 * Create a new DH object from the given engine, if the NULL is used,
71 * the default engine is used. Free the DH object with DH_free().
73 * @param engine The engine to use to allocate the DH object.
75 * @return a newly allocated DH object.
77 * @ingroup hcrypto_dh
80 DH *
81 DH_new_method(ENGINE *engine)
83 DH *dh;
85 dh = calloc(1, sizeof(*dh));
86 if (dh == NULL)
87 return NULL;
89 dh->references = 1;
91 if (engine) {
92 ENGINE_up_ref(engine);
93 dh->engine = engine;
94 } else {
95 dh->engine = ENGINE_get_default_DH();
98 if (dh->engine) {
99 dh->meth = ENGINE_get_DH(dh->engine);
100 if (dh->meth == NULL) {
101 ENGINE_finish(engine);
102 free(dh);
103 return 0;
107 if (dh->meth == NULL)
108 dh->meth = DH_get_default_method();
110 (*dh->meth->init)(dh);
112 return dh;
116 * Free a DH object and release related resources, like ENGINE, that
117 * the object was using.
119 * @param dh object to be freed.
121 * @ingroup hcrypto_dh
124 void
125 DH_free(DH *dh)
127 if (dh->references <= 0)
128 abort();
130 if (--dh->references > 0)
131 return;
133 (*dh->meth->finish)(dh);
135 if (dh->engine)
136 ENGINE_finish(dh->engine);
138 #define free_if(f) if (f) { BN_free(f); }
139 free_if(dh->p);
140 free_if(dh->g);
141 free_if(dh->pub_key);
142 free_if(dh->priv_key);
143 free_if(dh->q);
144 free_if(dh->j);
145 free_if(dh->counter);
146 #undef free_if
148 memset(dh, 0, sizeof(*dh));
149 free(dh);
153 * Add a reference to the DH object. The object should be free with
154 * DH_free() to drop the reference.
156 * @param dh the object to increase the reference count too.
158 * @return the updated reference count, can't safely be used except
159 * for debug printing.
161 * @ingroup hcrypto_dh
165 DH_up_ref(DH *dh)
167 return ++dh->references;
171 * The maximum output size of the DH_compute_key() function.
173 * @param dh The DH object to get the size from.
175 * @return the maximum size in bytes of the out data.
177 * @ingroup hcrypto_dh
181 DH_size(const DH *dh)
183 return BN_num_bytes(dh->p);
187 * Set the data index idx in the DH object to data.
189 * @param dh DH object.
190 * @param idx index to set the data for.
191 * @param data data to store for the index idx.
193 * @return 1 on success.
195 * @ingroup hcrypto_dh
199 DH_set_ex_data(DH *dh, int idx, void *data)
201 dh->ex_data.sk = data;
202 return 1;
206 * Get the data for index idx in the DH object.
208 * @param dh DH object.
209 * @param idx index to get the data for.
211 * @return the object store in index idx
213 * @ingroup hcrypto_dh
216 void *
217 DH_get_ex_data(DH *dh, int idx)
219 return dh->ex_data.sk;
223 * Generate DH parameters for the DH object give parameters.
225 * @param dh The DH object to generate parameters for.
226 * @param prime_len length of the prime
227 * @param generator generator, g
228 * @param cb Callback parameters to show progress, can be NULL.
230 * @return the maximum size in bytes of the out data.
232 * @ingroup hcrypto_dh
236 DH_generate_parameters_ex(DH *dh, int prime_len, int generator, BN_GENCB *cb)
238 if (dh->meth->generate_params)
239 return dh->meth->generate_params(dh, prime_len, generator, cb);
240 return 0;
244 * Check that the public key is sane.
246 * @param dh the local peer DH parameters.
247 * @param pub_key the remote peer public key parameters.
248 * @param codes return that the failures of the pub_key are.
250 * @return 1 on success, 0 on failure and *codes is set the the
251 * combined fail check for the public key
253 * @ingroup hcrypto_dh
257 DH_check_pubkey(const DH *dh, const BIGNUM *pub_key, int *codes)
259 BIGNUM *bn = NULL, *sum = NULL;
260 int ret = 0;
262 *codes = 0;
265 * Checks that the function performs are:
266 * - pub_key is not negative
269 if (BN_is_negative(pub_key))
270 goto out;
273 * - pub_key > 1 and pub_key < p - 1,
274 * to avoid small subgroups attack.
277 bn = BN_new();
278 if (bn == NULL)
279 goto out;
281 if (!BN_set_word(bn, 1))
282 goto out;
284 if (BN_cmp(bn, pub_key) >= 0)
285 *codes |= DH_CHECK_PUBKEY_TOO_SMALL;
287 sum = BN_new();
288 if (sum == NULL)
289 goto out;
291 BN_uadd(sum, pub_key, bn);
293 if (BN_cmp(sum, dh->p) >= 0)
294 *codes |= DH_CHECK_PUBKEY_TOO_LARGE;
297 * - if g == 2, pub_key have more then one bit set,
298 * if bits set is 1, log_2(pub_key) is trival
301 if (!BN_set_word(bn, 2))
302 goto out;
304 if (BN_cmp(bn, pub_key) == 0) {
305 unsigned i, n = BN_num_bits(pub_key);
306 unsigned bits = 0;
308 for (i = 0; i <= n; i++)
309 if (BN_is_bit_set(pub_key, i))
310 bits++;
312 if (bits > 1) {
313 *codes |= DH_CHECK_PUBKEY_TOO_SMALL;
314 goto out;
318 ret = 1;
319 out:
320 if (bn)
321 BN_free(bn);
322 if (sum)
323 BN_free(sum);
325 return ret;
329 * Generate a new DH private-public key pair. The dh parameter must be
330 * allocted first with DH_new(). dh->p and dp->g must be set.
332 * @param dh dh parameter.
334 * @return 1 on success.
336 * @ingroup hcrypto_dh
340 DH_generate_key(DH *dh)
342 return dh->meth->generate_key(dh);
346 * Complute the shared secret key.
348 * @param shared_key the resulting shared key, need to be at least
349 * DH_size() large.
350 * @param peer_pub_key the peer's public key.
351 * @param dh the dh key pair.
353 * @return 1 on success.
355 * @ingroup hcrypto_dh
359 DH_compute_key(unsigned char *shared_key,
360 const BIGNUM *peer_pub_key, DH *dh)
362 int codes;
365 * Checks that the pubkey passed in is valid using
366 * DH_check_pubkey().
369 if (!DH_check_pubkey(dh, peer_pub_key, &codes) || codes != 0)
370 return -1;
372 return dh->meth->compute_key(shared_key, peer_pub_key, dh);
376 * Set a new method for the DH keypair.
378 * @param dh dh parameter.
379 * @param method the new method for the DH parameter.
381 * @return 1 on success.
383 * @ingroup hcrypto_dh
387 DH_set_method(DH *dh, const DH_METHOD *method)
389 (*dh->meth->finish)(dh);
390 if (dh->engine) {
391 ENGINE_finish(dh->engine);
392 dh->engine = NULL;
394 dh->meth = method;
395 (*dh->meth->init)(dh);
396 return 1;
403 static int
404 dh_null_generate_key(DH *dh)
406 return 0;
409 static int
410 dh_null_compute_key(unsigned char *shared,const BIGNUM *pub, DH *dh)
412 return 0;
415 static int
416 dh_null_init(DH *dh)
418 return 1;
421 static int
422 dh_null_finish(DH *dh)
424 return 1;
427 static int
428 dh_null_generate_params(DH *dh, int prime_num, int len, BN_GENCB *cb)
430 return 0;
433 static const DH_METHOD dh_null_method = {
434 "hcrypto null DH",
435 dh_null_generate_key,
436 dh_null_compute_key,
437 NULL,
438 dh_null_init,
439 dh_null_finish,
441 NULL,
442 dh_null_generate_params
445 extern const DH_METHOD _hc_dh_imath_method;
446 static const DH_METHOD *dh_default_method = &_hc_dh_imath_method;
449 * Return the dummy DH implementation.
451 * @return pointer to a DH_METHOD.
453 * @ingroup hcrypto_dh
456 const DH_METHOD *
457 DH_null_method(void)
459 return &dh_null_method;
463 * Set the default DH implementation.
465 * @param meth pointer to a DH_METHOD.
467 * @ingroup hcrypto_dh
470 void
471 DH_set_default_method(const DH_METHOD *meth)
473 dh_default_method = meth;
477 * Return the default DH implementation.
479 * @return pointer to a DH_METHOD.
481 * @ingroup hcrypto_dh
484 const DH_METHOD *
485 DH_get_default_method(void)
487 return dh_default_method;