openssl: Support setting ECDH private values
[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, bool,
220 private_openssl_ec_diffie_hellman_t *this, chunk_t value)
221 {
222 if (!diffie_hellman_verify_value(this->group, value))
223 {
224 return FALSE;
225 }
226
227 if (!chunk2ecp(this->ec_group, value, this->pub_key))
228 {
229 DBG1(DBG_LIB, "ECDH public value is malformed");
230 return FALSE;
231 }
232
233 chunk_clear(&this->shared_secret);
234
235 if (!compute_shared_key(this, &this->shared_secret)) {
236 DBG1(DBG_LIB, "ECDH shared secret computation failed");
237 return FALSE;
238 }
239
240 this->computed = TRUE;
241 return TRUE;
242 }
243
244 METHOD(diffie_hellman_t, get_my_public_value, bool,
245 private_openssl_ec_diffie_hellman_t *this,chunk_t *value)
246 {
247 ecp2chunk(this->ec_group, EC_KEY_get0_public_key(this->key), value, FALSE);
248 return TRUE;
249 }
250
251 METHOD(diffie_hellman_t, set_private_value, bool,
252 private_openssl_ec_diffie_hellman_t *this, chunk_t value)
253 {
254 EC_POINT *pub = NULL;
255 BIGNUM *priv = NULL;
256 bool ret = FALSE;
257
258 priv = BN_bin2bn(value.ptr, value.len, NULL);
259 if (!priv)
260 {
261 goto error;
262 }
263 pub = EC_POINT_new(EC_KEY_get0_group(this->key));
264 if (!pub)
265 {
266 goto error;
267 }
268 if (EC_POINT_mul(this->ec_group, pub, priv, NULL, NULL, NULL) != 1)
269 {
270 goto error;
271 }
272 if (EC_KEY_set_private_key(this->key, priv) != 1)
273 {
274 goto error;
275 }
276 if (EC_KEY_set_public_key(this->key, pub) != 1)
277 {
278 goto error;
279 }
280 ret = TRUE;
281
282 error:
283 if (pub)
284 {
285 EC_POINT_free(pub);
286 }
287 if (priv)
288 {
289 BN_free(priv);
290 }
291 return ret;
292 }
293
294 METHOD(diffie_hellman_t, get_shared_secret, bool,
295 private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
296 {
297 if (!this->computed)
298 {
299 return FALSE;
300 }
301 *secret = chunk_clone(this->shared_secret);
302 return TRUE;
303 }
304
305 METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
306 private_openssl_ec_diffie_hellman_t *this)
307 {
308 return this->group;
309 }
310
311 METHOD(diffie_hellman_t, destroy, void,
312 private_openssl_ec_diffie_hellman_t *this)
313 {
314 if (this->pub_key)
315 {
316 EC_POINT_clear_free(this->pub_key);
317 }
318 if (this->key)
319 {
320 EC_KEY_free(this->key);
321 }
322 chunk_clear(&this->shared_secret);
323 free(this);
324 }
325
326 /**
327 * ECC Brainpool curves are not available in OpenSSL releases < 1.0.2, but we
328 * don't check the version in case somebody backported them.
329 */
330 #if (!defined(NID_brainpoolP224r1) || !defined(NID_brainpoolP256r1) || \
331 !defined(NID_brainpoolP384r1) || !defined(NID_brainpoolP512r1))
332
333 /**
334 * Parameters for ECC Brainpool curves
335 */
336 typedef struct {
337 /** DH group */
338 diffie_hellman_group_t group;
339
340 /** The prime p specifying the base field */
341 const chunk_t p;
342
343 /** Coefficient a of the elliptic curve E: y^2 = x^3 + ax + b (mod p) */
344 const chunk_t a;
345
346 /** Coefficient b */
347 const chunk_t b;
348
349 /** x coordinate of base point G (a point in E of prime order) */
350 const chunk_t x;
351
352 /** y coordinate of base point G */
353 const chunk_t y;
354
355 /** Prime order q of the group generated by G */
356 const chunk_t q;
357
358 } bp_curve;
359
360 /**
361 * List of ECC Brainpool curves
362 */
363 static bp_curve bp_curves[] = {
364 {
365 /* ECC Brainpool 224-bit curve (RFC 5639), brainpoolP224r1 */
366 .group = ECP_224_BP,
367 .p = chunk_from_chars(
368 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD1,0xD7,0x87,
369 0xB0,0x9F,0x07,0x57,0x97,0xDA,0x89,0xF5,0x7E,0xC8,0xC0,0xFF),
370 .a = chunk_from_chars(
371 0x68,0xA5,0xE6,0x2C,0xA9,0xCE,0x6C,0x1C,0x29,0x98,0x03,0xA6,0xC1,0x53,0x0B,0x51,
372 0x4E,0x18,0x2A,0xD8,0xB0,0x04,0x2A,0x59,0xCA,0xD2,0x9F,0x43),
373 .b = chunk_from_chars(
374 0x25,0x80,0xF6,0x3C,0xCF,0xE4,0x41,0x38,0x87,0x07,0x13,0xB1,0xA9,0x23,0x69,0xE3,
375 0x3E,0x21,0x35,0xD2,0x66,0xDB,0xB3,0x72,0x38,0x6C,0x40,0x0B),
376 .x = chunk_from_chars(
377 0x0D,0x90,0x29,0xAD,0x2C,0x7E,0x5C,0xF4,0x34,0x08,0x23,0xB2,0xA8,0x7D,0xC6,0x8C,
378 0x9E,0x4C,0xE3,0x17,0x4C,0x1E,0x6E,0xFD,0xEE,0x12,0xC0,0x7D),
379 .y = chunk_from_chars(
380 0x58,0xAA,0x56,0xF7,0x72,0xC0,0x72,0x6F,0x24,0xC6,0xB8,0x9E,0x4E,0xCD,0xAC,0x24,
381 0x35,0x4B,0x9E,0x99,0xCA,0xA3,0xF6,0xD3,0x76,0x14,0x02,0xCD),
382 .q = chunk_from_chars(
383 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD0,0xFB,0x98,
384 0xD1,0x16,0xBC,0x4B,0x6D,0xDE,0xBC,0xA3,0xA5,0xA7,0x93,0x9F),
385 },
386 {
387 /* ECC Brainpool 256-bit curve (RFC 5639), brainpoolP256r1 */
388 .group = ECP_256_BP,
389 .p = chunk_from_chars(
390 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x72,
391 0x6E,0x3B,0xF6,0x23,0xD5,0x26,0x20,0x28,0x20,0x13,0x48,0x1D,0x1F,0x6E,0x53,0x77),
392 .a = chunk_from_chars(
393 0x7D,0x5A,0x09,0x75,0xFC,0x2C,0x30,0x57,0xEE,0xF6,0x75,0x30,0x41,0x7A,0xFF,0xE7,
394 0xFB,0x80,0x55,0xC1,0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9),
395 .b = chunk_from_chars(
396 0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9,0xBB,0xD7,0x7C,0xBF,
397 0x95,0x84,0x16,0x29,0x5C,0xF7,0xE1,0xCE,0x6B,0xCC,0xDC,0x18,0xFF,0x8C,0x07,0xB6),
398 .x = chunk_from_chars(
399 0x8B,0xD2,0xAE,0xB9,0xCB,0x7E,0x57,0xCB,0x2C,0x4B,0x48,0x2F,0xFC,0x81,0xB7,0xAF,
400 0xB9,0xDE,0x27,0xE1,0xE3,0xBD,0x23,0xC2,0x3A,0x44,0x53,0xBD,0x9A,0xCE,0x32,0x62),
401 .y = chunk_from_chars(
402 0x54,0x7E,0xF8,0x35,0xC3,0xDA,0xC4,0xFD,0x97,0xF8,0x46,0x1A,0x14,0x61,0x1D,0xC9,
403 0xC2,0x77,0x45,0x13,0x2D,0xED,0x8E,0x54,0x5C,0x1D,0x54,0xC7,0x2F,0x04,0x69,0x97),
404 .q = chunk_from_chars(
405 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x71,
406 0x8C,0x39,0x7A,0xA3,0xB5,0x61,0xA6,0xF7,0x90,0x1E,0x0E,0x82,0x97,0x48,0x56,0xA7),
407 },
408 {
409 /* ECC Brainpool 384-bit curve (RFC 5639), brainpoolP384r1 */
410 .group = ECP_384_BP,
411 .p = chunk_from_chars(
412 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
413 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB4,0x12,0xB1,0xDA,0x19,0x7F,0xB7,0x11,0x23,
414 0xAC,0xD3,0xA7,0x29,0x90,0x1D,0x1A,0x71,0x87,0x47,0x00,0x13,0x31,0x07,0xEC,0x53),
415 .a = chunk_from_chars(
416 0x7B,0xC3,0x82,0xC6,0x3D,0x8C,0x15,0x0C,0x3C,0x72,0x08,0x0A,0xCE,0x05,0xAF,0xA0,
417 0xC2,0xBE,0xA2,0x8E,0x4F,0xB2,0x27,0x87,0x13,0x91,0x65,0xEF,0xBA,0x91,0xF9,0x0F,
418 0x8A,0xA5,0x81,0x4A,0x50,0x3A,0xD4,0xEB,0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26),
419 .b = chunk_from_chars(
420 0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26,0x8B,0x39,0xB5,0x54,0x16,0xF0,0x44,0x7C,
421 0x2F,0xB7,0x7D,0xE1,0x07,0xDC,0xD2,0xA6,0x2E,0x88,0x0E,0xA5,0x3E,0xEB,0x62,0xD5,
422 0x7C,0xB4,0x39,0x02,0x95,0xDB,0xC9,0x94,0x3A,0xB7,0x86,0x96,0xFA,0x50,0x4C,0x11),
423 .x = chunk_from_chars(
424 0x1D,0x1C,0x64,0xF0,0x68,0xCF,0x45,0xFF,0xA2,0xA6,0x3A,0x81,0xB7,0xC1,0x3F,0x6B,
425 0x88,0x47,0xA3,0xE7,0x7E,0xF1,0x4F,0xE3,0xDB,0x7F,0xCA,0xFE,0x0C,0xBD,0x10,0xE8,
426 0xE8,0x26,0xE0,0x34,0x36,0xD6,0x46,0xAA,0xEF,0x87,0xB2,0xE2,0x47,0xD4,0xAF,0x1E),
427 .y = chunk_from_chars(
428 0x8A,0xBE,0x1D,0x75,0x20,0xF9,0xC2,0xA4,0x5C,0xB1,0xEB,0x8E,0x95,0xCF,0xD5,0x52,
429 0x62,0xB7,0x0B,0x29,0xFE,0xEC,0x58,0x64,0xE1,0x9C,0x05,0x4F,0xF9,0x91,0x29,0x28,
430 0x0E,0x46,0x46,0x21,0x77,0x91,0x81,0x11,0x42,0x82,0x03,0x41,0x26,0x3C,0x53,0x15),
431 .q = chunk_from_chars(
432 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
433 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB3,0x1F,0x16,0x6E,0x6C,0xAC,0x04,0x25,0xA7,
434 0xCF,0x3A,0xB6,0xAF,0x6B,0x7F,0xC3,0x10,0x3B,0x88,0x32,0x02,0xE9,0x04,0x65,0x65),
435 },
436 {
437 /* ECC Brainpool 512-bit curve (RFC 5639), brainpoolP512r1 */
438 .group = ECP_512_BP,
439 .p = chunk_from_chars(
440 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
441 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x71,
442 0x7D,0x4D,0x9B,0x00,0x9B,0xC6,0x68,0x42,0xAE,0xCD,0xA1,0x2A,0xE6,0xA3,0x80,0xE6,
443 0x28,0x81,0xFF,0x2F,0x2D,0x82,0xC6,0x85,0x28,0xAA,0x60,0x56,0x58,0x3A,0x48,0xF3),
444 .a = chunk_from_chars(
445 0x78,0x30,0xA3,0x31,0x8B,0x60,0x3B,0x89,0xE2,0x32,0x71,0x45,0xAC,0x23,0x4C,0xC5,
446 0x94,0xCB,0xDD,0x8D,0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,
447 0x2D,0xED,0x5D,0x5A,0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,
448 0x7F,0x11,0x17,0xA7,0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA),
449 .b = chunk_from_chars(
450 0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,0x2D,0xED,0x5D,0x5A,
451 0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,0x7F,0x11,0x17,0xA7,
452 0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA,0xDC,0x08,0x3E,0x67,
453 0x98,0x40,0x50,0xB7,0x5E,0xBA,0xE5,0xDD,0x28,0x09,0xBD,0x63,0x80,0x16,0xF7,0x23),
454 .x = chunk_from_chars(
455 0x81,0xAE,0xE4,0xBD,0xD8,0x2E,0xD9,0x64,0x5A,0x21,0x32,0x2E,0x9C,0x4C,0x6A,0x93,
456 0x85,0xED,0x9F,0x70,0xB5,0xD9,0x16,0xC1,0xB4,0x3B,0x62,0xEE,0xF4,0xD0,0x09,0x8E,
457 0xFF,0x3B,0x1F,0x78,0xE2,0xD0,0xD4,0x8D,0x50,0xD1,0x68,0x7B,0x93,0xB9,0x7D,0x5F,
458 0x7C,0x6D,0x50,0x47,0x40,0x6A,0x5E,0x68,0x8B,0x35,0x22,0x09,0xBC,0xB9,0xF8,0x22),
459 .y = chunk_from_chars(
460 0x7D,0xDE,0x38,0x5D,0x56,0x63,0x32,0xEC,0xC0,0xEA,0xBF,0xA9,0xCF,0x78,0x22,0xFD,
461 0xF2,0x09,0xF7,0x00,0x24,0xA5,0x7B,0x1A,0xA0,0x00,0xC5,0x5B,0x88,0x1F,0x81,0x11,
462 0xB2,0xDC,0xDE,0x49,0x4A,0x5F,0x48,0x5E,0x5B,0xCA,0x4B,0xD8,0x8A,0x27,0x63,0xAE,
463 0xD1,0xCA,0x2B,0x2F,0xA8,0xF0,0x54,0x06,0x78,0xCD,0x1E,0x0F,0x3A,0xD8,0x08,0x92),
464 .q = chunk_from_chars(
465 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
466 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x70,
467 0x55,0x3E,0x5C,0x41,0x4C,0xA9,0x26,0x19,0x41,0x86,0x61,0x19,0x7F,0xAC,0x10,0x47,
468 0x1D,0xB1,0xD3,0x81,0x08,0x5D,0xDA,0xDD,0xB5,0x87,0x96,0x82,0x9C,0xA9,0x00,0x69),
469 },
470 };
471
472 /**
473 * Create an EC_GROUP object for an ECC Brainpool curve
474 */
475 EC_GROUP *ec_group_new_brainpool(bp_curve *curve)
476 {
477 BIGNUM *p, *a, *b, *x, *y, *q;
478 const BIGNUM *h;
479 EC_POINT *G = NULL;
480 EC_GROUP *group = NULL, *result = NULL;
481 BN_CTX *ctx = NULL;
482
483 ctx = BN_CTX_new();
484 p = BN_bin2bn(curve->p.ptr, curve->p.len, NULL);
485 a = BN_bin2bn(curve->a.ptr, curve->a.len, NULL);
486 b = BN_bin2bn(curve->b.ptr, curve->b.len, NULL);
487 x = BN_bin2bn(curve->x.ptr, curve->x.len, NULL);
488 y = BN_bin2bn(curve->y.ptr, curve->y.len, NULL);
489 q = BN_bin2bn(curve->q.ptr, curve->q.len, NULL);
490 /* all supported groups have a cofactor of 1 */
491 h = BN_value_one();
492 if (!ctx || !p || !a || !b || !x || !y || !q)
493 {
494 goto failed;
495 }
496 group = EC_GROUP_new_curve_GFp(p, a, b, ctx);
497 if (!group)
498 {
499 goto failed;
500 }
501 G = EC_POINT_new(group);
502 if (!G || !EC_POINT_set_affine_coordinates_GFp(group, G, x, y, ctx))
503 {
504 goto failed;
505 }
506 if (!EC_GROUP_set_generator(group, G, q, h))
507 {
508 goto failed;
509 }
510 result = group;
511
512 failed:
513 if (!result && group)
514 {
515 EC_GROUP_free(group);
516 }
517 if (G)
518 {
519 EC_POINT_free(G);
520 }
521 BN_CTX_free(ctx);
522 BN_free(p);
523 BN_free(a);
524 BN_free(b);
525 BN_free(x);
526 BN_free(y);
527 BN_free(q);
528 return result;
529 }
530
531 /**
532 * Create an EC_KEY for ECC Brainpool curves as defined above
533 */
534 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
535 {
536 bp_curve *curve = NULL;
537 EC_GROUP *ec_group;
538 EC_KEY *key = NULL;
539 int i;
540
541 for (i = 0; i < countof(bp_curves); i++)
542 {
543 if (bp_curves[i].group == group)
544 {
545 curve = &bp_curves[i];
546 }
547 }
548 if (!curve)
549 {
550 return NULL;
551 }
552 ec_group = ec_group_new_brainpool(curve);
553 if (!ec_group)
554 {
555 return NULL;
556 }
557 key = EC_KEY_new();
558 if (!key || !EC_KEY_set_group(key, ec_group))
559 {
560 EC_KEY_free(key);
561 key = NULL;
562 }
563 EC_GROUP_free(ec_group);
564 return key;
565 }
566
567 #else /* !NID_brainpoolP224r1 || ... */
568
569 /**
570 * Create an EC_KEY for ECC Brainpool curves as defined by OpenSSL
571 */
572 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
573 {
574 switch (group)
575 {
576 case ECP_224_BP:
577 return EC_KEY_new_by_curve_name(NID_brainpoolP224r1);
578 case ECP_256_BP:
579 return EC_KEY_new_by_curve_name(NID_brainpoolP256r1);
580 case ECP_384_BP:
581 return EC_KEY_new_by_curve_name(NID_brainpoolP384r1);
582 case ECP_512_BP:
583 return EC_KEY_new_by_curve_name(NID_brainpoolP512r1);
584 default:
585 return NULL;
586 }
587 }
588
589 #endif /* !NID_brainpoolP224r1 || ... */
590
591 /*
592 * Described in header.
593 */
594 openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
595 {
596 private_openssl_ec_diffie_hellman_t *this;
597
598 INIT(this,
599 .public = {
600 .dh = {
601 .get_shared_secret = _get_shared_secret,
602 .set_other_public_value = _set_other_public_value,
603 .get_my_public_value = _get_my_public_value,
604 .set_private_value = _set_private_value,
605 .get_dh_group = _get_dh_group,
606 .destroy = _destroy,
607 },
608 },
609 .group = group,
610 );
611
612 switch (group)
613 {
614 case ECP_192_BIT:
615 this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
616 break;
617 case ECP_224_BIT:
618 this->key = EC_KEY_new_by_curve_name(NID_secp224r1);
619 break;
620 case ECP_256_BIT:
621 this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
622 break;
623 case ECP_384_BIT:
624 this->key = EC_KEY_new_by_curve_name(NID_secp384r1);
625 break;
626 case ECP_521_BIT:
627 this->key = EC_KEY_new_by_curve_name(NID_secp521r1);
628 break;
629 case ECP_224_BP:
630 case ECP_256_BP:
631 case ECP_384_BP:
632 case ECP_512_BP:
633 this->key = ec_key_new_brainpool(group);
634 break;
635 default:
636 this->key = NULL;
637 break;
638 }
639
640 if (!this->key)
641 {
642 free(this);
643 return NULL;
644 }
645
646 /* caching the EC group */
647 this->ec_group = EC_KEY_get0_group(this->key);
648
649 this->pub_key = EC_POINT_new(this->ec_group);
650 if (!this->pub_key)
651 {
652 destroy(this);
653 return NULL;
654 }
655
656 /* generate an EC private (public) key */
657 if (!EC_KEY_generate_key(this->key))
658 {
659 destroy(this);
660 return NULL;
661 }
662
663 return &this->public;
664 }
665 #endif /* OPENSSL_NO_EC */