Skip to content

Commit

Permalink
contrib: rename privkey/private key to seckey/secret key
Browse files Browse the repository at this point in the history
For consistency with bitcoin-core#701.
  • Loading branch information
real-or-random committed Jul 29, 2020
1 parent 214cb3c commit d08aa3f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ noinst_HEADERS += src/field_impl.h
noinst_HEADERS += src/bench.h
noinst_HEADERS += contrib/lax_der_parsing.h
noinst_HEADERS += contrib/lax_der_parsing.c
noinst_HEADERS += contrib/lax_der_privatekey_parsing.h
noinst_HEADERS += contrib/lax_der_privatekey_parsing.c
noinst_HEADERS += contrib/lax_der_seckey_parsing.h
noinst_HEADERS += contrib/lax_der_seckey_parsing.c

if USE_EXTERNAL_ASM
COMMON_LIB = libsecp256k1_common.la
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,57 @@
#include <string.h>
#include <secp256k1.h>

#include "lax_der_privatekey_parsing.h"
#include "lax_der_seckey_parsing.h"

int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
const unsigned char *end = privkey + privkeylen;
int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
const unsigned char *end = seckey + seckeylen;
int lenb = 0;
int len = 0;
memset(out32, 0, 32);
/* sequence header */
if (end < privkey+1 || *privkey != 0x30) {
if (end < seckey+1 || *seckey != 0x30) {
return 0;
}
privkey++;
seckey++;
/* sequence length constructor */
if (end < privkey+1 || !(*privkey & 0x80)) {
if (end < seckey+1 || !(*seckey & 0x80)) {
return 0;
}
lenb = *privkey & ~0x80; privkey++;
lenb = *seckey & ~0x80; seckey++;
if (lenb < 1 || lenb > 2) {
return 0;
}
if (end < privkey+lenb) {
if (end < seckey+lenb) {
return 0;
}
/* sequence length */
len = privkey[lenb-1] | (lenb > 1 ? privkey[lenb-2] << 8 : 0);
privkey += lenb;
if (end < privkey+len) {
len = seckey[lenb-1] | (lenb > 1 ? seckey[lenb-2] << 8 : 0);
seckey += lenb;
if (end < seckey+len) {
return 0;
}
/* sequence element 0: version number (=1) */
if (end < privkey+3 || privkey[0] != 0x02 || privkey[1] != 0x01 || privkey[2] != 0x01) {
if (end < seckey+3 || seckey[0] != 0x02 || seckey[1] != 0x01 || seckey[2] != 0x01) {
return 0;
}
privkey += 3;
seckey += 3;
/* sequence element 1: octet string, up to 32 bytes */
if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
if (end < seckey+2 || seckey[0] != 0x04 || seckey[1] > 0x20 || end < seckey+2+seckey[1]) {
return 0;
}
memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
memcpy(out32 + 32 - seckey[1], seckey + 2, seckey[1]);
if (!secp256k1_ec_seckey_verify(ctx, out32)) {
memset(out32, 0, 32);
return 0;
}
return 1;
}

int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, int compressed) {
secp256k1_pubkey pubkey;
size_t pubkeylen = 0;
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
*privkeylen = 0;
*seckeylen = 0;
return 0;
}
if (compressed) {
Expand All @@ -75,14 +75,14 @@ int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey,
0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
};
unsigned char *ptr = privkey;
unsigned char *ptr = seckey;
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
memcpy(ptr, key32, 32); ptr += 32;
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
pubkeylen = 33;
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
ptr += pubkeylen;
*privkeylen = ptr - privkey;
*seckeylen = ptr - seckey;
} else {
static const unsigned char begin[] = {
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
Expand All @@ -100,14 +100,14 @@ int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey,
0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
};
unsigned char *ptr = privkey;
unsigned char *ptr = seckey;
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
memcpy(ptr, key32, 32); ptr += 32;
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
pubkeylen = 65;
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
ptr += pubkeylen;
*privkeylen = ptr - privkey;
*seckeylen = ptr - seckey;
}
return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,79 @@
* and its accompanying .c file directly into their codebase.
****/

/* This file contains code snippets that parse DER private keys with
/* This file contains code snippets that parse DER secret keys with
* various errors and violations. This is not a part of the library
* itself, because the allowed violations are chosen arbitrarily and
* do not follow or establish any standard.
*
* It also contains code to serialize private keys in a compatible
* It also contains code to serialize secret keys in a compatible
* manner.
*
* These functions are meant for compatibility with applications
* that require BER encoded keys. When working with secp256k1-specific
* code, the simple 32-byte private keys normally used by the
* code, the simple 32-byte secret keys normally used by the
* library are sufficient.
*/

#ifndef SECP256K1_CONTRIB_BER_PRIVATEKEY_H
#define SECP256K1_CONTRIB_BER_PRIVATEKEY_H
#ifndef SECP256K1_CONTRIB_DER_SECKEY_H
#define SECP256K1_CONTRIB_DER_SECKEY_H

#include <secp256k1.h>

#ifdef __cplusplus
extern "C" {
#endif

/** Export a private key in DER format.
/** Export a secret key in DER format.
*
* Returns: 1 if the private key was valid.
* Args: ctx: pointer to a context object, initialized for signing (cannot
* be NULL)
* Out: privkey: pointer to an array for storing the private key in BER.
* Should have space for 279 bytes, and cannot be NULL.
* privkeylen: Pointer to an int where the length of the private key in
* privkey will be stored.
* In: seckey: pointer to a 32-byte secret key to export.
* compressed: 1 if the key should be exported in
* compressed format, 0 otherwise
* Returns: 1 if the secret key was valid.
* Args: ctx: pointer to a context object, initialized for signing
* (cannot be NULL).
* Out: seckeyder: pointer to an array for storing the secret key in BER
* (should have space for 279 bytes, and cannot be NULL).
* seckeyderlen: pointer to an int where the length of the secret key in
* seckeyder will be stored.
* In: seckey32: pointer to a 32-byte secret key to export.
* compressed: 1 if the key should be exported in compressed format,
* 0 otherwise.
*
* This function is purely meant for compatibility with applications that
* require BER encoded keys. When working with secp256k1-specific code, the
* simple 32-byte private keys are sufficient.
* simple 32-byte secret keys are sufficient.
*
* Note that this function does not guarantee correct DER output. It is
* guaranteed to be parsable by secp256k1_ec_privkey_import_der
* guaranteed to be parsable by secp256k1_ec_seckey_import_der.
*/
SECP256K1_WARN_UNUSED_RESULT int ec_privkey_export_der(
SECP256K1_WARN_UNUSED_RESULT int ec_seckey_export_der(
const secp256k1_context* ctx,
unsigned char *privkey,
size_t *privkeylen,
const unsigned char *seckey,
unsigned char *seckey,
size_t *seckeylen,
const unsigned char *seckey32,
int compressed
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);

/** Import a private key in DER format.
* Returns: 1 if a private key was extracted.
* Args: ctx: pointer to a context object (cannot be NULL).
* Out: seckey: pointer to a 32-byte array for storing the private key.
* (cannot be NULL).
* In: privkey: pointer to a private key in DER format (cannot be NULL).
* privkeylen: length of the DER private key pointed to be privkey.
/** Import a secret key in DER format.
* Returns: 1 if a key was extracted.
* Args: ctx: pointer to a context object (cannot be NULL).
* Out: seckey32: pointer to a 32-byte array for storing the secret key
* (cannot be NULL).
* In: seckeyder: pointer to a secret key in DER format (cannot be NULL).
* seckeyderlen: length of the DER secret key pointed to be seckey.
*
* This function will accept more than just strict DER, and even allow some BER
* violations. The public key stored inside the DER-encoded private key is not
* violations. The public key stored inside the DER-encoded secret key is not
* verified for correctness, nor are the curve parameters. Use this function
* only if you know in advance it is supposed to contain a secp256k1 private
* key.
* only if you know in advance it is supposed to contain a secp256k1 secret key.
*/
SECP256K1_WARN_UNUSED_RESULT int ec_privkey_import_der(
SECP256K1_WARN_UNUSED_RESULT int ec_seckey_import_der(
const secp256k1_context* ctx,
unsigned char *seckey,
const unsigned char *privkey,
size_t privkeylen
const unsigned char *seckeyder,
size_t seckeyderlen
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);

#ifdef __cplusplus
}
#endif

#endif /* SECP256K1_CONTRIB_BER_PRIVATEKEY_H */
#endif /* SECP256K1_CONTRIB_DER_SECKEY_H */
12 changes: 6 additions & 6 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
#endif

#include "contrib/lax_der_parsing.c"
#include "contrib/lax_der_privatekey_parsing.c"
#include "contrib/lax_der_seckey_parsing.c"

static int count = 64;
static secp256k1_context *ctx = NULL;
Expand Down Expand Up @@ -4358,8 +4358,8 @@ void test_ecdsa_end_to_end(void) {
CHECK(memcmp(&pubkey_tmp, &pubkey, sizeof(pubkey)) == 0);

/* Verify private key import and export. */
CHECK(ec_privkey_export_der(ctx, seckey, &seckeylen, privkey, secp256k1_rand_bits(1) == 1));
CHECK(ec_privkey_import_der(ctx, privkey2, seckey, seckeylen) == 1);
CHECK(ec_seckey_export_der(ctx, seckey, &seckeylen, privkey, secp256k1_rand_bits(1) == 1));
CHECK(ec_seckey_import_der(ctx, privkey2, seckey, seckeylen) == 1);
CHECK(memcmp(privkey, privkey2, 32) == 0);

/* Optionally tweak the keys using addition. */
Expand Down Expand Up @@ -5229,9 +5229,9 @@ void test_ecdsa_edge_cases(void) {
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41,
};
size_t outlen = 300;
CHECK(!ec_privkey_export_der(ctx, privkey, &outlen, seckey, 0));
CHECK(!ec_seckey_export_der(ctx, privkey, &outlen, seckey, 0));
outlen = 300;
CHECK(!ec_privkey_export_der(ctx, privkey, &outlen, seckey, 1));
CHECK(!ec_seckey_export_der(ctx, privkey, &outlen, seckey, 1));
}
}

Expand All @@ -5246,7 +5246,7 @@ EC_KEY *get_openssl_key(const unsigned char *key32) {
const unsigned char* pbegin = privkey;
int compr = secp256k1_rand_bits(1);
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
CHECK(ec_privkey_export_der(ctx, privkey, &privkeylen, key32, compr));
CHECK(ec_seckey_export_der(ctx, privkey, &privkeylen, key32, compr));
CHECK(d2i_ECPrivateKey(&ec_key, &pbegin, privkeylen));
CHECK(EC_KEY_check_key(ec_key));
return ec_key;
Expand Down

0 comments on commit d08aa3f

Please sign in to comment.