1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
* Copyright (C) 2018 René Korthaus
* Copyright (C) 2018 Konstantinos Kolelis
* Rohde & Schwarz Cybersecurity GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "botan_diffie_hellman.h"
#include <botan/build.h>
#ifdef BOTAN_HAS_DIFFIE_HELLMAN
#include "botan_util.h"
#include <botan/ffi.h>
#include <utils/debug.h>
typedef struct private_botan_diffie_hellman_t private_botan_diffie_hellman_t;
/**
* Private data of an botan_diffie_hellman_t object.
*/
struct private_botan_diffie_hellman_t {
/**
* Public botan_diffie_hellman_t interface
*/
botan_diffie_hellman_t public;
/**
* Diffie Hellman group number
*/
diffie_hellman_group_t group;
/**
* Private key
*/
botan_privkey_t dh_key;
/**
* Diffie hellman shared secret
*/
chunk_t shared_secret;
/**
* Generator value
*/
botan_mp_t g;
/**
* Modulus
*/
botan_mp_t p;
};
/**
* Load a DH private key
*/
bool load_private_key(private_botan_diffie_hellman_t *this, chunk_t value)
{
botan_mp_t xa;
if (!chunk_to_botan_mp(value, &xa))
{
return FALSE;
}
if (botan_privkey_destroy(this->dh_key) ||
botan_privkey_load_dh(&this->dh_key, this->p, this->g, xa))
{
botan_mp_destroy(xa);
return FALSE;
}
botan_mp_destroy(xa);
return TRUE;
}
METHOD(diffie_hellman_t, set_other_public_value, bool,
private_botan_diffie_hellman_t *this, chunk_t value)
{
if (!diffie_hellman_verify_value(this->group, value))
{
return FALSE;
}
chunk_clear(&this->shared_secret);
return botan_dh_key_derivation(this->dh_key, value, &this->shared_secret);
}
METHOD(diffie_hellman_t, get_my_public_value, bool,
private_botan_diffie_hellman_t *this, chunk_t *value)
{
*value = chunk_empty;
/* get key size of public key first */
if (botan_pk_op_key_agreement_export_public(this->dh_key, NULL, &value->len)
!= BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
{
return FALSE;
}
*value = chunk_alloc(value->len);
if (botan_pk_op_key_agreement_export_public(this->dh_key, value->ptr,
&value->len))
{
chunk_clear(value);
return FALSE;
}
return TRUE;
}
METHOD(diffie_hellman_t, set_private_value, bool,
private_botan_diffie_hellman_t *this, chunk_t value)
{
chunk_clear(&this->shared_secret);
return load_private_key(this, value);
}
METHOD(diffie_hellman_t, get_shared_secret, bool,
private_botan_diffie_hellman_t *this, chunk_t *secret)
{
if (!this->shared_secret.len)
{
return FALSE;
}
*secret = chunk_clone(this->shared_secret);
return TRUE;
}
METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
private_botan_diffie_hellman_t *this)
{
return this->group;
}
METHOD(diffie_hellman_t, destroy, void,
private_botan_diffie_hellman_t *this)
{
botan_mp_destroy(this->p);
botan_mp_destroy(this->g);
botan_privkey_destroy(this->dh_key);
chunk_clear(&this->shared_secret);
free(this);
}
/*
* Generic internal constructor
*/
static botan_diffie_hellman_t *create_generic(diffie_hellman_group_t group,
chunk_t g, chunk_t p, size_t exp_len)
{
private_botan_diffie_hellman_t *this;
chunk_t random;
rng_t *rng;
INIT(this,
.public = {
.dh = {
.get_shared_secret = _get_shared_secret,
.set_other_public_value = _set_other_public_value,
.get_my_public_value = _get_my_public_value,
.set_private_value = _set_private_value,
.get_dh_group = _get_dh_group,
.destroy = _destroy,
},
},
.group = group,
);
if (!chunk_to_botan_mp(p, &this->p))
{
destroy(this);
return NULL;
}
if (!chunk_to_botan_mp(g, &this->g))
{
destroy(this);
return NULL;
}
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
if (!rng || !rng->allocate_bytes(rng, exp_len, &random))
{
DESTROY_IF(rng);
destroy(this);
return NULL;
}
rng->destroy(rng);
if (!load_private_key(this, random))
{
chunk_clear(&random);
destroy(this);
return NULL;
}
chunk_clear(&random);
return &this->public;
}
/*
* Described in header.
*/
botan_diffie_hellman_t *botan_diffie_hellman_create(
diffie_hellman_group_t group, ...)
{
diffie_hellman_params_t *params;
chunk_t g, p;
if (group == MODP_CUSTOM)
{
VA_ARGS_GET(group, g, p);
return create_generic(group, g, p, p.len);
}
params = diffie_hellman_get_params(group);
if (!params)
{
return NULL;
}
return create_generic(group, params->generator, params->prime,
params->exp_len);
}
#endif
|