ec_backend.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core_names.h>
  10. #include <openssl/objects.h>
  11. #include <openssl/params.h>
  12. #include "crypto/bn.h"
  13. #include "crypto/ec.h"
  14. /*
  15. * The intention with the "backend" source file is to offer backend support
  16. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  17. * implementations alike.
  18. */
  19. int ec_set_ecdh_cofactor_mode(EC_KEY *ec, int mode)
  20. {
  21. const EC_GROUP *ecg = EC_KEY_get0_group(ec);
  22. const BIGNUM *cofactor;
  23. /*
  24. * mode can be only 0 for disable, or 1 for enable here.
  25. *
  26. * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
  27. * also supports mode == -1 with the meaning of "reset to the default for
  28. * the associated key".
  29. */
  30. if (mode < 0 || mode > 1)
  31. return 0;
  32. if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
  33. return 0;
  34. /* ECDH cofactor mode has no effect if cofactor is 1 */
  35. if (BN_is_one(cofactor))
  36. return 1;
  37. if (mode == 1)
  38. EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
  39. else if (mode == 0)
  40. EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
  41. return 1;
  42. }
  43. /*
  44. * Callers of ec_key_fromdata MUST make sure that ec_key_params_fromdata has
  45. * been called before!
  46. *
  47. * This function only gets the bare keypair, domain parameters and other
  48. * parameters are treated separately, and domain parameters are required to
  49. * define a keypair.
  50. */
  51. int ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
  52. {
  53. const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
  54. BN_CTX *ctx = NULL;
  55. BIGNUM *priv_key = NULL;
  56. unsigned char *pub_key = NULL;
  57. size_t pub_key_len;
  58. const EC_GROUP *ecg = NULL;
  59. EC_POINT *pub_point = NULL;
  60. int ok = 0;
  61. ecg = EC_KEY_get0_group(ec);
  62. if (ecg == NULL)
  63. return 0;
  64. param_pub_key =
  65. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  66. if (include_private)
  67. param_priv_key =
  68. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  69. ctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
  70. if (ctx == NULL)
  71. goto err;
  72. /* OpenSSL decree: If there's a private key, there must be a public key */
  73. if (param_priv_key != NULL && param_pub_key == NULL)
  74. goto err;
  75. if (param_pub_key != NULL)
  76. if (!OSSL_PARAM_get_octet_string(param_pub_key,
  77. (void **)&pub_key, 0, &pub_key_len)
  78. || (pub_point = EC_POINT_new(ecg)) == NULL
  79. || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
  80. goto err;
  81. if (param_priv_key != NULL && include_private) {
  82. int fixed_words;
  83. const BIGNUM *order;
  84. /*
  85. * Key import/export should never leak the bit length of the secret
  86. * scalar in the key.
  87. *
  88. * For this reason, on export we use padded BIGNUMs with fixed length.
  89. *
  90. * When importing we also should make sure that, even if short lived,
  91. * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
  92. * soon as possible, so that any processing of this BIGNUM might opt for
  93. * constant time implementations in the backend.
  94. *
  95. * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
  96. * to preallocate the BIGNUM internal buffer to a fixed public size big
  97. * enough that operations performed during the processing never trigger
  98. * a realloc which would leak the size of the scalar through memory
  99. * accesses.
  100. *
  101. * Fixed Length
  102. * ------------
  103. *
  104. * The order of the large prime subgroup of the curve is our choice for
  105. * a fixed public size, as that is generally the upper bound for
  106. * generating a private key in EC cryptosystems and should fit all valid
  107. * secret scalars.
  108. *
  109. * For padding on export we just use the bit length of the order
  110. * converted to bytes (rounding up).
  111. *
  112. * For preallocating the BIGNUM storage we look at the number of "words"
  113. * required for the internal representation of the order, and we
  114. * preallocate 2 extra "words" in case any of the subsequent processing
  115. * might temporarily overflow the order length.
  116. */
  117. order = EC_GROUP_get0_order(ecg);
  118. if (order == NULL || BN_is_zero(order))
  119. goto err;
  120. fixed_words = bn_get_top(order) + 2;
  121. if ((priv_key = BN_secure_new()) == NULL)
  122. goto err;
  123. if (bn_wexpand(priv_key, fixed_words) == NULL)
  124. goto err;
  125. BN_set_flags(priv_key, BN_FLG_CONSTTIME);
  126. if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  127. goto err;
  128. }
  129. if (priv_key != NULL
  130. && !EC_KEY_set_private_key(ec, priv_key))
  131. goto err;
  132. if (pub_point != NULL
  133. && !EC_KEY_set_public_key(ec, pub_point))
  134. goto err;
  135. ok = 1;
  136. err:
  137. BN_CTX_free(ctx);
  138. BN_clear_free(priv_key);
  139. OPENSSL_free(pub_key);
  140. EC_POINT_free(pub_point);
  141. return ok;
  142. }
  143. int ec_key_domparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  144. {
  145. const OSSL_PARAM *param_ec_name;
  146. EC_GROUP *ecg = NULL;
  147. char *curve_name = NULL;
  148. int ok = 0;
  149. if (ec == NULL)
  150. return 0;
  151. param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
  152. if (param_ec_name == NULL) {
  153. /* explicit parameters */
  154. /*
  155. * TODO(3.0): should we support explicit parameters curves?
  156. */
  157. return 0;
  158. } else {
  159. /* named curve */
  160. int curve_nid;
  161. if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
  162. || curve_name == NULL
  163. || (curve_nid = ec_curve_name2nid(curve_name)) == NID_undef)
  164. goto err;
  165. if ((ecg = EC_GROUP_new_by_curve_name_ex(ec_key_get_libctx(ec),
  166. curve_nid)) == NULL)
  167. goto err;
  168. }
  169. if (!EC_KEY_set_group(ec, ecg))
  170. goto err;
  171. /*
  172. * TODO(3.0): if the group has changed, should we invalidate the private and
  173. * public key?
  174. */
  175. ok = 1;
  176. err:
  177. OPENSSL_free(curve_name);
  178. EC_GROUP_free(ecg);
  179. return ok;
  180. }
  181. int ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
  182. {
  183. const OSSL_PARAM *p;
  184. if (ec == NULL)
  185. return 0;
  186. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
  187. if (p != NULL) {
  188. int mode;
  189. if (!OSSL_PARAM_get_int(p, &mode)
  190. || !ec_set_ecdh_cofactor_mode(ec, mode))
  191. return 0;
  192. }
  193. return 1;
  194. }