ec_key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec_key.h>
  68. #include <string.h>
  69. #include <openssl/ec.h>
  70. #include <openssl/ecdsa.h>
  71. #include <openssl/engine.h>
  72. #include <openssl/err.h>
  73. #include <openssl/ex_data.h>
  74. #include <openssl/mem.h>
  75. #include <openssl/thread.h>
  76. #include "internal.h"
  77. #include "../delocate.h"
  78. #include "../../internal.h"
  79. DEFINE_STATIC_EX_DATA_CLASS(g_ec_ex_data_class);
  80. static EC_WRAPPED_SCALAR *ec_wrapped_scalar_new(const EC_GROUP *group) {
  81. EC_WRAPPED_SCALAR *wrapped = OPENSSL_malloc(sizeof(EC_WRAPPED_SCALAR));
  82. if (wrapped == NULL) {
  83. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  84. return NULL;
  85. }
  86. OPENSSL_memset(wrapped, 0, sizeof(EC_WRAPPED_SCALAR));
  87. wrapped->bignum.d = wrapped->scalar.words;
  88. wrapped->bignum.width = group->order.width;
  89. wrapped->bignum.dmax = group->order.width;
  90. wrapped->bignum.flags = BN_FLG_STATIC_DATA;
  91. return wrapped;
  92. }
  93. static void ec_wrapped_scalar_free(EC_WRAPPED_SCALAR *scalar) {
  94. OPENSSL_free(scalar);
  95. }
  96. EC_KEY *EC_KEY_new(void) { return EC_KEY_new_method(NULL); }
  97. EC_KEY *EC_KEY_new_method(const ENGINE *engine) {
  98. EC_KEY *ret = OPENSSL_malloc(sizeof(EC_KEY));
  99. if (ret == NULL) {
  100. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  101. return NULL;
  102. }
  103. OPENSSL_memset(ret, 0, sizeof(EC_KEY));
  104. if (engine) {
  105. ret->ecdsa_meth = ENGINE_get_ECDSA_method(engine);
  106. }
  107. if (ret->ecdsa_meth) {
  108. METHOD_ref(ret->ecdsa_meth);
  109. }
  110. ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
  111. ret->references = 1;
  112. CRYPTO_new_ex_data(&ret->ex_data);
  113. if (ret->ecdsa_meth && ret->ecdsa_meth->init && !ret->ecdsa_meth->init(ret)) {
  114. CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), ret, &ret->ex_data);
  115. if (ret->ecdsa_meth) {
  116. METHOD_unref(ret->ecdsa_meth);
  117. }
  118. OPENSSL_free(ret);
  119. return NULL;
  120. }
  121. return ret;
  122. }
  123. EC_KEY *EC_KEY_new_by_curve_name(int nid) {
  124. EC_KEY *ret = EC_KEY_new();
  125. if (ret == NULL) {
  126. OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
  127. return NULL;
  128. }
  129. ret->group = EC_GROUP_new_by_curve_name(nid);
  130. if (ret->group == NULL) {
  131. EC_KEY_free(ret);
  132. return NULL;
  133. }
  134. return ret;
  135. }
  136. void EC_KEY_free(EC_KEY *r) {
  137. if (r == NULL) {
  138. return;
  139. }
  140. if (!CRYPTO_refcount_dec_and_test_zero(&r->references)) {
  141. return;
  142. }
  143. if (r->ecdsa_meth) {
  144. if (r->ecdsa_meth->finish) {
  145. r->ecdsa_meth->finish(r);
  146. }
  147. METHOD_unref(r->ecdsa_meth);
  148. }
  149. EC_GROUP_free(r->group);
  150. EC_POINT_free(r->pub_key);
  151. ec_wrapped_scalar_free(r->priv_key);
  152. BN_free(r->fixed_k);
  153. CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
  154. OPENSSL_free(r);
  155. }
  156. EC_KEY *EC_KEY_dup(const EC_KEY *src) {
  157. if (src == NULL) {
  158. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  159. return NULL;
  160. }
  161. EC_KEY *ret = EC_KEY_new();
  162. if (ret == NULL) {
  163. return NULL;
  164. }
  165. if ((src->group != NULL &&
  166. !EC_KEY_set_group(ret, src->group)) ||
  167. (src->pub_key != NULL &&
  168. !EC_KEY_set_public_key(ret, src->pub_key)) ||
  169. (src->priv_key != NULL &&
  170. !EC_KEY_set_private_key(ret, EC_KEY_get0_private_key(src)))) {
  171. EC_KEY_free(ret);
  172. return NULL;
  173. }
  174. ret->enc_flag = src->enc_flag;
  175. ret->conv_form = src->conv_form;
  176. return ret;
  177. }
  178. int EC_KEY_up_ref(EC_KEY *r) {
  179. CRYPTO_refcount_inc(&r->references);
  180. return 1;
  181. }
  182. int EC_KEY_is_opaque(const EC_KEY *key) {
  183. return key->ecdsa_meth && (key->ecdsa_meth->flags & ECDSA_FLAG_OPAQUE);
  184. }
  185. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) { return key->group; }
  186. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) {
  187. // If |key| already has a group, it is an error to switch to another one.
  188. if (key->group != NULL) {
  189. if (EC_GROUP_cmp(key->group, group, NULL) != 0) {
  190. OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. assert(key->priv_key == NULL);
  196. assert(key->pub_key == NULL);
  197. EC_GROUP_free(key->group);
  198. key->group = EC_GROUP_dup(group);
  199. return key->group != NULL;
  200. }
  201. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key) {
  202. return key->priv_key != NULL ? &key->priv_key->bignum : NULL;
  203. }
  204. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key) {
  205. if (key->group == NULL) {
  206. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
  207. return 0;
  208. }
  209. EC_WRAPPED_SCALAR *scalar = ec_wrapped_scalar_new(key->group);
  210. if (scalar == NULL) {
  211. return 0;
  212. }
  213. if (!ec_bignum_to_scalar(key->group, &scalar->scalar, priv_key)) {
  214. OPENSSL_PUT_ERROR(EC, EC_R_WRONG_ORDER);
  215. ec_wrapped_scalar_free(scalar);
  216. return 0;
  217. }
  218. ec_wrapped_scalar_free(key->priv_key);
  219. key->priv_key = scalar;
  220. return 1;
  221. }
  222. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key) {
  223. return key->pub_key;
  224. }
  225. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key) {
  226. if (key->group == NULL) {
  227. OPENSSL_PUT_ERROR(EC, EC_R_MISSING_PARAMETERS);
  228. return 0;
  229. }
  230. if (EC_GROUP_cmp(key->group, pub_key->group, NULL) != 0) {
  231. OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
  232. return 0;
  233. }
  234. EC_POINT_free(key->pub_key);
  235. key->pub_key = EC_POINT_dup(pub_key, key->group);
  236. return (key->pub_key == NULL) ? 0 : 1;
  237. }
  238. unsigned int EC_KEY_get_enc_flags(const EC_KEY *key) { return key->enc_flag; }
  239. void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags) {
  240. key->enc_flag = flags;
  241. }
  242. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key) {
  243. return key->conv_form;
  244. }
  245. void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform) {
  246. key->conv_form = cform;
  247. }
  248. int EC_KEY_check_key(const EC_KEY *eckey) {
  249. int ok = 0;
  250. BN_CTX *ctx = NULL;
  251. EC_POINT *point = NULL;
  252. if (!eckey || !eckey->group || !eckey->pub_key) {
  253. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  254. return 0;
  255. }
  256. if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
  257. OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
  258. goto err;
  259. }
  260. ctx = BN_CTX_new();
  261. if (ctx == NULL) {
  262. goto err;
  263. }
  264. // testing whether the pub_key is on the elliptic curve
  265. if (!EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx)) {
  266. OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
  267. goto err;
  268. }
  269. // in case the priv_key is present :
  270. // check if generator * priv_key == pub_key
  271. if (eckey->priv_key != NULL) {
  272. point = EC_POINT_new(eckey->group);
  273. if (point == NULL ||
  274. !ec_point_mul_scalar(eckey->group, point, &eckey->priv_key->scalar,
  275. NULL, NULL, ctx)) {
  276. OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
  277. goto err;
  278. }
  279. if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
  280. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_PRIVATE_KEY);
  281. goto err;
  282. }
  283. }
  284. ok = 1;
  285. err:
  286. BN_CTX_free(ctx);
  287. EC_POINT_free(point);
  288. return ok;
  289. }
  290. int EC_KEY_check_fips(const EC_KEY *key) {
  291. if (EC_KEY_is_opaque(key)) {
  292. // Opaque keys can't be checked.
  293. OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
  294. return 0;
  295. }
  296. if (!EC_KEY_check_key(key)) {
  297. return 0;
  298. }
  299. if (key->priv_key) {
  300. uint8_t data[16] = {0};
  301. ECDSA_SIG *sig = ECDSA_do_sign(data, sizeof(data), key);
  302. #if defined(BORINGSSL_FIPS_BREAK_ECDSA_PWCT)
  303. data[0] = ~data[0];
  304. #endif
  305. int ok = sig != NULL &&
  306. ECDSA_do_verify(data, sizeof(data), sig, key);
  307. ECDSA_SIG_free(sig);
  308. if (!ok) {
  309. OPENSSL_PUT_ERROR(EC, EC_R_PUBLIC_KEY_VALIDATION_FAILED);
  310. return 0;
  311. }
  312. }
  313. return 1;
  314. }
  315. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  316. BIGNUM *y) {
  317. EC_POINT *point = NULL;
  318. int ok = 0;
  319. if (!key || !key->group || !x || !y) {
  320. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  321. return 0;
  322. }
  323. point = EC_POINT_new(key->group);
  324. if (point == NULL ||
  325. !EC_POINT_set_affine_coordinates_GFp(key->group, point, x, y, NULL) ||
  326. !EC_KEY_set_public_key(key, point) ||
  327. !EC_KEY_check_key(key)) {
  328. goto err;
  329. }
  330. ok = 1;
  331. err:
  332. EC_POINT_free(point);
  333. return ok;
  334. }
  335. int EC_KEY_generate_key(EC_KEY *key) {
  336. if (key == NULL || key->group == NULL) {
  337. OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
  338. return 0;
  339. }
  340. // Check that the group order is FIPS compliant (FIPS 186-4 B.4.2).
  341. if (BN_num_bits(EC_GROUP_get0_order(key->group)) < 160) {
  342. OPENSSL_PUT_ERROR(EC, EC_R_INVALID_GROUP_ORDER);
  343. return 0;
  344. }
  345. static const uint8_t kDefaultAdditionalData[32] = {0};
  346. EC_WRAPPED_SCALAR *priv_key = ec_wrapped_scalar_new(key->group);
  347. EC_POINT *pub_key = EC_POINT_new(key->group);
  348. if (priv_key == NULL || pub_key == NULL ||
  349. // Generate the private key by testing candidates (FIPS 186-4 B.4.2).
  350. !ec_random_nonzero_scalar(key->group, &priv_key->scalar,
  351. kDefaultAdditionalData) ||
  352. !ec_point_mul_scalar(key->group, pub_key, &priv_key->scalar, NULL, NULL,
  353. NULL)) {
  354. EC_POINT_free(pub_key);
  355. ec_wrapped_scalar_free(priv_key);
  356. return 0;
  357. }
  358. ec_wrapped_scalar_free(key->priv_key);
  359. key->priv_key = priv_key;
  360. EC_POINT_free(key->pub_key);
  361. key->pub_key = pub_key;
  362. return 1;
  363. }
  364. int EC_KEY_generate_key_fips(EC_KEY *eckey) {
  365. return EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey);
  366. }
  367. int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  368. CRYPTO_EX_dup *dup_unused,
  369. CRYPTO_EX_free *free_func) {
  370. int index;
  371. if (!CRYPTO_get_ex_new_index(g_ec_ex_data_class_bss_get(), &index, argl, argp,
  372. free_func)) {
  373. return -1;
  374. }
  375. return index;
  376. }
  377. int EC_KEY_set_ex_data(EC_KEY *d, int idx, void *arg) {
  378. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  379. }
  380. void *EC_KEY_get_ex_data(const EC_KEY *d, int idx) {
  381. return CRYPTO_get_ex_data(&d->ex_data, idx);
  382. }
  383. void EC_KEY_set_asn1_flag(EC_KEY *key, int flag) {}