1 /* $NetBSD: dh.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
42 #include <krb5/krb5-types.h>
43 #include <krb5/rfc2459_asn1.h>
47 #include <krb5/roken.h>
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
61 * Create a new DH object using DH_new_method(NULL), see DH_new_method().
63 * @return a newly allocated DH object.
71 return DH_new_method(NULL
);
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.
86 DH_new_method(ENGINE
*engine
)
90 dh
= calloc(1, sizeof(*dh
));
97 ENGINE_up_ref(engine
);
100 dh
->engine
= ENGINE_get_default_DH();
104 dh
->meth
= ENGINE_get_DH(dh
->engine
);
105 if (dh
->meth
== NULL
) {
106 ENGINE_finish(engine
);
112 if (dh
->meth
== NULL
)
113 dh
->meth
= DH_get_default_method();
115 (*dh
->meth
->init
)(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
132 if (dh
->references
<= 0)
135 if (--dh
->references
> 0)
138 (*dh
->meth
->finish
)(dh
);
141 ENGINE_finish(dh
->engine
);
143 #define free_if(f) if (f) { BN_free(f); }
146 free_if(dh
->pub_key
);
147 free_if(dh
->priv_key
);
150 free_if(dh
->counter
);
153 memset(dh
, 0, sizeof(*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
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
;
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
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
);
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
;
270 * Checks that the function performs are:
271 * - pub_key is not negative
274 if (BN_is_negative(pub_key
))
278 * - pub_key > 1 and pub_key < p - 1,
279 * to avoid small subgroups attack.
286 if (!BN_set_word(bn
, 1))
289 if (BN_cmp(bn
, pub_key
) >= 0)
290 *codes
|= DH_CHECK_PUBKEY_TOO_SMALL
;
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))
309 if (BN_cmp(bn
, dh
->g
) == 0) {
310 unsigned i
, n
= BN_num_bits(pub_key
);
313 for (i
= 0; i
<= n
; i
++)
314 if (BN_is_bit_set(pub_key
, i
))
318 *codes
|= DH_CHECK_PUBKEY_TOO_SMALL
;
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
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
)
370 * Checks that the pubkey passed in is valid using
374 if (!DH_check_pubkey(dh
, peer_pub_key
, &codes
) || codes
!= 0)
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
);
396 ENGINE_finish(dh
->engine
);
400 (*dh
->meth
->init
)(dh
);
409 dh_null_generate_key(DH
*dh
)
415 dh_null_compute_key(unsigned char *shared
,const BIGNUM
*pub
, DH
*dh
)
427 dh_null_finish(DH
*dh
)
433 dh_null_generate_params(DH
*dh
, int prime_num
, int len
, BN_GENCB
*cb
)
438 static const DH_METHOD dh_null_method
= {
440 dh_null_generate_key
,
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
464 return &dh_null_method
;
468 * Set the default DH implementation.
470 * @param meth pointer to a DH_METHOD.
472 * @ingroup hcrypto_dh
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
490 DH_get_default_method(void)
492 return dh_default_method
;
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
) {
508 BN_bn2bin(bn
, integer
->data
);
509 integer
->negative
= BN_is_negative(bn
);
518 i2d_DHparams(DH
*dh
, unsigned char **pp
)
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
);
534 size
= length_DHParameter(&data
);
535 free_DHParameter(&data
);
540 ASN1_MALLOC_ENCODE(DHParameter
, p
, len
, &data
, &size
, ret
);
541 free_DHParameter(&data
);
549 memcpy(*pp
, p
, size
);