BN_is_negative is no longer a macro in OpenSSL master
[heimdal.git] / lib / hcrypto / dh-tfm.c
blob0ebba6ffe77ea24b7639250ae9ba665f7fbeeef2
1 /*
2 * Copyright (c) 2006 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 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <dh.h>
40 #include <roken.h>
42 #ifdef USE_HCRYPTO_TFM
44 #include "tfm.h"
46 static void
47 BN2mpz(fp_int *s, const BIGNUM *bn)
49 size_t len;
50 void *p;
52 len = BN_num_bytes(bn);
53 p = malloc(len);
54 BN_bn2bin(bn, p);
55 fp_read_unsigned_bin(s, p, len);
56 free(p);
60 static BIGNUM *
61 mpz2BN(fp_int *s)
63 size_t size;
64 BIGNUM *bn;
65 void *p;
67 size = fp_unsigned_bin_size(s);
68 p = malloc(size);
69 if (p == NULL && size != 0)
70 return NULL;
71 fp_to_unsigned_bin(s, p);
73 bn = BN_bin2bn(p, size, NULL);
74 free(p);
75 return bn;
82 #define DH_NUM_TRIES 10
84 static int
85 tfm_dh_generate_key(DH *dh)
87 fp_int pub, priv_key, g, p;
88 int have_private_key = (dh->priv_key != NULL);
89 int codes, times = 0;
90 int res;
92 if (dh->p == NULL || dh->g == NULL)
93 return 0;
95 while (times++ < DH_NUM_TRIES) {
96 if (!have_private_key) {
97 size_t bits = BN_num_bits(dh->p);
99 if (dh->priv_key)
100 BN_free(dh->priv_key);
102 dh->priv_key = BN_new();
103 if (dh->priv_key == NULL)
104 return 0;
105 if (!BN_rand(dh->priv_key, bits - 1, 0, 0)) {
106 BN_clear_free(dh->priv_key);
107 dh->priv_key = NULL;
108 return 0;
111 if (dh->pub_key)
112 BN_free(dh->pub_key);
114 fp_init_multi(&pub, &priv_key, &g, &p, NULL);
116 BN2mpz(&priv_key, dh->priv_key);
117 BN2mpz(&g, dh->g);
118 BN2mpz(&p, dh->p);
120 res = fp_exptmod(&g, &priv_key, &p, &pub);
122 fp_zero(&priv_key);
123 fp_zero(&g);
124 fp_zero(&p);
125 if (res != 0)
126 continue;
128 dh->pub_key = mpz2BN(&pub);
129 fp_zero(&pub);
130 if (dh->pub_key == NULL)
131 return 0;
133 if (DH_check_pubkey(dh, dh->pub_key, &codes) && codes == 0)
134 break;
135 if (have_private_key)
136 return 0;
139 if (times >= DH_NUM_TRIES) {
140 if (!have_private_key && dh->priv_key) {
141 BN_free(dh->priv_key);
142 dh->priv_key = NULL;
144 if (dh->pub_key) {
145 BN_free(dh->pub_key);
146 dh->pub_key = NULL;
148 return 0;
151 return 1;
154 static int
155 tfm_dh_compute_key(unsigned char *shared, const BIGNUM * pub, DH *dh)
157 fp_int s, priv_key, p, peer_pub;
158 size_t size = 0;
159 int ret;
161 if (dh->pub_key == NULL || dh->g == NULL || dh->priv_key == NULL)
162 return -1;
164 fp_init(&p);
165 BN2mpz(&p, dh->p);
167 fp_init(&peer_pub);
168 BN2mpz(&peer_pub, pub);
170 /* check if peers pubkey is reasonable */
171 if (fp_isneg(&peer_pub)
172 || fp_cmp(&peer_pub, &p) >= 0
173 || fp_cmp_d(&peer_pub, 1) <= 0)
175 fp_zero(&p);
176 fp_zero(&peer_pub);
177 return -1;
180 fp_init(&priv_key);
181 BN2mpz(&priv_key, dh->priv_key);
183 fp_init(&s);
185 ret = fp_exptmod(&peer_pub, &priv_key, &p, &s);
187 fp_zero(&p);
188 fp_zero(&peer_pub);
189 fp_zero(&priv_key);
191 if (ret != 0)
192 return -1;
194 size = fp_unsigned_bin_size(&s);
195 fp_to_unsigned_bin(&s, shared);
196 fp_zero(&s);
198 return size;
201 static int
202 tfm_dh_generate_params(DH *dh, int a, int b, BN_GENCB *callback)
204 /* groups should already be known, we don't care about this */
205 return 0;
208 static int
209 tfm_dh_init(DH *dh)
211 return 1;
214 static int
215 tfm_dh_finish(DH *dh)
217 return 1;
225 const DH_METHOD _hc_dh_tfm_method = {
226 "hcrypto tfm DH",
227 tfm_dh_generate_key,
228 tfm_dh_compute_key,
229 NULL,
230 tfm_dh_init,
231 tfm_dh_finish,
233 NULL,
234 tfm_dh_generate_params
238 * DH implementation using tfm.
240 * @return the DH_METHOD for the DH implementation using tfm.
242 * @ingroup hcrypto_dh
245 const DH_METHOD *
246 DH_tfm_method(void)
248 return &_hc_dh_tfm_method;
251 #endif