ec_key.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. #ifndef OPENSSL_HEADER_EC_KEY_H
  68. #define OPENSSL_HEADER_EC_KEY_H
  69. #include <openssl/base.h>
  70. #include <openssl/ec.h>
  71. #include <openssl/engine.h>
  72. #include <openssl/ex_data.h>
  73. #if defined(__cplusplus)
  74. extern "C" {
  75. #endif
  76. // ec_key.h contains functions that handle elliptic-curve points that are
  77. // public/private keys.
  78. // EC key objects.
  79. //
  80. // An |EC_KEY| object represents a public or private EC key. A given object may
  81. // be used concurrently on multiple threads by non-mutating functions, provided
  82. // no other thread is concurrently calling a mutating function. Unless otherwise
  83. // documented, functions which take a |const| pointer are non-mutating and
  84. // functions which take a non-|const| pointer are mutating.
  85. // EC_KEY_new returns a fresh |EC_KEY| object or NULL on error.
  86. OPENSSL_EXPORT EC_KEY *EC_KEY_new(void);
  87. // EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit
  88. // |ENGINE|.
  89. OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine);
  90. // EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid|
  91. // or NULL on error.
  92. OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid);
  93. // EC_KEY_free frees all the data owned by |key| and |key| itself.
  94. OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key);
  95. // EC_KEY_dup returns a fresh copy of |src| or NULL on error.
  96. OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src);
  97. // EC_KEY_up_ref increases the reference count of |key| and returns one. It does
  98. // not mutate |key| for thread-safety purposes and may be used concurrently.
  99. OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key);
  100. // EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key
  101. // material. Otherwise it return zero.
  102. OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key);
  103. // EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|.
  104. OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
  105. // EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|.
  106. // It returns one on success and zero if |key| is already configured with a
  107. // different group.
  108. OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
  109. // EC_KEY_get0_private_key returns a pointer to the private key inside |key|.
  110. OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
  111. // EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns
  112. // one on success and zero otherwise. |key| must already have had a group
  113. // configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|).
  114. OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv);
  115. // EC_KEY_get0_public_key returns a pointer to the public key point inside
  116. // |key|.
  117. OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
  118. // EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
  119. // It returns one on success and zero otherwise. |key| must already have had a
  120. // group configured (see |EC_KEY_set_group| and |EC_KEY_new_by_curve_name|), and
  121. // |pub| must also belong to that group.
  122. OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
  123. #define EC_PKEY_NO_PARAMETERS 0x001
  124. #define EC_PKEY_NO_PUBKEY 0x002
  125. // EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a
  126. // bitwise-OR of |EC_PKEY_*| values.
  127. OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
  128. // EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a
  129. // bitwise-OR of |EC_PKEY_*| values.
  130. OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags);
  131. // EC_KEY_get_conv_form returns the conversation form that will be used by
  132. // |key|.
  133. OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
  134. // EC_KEY_set_conv_form sets the conversion form to be used by |key|.
  135. OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key,
  136. point_conversion_form_t cform);
  137. // EC_KEY_check_key performs several checks on |key| (possibly including an
  138. // expensive check that the public key is in the primary subgroup). It returns
  139. // one if all checks pass and zero otherwise. If it returns zero then detail
  140. // about the problem can be found on the error stack.
  141. OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key);
  142. // EC_KEY_check_fips performs both a signing pairwise consistency test
  143. // (FIPS 140-2 4.9.2) and the consistency test from SP 800-56Ar3 section
  144. // 5.6.2.1.4. It returns one if it passes and zero otherwise.
  145. OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key);
  146. // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to
  147. // (|x|, |y|). It returns one on success and zero on error. It's considered an
  148. // error if |x| and |y| do not represent a point on |key|'s curve.
  149. OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
  150. const BIGNUM *x,
  151. const BIGNUM *y);
  152. // EC_KEY_key2buf encodes the public key in |key| to an allocated octet string
  153. // and sets |*out_buf| to point to it. It returns the length of the encoded
  154. // octet string or zero if an error occurred.
  155. OPENSSL_EXPORT size_t EC_KEY_key2buf(const EC_KEY *key,
  156. point_conversion_form_t form,
  157. unsigned char **out_buf, BN_CTX *ctx);
  158. // Key generation.
  159. // EC_KEY_generate_key generates a random, private key, calculates the
  160. // corresponding public key and stores both in |key|. It returns one on success
  161. // or zero otherwise.
  162. OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key);
  163. // EC_KEY_generate_key_fips behaves like |EC_KEY_generate_key| but performs
  164. // additional checks for FIPS compliance. This function is applicable when
  165. // generating keys for either signing/verification or key agreement because
  166. // both types of consistency check (PCT) are performed.
  167. OPENSSL_EXPORT int EC_KEY_generate_key_fips(EC_KEY *key);
  168. // EC_KEY_derive_from_secret deterministically derives a private key for |group|
  169. // from an input secret using HKDF-SHA256. It returns a newly-allocated |EC_KEY|
  170. // on success or NULL on error. |secret| must not be used in any other
  171. // algorithm. If using a base secret for multiple operations, derive separate
  172. // values with a KDF such as HKDF first.
  173. //
  174. // Note this function implements an arbitrary derivation scheme, rather than any
  175. // particular standard one. New protocols are recommended to use X25519 and
  176. // Ed25519, which have standard byte import functions. See
  177. // |X25519_public_from_private| and |ED25519_keypair_from_seed|.
  178. OPENSSL_EXPORT EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group,
  179. const uint8_t *secret,
  180. size_t secret_len);
  181. // Serialisation.
  182. // EC_KEY_parse_private_key parses a DER-encoded ECPrivateKey structure (RFC
  183. // 5915) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_KEY| or
  184. // NULL on error. If |group| is non-null, the parameters field of the
  185. // ECPrivateKey may be omitted (but must match |group| if present). Otherwise,
  186. // the parameters field is required.
  187. OPENSSL_EXPORT EC_KEY *EC_KEY_parse_private_key(CBS *cbs,
  188. const EC_GROUP *group);
  189. // EC_KEY_marshal_private_key marshals |key| as a DER-encoded ECPrivateKey
  190. // structure (RFC 5915) and appends the result to |cbb|. It returns one on
  191. // success and zero on failure. |enc_flags| is a combination of |EC_PKEY_*|
  192. // values and controls whether corresponding fields are omitted.
  193. OPENSSL_EXPORT int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
  194. unsigned enc_flags);
  195. // EC_KEY_parse_curve_name parses a DER-encoded OBJECT IDENTIFIER as a curve
  196. // name from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP|
  197. // or NULL on error.
  198. OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs);
  199. // EC_KEY_marshal_curve_name marshals |group| as a DER-encoded OBJECT IDENTIFIER
  200. // and appends the result to |cbb|. It returns one on success and zero on
  201. // failure.
  202. OPENSSL_EXPORT int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group);
  203. // EC_KEY_parse_parameters parses a DER-encoded ECParameters structure (RFC
  204. // 5480) from |cbs| and advances |cbs|. It returns a newly-allocated |EC_GROUP|
  205. // or NULL on error. It supports the namedCurve and specifiedCurve options, but
  206. // use of specifiedCurve is deprecated. Use |EC_KEY_parse_curve_name|
  207. // instead.
  208. OPENSSL_EXPORT EC_GROUP *EC_KEY_parse_parameters(CBS *cbs);
  209. // ex_data functions.
  210. //
  211. // These functions are wrappers. See |ex_data.h| for details.
  212. OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp,
  213. CRYPTO_EX_unused *unused,
  214. CRYPTO_EX_dup *dup_unused,
  215. CRYPTO_EX_free *free_func);
  216. OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg);
  217. OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx);
  218. // ECDSA method.
  219. // ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key
  220. // material. This may be set if, for instance, it is wrapping some other crypto
  221. // API, like a platform key store.
  222. #define ECDSA_FLAG_OPAQUE 1
  223. // ecdsa_method_st is a structure of function pointers for implementing ECDSA.
  224. // See engine.h.
  225. struct ecdsa_method_st {
  226. struct openssl_method_common_st common;
  227. void *app_data;
  228. int (*init)(EC_KEY *key);
  229. int (*finish)(EC_KEY *key);
  230. // group_order_size returns the number of bytes needed to represent the order
  231. // of the group. This is used to calculate the maximum size of an ECDSA
  232. // signature in |ECDSA_size|.
  233. size_t (*group_order_size)(const EC_KEY *key);
  234. // sign matches the arguments and behaviour of |ECDSA_sign|.
  235. int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig,
  236. unsigned int *sig_len, EC_KEY *eckey);
  237. int flags;
  238. };
  239. // Deprecated functions.
  240. // EC_KEY_set_asn1_flag does nothing.
  241. OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag);
  242. // d2i_ECPrivateKey parses a DER-encoded ECPrivateKey structure (RFC 5915) from
  243. // |len| bytes at |*inp|, as described in |d2i_SAMPLE|. On input, if |*out_key|
  244. // is non-NULL and has a group configured, the parameters field may be omitted
  245. // but must match that group if present.
  246. //
  247. // Use |EC_KEY_parse_private_key| instead.
  248. OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp,
  249. long len);
  250. // i2d_ECPrivateKey marshals |key| as a DER-encoded ECPrivateKey structure (RFC
  251. // 5915), as described in |i2d_SAMPLE|.
  252. //
  253. // Use |EC_KEY_marshal_private_key| instead.
  254. OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp);
  255. // d2i_ECParameters parses a DER-encoded ECParameters structure (RFC 5480) from
  256. // |len| bytes at |*inp|, as described in |d2i_SAMPLE|.
  257. //
  258. // Use |EC_KEY_parse_parameters| or |EC_KEY_parse_curve_name| instead.
  259. OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp,
  260. long len);
  261. // i2d_ECParameters marshals |key|'s parameters as a DER-encoded OBJECT
  262. // IDENTIFIER, as described in |i2d_SAMPLE|.
  263. //
  264. // Use |EC_KEY_marshal_curve_name| instead.
  265. OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp);
  266. // o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into
  267. // |*out_key|. Note that this differs from the d2i format in that |*out_key|
  268. // must be non-NULL with a group set. On successful exit, |*inp| is advanced by
  269. // |len| bytes. It returns |*out_key| or NULL on error.
  270. //
  271. // Use |EC_POINT_oct2point| instead.
  272. OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp,
  273. long len);
  274. // i2o_ECPublicKey marshals an EC point from |key|, as described in
  275. // |i2d_SAMPLE|.
  276. //
  277. // Use |EC_POINT_point2cbb| instead.
  278. OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp);
  279. #if defined(__cplusplus)
  280. } // extern C
  281. extern "C++" {
  282. BSSL_NAMESPACE_BEGIN
  283. BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free)
  284. BORINGSSL_MAKE_UP_REF(EC_KEY, EC_KEY_up_ref)
  285. BSSL_NAMESPACE_END
  286. } // extern C++
  287. #endif
  288. #endif // OPENSSL_HEADER_EC_KEY_H