MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / ec_privkey_import_der

Function ec_privkey_import_der

src/key.cpp:35–80  ·  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 greate

Source from the content-addressed store, hash-verified

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

Callers 1

LoadMethod · 0.70

Calls 2

memcpyFunction · 0.85

Tested by

no test coverage detected