xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/dh-tfm.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: dh-tfm.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include <config.h>
37ca1c9b0cSelric #include <krb5/roken.h>
38ca1c9b0cSelric 
39b9d004c6Schristos #include <dh.h>
40b9d004c6Schristos 
41ca1c9b0cSelric #ifdef USE_HCRYPTO_TFM
42ca1c9b0cSelric 
43ca1c9b0cSelric #include "tfm.h"
44ca1c9b0cSelric 
45ca1c9b0cSelric static void
BN2mpz(fp_int * s,const BIGNUM * bn)46ca1c9b0cSelric BN2mpz(fp_int *s, const BIGNUM *bn)
47ca1c9b0cSelric {
48ca1c9b0cSelric     size_t len;
49ca1c9b0cSelric     void *p;
50ca1c9b0cSelric 
51ca1c9b0cSelric     len = BN_num_bytes(bn);
52ca1c9b0cSelric     p = malloc(len);
53ca1c9b0cSelric     BN_bn2bin(bn, p);
54ca1c9b0cSelric     fp_read_unsigned_bin(s, p, len);
55ca1c9b0cSelric     free(p);
56ca1c9b0cSelric }
57ca1c9b0cSelric 
58ca1c9b0cSelric 
59ca1c9b0cSelric static BIGNUM *
mpz2BN(fp_int * s)60ca1c9b0cSelric mpz2BN(fp_int *s)
61ca1c9b0cSelric {
62ca1c9b0cSelric     size_t size;
63ca1c9b0cSelric     BIGNUM *bn;
64ca1c9b0cSelric     void *p;
65ca1c9b0cSelric 
66ca1c9b0cSelric     size = fp_unsigned_bin_size(s);
67ca1c9b0cSelric     p = malloc(size);
68ca1c9b0cSelric     if (p == NULL && size != 0)
69ca1c9b0cSelric 	return NULL;
70ca1c9b0cSelric     fp_to_unsigned_bin(s, p);
71ca1c9b0cSelric 
72ca1c9b0cSelric     bn = BN_bin2bn(p, size, NULL);
73ca1c9b0cSelric     free(p);
74ca1c9b0cSelric     return bn;
75ca1c9b0cSelric }
76ca1c9b0cSelric 
77ca1c9b0cSelric /*
78ca1c9b0cSelric  *
79ca1c9b0cSelric  */
80ca1c9b0cSelric 
81ca1c9b0cSelric #define DH_NUM_TRIES 10
82ca1c9b0cSelric 
83ca1c9b0cSelric static int
tfm_dh_generate_key(DH * dh)84ca1c9b0cSelric tfm_dh_generate_key(DH *dh)
85ca1c9b0cSelric {
86ca1c9b0cSelric     fp_int pub, priv_key, g, p;
87ca1c9b0cSelric     int have_private_key = (dh->priv_key != NULL);
88ca1c9b0cSelric     int codes, times = 0;
89ca1c9b0cSelric     int res;
90ca1c9b0cSelric 
91ca1c9b0cSelric     if (dh->p == NULL || dh->g == NULL)
92ca1c9b0cSelric 	return 0;
93ca1c9b0cSelric 
94ca1c9b0cSelric     while (times++ < DH_NUM_TRIES) {
95ca1c9b0cSelric 	if (!have_private_key) {
96ca1c9b0cSelric 	    size_t bits = BN_num_bits(dh->p);
97ca1c9b0cSelric 
98ca1c9b0cSelric 	    if (dh->priv_key)
99ca1c9b0cSelric 		BN_free(dh->priv_key);
100ca1c9b0cSelric 
101ca1c9b0cSelric 	    dh->priv_key = BN_new();
102ca1c9b0cSelric 	    if (dh->priv_key == NULL)
103ca1c9b0cSelric 		return 0;
104ca1c9b0cSelric 	    if (!BN_rand(dh->priv_key, bits - 1, 0, 0)) {
105ca1c9b0cSelric 		BN_clear_free(dh->priv_key);
106ca1c9b0cSelric 		dh->priv_key = NULL;
107ca1c9b0cSelric 		return 0;
108ca1c9b0cSelric 	    }
109ca1c9b0cSelric 	}
110ca1c9b0cSelric 	if (dh->pub_key)
111ca1c9b0cSelric 	    BN_free(dh->pub_key);
112ca1c9b0cSelric 
113ca1c9b0cSelric 	fp_init_multi(&pub, &priv_key, &g, &p, NULL);
114ca1c9b0cSelric 
115ca1c9b0cSelric 	BN2mpz(&priv_key, dh->priv_key);
116ca1c9b0cSelric 	BN2mpz(&g, dh->g);
117ca1c9b0cSelric 	BN2mpz(&p, dh->p);
118ca1c9b0cSelric 
119ca1c9b0cSelric 	res = fp_exptmod(&g, &priv_key, &p, &pub);
120ca1c9b0cSelric 
121ca1c9b0cSelric 	fp_zero(&priv_key);
122ca1c9b0cSelric 	fp_zero(&g);
123ca1c9b0cSelric 	fp_zero(&p);
124ca1c9b0cSelric 	if (res != 0)
125ca1c9b0cSelric 	    continue;
126ca1c9b0cSelric 
127ca1c9b0cSelric 	dh->pub_key = mpz2BN(&pub);
128ca1c9b0cSelric 	fp_zero(&pub);
129ca1c9b0cSelric 	if (dh->pub_key == NULL)
130ca1c9b0cSelric 	    return 0;
131ca1c9b0cSelric 
132ca1c9b0cSelric 	if (DH_check_pubkey(dh, dh->pub_key, &codes) && codes == 0)
133ca1c9b0cSelric 	    break;
134ca1c9b0cSelric 	if (have_private_key)
135ca1c9b0cSelric 	    return 0;
136ca1c9b0cSelric     }
137ca1c9b0cSelric 
138ca1c9b0cSelric     if (times >= DH_NUM_TRIES) {
139ca1c9b0cSelric 	if (!have_private_key && dh->priv_key) {
140ca1c9b0cSelric 	    BN_free(dh->priv_key);
141ca1c9b0cSelric 	    dh->priv_key = NULL;
142ca1c9b0cSelric 	}
143ca1c9b0cSelric 	if (dh->pub_key) {
144ca1c9b0cSelric 	    BN_free(dh->pub_key);
145ca1c9b0cSelric 	    dh->pub_key = NULL;
146ca1c9b0cSelric 	}
147ca1c9b0cSelric 	return 0;
148ca1c9b0cSelric     }
149ca1c9b0cSelric 
150ca1c9b0cSelric     return 1;
151ca1c9b0cSelric }
152ca1c9b0cSelric 
153ca1c9b0cSelric static int
tfm_dh_compute_key(unsigned char * shared,const BIGNUM * pub,DH * dh)154ca1c9b0cSelric tfm_dh_compute_key(unsigned char *shared, const BIGNUM * pub, DH *dh)
155ca1c9b0cSelric {
156ca1c9b0cSelric     fp_int s, priv_key, p, peer_pub;
157ca1c9b0cSelric     size_t size = 0;
158ca1c9b0cSelric     int ret;
159ca1c9b0cSelric 
160ca1c9b0cSelric     if (dh->pub_key == NULL || dh->g == NULL || dh->priv_key == NULL)
161ca1c9b0cSelric 	return -1;
162ca1c9b0cSelric 
163ca1c9b0cSelric     fp_init(&p);
164ca1c9b0cSelric     BN2mpz(&p, dh->p);
165ca1c9b0cSelric 
166ca1c9b0cSelric     fp_init(&peer_pub);
167ca1c9b0cSelric     BN2mpz(&peer_pub, pub);
168ca1c9b0cSelric 
169ca1c9b0cSelric     /* check if peers pubkey is reasonable */
170ca1c9b0cSelric     if (fp_isneg(&peer_pub)
171ca1c9b0cSelric 	|| fp_cmp(&peer_pub, &p) >= 0
172ca1c9b0cSelric 	|| fp_cmp_d(&peer_pub, 1) <= 0)
173ca1c9b0cSelric     {
174ca1c9b0cSelric 	fp_zero(&p);
175ca1c9b0cSelric 	fp_zero(&peer_pub);
176ca1c9b0cSelric 	return -1;
177ca1c9b0cSelric     }
178ca1c9b0cSelric 
179ca1c9b0cSelric     fp_init(&priv_key);
180ca1c9b0cSelric     BN2mpz(&priv_key, dh->priv_key);
181ca1c9b0cSelric 
182ca1c9b0cSelric     fp_init(&s);
183ca1c9b0cSelric 
184ca1c9b0cSelric     ret = fp_exptmod(&peer_pub, &priv_key, &p, &s);
185ca1c9b0cSelric 
186ca1c9b0cSelric     fp_zero(&p);
187ca1c9b0cSelric     fp_zero(&peer_pub);
188ca1c9b0cSelric     fp_zero(&priv_key);
189ca1c9b0cSelric 
190ca1c9b0cSelric     if (ret != 0)
191ca1c9b0cSelric 	return -1;
192ca1c9b0cSelric 
193ca1c9b0cSelric     size = fp_unsigned_bin_size(&s);
194ca1c9b0cSelric     fp_to_unsigned_bin(&s, shared);
195ca1c9b0cSelric     fp_zero(&s);
196ca1c9b0cSelric 
197ca1c9b0cSelric     return size;
198ca1c9b0cSelric }
199ca1c9b0cSelric 
200ca1c9b0cSelric static int
tfm_dh_generate_params(DH * dh,int a,int b,BN_GENCB * callback)201ca1c9b0cSelric tfm_dh_generate_params(DH *dh, int a, int b, BN_GENCB *callback)
202ca1c9b0cSelric {
203ca1c9b0cSelric     /* groups should already be known, we don't care about this */
204ca1c9b0cSelric     return 0;
205ca1c9b0cSelric }
206ca1c9b0cSelric 
207ca1c9b0cSelric static int
tfm_dh_init(DH * dh)208ca1c9b0cSelric tfm_dh_init(DH *dh)
209ca1c9b0cSelric {
210ca1c9b0cSelric     return 1;
211ca1c9b0cSelric }
212ca1c9b0cSelric 
213ca1c9b0cSelric static int
tfm_dh_finish(DH * dh)214ca1c9b0cSelric tfm_dh_finish(DH *dh)
215ca1c9b0cSelric {
216ca1c9b0cSelric     return 1;
217ca1c9b0cSelric }
218ca1c9b0cSelric 
219ca1c9b0cSelric 
220ca1c9b0cSelric /*
221ca1c9b0cSelric  *
222ca1c9b0cSelric  */
223ca1c9b0cSelric 
224ca1c9b0cSelric const DH_METHOD _hc_dh_tfm_method = {
225ca1c9b0cSelric     "hcrypto tfm DH",
226ca1c9b0cSelric     tfm_dh_generate_key,
227ca1c9b0cSelric     tfm_dh_compute_key,
228ca1c9b0cSelric     NULL,
229ca1c9b0cSelric     tfm_dh_init,
230ca1c9b0cSelric     tfm_dh_finish,
231ca1c9b0cSelric     0,
232ca1c9b0cSelric     NULL,
233ca1c9b0cSelric     tfm_dh_generate_params
234ca1c9b0cSelric };
235ca1c9b0cSelric 
236ca1c9b0cSelric /**
237ca1c9b0cSelric  * DH implementation using tfm.
238ca1c9b0cSelric  *
239ca1c9b0cSelric  * @return the DH_METHOD for the DH implementation using tfm.
240ca1c9b0cSelric  *
241ca1c9b0cSelric  * @ingroup hcrypto_dh
242ca1c9b0cSelric  */
243ca1c9b0cSelric 
244ca1c9b0cSelric const DH_METHOD *
DH_tfm_method(void)245ca1c9b0cSelric DH_tfm_method(void)
246ca1c9b0cSelric {
247ca1c9b0cSelric     return &_hc_dh_tfm_method;
248ca1c9b0cSelric }
249ca1c9b0cSelric 
250ca1c9b0cSelric #endif
251