MCPcopy Create free account
hub / github.com/ElementsProject/elements / ec_seckey_import_der

Function ec_seckey_import_der

src/key.cpp:38–83  ·  view source on GitHub ↗

* This parses a format loosely based on a DER encoding of the ECPrivateKey type from * section C.4 of SEC 1 , with the following caveats: * * * The octet-length of the SEQUENCE must be encoded as 1 or 2 octets. It is not * required to be encoded as one octet if it is less than 256, as DER would require. * * The octet-length of the SEQUENCE must not be great

Source from the content-addressed store, hash-verified

36 * out32 must point to an output buffer of length at least 32 bytes.
37 */
38int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
39 const unsigned char *end = seckey + seckeylen;
40 memset(out32, 0, 32);
41 /* sequence header */
42 if (end - seckey < 1 || *seckey != 0x30u) {
43 return 0;
44 }
45 seckey++;
46 /* sequence length constructor */
47 if (end - seckey < 1 || !(*seckey & 0x80u)) {
48 return 0;
49 }
50 ptrdiff_t lenb = *seckey & ~0x80u; seckey++;
51 if (lenb < 1 || lenb > 2) {
52 return 0;
53 }
54 if (end - seckey < lenb) {
55 return 0;
56 }
57 /* sequence length */
58 ptrdiff_t len = seckey[lenb-1] | (lenb > 1 ? seckey[lenb-2] << 8 : 0u);
59 seckey += lenb;
60 if (end - seckey < len) {
61 return 0;
62 }
63 /* sequence element 0: version number (=1) */
64 if (end - seckey < 3 || seckey[0] != 0x02u || seckey[1] != 0x01u || seckey[2] != 0x01u) {
65 return 0;
66 }
67 seckey += 3;
68 /* sequence element 1: octet string, up to 32 bytes */
69 if (end - seckey < 2 || seckey[0] != 0x04u) {
70 return 0;
71 }
72 ptrdiff_t oslen = seckey[1];
73 seckey += 2;
74 if (oslen > 32 || end - seckey < oslen) {
75 return 0;
76 }
77 memcpy(out32 + (32 - oslen), seckey, oslen);
78 if (!secp256k1_ec_seckey_verify(ctx, out32)) {
79 memset(out32, 0, 32);
80 return 0;
81 }
82 return 1;
83}
84
85/**
86 * This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1

Callers 2

LoadMethod · 0.85
FUZZ_TARGETFunction · 0.85

Calls 1

Tested by 1

FUZZ_TARGETFunction · 0.68