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