1 /* $NetBSD: dh-ltm.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2006 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
44 #include <krb5/roken.h>
49 BN2mpz(mp_int
*s
, const BIGNUM
*bn
)
54 len
= BN_num_bytes(bn
);
57 mp_read_unsigned_bin(s
, p
, len
);
69 size
= mp_unsigned_bin_size(s
);
71 if (p
== NULL
&& size
!= 0)
73 mp_to_unsigned_bin(s
, p
);
75 bn
= BN_bin2bn(p
, size
, NULL
);
84 #define DH_NUM_TRIES 10
87 ltm_dh_generate_key(DH
*dh
)
89 mp_int pub
, priv_key
, g
, p
;
90 int have_private_key
= (dh
->priv_key
!= NULL
);
94 if (dh
->p
== NULL
|| dh
->g
== NULL
)
97 while (times
++ < DH_NUM_TRIES
) {
98 if (!have_private_key
) {
99 size_t bits
= BN_num_bits(dh
->p
);
102 BN_free(dh
->priv_key
);
104 dh
->priv_key
= BN_new();
105 if (dh
->priv_key
== NULL
)
107 if (!BN_rand(dh
->priv_key
, bits
- 1, 0, 0)) {
108 BN_clear_free(dh
->priv_key
);
114 BN_free(dh
->pub_key
);
116 mp_init_multi(&pub
, &priv_key
, &g
, &p
, NULL
);
118 BN2mpz(&priv_key
, dh
->priv_key
);
122 res
= mp_exptmod(&g
, &priv_key
, &p
, &pub
);
124 mp_clear_multi(&priv_key
, &g
, &p
, NULL
);
128 dh
->pub_key
= mpz2BN(&pub
);
130 if (dh
->pub_key
== NULL
)
133 if (DH_check_pubkey(dh
, dh
->pub_key
, &codes
) && codes
== 0)
135 if (have_private_key
)
139 if (times
>= DH_NUM_TRIES
) {
140 if (!have_private_key
&& dh
->priv_key
) {
141 BN_free(dh
->priv_key
);
145 BN_free(dh
->pub_key
);
155 ltm_dh_compute_key(unsigned char *shared
, const BIGNUM
* pub
, DH
*dh
)
157 mp_int s
, priv_key
, p
, peer_pub
;
160 if (dh
->pub_key
== NULL
|| dh
->g
== NULL
|| dh
->priv_key
== NULL
)
163 mp_init_multi(&s
, &priv_key
, &p
, &peer_pub
, NULL
);
165 BN2mpz(&peer_pub
, pub
);
167 /* check if peers pubkey is reasonable */
168 if (mp_isneg(&peer_pub
)
169 || mp_cmp(&peer_pub
, &p
) >= 0
170 || mp_cmp_d(&peer_pub
, 1) <= 0)
176 BN2mpz(&priv_key
, dh
->priv_key
);
178 ret
= mp_exptmod(&peer_pub
, &priv_key
, &p
, &s
);
185 ret
= mp_unsigned_bin_size(&s
);
186 mp_to_unsigned_bin(&s
, shared
);
189 mp_clear_multi(&s
, &priv_key
, &p
, &peer_pub
, NULL
);
195 ltm_dh_generate_params(DH
*dh
, int a
, int b
, BN_GENCB
*callback
)
197 /* groups should already be known, we don't care about this */
208 ltm_dh_finish(DH
*dh
)
218 const DH_METHOD _hc_dh_ltm_method
= {
227 ltm_dh_generate_params
231 * DH implementation using libtommath.
233 * @return the DH_METHOD for the DH implementation using libtommath.
235 * @ingroup hcrypto_dh
241 return &_hc_dh_ltm_method
;