openssl_ec_diffie_hellman.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright (C) 2008-2013 Tobias Brunner
  3. * HSR 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. #include <openssl/opensslconf.h>
  16. #ifndef OPENSSL_NO_EC
  17. #include <openssl/bn.h>
  18. #include <openssl/ec.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/bn.h>
  21. #include "openssl_ec_diffie_hellman.h"
  22. #include "openssl_util.h"
  23. #include <utils/debug.h>
  24. typedef struct private_openssl_ec_diffie_hellman_t private_openssl_ec_diffie_hellman_t;
  25. /**
  26. * Private data of an openssl_ec_diffie_hellman_t object.
  27. */
  28. struct private_openssl_ec_diffie_hellman_t {
  29. /**
  30. * Public openssl_ec_diffie_hellman_t interface.
  31. */
  32. openssl_ec_diffie_hellman_t public;
  33. /**
  34. * Diffie Hellman group number.
  35. */
  36. diffie_hellman_group_t group;
  37. /**
  38. * EC private (public) key
  39. */
  40. EC_KEY *key;
  41. /**
  42. * EC group
  43. */
  44. const EC_GROUP *ec_group;
  45. /**
  46. * Other public key
  47. */
  48. EC_POINT *pub_key;
  49. /**
  50. * Shared secret
  51. */
  52. chunk_t shared_secret;
  53. /**
  54. * True if shared secret is computed
  55. */
  56. bool computed;
  57. };
  58. /**
  59. * Convert a chunk to an EC_POINT (which must already exist). The x and y
  60. * coordinates of the point have to be concatenated in the chunk.
  61. */
  62. static bool chunk2ecp(const EC_GROUP *group, chunk_t chunk, EC_POINT *point)
  63. {
  64. BN_CTX *ctx;
  65. BIGNUM *x, *y;
  66. bool ret = FALSE;
  67. ctx = BN_CTX_new();
  68. if (!ctx)
  69. {
  70. return FALSE;
  71. }
  72. BN_CTX_start(ctx);
  73. x = BN_CTX_get(ctx);
  74. y = BN_CTX_get(ctx);
  75. if (!x || !y)
  76. {
  77. goto error;
  78. }
  79. if (!openssl_bn_split(chunk, x, y))
  80. {
  81. goto error;
  82. }
  83. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
  84. {
  85. goto error;
  86. }
  87. if (!EC_POINT_is_on_curve(group, point, ctx))
  88. {
  89. goto error;
  90. }
  91. ret = TRUE;
  92. error:
  93. BN_CTX_end(ctx);
  94. BN_CTX_free(ctx);
  95. return ret;
  96. }
  97. /**
  98. * Convert an EC_POINT to a chunk by concatenating the x and y coordinates of
  99. * the point. This function allocates memory for the chunk.
  100. */
  101. static bool ecp2chunk(const EC_GROUP *group, const EC_POINT *point,
  102. chunk_t *chunk, bool x_coordinate_only)
  103. {
  104. BN_CTX *ctx;
  105. BIGNUM *x, *y;
  106. bool ret = FALSE;
  107. ctx = BN_CTX_new();
  108. if (!ctx)
  109. {
  110. return FALSE;
  111. }
  112. BN_CTX_start(ctx);
  113. x = BN_CTX_get(ctx);
  114. y = BN_CTX_get(ctx);
  115. if (!x || !y)
  116. {
  117. goto error;
  118. }
  119. if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
  120. {
  121. goto error;
  122. }
  123. if (x_coordinate_only)
  124. {
  125. y = NULL;
  126. }
  127. if (!openssl_bn_cat(EC_FIELD_ELEMENT_LEN(group), x, y, chunk))
  128. {
  129. goto error;
  130. }
  131. ret = TRUE;
  132. error:
  133. BN_CTX_end(ctx);
  134. BN_CTX_free(ctx);
  135. return ret;
  136. }
  137. /**
  138. * Compute the shared secret.
  139. *
  140. * We cannot use the function ECDH_compute_key() because that returns only the
  141. * x coordinate of the shared secret point (which is defined, for instance, in
  142. * 'NIST SP 800-56A').
  143. * However, we need both coordinates as RFC 4753 says: "The Diffie-Hellman
  144. * public value is obtained by concatenating the x and y values. The format
  145. * of the Diffie-Hellman shared secret value is the same as that of the
  146. * Diffie-Hellman public value."
  147. */
  148. static bool compute_shared_key(private_openssl_ec_diffie_hellman_t *this,
  149. chunk_t *shared_secret)
  150. {
  151. const BIGNUM *priv_key;
  152. EC_POINT *secret = NULL;
  153. bool x_coordinate_only, ret = FALSE;
  154. priv_key = EC_KEY_get0_private_key(this->key);
  155. if (!priv_key)
  156. {
  157. goto error;
  158. }
  159. secret = EC_POINT_new(this->ec_group);
  160. if (!secret)
  161. {
  162. goto error;
  163. }
  164. if (!EC_POINT_mul(this->ec_group, secret, NULL, this->pub_key, priv_key, NULL))
  165. {
  166. goto error;
  167. }
  168. /*
  169. * The default setting ecp_x_coordinate_only = TRUE
  170. * applies the following errata for RFC 4753:
  171. * http://www.rfc-editor.org/errata_search.php?eid=9
  172. */
  173. x_coordinate_only = lib->settings->get_bool(lib->settings,
  174. "%s.ecp_x_coordinate_only", TRUE, lib->ns);
  175. if (!ecp2chunk(this->ec_group, secret, shared_secret, x_coordinate_only))
  176. {
  177. goto error;
  178. }
  179. ret = TRUE;
  180. error:
  181. if (secret)
  182. {
  183. EC_POINT_clear_free(secret);
  184. }
  185. return ret;
  186. }
  187. METHOD(diffie_hellman_t, set_other_public_value, bool,
  188. private_openssl_ec_diffie_hellman_t *this, chunk_t value)
  189. {
  190. if (!diffie_hellman_verify_value(this->group, value))
  191. {
  192. return FALSE;
  193. }
  194. if (!chunk2ecp(this->ec_group, value, this->pub_key))
  195. {
  196. DBG1(DBG_LIB, "ECDH public value is malformed");
  197. return FALSE;
  198. }
  199. chunk_clear(&this->shared_secret);
  200. if (!compute_shared_key(this, &this->shared_secret)) {
  201. DBG1(DBG_LIB, "ECDH shared secret computation failed");
  202. return FALSE;
  203. }
  204. this->computed = TRUE;
  205. return TRUE;
  206. }
  207. METHOD(diffie_hellman_t, get_my_public_value, bool,
  208. private_openssl_ec_diffie_hellman_t *this,chunk_t *value)
  209. {
  210. ecp2chunk(this->ec_group, EC_KEY_get0_public_key(this->key), value, FALSE);
  211. return TRUE;
  212. }
  213. METHOD(diffie_hellman_t, set_private_value, bool,
  214. private_openssl_ec_diffie_hellman_t *this, chunk_t value)
  215. {
  216. EC_POINT *pub = NULL;
  217. BIGNUM *priv = NULL;
  218. bool ret = FALSE;
  219. priv = BN_bin2bn(value.ptr, value.len, NULL);
  220. if (!priv)
  221. {
  222. goto error;
  223. }
  224. pub = EC_POINT_new(EC_KEY_get0_group(this->key));
  225. if (!pub)
  226. {
  227. goto error;
  228. }
  229. if (EC_POINT_mul(this->ec_group, pub, priv, NULL, NULL, NULL) != 1)
  230. {
  231. goto error;
  232. }
  233. if (EC_KEY_set_private_key(this->key, priv) != 1)
  234. {
  235. goto error;
  236. }
  237. if (EC_KEY_set_public_key(this->key, pub) != 1)
  238. {
  239. goto error;
  240. }
  241. ret = TRUE;
  242. error:
  243. if (pub)
  244. {
  245. EC_POINT_free(pub);
  246. }
  247. if (priv)
  248. {
  249. BN_free(priv);
  250. }
  251. return ret;
  252. }
  253. METHOD(diffie_hellman_t, get_shared_secret, bool,
  254. private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
  255. {
  256. if (!this->computed)
  257. {
  258. return FALSE;
  259. }
  260. *secret = chunk_clone(this->shared_secret);
  261. return TRUE;
  262. }
  263. METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
  264. private_openssl_ec_diffie_hellman_t *this)
  265. {
  266. return this->group;
  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. * ECC Brainpool curves are not available in OpenSSL releases < 1.0.2, but we
  284. * don't check the version in case somebody backported them.
  285. *
  286. * BoringSSL defines the constants but not the curves.
  287. */
  288. #if (!defined(NID_brainpoolP224r1) || !defined(NID_brainpoolP256r1) || \
  289. !defined(NID_brainpoolP384r1) || !defined(NID_brainpoolP512r1) || \
  290. defined(OPENSSL_IS_BORINGSSL))
  291. /**
  292. * Parameters for ECC Brainpool curves
  293. */
  294. typedef struct {
  295. /** DH group */
  296. diffie_hellman_group_t group;
  297. /** The prime p specifying the base field */
  298. const chunk_t p;
  299. /** Coefficient a of the elliptic curve E: y^2 = x^3 + ax + b (mod p) */
  300. const chunk_t a;
  301. /** Coefficient b */
  302. const chunk_t b;
  303. /** x coordinate of base point G (a point in E of prime order) */
  304. const chunk_t x;
  305. /** y coordinate of base point G */
  306. const chunk_t y;
  307. /** Prime order q of the group generated by G */
  308. const chunk_t q;
  309. } bp_curve;
  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. * Create an EC_GROUP object for an ECC Brainpool curve
  423. */
  424. EC_GROUP *ec_group_new_brainpool(bp_curve *curve)
  425. {
  426. BIGNUM *p, *a, *b, *x, *y, *q;
  427. const BIGNUM *h;
  428. EC_POINT *G = NULL;
  429. EC_GROUP *group = NULL, *result = NULL;
  430. BN_CTX *ctx = NULL;
  431. ctx = BN_CTX_new();
  432. p = BN_bin2bn(curve->p.ptr, curve->p.len, NULL);
  433. a = BN_bin2bn(curve->a.ptr, curve->a.len, NULL);
  434. b = BN_bin2bn(curve->b.ptr, curve->b.len, NULL);
  435. x = BN_bin2bn(curve->x.ptr, curve->x.len, NULL);
  436. y = BN_bin2bn(curve->y.ptr, curve->y.len, NULL);
  437. q = BN_bin2bn(curve->q.ptr, curve->q.len, NULL);
  438. /* all supported groups have a cofactor of 1 */
  439. h = BN_value_one();
  440. if (!ctx || !p || !a || !b || !x || !y || !q)
  441. {
  442. goto failed;
  443. }
  444. group = EC_GROUP_new_curve_GFp(p, a, b, ctx);
  445. if (!group)
  446. {
  447. goto failed;
  448. }
  449. G = EC_POINT_new(group);
  450. if (!G || !EC_POINT_set_affine_coordinates_GFp(group, G, x, y, ctx))
  451. {
  452. goto failed;
  453. }
  454. if (!EC_GROUP_set_generator(group, G, q, h))
  455. {
  456. goto failed;
  457. }
  458. result = group;
  459. failed:
  460. if (!result && group)
  461. {
  462. EC_GROUP_free(group);
  463. }
  464. if (G)
  465. {
  466. EC_POINT_free(G);
  467. }
  468. BN_CTX_free(ctx);
  469. BN_free(p);
  470. BN_free(a);
  471. BN_free(b);
  472. BN_free(x);
  473. BN_free(y);
  474. BN_free(q);
  475. return result;
  476. }
  477. /**
  478. * Create an EC_KEY for ECC Brainpool curves as defined above
  479. */
  480. static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
  481. {
  482. bp_curve *curve = NULL;
  483. EC_GROUP *ec_group;
  484. EC_KEY *key = NULL;
  485. int i;
  486. for (i = 0; i < countof(bp_curves); i++)
  487. {
  488. if (bp_curves[i].group == group)
  489. {
  490. curve = &bp_curves[i];
  491. }
  492. }
  493. if (!curve)
  494. {
  495. return NULL;
  496. }
  497. ec_group = ec_group_new_brainpool(curve);
  498. if (!ec_group)
  499. {
  500. return NULL;
  501. }
  502. key = EC_KEY_new();
  503. if (!key || !EC_KEY_set_group(key, ec_group))
  504. {
  505. EC_KEY_free(key);
  506. key = NULL;
  507. }
  508. EC_GROUP_free(ec_group);
  509. return key;
  510. }
  511. #else /* !NID_brainpoolP224r1 || ... */
  512. /**
  513. * Create an EC_KEY for ECC Brainpool curves as defined by OpenSSL
  514. */
  515. static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
  516. {
  517. switch (group)
  518. {
  519. case ECP_224_BP:
  520. return EC_KEY_new_by_curve_name(NID_brainpoolP224r1);
  521. case ECP_256_BP:
  522. return EC_KEY_new_by_curve_name(NID_brainpoolP256r1);
  523. case ECP_384_BP:
  524. return EC_KEY_new_by_curve_name(NID_brainpoolP384r1);
  525. case ECP_512_BP:
  526. return EC_KEY_new_by_curve_name(NID_brainpoolP512r1);
  527. default:
  528. return NULL;
  529. }
  530. }
  531. #endif /* !NID_brainpoolP224r1 || ... */
  532. /*
  533. * Described in header.
  534. */
  535. openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
  536. {
  537. private_openssl_ec_diffie_hellman_t *this;
  538. INIT(this,
  539. .public = {
  540. .dh = {
  541. .get_shared_secret = _get_shared_secret,
  542. .set_other_public_value = _set_other_public_value,
  543. .get_my_public_value = _get_my_public_value,
  544. .set_private_value = _set_private_value,
  545. .get_dh_group = _get_dh_group,
  546. .destroy = _destroy,
  547. },
  548. },
  549. .group = group,
  550. );
  551. switch (group)
  552. {
  553. case ECP_192_BIT:
  554. this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
  555. break;
  556. case ECP_224_BIT:
  557. this->key = EC_KEY_new_by_curve_name(NID_secp224r1);
  558. break;
  559. case ECP_256_BIT:
  560. this->key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  561. break;
  562. case ECP_384_BIT:
  563. this->key = EC_KEY_new_by_curve_name(NID_secp384r1);
  564. break;
  565. case ECP_521_BIT:
  566. this->key = EC_KEY_new_by_curve_name(NID_secp521r1);
  567. break;
  568. case ECP_224_BP:
  569. case ECP_256_BP:
  570. case ECP_384_BP:
  571. case ECP_512_BP:
  572. this->key = ec_key_new_brainpool(group);
  573. break;
  574. default:
  575. this->key = NULL;
  576. break;
  577. }
  578. if (!this->key)
  579. {
  580. free(this);
  581. return NULL;
  582. }
  583. /* caching the EC group */
  584. this->ec_group = EC_KEY_get0_group(this->key);
  585. this->pub_key = EC_POINT_new(this->ec_group);
  586. if (!this->pub_key)
  587. {
  588. destroy(this);
  589. return NULL;
  590. }
  591. /* generate an EC private (public) key */
  592. if (!EC_KEY_generate_key(this->key))
  593. {
  594. destroy(this);
  595. return NULL;
  596. }
  597. return &this->public;
  598. }
  599. #endif /* OPENSSL_NO_EC */