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