Adding DBG_LIB to all calls of libstrongswan's version of DBG*.
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_diffie_hellman.c
1 /*
2 * Copyright (C) 2008-2010 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <openssl/dh.h>
18
19 #include "openssl_diffie_hellman.h"
20
21 #include <debug.h>
22
23 typedef struct private_openssl_diffie_hellman_t private_openssl_diffie_hellman_t;
24
25 /**
26 * Private data of an openssl_diffie_hellman_t object.
27 */
28 struct private_openssl_diffie_hellman_t {
29 /**
30 * Public openssl_diffie_hellman_t interface.
31 */
32 openssl_diffie_hellman_t public;
33
34 /**
35 * Diffie Hellman group number.
36 */
37 u_int16_t group;
38
39 /**
40 * Diffie Hellman object
41 */
42 DH *dh;
43
44 /**
45 * Other public value
46 */
47 BIGNUM *pub_key;
48
49 /**
50 * Shared secret
51 */
52 chunk_t shared_secret;
53
54 /**
55 * True if shared secret is computed
56 */
57 bool computed;
58 };
59
60 /**
61 * Implementation of openssl_diffie_hellman_t.get_my_public_value.
62 */
63 static void get_my_public_value(private_openssl_diffie_hellman_t *this,
64 chunk_t *value)
65 {
66 *value = chunk_alloc(DH_size(this->dh));
67 memset(value->ptr, 0, value->len);
68 BN_bn2bin(this->dh->pub_key,
69 value->ptr + value->len - BN_num_bytes(this->dh->pub_key));
70 }
71
72 /**
73 * Implementation of openssl_diffie_hellman_t.get_shared_secret.
74 */
75 static status_t get_shared_secret(private_openssl_diffie_hellman_t *this,
76 chunk_t *secret)
77 {
78 if (!this->computed)
79 {
80 return FAILED;
81 }
82 /* shared secret should requires a len according the DH group */
83 *secret = chunk_alloc(DH_size(this->dh));
84 memset(secret->ptr, 0, secret->len);
85 memcpy(secret->ptr + secret->len - this->shared_secret.len,
86 this->shared_secret.ptr, this->shared_secret.len);
87 return SUCCESS;
88 }
89
90
91 /**
92 * Implementation of openssl_diffie_hellman_t.set_other_public_value.
93 */
94 static void set_other_public_value(private_openssl_diffie_hellman_t *this,
95 chunk_t value)
96 {
97 int len;
98
99 BN_bin2bn(value.ptr, value.len, this->pub_key);
100 chunk_clear(&this->shared_secret);
101 this->shared_secret.ptr = malloc(DH_size(this->dh));
102 memset(this->shared_secret.ptr, 0xFF, this->shared_secret.len);
103 len = DH_compute_key(this->shared_secret.ptr, this->pub_key, this->dh);
104 if (len < 0)
105 {
106 DBG1(DBG_LIB, "DH shared secret computation failed");
107 return;
108 }
109 this->shared_secret.len = len;
110 this->computed = TRUE;
111 }
112
113 /**
114 * Implementation of openssl_diffie_hellman_t.get_dh_group.
115 */
116 static diffie_hellman_group_t get_dh_group(private_openssl_diffie_hellman_t *this)
117 {
118 return this->group;
119 }
120
121 /**
122 * Lookup the modulus in modulo table
123 */
124 static status_t set_modulus(private_openssl_diffie_hellman_t *this)
125 {
126 diffie_hellman_params_t *params = diffie_hellman_get_params(this->group);
127 if (!params)
128 {
129 return NOT_FOUND;
130 }
131 this->dh->p = BN_bin2bn(params->prime, params->prime_len, NULL);
132 this->dh->g = BN_new();
133 BN_set_word(this->dh->g, params->generator);
134 if (params->exp_len != params->prime_len)
135 {
136 this->dh->length = params->exp_len * 8;
137 }
138 return SUCCESS;
139 }
140
141 /**
142 * Implementation of openssl_diffie_hellman_t.destroy.
143 */
144 static void destroy(private_openssl_diffie_hellman_t *this)
145 {
146 BN_clear_free(this->pub_key);
147 DH_free(this->dh);
148 chunk_clear(&this->shared_secret);
149 free(this);
150 }
151
152 /*
153 * Described in header.
154 */
155 openssl_diffie_hellman_t *openssl_diffie_hellman_create(diffie_hellman_group_t group)
156 {
157 private_openssl_diffie_hellman_t *this = malloc_thing(private_openssl_diffie_hellman_t);
158
159 this->public.dh.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;
160 this->public.dh.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;
161 this->public.dh.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;
162 this->public.dh.get_dh_group = (diffie_hellman_group_t (*)(diffie_hellman_t *)) get_dh_group;
163 this->public.dh.destroy = (void (*)(diffie_hellman_t *)) destroy;
164
165 this->dh = DH_new();
166 if (!this->dh)
167 {
168 free(this);
169 return NULL;
170 }
171
172 this->group = group;
173 this->computed = FALSE;
174 this->pub_key = BN_new();
175 this->shared_secret = chunk_empty;
176
177 /* find a modulus according to group */
178 if (set_modulus(this) != SUCCESS)
179 {
180 destroy(this);
181 return NULL;
182 }
183
184 /* generate my public and private values */
185 if (!DH_generate_key(this->dh))
186 {
187 destroy(this);
188 return NULL;
189 }
190 DBG2(DBG_LIB, "size of DH secret exponent: %d bits",
191 BN_num_bits(this->dh->priv_key));
192
193 return &this->public;
194 }