Remove building with NOCRYPTO option
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hcrypto / dh.c
blobfae21121107c008fa1b0ab619e12725b84afd670
1 /* $NetBSD: dh.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
3 /*
4 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <krb5/krb5-types.h>
43 #include <krb5/rfc2459_asn1.h>
45 #include <dh.h>
47 #include <krb5/roken.h>
49 /**
50 * @page page_dh DH - Diffie-Hellman key exchange
52 * Diffie-Hellman key exchange is a protocol that allows two parties
53 * to establish a shared secret key.
55 * Include and example how to use DH_new() and friends here.
57 * See the library functions here: @ref hcrypto_dh
60 /**
61 * Create a new DH object using DH_new_method(NULL), see DH_new_method().
63 * @return a newly allocated DH object.
65 * @ingroup hcrypto_dh
68 DH *
69 DH_new(void)
71 return DH_new_method(NULL);
74 /**
75 * Create a new DH object from the given engine, if the NULL is used,
76 * the default engine is used. Free the DH object with DH_free().
78 * @param engine The engine to use to allocate the DH object.
80 * @return a newly allocated DH object.
82 * @ingroup hcrypto_dh
85 DH *
86 DH_new_method(ENGINE *engine)
88 DH *dh;
90 dh = calloc(1, sizeof(*dh));
91 if (dh == NULL)
92 return NULL;
94 dh->references = 1;
96 if (engine) {
97 ENGINE_up_ref(engine);
98 dh->engine = engine;
99 } else {
100 dh->engine = ENGINE_get_default_DH();
103 if (dh->engine) {
104 dh->meth = ENGINE_get_DH(dh->engine);
105 if (dh->meth == NULL) {
106 ENGINE_finish(engine);
107 free(dh);
108 return 0;
112 if (dh->meth == NULL)
113 dh->meth = DH_get_default_method();
115 (*dh->meth->init)(dh);
117 return dh;
121 * Free a DH object and release related resources, like ENGINE, that
122 * the object was using.
124 * @param dh object to be freed.
126 * @ingroup hcrypto_dh
129 void
130 DH_free(DH *dh)
132 if (dh->references <= 0)
133 abort();
135 if (--dh->references > 0)
136 return;
138 (*dh->meth->finish)(dh);
140 if (dh->engine)
141 ENGINE_finish(dh->engine);
143 #define free_if(f) if (f) { BN_free(f); }
144 free_if(dh->p);
145 free_if(dh->g);
146 free_if(dh->pub_key);
147 free_if(dh->priv_key);
148 free_if(dh->q);
149 free_if(dh->j);
150 free_if(dh->counter);
151 #undef free_if
153 memset(dh, 0, sizeof(*dh));
154 free(dh);
158 * Add a reference to the DH object. The object should be free with
159 * DH_free() to drop the reference.
161 * @param dh the object to increase the reference count too.
163 * @return the updated reference count, can't safely be used except
164 * for debug printing.
166 * @ingroup hcrypto_dh
170 DH_up_ref(DH *dh)
172 return ++dh->references;
176 * The maximum output size of the DH_compute_key() function.
178 * @param dh The DH object to get the size from.
180 * @return the maximum size in bytes of the out data.
182 * @ingroup hcrypto_dh
186 DH_size(const DH *dh)
188 return BN_num_bytes(dh->p);
192 * Set the data index idx in the DH object to data.
194 * @param dh DH object.
195 * @param idx index to set the data for.
196 * @param data data to store for the index idx.
198 * @return 1 on success.
200 * @ingroup hcrypto_dh
204 DH_set_ex_data(DH *dh, int idx, void *data)
206 dh->ex_data.sk = data;
207 return 1;
211 * Get the data for index idx in the DH object.
213 * @param dh DH object.
214 * @param idx index to get the data for.
216 * @return the object store in index idx
218 * @ingroup hcrypto_dh
221 void *
222 DH_get_ex_data(DH *dh, int idx)
224 return dh->ex_data.sk;
228 * Generate DH parameters for the DH object give parameters.
230 * @param dh The DH object to generate parameters for.
231 * @param prime_len length of the prime
232 * @param generator generator, g
233 * @param cb Callback parameters to show progress, can be NULL.
235 * @return the maximum size in bytes of the out data.
237 * @ingroup hcrypto_dh
241 DH_generate_parameters_ex(DH *dh, int prime_len, int generator, BN_GENCB *cb)
243 if (dh->meth->generate_params)
244 return dh->meth->generate_params(dh, prime_len, generator, cb);
245 return 0;
249 * Check that the public key is sane.
251 * @param dh the local peer DH parameters.
252 * @param pub_key the remote peer public key parameters.
253 * @param codes return that the failures of the pub_key are.
255 * @return 1 on success, 0 on failure and *codes is set the the
256 * combined fail check for the public key
258 * @ingroup hcrypto_dh
262 DH_check_pubkey(const DH *dh, const BIGNUM *pub_key, int *codes)
264 BIGNUM *bn = NULL, *sum = NULL;
265 int ret = 0;
267 *codes = 0;
270 * Checks that the function performs are:
271 * - pub_key is not negative
274 if (BN_is_negative(pub_key))
275 goto out;
278 * - pub_key > 1 and pub_key < p - 1,
279 * to avoid small subgroups attack.
282 bn = BN_new();
283 if (bn == NULL)
284 goto out;
286 if (!BN_set_word(bn, 1))
287 goto out;
289 if (BN_cmp(bn, pub_key) >= 0)
290 *codes |= DH_CHECK_PUBKEY_TOO_SMALL;
292 sum = BN_new();
293 if (sum == NULL)
294 goto out;
296 BN_uadd(sum, pub_key, bn);
298 if (BN_cmp(sum, dh->p) >= 0)
299 *codes |= DH_CHECK_PUBKEY_TOO_LARGE;
302 * - if g == 2, pub_key have more then one bit set,
303 * if bits set is 1, log_2(pub_key) is trival
306 if (!BN_set_word(bn, 2))
307 goto out;
309 if (BN_cmp(bn, dh->g) == 0) {
310 unsigned i, n = BN_num_bits(pub_key);
311 unsigned bits = 0;
313 for (i = 0; i <= n; i++)
314 if (BN_is_bit_set(pub_key, i))
315 bits++;
317 if (bits < 2) {
318 *codes |= DH_CHECK_PUBKEY_TOO_SMALL;
319 goto out;
323 ret = 1;
324 out:
325 if (bn)
326 BN_free(bn);
327 if (sum)
328 BN_free(sum);
330 return ret;
334 * Generate a new DH private-public key pair. The dh parameter must be
335 * allocted first with DH_new(). dh->p and dp->g must be set.
337 * @param dh dh parameter.
339 * @return 1 on success.
341 * @ingroup hcrypto_dh
345 DH_generate_key(DH *dh)
347 return dh->meth->generate_key(dh);
351 * Complute the shared secret key.
353 * @param shared_key the resulting shared key, need to be at least
354 * DH_size() large.
355 * @param peer_pub_key the peer's public key.
356 * @param dh the dh key pair.
358 * @return 1 on success.
360 * @ingroup hcrypto_dh
364 DH_compute_key(unsigned char *shared_key,
365 const BIGNUM *peer_pub_key, DH *dh)
367 int codes;
370 * Checks that the pubkey passed in is valid using
371 * DH_check_pubkey().
374 if (!DH_check_pubkey(dh, peer_pub_key, &codes) || codes != 0)
375 return -1;
377 return dh->meth->compute_key(shared_key, peer_pub_key, dh);
381 * Set a new method for the DH keypair.
383 * @param dh dh parameter.
384 * @param method the new method for the DH parameter.
386 * @return 1 on success.
388 * @ingroup hcrypto_dh
392 DH_set_method(DH *dh, const DH_METHOD *method)
394 (*dh->meth->finish)(dh);
395 if (dh->engine) {
396 ENGINE_finish(dh->engine);
397 dh->engine = NULL;
399 dh->meth = method;
400 (*dh->meth->init)(dh);
401 return 1;
408 static int
409 dh_null_generate_key(DH *dh)
411 return 0;
414 static int
415 dh_null_compute_key(unsigned char *shared,const BIGNUM *pub, DH *dh)
417 return 0;
420 static int
421 dh_null_init(DH *dh)
423 return 1;
426 static int
427 dh_null_finish(DH *dh)
429 return 1;
432 static int
433 dh_null_generate_params(DH *dh, int prime_num, int len, BN_GENCB *cb)
435 return 0;
438 static const DH_METHOD dh_null_method = {
439 "hcrypto null DH",
440 dh_null_generate_key,
441 dh_null_compute_key,
442 NULL,
443 dh_null_init,
444 dh_null_finish,
446 NULL,
447 dh_null_generate_params
450 extern const DH_METHOD _hc_dh_ltm_method;
451 static const DH_METHOD *dh_default_method = &_hc_dh_ltm_method;
454 * Return the dummy DH implementation.
456 * @return pointer to a DH_METHOD.
458 * @ingroup hcrypto_dh
461 const DH_METHOD *
462 DH_null_method(void)
464 return &dh_null_method;
468 * Set the default DH implementation.
470 * @param meth pointer to a DH_METHOD.
472 * @ingroup hcrypto_dh
475 void
476 DH_set_default_method(const DH_METHOD *meth)
478 dh_default_method = meth;
482 * Return the default DH implementation.
484 * @return pointer to a DH_METHOD.
486 * @ingroup hcrypto_dh
489 const DH_METHOD *
490 DH_get_default_method(void)
492 return dh_default_method;
499 static int
500 bn2heim_int(BIGNUM *bn, heim_integer *integer)
502 integer->length = BN_num_bytes(bn);
503 integer->data = malloc(integer->length);
504 if (integer->data == NULL) {
505 integer->length = 0;
506 return ENOMEM;
508 BN_bn2bin(bn, integer->data);
509 integer->negative = BN_is_negative(bn);
510 return 0;
518 i2d_DHparams(DH *dh, unsigned char **pp)
520 DHParameter data;
521 size_t size;
522 int ret;
524 memset(&data, 0, sizeof(data));
526 if (bn2heim_int(dh->p, &data.prime) ||
527 bn2heim_int(dh->g, &data.base))
529 free_DHParameter(&data);
530 return -1;
533 if (pp == NULL) {
534 size = length_DHParameter(&data);
535 free_DHParameter(&data);
536 } else {
537 void *p;
538 size_t len;
540 ASN1_MALLOC_ENCODE(DHParameter, p, len, &data, &size, ret);
541 free_DHParameter(&data);
542 if (ret)
543 return -1;
544 if (len != size) {
545 abort();
546 return -1;
549 memcpy(*pp, p, size);
550 free(p);
552 *pp += size;
555 return size;