You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* EC_KEY_new returns a fresh |EC_KEY| object or NULL on error. */
  80. OPENSSL_EXPORT EC_KEY *EC_KEY_new(void);
  81. /* EC_KEY_new_method acts the same as |EC_KEY_new|, but takes an explicit
  82. * |ENGINE|. */
  83. OPENSSL_EXPORT EC_KEY *EC_KEY_new_method(const ENGINE *engine);
  84. /* EC_KEY_new_by_curve_name returns a fresh EC_KEY for group specified by |nid|
  85. * or NULL on error. */
  86. OPENSSL_EXPORT EC_KEY *EC_KEY_new_by_curve_name(int nid);
  87. /* EC_KEY_free frees all the data owned by |key| and |key| itself. */
  88. OPENSSL_EXPORT void EC_KEY_free(EC_KEY *key);
  89. /* EC_KEY_copy sets |dst| equal to |src| and returns |dst| or NULL on error. */
  90. OPENSSL_EXPORT EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
  91. /* EC_KEY_dup returns a fresh copy of |src| or NULL on error. */
  92. OPENSSL_EXPORT EC_KEY *EC_KEY_dup(const EC_KEY *src);
  93. /* EC_KEY_up_ref increases the reference count of |key|. It returns one on
  94. * success and zero otherwise. */
  95. OPENSSL_EXPORT int EC_KEY_up_ref(EC_KEY *key);
  96. /* EC_KEY_is_opaque returns one if |key| is opaque and doesn't expose its key
  97. * material. Otherwise it return zero. */
  98. OPENSSL_EXPORT int EC_KEY_is_opaque(const EC_KEY *key);
  99. /* EC_KEY_get0_group returns a pointer to the |EC_GROUP| object inside |key|. */
  100. OPENSSL_EXPORT const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
  101. /* EC_KEY_set_group sets the |EC_GROUP| object that |key| will use to |group|.
  102. * It returns one on success and zero otherwise. */
  103. OPENSSL_EXPORT int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
  104. /* EC_KEY_get0_private_key returns a pointer to the private key inside |key|. */
  105. OPENSSL_EXPORT const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
  106. /* EC_KEY_set_private_key sets the private key of |key| to |priv|. It returns
  107. * one on success and zero otherwise. */
  108. OPENSSL_EXPORT int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
  109. /* EC_KEY_get0_public_key returns a pointer to the public key point inside
  110. * |key|. */
  111. OPENSSL_EXPORT const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
  112. /* EC_KEY_set_public_key sets the public key of |key| to |pub|, by copying it.
  113. * It returns one on success and zero otherwise. */
  114. OPENSSL_EXPORT int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
  115. #define EC_PKEY_NO_PARAMETERS 0x001
  116. #define EC_PKEY_NO_PUBKEY 0x002
  117. /* EC_KEY_get_enc_flags returns the encoding flags for |key|, which is a
  118. * bitwise-OR of |EC_PKEY_*| values. */
  119. OPENSSL_EXPORT unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
  120. /* EC_KEY_set_enc_flags sets the encoding flags for |key|, which is a
  121. * bitwise-OR of |EC_PKEY_*| values. */
  122. OPENSSL_EXPORT void EC_KEY_set_enc_flags(EC_KEY *key, unsigned flags);
  123. /* EC_KEY_get_conv_form returns the conversation form that will be used by
  124. * |key|. */
  125. OPENSSL_EXPORT point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
  126. /* EC_KEY_set_conv_form sets the conversion form to be used by |key|. */
  127. OPENSSL_EXPORT void EC_KEY_set_conv_form(EC_KEY *key,
  128. point_conversion_form_t cform);
  129. /* EC_KEY_precompute_mult precomputes multiplies of the generator of the
  130. * underlying group in order to speed up operations that calculate generator
  131. * multiples. If |ctx| is not NULL, it may be used. It returns one on success
  132. * and zero otherwise. */
  133. OPENSSL_EXPORT int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
  134. /* EC_KEY_check_key performs several checks on |key| (possibly including an
  135. * expensive check that the public key is in the primary subgroup). It returns
  136. * one if all checks pass and zero otherwise. If it returns zero then detail
  137. * about the problem can be found on the error stack. */
  138. OPENSSL_EXPORT int EC_KEY_check_key(const EC_KEY *key);
  139. /* EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to
  140. * (|x|, |y|). It returns one on success and zero otherwise. */
  141. OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
  142. BIGNUM *x,
  143. BIGNUM *y);
  144. /* Key generation. */
  145. /* EC_KEY_generate_key generates a random, private key, calculates the
  146. * corresponding public key and stores both in |key|. It returns one on success
  147. * or zero otherwise. */
  148. OPENSSL_EXPORT int EC_KEY_generate_key(EC_KEY *key);
  149. /* Serialisation. */
  150. /* d2i_ECPrivateKey parses an ASN.1, DER-encoded, private key from |len| bytes
  151. * at |*inp|. If |out_key| is not NULL then, on exit, a pointer to the result
  152. * is in |*out_key|. If |*out_key| is already non-NULL on entry then the result
  153. * is written directly into |*out_key|, otherwise a fresh |EC_KEY| is
  154. * allocated. On successful exit, |*inp| is advanced past the DER structure. It
  155. * returns the result or NULL on error. */
  156. OPENSSL_EXPORT EC_KEY *d2i_ECPrivateKey(EC_KEY **out_key, const uint8_t **inp,
  157. long len);
  158. /* i2d_ECParameters marshals an EC private key from |key| to an ASN.1, DER
  159. * structure. If |outp| is not NULL then the result is written to |*outp| and
  160. * |*outp| is advanced just past the output. It returns the number of bytes in
  161. * the result, whether written or not, or a negative value on error. */
  162. OPENSSL_EXPORT int i2d_ECPrivateKey(const EC_KEY *key, uint8_t **outp);
  163. /* d2i_ECParameters parses an ASN.1, DER-encoded, set of EC parameters from
  164. * |len| bytes at |*inp|. If |out_key| is not NULL then, on exit, a pointer to
  165. * the result is in |*out_key|. If |*out_key| is already non-NULL on entry then
  166. * the result is written directly into |*out_key|, otherwise a fresh |EC_KEY|
  167. * is allocated. On successful exit, |*inp| is advanced past the DER structure.
  168. * It returns the result or NULL on error. */
  169. OPENSSL_EXPORT EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp,
  170. long len);
  171. /* i2d_ECParameters marshals EC parameters from |key| to an ASN.1, DER
  172. * structure. If |outp| is not NULL then the result is written to |*outp| and
  173. * |*outp| is advanced just past the output. It returns the number of bytes in
  174. * the result, whether written or not, or a negative value on error. */
  175. OPENSSL_EXPORT int i2d_ECParameters(const EC_KEY *key, uint8_t **outp);
  176. /* o2i_ECPublicKey parses an EC point from |len| bytes at |*inp| into
  177. * |*out_key|. Note that this differs from the d2i format in that |*out_key|
  178. * must be non-NULL. On successful exit, |*inp| is advanced past the DER
  179. * structure. It returns |*out_key| or NULL on error. */
  180. OPENSSL_EXPORT EC_KEY *o2i_ECPublicKey(EC_KEY **out_key, const uint8_t **inp,
  181. long len);
  182. /* i2o_ECPublicKey marshals an EC point from |key|. If |outp| is not NULL then
  183. * the result is written to |*outp| and |*outp| is advanced just past the
  184. * output. It returns the number of bytes in the result, whether written or
  185. * not, or a negative value on error. */
  186. OPENSSL_EXPORT int i2o_ECPublicKey(const EC_KEY *key, unsigned char **outp);
  187. /* ex_data functions.
  188. *
  189. * These functions are wrappers. See |ex_data.h| for details. */
  190. OPENSSL_EXPORT int EC_KEY_get_ex_new_index(long argl, void *argp,
  191. CRYPTO_EX_new *new_func,
  192. CRYPTO_EX_dup *dup_func,
  193. CRYPTO_EX_free *free_func);
  194. OPENSSL_EXPORT int EC_KEY_set_ex_data(EC_KEY *r, int idx, void *arg);
  195. OPENSSL_EXPORT void *EC_KEY_get_ex_data(const EC_KEY *r, int idx);
  196. /* ECDSA method. */
  197. /* ECDSA_FLAG_OPAQUE specifies that this ECDSA_METHOD does not expose its key
  198. * material. This may be set if, for instance, it is wrapping some other crypto
  199. * API, like a platform key store. */
  200. #define ECDSA_FLAG_OPAQUE 1
  201. /* ecdsa_method_st is a structure of function pointers for implementing ECDSA.
  202. * See engine.h. */
  203. struct ecdsa_method_st {
  204. struct openssl_method_common_st common;
  205. void *app_data;
  206. int (*init)(EC_KEY *key);
  207. int (*finish)(EC_KEY *key);
  208. /* group_order_size returns the number of bytes needed to represent the order
  209. * of the group. This is used to calculate the maximum size of an ECDSA
  210. * signature in |ECDSA_size|. */
  211. size_t (*group_order_size)(const EC_KEY *key);
  212. /* sign matches the arguments and behaviour of |ECDSA_sign|. */
  213. int (*sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig,
  214. unsigned int *sig_len, EC_KEY *eckey);
  215. /* verify matches the arguments and behaviour of |ECDSA_verify|. */
  216. int (*verify)(const uint8_t *digest, size_t digest_len, const uint8_t *sig,
  217. size_t sig_len, EC_KEY *eckey);
  218. int flags;
  219. };
  220. /* Deprecated functions. */
  221. /* EC_KEY_set_asn1_flag does nothing. */
  222. OPENSSL_EXPORT void EC_KEY_set_asn1_flag(EC_KEY *key, int flag);
  223. #if defined(__cplusplus)
  224. } /* extern C */
  225. #endif
  226. #endif /* OPENSSL_HEADER_EC_KEY_H */