Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
secp256k1_ec_seckey_import_export_der.cpp
Go to the documentation of this file.
1
// Copyright (c) 2020-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
key.h
>
6
#include <
secp256k1.h
>
7
#include <
test/fuzz/FuzzedDataProvider.h
>
8
#include <
test/fuzz/fuzz.h
>
9
#include <
test/fuzz/util.h
>
10
11
#include <cstdint>
12
#include <vector>
13
14
int
ec_seckey_import_der
(
const
secp256k1_context
* ctx,
unsigned
char
* out32,
const
unsigned
char
* seckey,
size_t
seckeylen);
15
int
ec_seckey_export_der
(
const
secp256k1_context
* ctx,
unsigned
char
* seckey,
size_t
* seckeylen,
const
unsigned
char
* key32,
bool
compressed);
16
17
FUZZ_TARGET
(secp256k1_ec_seckey_import_export_der)
18
{
19
FuzzedDataProvider
fuzzed_data_provider{buffer.data(), buffer.size()};
20
{
21
std::vector<uint8_t> out32(32);
22
(void)
ec_seckey_import_der
(
secp256k1_context_static
, out32.data(),
ConsumeFixedLengthByteVector
(fuzzed_data_provider,
CKey::SIZE
).data(),
CKey::SIZE
);
23
}
24
{
25
std::vector<uint8_t> seckey(
CKey::SIZE
);
26
const
std::vector<uint8_t> key32 =
ConsumeFixedLengthByteVector
(fuzzed_data_provider, 32);
27
size_t
seckeylen =
CKey::SIZE
;
28
const
bool
compressed = fuzzed_data_provider.
ConsumeBool
();
29
secp256k1_context
*
secp256k1_context_sign
=
secp256k1_context_create
(
SECP256K1_CONTEXT_NONE
);
30
const
bool
exported =
ec_seckey_export_der
(
secp256k1_context_sign
, seckey.data(), &seckeylen, key32.data(), compressed);
31
secp256k1_context_destroy
(
secp256k1_context_sign
);
32
if
(exported) {
33
std::vector<uint8_t> out32(32);
34
const
bool
imported =
ec_seckey_import_der
(
secp256k1_context_static
, out32.data(), seckey.data(), seckey.size()) == 1;
35
assert
(imported && key32 == out32);
36
}
37
}
38
}
FuzzedDataProvider.h
CKey::SIZE
static const unsigned int SIZE
secp256k1:
Definition
key.h:41
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
secp256k1_context_sign
static secp256k1_context * secp256k1_context_sign
Definition
key.cpp:20
key.h
secp256k1.h
secp256k1_context_destroy
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition
secp256k1.c:187
secp256k1_context
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
Definition
secp256k1.h:50
secp256k1_context_create
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition
secp256k1.c:141
SECP256K1_CONTEXT_NONE
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition
secp256k1.h:214
secp256k1_context_static
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition
secp256k1.h:245
ec_seckey_export_der
int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed)
This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1 https://www....
Definition
key.cpp:96
ec_seckey_import_der
int ec_seckey_import_der(const secp256k1_context *ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen)
These functions are taken from the libsecp256k1 distribution and are very ugly.
Definition
key.cpp:39
util.h
ConsumeFixedLengthByteVector
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition
util.h:252
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0