50853d6f0bdeefbe58f5e333e94ad680d9dc3adf
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_ec_diffie_hellman.c
1 /*
2 * Copyright (C) 2008-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <openssl/opensslconf.h>
17
18 #ifndef OPENSSL_NO_EC
19
20 #include <openssl/ec.h>
21 #include <openssl/objects.h>
22 #include <openssl/bn.h>
23
24 #include "openssl_ec_diffie_hellman.h"
25 #include "openssl_util.h"
26
27 #include <utils/debug.h>
28
29 typedef struct private_openssl_ec_diffie_hellman_t private_openssl_ec_diffie_hellman_t;
30
31 /**
32 * Private data of an openssl_ec_diffie_hellman_t object.
33 */
34 struct private_openssl_ec_diffie_hellman_t {
35 /**
36 * Public openssl_ec_diffie_hellman_t interface.
37 */
38 openssl_ec_diffie_hellman_t public;
39
40 /**
41 * Diffie Hellman group number.
42 */
43 diffie_hellman_group_t group;
44
45 /**
46 * EC private (public) key
47 */
48 EC_KEY *key;
49
50 /**
51 * EC group
52 */
53 const EC_GROUP *ec_group;
54
55 /**
56 * Other public key
57 */
58 EC_POINT *pub_key;
59
60 /**
61 * Shared secret
62 */
63 chunk_t shared_secret;
64
65 /**
66 * True if shared secret is computed
67 */
68 bool computed;
69 };
70
71 /**
72 * Convert a chunk to an EC_POINT (which must already exist). The x and y
73 * coordinates of the point have to be concatenated in the chunk.
74 */
75 static bool chunk2ecp(const EC_GROUP *group, chunk_t chunk, EC_POINT *point)
76 {
77 BN_CTX *ctx;
78 BIGNUM *x, *y;
79 bool ret = FALSE;
80
81 ctx = BN_CTX_new();
82 if (!ctx)
83 {
84 return FALSE;
85 }
86
87 BN_CTX_start(ctx);
88 x = BN_CTX_get(ctx);
89 y = BN_CTX_get(ctx);
90 if (!x || !y)
91 {
92 goto error;
93 }
94
95 if (!openssl_bn_split(chunk, x, y))
96 {
97 goto error;
98 }
99
100 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
101 {
102 goto error;
103 }
104
105 if (!EC_POINT_is_on_curve(group, point, ctx))
106 {
107 goto error;
108 }
109
110 ret = TRUE;
111 error:
112 BN_CTX_end(ctx);
113 BN_CTX_free(ctx);
114 return ret;
115 }
116
117 /**
118 * Convert an EC_POINT to a chunk by concatenating the x and y coordinates of
119 * the point. This function allocates memory for the chunk.
120 */
121 static bool ecp2chunk(const EC_GROUP *group, const EC_POINT *point,
122 chunk_t *chunk, bool x_coordinate_only)
123 {
124 BN_CTX *ctx;
125 BIGNUM *x, *y;
126 bool ret = FALSE;
127
128 ctx = BN_CTX_new();
129 if (!ctx)
130 {
131 return FALSE;
132 }
133
134 BN_CTX_start(ctx);
135 x = BN_CTX_get(ctx);
136 y = BN_CTX_get(ctx);
137 if (!x || !y)
138 {
139 goto error;
140 }
141
142 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
143 {
144 goto error;
145 }
146
147 if (x_coordinate_only)
148 {
149 y = NULL;
150 }
151 if (!openssl_bn_cat(EC_FIELD_ELEMENT_LEN(group), x, y, chunk))
152 {
153 goto error;
154 }
155
156 ret = TRUE;
157 error:
158 BN_CTX_end(ctx);
159 BN_CTX_free(ctx);
160 return ret;
161 }
162
163 /**
164 * Compute the shared secret.
165 *
166 * We cannot use the function ECDH_compute_key() because that returns only the
167 * x coordinate of the shared secret point (which is defined, for instance, in
168 * 'NIST SP 800-56A').
169 * However, we need both coordinates as RFC 4753 says: "The Diffie-Hellman
170 * public value is obtained by concatenating the x and y values. The format
171 * of the Diffie-Hellman shared secret value is the same as that of the
172 * Diffie-Hellman public value."
173 */
174 static bool compute_shared_key(private_openssl_ec_diffie_hellman_t *this,
175 chunk_t *shared_secret)
176 {
177 const BIGNUM *priv_key;
178 EC_POINT *secret = NULL;
179 bool x_coordinate_only, ret = FALSE;
180
181 priv_key = EC_KEY_get0_private_key(this->key);
182 if (!priv_key)
183 {
184 goto error;
185 }
186
187 secret = EC_POINT_new(this->ec_group);
188 if (!secret)
189 {
190 goto error;
191 }
192
193 if (!EC_POINT_mul(this->ec_group, secret, NULL, this->pub_key, priv_key, NULL))
194 {
195 goto error;
196 }
197
198 /*
199 * The default setting ecp_x_coordinate_only = TRUE
200 * applies the following errata for RFC 4753:
201 * http://www.rfc-editor.org/errata_search.php?eid=9
202 */
203 x_coordinate_only = lib->settings->get_bool(lib->settings,
204 "%s.ecp_x_coordinate_only", TRUE, lib->ns);
205 if (!ecp2chunk(this->ec_group, secret, shared_secret, x_coordinate_only))
206 {
207 goto error;
208 }
209
210 ret = TRUE;
211 error:
212 if (secret)
213 {
214 EC_POINT_clear_free(secret);
215 }
216 return ret;
217 }
218
219 METHOD(diffie_hellman_t, set_other_public_value, void,
220 private_openssl_ec_diffie_hellman_t *this, chunk_t value)
221 {
222 if (!chunk2ecp(this->ec_group, value, this->pub_key))
223 {
224 DBG1(DBG_LIB, "ECDH public value is malformed");
225 return;
226 }
227
228 chunk_clear(&this->shared_secret);
229
230 if (!compute_shared_key(this, &this->shared_secret)) {
231 DBG1(DBG_LIB, "ECDH shared secret computation failed");
232 return;
233 }
234
235 this->computed = TRUE;
236 }
237
238 METHOD(diffie_hellman_t, get_my_public_value, void,
239 private_openssl_ec_diffie_hellman_t *this,chunk_t *value)
240 {
241 ecp2chunk(this->ec_group, EC_KEY_get0_public_key(this->key), value, FALSE);
242 }
243
244 METHOD(diffie_hellman_t, get_shared_secret, status_t,
245 private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
246 {
247 if (!this->computed)
248 {
249 return FAILED;
250 }
251 *secret = chunk_clone(this->shared_secret);
252 return SUCCESS;
253 }
254
255 METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
256 private_openssl_ec_diffie_hellman_t *this)
257 {
258 return this->group;
259 }
260
261 METHOD(diffie_hellman_t, destroy, void,
262 private_openssl_ec_diffie_hellman_t *this)
263 {
264 if (this->pub_key)
265 {
266 EC_POINT_clear_free(this->pub_key);
267 }
268 if (this->key)
269 {
270 EC_KEY_free(this->key);
271 }
272 chunk_clear(&this->shared_secret);
273 free(this);
274 }
275
276 /**
277 * ECC Brainpool curves are not available in OpenSSL releases < 1.0.2, but we
278 * don't check the version in case somebody backported them.
279 */
280 #if (!defined(NID_brainpoolP224r1) || !defined(NID_brainpoolP256r1) || \
281 !defined(NID_brainpoolP384r1) || !defined(NID_brainpoolP512r1))
282
283 /**
284 * Parameters for ECC Brainpool curves
285 */
286 typedef struct {
287 /** DH group */
288 diffie_hellman_group_t group;
289
290 /** The prime p specifying the base field */
291 const chunk_t p;
292
293 /** Coefficient a of the elliptic curve E: y^2 = x^3 + ax + b (mod p) */
294 const chunk_t a;
295
296 /** Coefficient b */
297 const chunk_t b;
298
299 /** x coordinate of base point G (a point in E of prime order) */
300 const chunk_t x;
301
302 /** y coordinate of base point G */
303 const chunk_t y;
304
305 /** Prime order q of the group generated by G */
306 const chunk_t q;
307
308 } bp_curve;
309
310 /**
311 * List of ECC Brainpool curves
312 */
313 static bp_curve bp_curves[] = {
314 {
315 /* ECC Brainpool 224-bit curve (RFC 5639), brainpoolP224r1 */
316 .group = ECP_224_BP,
317 .p = chunk_from_chars(
318 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD1,0xD7,0x87,
319 0xB0,0x9F,0x07,0x57,0x97,0xDA,0x89,0xF5,0x7E,0xC8,0xC0,0xFF),
320 .a = chunk_from_chars(
321 0x68,0xA5,0xE6,0x2C,0xA9,0xCE,0x6C,0x1C,0x29,0x98,0x03,0xA6,0xC1,0x53,0x0B,0x51,
322 0x4E,0x18,0x2A,0xD8,0xB0,0x04,0x2A,0x59,0xCA,0xD2,0x9F,0x43),
323 .b = chunk_from_chars(
324 0x25,0x80,0xF6,0x3C,0xCF,0xE4,0x41,0x38,0x87,0x07,0x13,0xB1,0xA9,0x23,0x69,0xE3,
325 0x3E,0x21,0x35,0xD2,0x66,0xDB,0xB3,0x72,0x38,0x6C,0x40,0x0B),
326 .x = chunk_from_chars(
327 0x0D,0x90,0x29,0xAD,0x2C,0x7E,0x5C,0xF4,0x34,0x08,0x23,0xB2,0xA8,0x7D,0xC6,0x8C,
328 0x9E,0x4C,0xE3,0x17,0x4C,0x1E,0x6E,0xFD,0xEE,0x12,0xC0,0x7D),
329 .y = chunk_from_chars(
330 0x58,0xAA,0x56,0xF7,0x72,0xC0,0x72,0x6F,0x24,0xC6,0xB8,0x9E,0x4E,0xCD,0xAC,0x24,
331 0x35,0x4B,0x9E,0x99,0xCA,0xA3,0xF6,0xD3,0x76,0x14,0x02,0xCD),
332 .q = chunk_from_chars(
333 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD0,0xFB,0x98,
334 0xD1,0x16,0xBC,0x4B,0x6D,0xDE,0xBC,0xA3,0xA5,0xA7,0x93,0x9F),
335 },
336 {
337 /* ECC Brainpool 256-bit curve (RFC 5639), brainpoolP256r1 */
338 .group = ECP_256_BP,
339 .p = chunk_from_chars(
340 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x72,
341 0x6E,0x3B,0xF6,0x23,0xD5,0x26,0x20,0x28,0x20,0x13,0x48,0x1D,0x1F,0x6E,0x53,0x77),
342 .a = chunk_from_chars(
343 0x7D,0x5A,0x09,0x75,0xFC,0x2C,0x30,0x57,0xEE,0xF6,0x75,0x30,0x41,0x7A,0xFF,0xE7,
344 0xFB,0x80,0x55,0xC1,0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9),
345 .b = chunk_from_chars(
346 0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9,0xBB,0xD7,0x7C,0xBF,
347 0x95,0x84,0x16,0x29,0x5C,0xF7,0xE1,0xCE,0x6B,0xCC,0xDC,0x18,0xFF,0x8C,0x07,0xB6),
348 .x = chunk_from_chars(
349 0x8B,0xD2,0xAE,0xB9,0xCB,0x7E,0x57,0xCB,0x2C,0x4B,0x48,0x2F,0xFC,0x81,0xB7,0xAF,
350 0xB9,0xDE,0x27,0xE1,0xE3,0xBD,0x23,0xC2,0x3A,0x44,0x53,0xBD,0x9A,0xCE,0x32,0x62),
351 .y = chunk_from_chars(
352 0x54,0x7E,0xF8,0x35,0xC3,0xDA,0xC4,0xFD,0x97,0xF8,0x46,0x1A,0x14,0x61,0x1D,0xC9,
353 0xC2,0x77,0x45,0x13,0x2D,0xED,0x8E,0x54,0x5C,0x1D,0x54,0xC7,0x2F,0x04,0x69,0x97),
354 .q = chunk_from_chars(
355 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x71,
356 0x8C,0x39,0x7A,0xA3,0xB5,0x61,0xA6,0xF7,0x90,0x1E,0x0E,0x82,0x97,0x48,0x56,0xA7),
357 },
358 {
359 /* ECC Brainpool 384-bit curve (RFC 5639), brainpoolP384r1 */
360 .group = ECP_384_BP,
361 .p = chunk_from_chars(
362 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
363 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB4,0x12,0xB1,0xDA,0x19,0x7F,0xB7,0x11,0x23,
364 0xAC,0xD3,0xA7,0x29,0x90,0x1D,0x1A,0x71,0x87,0x47,0x00,0x13,0x31,0x07,0xEC,0x53),
365 .a = chunk_from_chars(
366 0x7B,0xC3,0x82,0xC6,0x3D,0x8C,0x15,0x0C,0x3C,0x72,0x08,0x0A,0xCE,0x05,0xAF,0xA0,
367 0xC2,0xBE,0xA2,0x8E,0x4F,0xB2,0x27,0x87,0x13,0x91,0x65,0xEF,0xBA,0x91,0xF9,0x0F,
368 0x8A,0xA5,0x81,0x4A,0x50,0x3A,0xD4,0xEB,0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26),
369 .b = chunk_from_chars(
370 0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26,0x8B,0x39,0xB5,0x54,0x16,0xF0,0x44,0x7C,
371 0x2F,0xB7,0x7D,0xE1,0x07,0xDC,0xD2,0xA6,0x2E,0x88,0x0E,0xA5,0x3E,0xEB,0x62,0xD5,
372 0x7C,0xB4,0x39,0x02,0x95,0xDB,0xC9,0x94,0x3A,0xB7,0x86,0x96,0xFA,0x50,0x4C,0x11),
373 .x = chunk_from_chars(
374 0x1D,0x1C,0x64,0xF0,0x68,0xCF,0x45,0xFF,0xA2,0xA6,0x3A,0x81,0xB7,0xC1,0x3F,0x6B,
375 0x88,0x47,0xA3,0xE7,0x7E,0xF1,0x4F,0xE3,0xDB,0x7F,0xCA,0xFE,0x0C,0xBD,0x10,0xE8,
376 0xE8,0x26,0xE0,0x34,0x36,0xD6,0x46,0xAA,0xEF,0x87,0xB2,0xE2,0x47,0xD4,0xAF,0x1E),
377 .y = chunk_from_chars(
378 0x8A,0xBE,0x1D,0x75,0x20,0xF9,0xC2,0xA4,0x5C,0xB1,0xEB,0x8E,0x95,0xCF,0xD5,0x52,
379 0x62,0xB7,0x0B,0x29,0xFE,0xEC,0x58,0x64,0xE1,0x9C,0x05,0x4F,0xF9,0x91,0x29,0x28,
380 0x0E,0x46,0x46,0x21,0x77,0x91,0x81,0x11,0x42,0x82,0x03,0x41,0x26,0x3C,0x53,0x15),
381 .q = chunk_from_chars(
382 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
383 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB3,0x1F,0x16,0x6E,0x6C,0xAC,0x04,0x25,0xA7,
384 0xCF,0x3A,0xB6,0xAF,0x6B,0x7F,0xC3,0x10,0x3B,0x88,0x32,0x02,0xE9,0x04,0x65,0x65),
385 },
386 {
387 /* ECC Brainpool 512-bit curve (RFC 5639), brainpoolP512r1 */
388 .group = ECP_512_BP,
389 .p = chunk_from_chars(
390 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
391 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x71,
392 0x7D,0x4D,0x9B,0x00,0x9B,0xC6,0x68,0x42,0xAE,0xCD,0xA1,0x2A,0xE6,0xA3,0x80,0xE6,
393 0x28,0x81,0xFF,0x2F,0x2D,0x82,0xC6,0x85,0x28,0xAA,0x60,0x56,0x58,0x3A,0x48,0xF3),
394 .a = chunk_from_chars(
395 0x78,0x30,0xA3,0x31,0x8B,0x60,0x3B,0x89,0xE2,0x32,0x71,0x45,0xAC,0x23,0x4C,0xC5,
396 0x94,0xCB,0xDD,0x8D,0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,
397 0x2D,0xED,0x5D,0x5A,0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,
398 0x7F,0x11,0x17,0xA7,0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA),
399 .b = chunk_from_chars(
400 0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,0x2D,0xED,0x5D,0x5A,
401 0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,0x7F,0x11,0x17,0xA7,
402 0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA,0xDC,0x08,0x3E,0x67,
403 0x98,0x40,0x50,0xB7,0x5E,0xBA,0xE5,0xDD,0x28,0x09,0xBD,0x63,0x80,0x16,0xF7,0x23),
404 .x = chunk_from_chars(
405 0x81,0xAE,0xE4,0xBD,0xD8,0x2E,0xD9,0x64,0x5A,0x21,0x32,0x2E,0x9C,0x4C,0x6A,0x93,
406 0x85,0xED,0x9F,0x70,0xB5,0xD9,0x16,0xC1,0xB4,0x3B,0x62,0xEE,0xF4,0xD0,0x09,0x8E,
407 0xFF,0x3B,0x1F,0x78,0xE2,0xD0,0xD4,0x8D,0x50,0xD1,0x68,0x7B,0x93,0xB9,0x7D,0x5F,
408 0x7C,0x6D,0x50,0x47,0x40,0x6A,0x5E,0x68,0x8B,0x35,0x22,0x09,0xBC,0xB9,0xF8,0x22),
409 .y = chunk_from_chars(
410 0x7D,0xDE,0x38,0x5D,0x56,0x63,0x32,0xEC,0xC0,0xEA,0xBF,0xA9,0xCF,0x78,0x22,0xFD,
411 0xF2,0x09,0xF7,0x00,0x24,0xA5,0x7B,0x1A,0xA0,0x00,0xC5,0x5B,0x88,0x1F,0x81,0x11,
412 0xB2,0xDC,0xDE,0x49,0x4A,0x5F,0x48,0x5E,0x5B,0xCA,0x4B,0xD8,0x8A,0x27,0x63,0xAE,
413 0xD1,0xCA,0x2B,0x2F,0xA8,0xF0,0x54,0x06,0x78,0xCD,0x1E,0x0F,0x3A,0xD8,0x08,0x92),
414 .q = chunk_from_chars(
415 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
416 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x70,
417 0x55,0x3E,0x5C,0x41,0x4C,0xA9,0x26,0x19,0x41,0x86,0x61,0x19,0x7F,0xAC,0x10,0x47,
418 0x1D,0xB1,0xD3,0x81,0x08,0x5D,0xDA,0xDD,0xB5,0x87,0x96,0x82,0x9C,0xA9,0x00,0x69),
419 },
420 };
421
422 /**
423 * Create an EC_GROUP object for an ECC Brainpool curve
424 */
425 EC_GROUP *ec_group_new_brainpool(bp_curve *curve)
426 {
427 BIGNUM *p, *a, *b, *x, *y, *q;
428 const BIGNUM *h;
429 EC_POINT *G = NULL;
430 EC_GROUP *group = NULL, *result = NULL;
431 BN_CTX *ctx = NULL;
432
433 ctx = BN_CTX_new();
434 p = BN_bin2bn(curve->p.ptr, curve->p.len, NULL);
435 a = BN_bin2bn(curve->a.ptr, curve->a.len, NULL);
436 b = BN_bin2bn(curve->b.ptr, curve->b.len, NULL);
437 x = BN_bin2bn(curve->x.ptr, curve->x.len, NULL);
438 y = BN_bin2bn(curve->y.ptr, curve->y.len, NULL);
439 q = BN_bin2bn(curve->q.ptr, curve->q.len, NULL);
440 /* all supported groups have a cofactor of 1 */
441 h = BN_value_one();
442 if (!ctx || !p || !a || !b || !x || !y || !q)
443 {
444 goto failed;
445 }
446 group = EC_GROUP_new_curve_GFp(p, a, b, ctx);
447 if (!group)
448 {
449 goto failed;
450 }
451 G = EC_POINT_new(group);
452 if (!G || !EC_POINT_set_affine_coordinates_GFp(group, G, x, y, ctx))
453 {
454 goto failed;
455 }
456 if (!EC_GROUP_set_generator(group, G, q, h))
457 {
458 goto failed;
459 }
460 result = group;
461
462 failed:
463 if (!result && group)
464 {
465 EC_GROUP_free(group);
466 }
467 if (G)
468 {
469 EC_POINT_free(G);
470 }
471 BN_CTX_free(ctx);
472 BN_free(p);
473 BN_free(a);
474 BN_free(b);
475 BN_free(x);
476 BN_free(y);
477 BN_free(q);
478 return result;
479 }
480
481 /**
482 * Create an EC_KEY for ECC Brainpool curves as defined above
483 */
484 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
485 {
486 bp_curve *curve = NULL;
487 EC_GROUP *ec_group;
488 EC_KEY *key = NULL;
489 int i;
490
491 for (i = 0; i < countof(bp_curves); i++)
492 {
493 if (bp_curves[i].group == group)
494 {
495 curve = &bp_curves[i];
496 }
497 }
498 if (!curve)
499 {
500 return NULL;
501 }
502 ec_group = ec_group_new_brainpool(curve);
503 if (!ec_group)
504 {
505 return NULL;
506 }
507 key = EC_KEY_new();
508 if (!key || !EC_KEY_set_group(key, ec_group))
509 {
510 EC_KEY_free(key);
511 key = NULL;
512 }
513 EC_GROUP_free(ec_group);
514 return key;
515 }
516
517 #else /* !NID_brainpoolP224r1 || ... */
518
519 /**
520 * Create an EC_KEY for ECC Brainpool curves as defined by OpenSSL
521 */
522 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
523 {
524 switch (group)
525 {
526 case ECP_224_BP:
527 return EC_KEY_new_by_curve_name(NID_brainpoolP224r1);
528 case ECP_256_BP:
529 return EC_KEY_new_by_curve_name(NID_brainpoolP256r1);
530 case ECP_384_BP:
531 return EC_KEY_new_by_curve_name(NID_brainpoolP384r1);
532 case ECP_512_BP:
533 return EC_KEY_new_by_curve_name(NID_brainpoolP512r1);
534 default:
535 return NULL;
536 }
537 }
538
539 #endif /* !NID_brainpoolP224r1 || ... */
540
541 /*
542 * Described in header.
543 */
544 openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
545 {
546 private_openssl_ec_diffie_hellman_t *this;
547
548 INIT(this,
549 .public = {
550 .dh = {
551 .get_shared_secret = _get_shared_secret,
552 .set_other_public_value = _set_other_public_value,
553 .get_my_public_value = _get_my_public_value,
554 .get_dh_group = _get_dh_group,
555 .destroy = _destroy,
556 },
557 },
558 .group = group,
559 );
560
561 switch (group)
562 {
563 case ECP_192_BIT:
564 this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
565 break;
566 case ECP_224_BIT:
567 this->key = EC_KEY_new_by_curve_name(NID_secp224r1);
568 break;
569 case ECP_256_BIT:
570 this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
571 break;
572 case ECP_384_BIT:
573 this->key = EC_KEY_new_by_curve_name(NID_secp384r1);
574 break;
575 case ECP_521_BIT:
576 this->key = EC_KEY_new_by_curve_name(NID_secp521r1);
577 break;
578 case ECP_224_BP:
579 case ECP_256_BP:
580 case ECP_384_BP:
581 case ECP_512_BP:
582 this->key = ec_key_new_brainpool(group);
583 break;
584 default:
585 this->key = NULL;
586 break;
587 }
588
589 if (!this->key)
590 {
591 free(this);
592 return NULL;
593 }
594
595 /* caching the EC group */
596 this->ec_group = EC_KEY_get0_group(this->key);
597
598 this->pub_key = EC_POINT_new(this->ec_group);
599 if (!this->pub_key)
600 {
601 destroy(this);
602 return NULL;
603 }
604
605 /* generate an EC private (public) key */
606 if (!EC_KEY_generate_key(this->key))
607 {
608 destroy(this);
609 return NULL;
610 }
611
612 return &this->public;
613 }
614 #endif /* OPENSSL_NO_EC */