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 **********************************************************************/
8 * Please do not link this file directly. It is not part of the libsecp256k1
9 * project and does not promise any stability in its API, functionality or
10 * presence. Projects which use this code should instead copy this header
11 * and its accompanying .c file directly into their codebase.
14 /* This file contains code snippets that parse DER private keys with
15 * various errors and violations. This is not a part of the library
16 * itself, because the allowed violations are chosen arbitrarily and
17 * do not follow or establish any standard.
19 * It also contains code to serialize private keys in a compatible
22 * These functions are meant for compatibility with applications
23 * that require BER encoded keys. When working with secp256k1-specific
24 * code, the simple 32-byte private keys normally used by the
25 * library are sufficient.
28 #ifndef SECP256K1_CONTRIB_BER_PRIVATEKEY_H
29 #define SECP256K1_CONTRIB_BER_PRIVATEKEY_H
31 #include <secp256k1.h>
37 /** Export a private key in DER format.
39 * Returns: 1 if the private key was valid.
40 * Args: ctx: pointer to a context object, initialized for signing (cannot
42 * Out: privkey: pointer to an array for storing the private key in BER.
43 * Should have space for 279 bytes, and cannot be NULL.
44 * privkeylen: Pointer to an int where the length of the private key in
45 * privkey will be stored.
46 * In: seckey: pointer to a 32-byte secret key to export.
47 * compressed: 1 if the key should be exported in
48 * compressed format, 0 otherwise
50 * This function is purely meant for compatibility with applications that
51 * require BER encoded keys. When working with secp256k1-specific code, the
52 * simple 32-byte private keys are sufficient.
54 * Note that this function does not guarantee correct DER output. It is
55 * guaranteed to be parsable by secp256k1_ec_privkey_import_der
57 SECP256K1_WARN_UNUSED_RESULT
int ec_privkey_export_der(
58 const secp256k1_context
* ctx
,
59 unsigned char *privkey
,
61 const unsigned char *seckey
,
63 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
65 /** Import a private key in DER format.
66 * Returns: 1 if a private key was extracted.
67 * Args: ctx: pointer to a context object (cannot be NULL).
68 * Out: seckey: pointer to a 32-byte array for storing the private key.
70 * In: privkey: pointer to a private key in DER format (cannot be NULL).
71 * privkeylen: length of the DER private key pointed to be privkey.
73 * This function will accept more than just strict DER, and even allow some BER
74 * violations. The public key stored inside the DER-encoded private key is not
75 * verified for correctness, nor are the curve parameters. Use this function
76 * only if you know in advance it is supposed to contain a secp256k1 private
79 SECP256K1_WARN_UNUSED_RESULT
int ec_privkey_import_der(
80 const secp256k1_context
* ctx
,
81 unsigned char *seckey
,
82 const unsigned char *privkey
,
84 ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
90 #endif /* SECP256K1_CONTRIB_BER_PRIVATEKEY_H */