20 static int ec_privkey_import_der(
const secp256k1_context* ctx,
unsigned char *out32,
const unsigned char *privkey,
size_t privkeylen) {
21 const unsigned char *end = privkey + privkeylen;
26 if (end < privkey+1 || *privkey != 0x30) {
31 if (end < privkey+1 || !(*privkey & 0x80)) {
34 lenb = *privkey & ~0x80; privkey++;
35 if (lenb < 1 || lenb > 2) {
38 if (end < privkey+lenb) {
42 len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
44 if (end < privkey+len) {
48 if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
53 if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
56 memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
64 static int ec_privkey_export_der(
const secp256k1_context *ctx,
unsigned char *privkey,
size_t *privkeylen,
const unsigned char *key32,
int compressed) {
72 static const unsigned char begin[] = {
73 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
75 static const unsigned char middle[] = {
76 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
77 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
78 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
79 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
80 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
81 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
82 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
83 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
84 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
86 unsigned char *ptr = privkey;
87 memcpy(ptr, begin,
sizeof(begin)); ptr +=
sizeof(begin);
88 memcpy(ptr, key32, 32); ptr += 32;
89 memcpy(ptr, middle,
sizeof(middle)); ptr +=
sizeof(middle);
93 *privkeylen = ptr - privkey;
95 static const unsigned char begin[] = {
96 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
98 static const unsigned char middle[] = {
99 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
100 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
101 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
102 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
103 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
104 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
105 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
106 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
107 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
108 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
109 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
111 unsigned char *ptr = privkey;
112 memcpy(ptr, begin,
sizeof(begin)); ptr +=
sizeof(begin);
113 memcpy(ptr, key32, 32); ptr += 32;
114 memcpy(ptr, middle,
sizeof(middle)); ptr +=
sizeof(middle);
118 *privkeylen = ptr - privkey;
144 privkey.resize(privkeylen);
156 assert(result.
size() == clen);
161 bool CKey::Sign(
const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case)
const {
166 unsigned char extra_entropy[32] = {0};
167 WriteLE32(extra_entropy, test_case);
172 vchSig.resize(nSigLen);
180 unsigned char rnd[8];
181 std::string str =
"Raven key verification\n";
184 CHash256().
Write((
unsigned char*)str.data(), str.size()).Write(rnd,
sizeof(rnd)).Finalize(hash.
begin());
185 std::vector<unsigned char> vchSig;
187 return pubkey.
Verify(hash, vchSig);
206 if (!ec_privkey_import_der(secp256k1_context_sign, (
unsigned char*)
begin(), privkey.data(), privkey.size()))
220 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
221 if ((nChild >> 31) == 0) {
223 assert(pubkey.
begin() + 33 == pubkey.
end());
239 CKeyID id = key.GetPubKey().GetID();
242 return key.Derive(out.
key, out.
chaincode, _nChild, chaincode);
246 static const unsigned char hashkey[] = {
'B',
'i',
't',
'c',
'o',
'i',
'n',
' ',
's',
'e',
'e',
'd'};
247 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
249 key.Set(vout.data(), vout.data() + 32,
true);
250 memcpy(chaincode.begin(), vout.data() + 32, 32);
253 memset(vchFingerprint, 0,
sizeof(vchFingerprint));
261 ret.
pubkey = key.GetPubKey();
268 memcpy(code+1, vchFingerprint, 4);
269 code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
270 code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
271 memcpy(code+9, chaincode.begin(), 32);
273 assert(key.size() == 32);
274 memcpy(code+42, key.begin(), 32);
279 memcpy(vchFingerprint, code+1, 4);
280 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
281 memcpy(chaincode.begin(), code+9, 32);
282 key.Set(code+42, code+BIP32_EXTKEY_SIZE,
true);
293 assert(secp256k1_context_sign ==
nullptr);
296 assert(ctx !=
nullptr);
300 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
306 secp256k1_context_sign = ctx;
311 secp256k1_context_sign =
nullptr;
void Finalize(unsigned char hash[OUTPUT_SIZE])
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
void ECC_Start()
Initialize the elliptic curve support.
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
unsigned char vchFingerprint[4]
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
bool Derive(CExtKey &out, unsigned int nChild) const
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
CPubKey GetPubKey() const
Compute the public key from a private key.
CHash256 & Write(const unsigned char *data, size_t len)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
unsigned char vchFingerprint[4]
void GetStrongRandBytes(unsigned char *out, int num)
Function to gather random data from multiple sources, failing whenever any of those source fail to pr...
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
A hasher class for Raven's 256-bit hash (double SHA-256).
const unsigned char * begin() const
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a private key by adding tweak to it.
bool fValid
Whether this private key is valid.
const unsigned char * begin() const
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
#define SECP256K1_EC_UNCOMPRESSED
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export.
const unsigned char * end() const
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
void ECC_Stop()
Deinitialize the elliptic curve support.
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a recoverable ECDSA signature.
An encapsulated public key.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Opaque data structured that holds a parsed ECDSA signature.
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
void SetSeed(const unsigned char *seed, unsigned int nSeedLen)
bool Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
CExtPubKey Neuter() const
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
const unsigned int BIP32_EXTKEY_SIZE
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
A reference to a CKey: the Hash160 of its serialized public key.
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
An encapsulated private key.
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
bool IsValid() const
Check whether this private key is valid.
Opaque data structure that holds a parsed and valid public key.
bool IsCompressed() const
Check whether this is a compressed public key.
A hasher class for HMAC-SHA-512.