lax_der_privatekey_parsing.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**********************************************************************
  2. * Copyright (c) 2014, 2015 Pieter Wuille *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. /****
  7. * Please do not link this file directly. It is not part of the libsecp256k1
  8. * project and does not promise any stability in its API, functionality or
  9. * presence. Projects which use this code should instead copy this header
  10. * and its accompanying .c file directly into their codebase.
  11. ****/
  12. /* This file contains code snippets that parse DER private keys with
  13. * various errors and violations. This is not a part of the library
  14. * itself, because the allowed violations are chosen arbitrarily and
  15. * do not follow or establish any standard.
  16. *
  17. * It also contains code to serialize private keys in a compatible
  18. * manner.
  19. *
  20. * These functions are meant for compatibility with applications
  21. * that require BER encoded keys. When working with secp256k1-specific
  22. * code, the simple 32-byte private keys normally used by the
  23. * library are sufficient.
  24. */
  25. #ifndef _SECP256K1_CONTRIB_BER_PRIVATEKEY_H_
  26. #define _SECP256K1_CONTRIB_BER_PRIVATEKEY_H_
  27. #include <secp256k1.h>
  28. # ifdef __cplusplus
  29. extern "C" {
  30. # endif
  31. /** Export a private key in DER format.
  32. *
  33. * Returns: 1 if the private key was valid.
  34. * Args: ctx: pointer to a context object, initialized for signing (cannot
  35. * be NULL)
  36. * Out: privkey: pointer to an array for storing the private key in BER.
  37. * Should have space for 279 bytes, and cannot be NULL.
  38. * privkeylen: Pointer to an int where the length of the private key in
  39. * privkey will be stored.
  40. * In: seckey: pointer to a 32-byte secret key to export.
  41. * compressed: 1 if the key should be exported in
  42. * compressed format, 0 otherwise
  43. *
  44. * This function is purely meant for compatibility with applications that
  45. * require BER encoded keys. When working with secp256k1-specific code, the
  46. * simple 32-byte private keys are sufficient.
  47. *
  48. * Note that this function does not guarantee correct DER output. It is
  49. * guaranteed to be parsable by secp256k1_ec_privkey_import_der
  50. */
  51. SECP256K1_WARN_UNUSED_RESULT int ec_privkey_export_der(
  52. const secp256k1_context* ctx,
  53. unsigned char *privkey,
  54. size_t *privkeylen,
  55. const unsigned char *seckey,
  56. int compressed
  57. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
  58. /** Import a private key in DER format.
  59. * Returns: 1 if a private key was extracted.
  60. * Args: ctx: pointer to a context object (cannot be NULL).
  61. * Out: seckey: pointer to a 32-byte array for storing the private key.
  62. * (cannot be NULL).
  63. * In: privkey: pointer to a private key in DER format (cannot be NULL).
  64. * privkeylen: length of the DER private key pointed to be privkey.
  65. *
  66. * This function will accept more than just strict DER, and even allow some BER
  67. * violations. The public key stored inside the DER-encoded private key is not
  68. * verified for correctness, nor are the curve parameters. Use this function
  69. * only if you know in advance it is supposed to contain a secp256k1 private
  70. * key.
  71. */
  72. SECP256K1_WARN_UNUSED_RESULT int ec_privkey_import_der(
  73. const secp256k1_context* ctx,
  74. unsigned char *seckey,
  75. const unsigned char *privkey,
  76. size_t privkeylen
  77. ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif