Fix bip340_algo missing space for null-terminator in C++

This commit is contained in:
doyle 2022-01-24 22:38:00 +11:00
parent 2efe69b68e
commit e7fe52f1b6
4 changed files with 20 additions and 10 deletions

View File

@ -36753,7 +36753,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *
/* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
* by using the correct tagged hash function. */ * by using the correct tagged hash function. */
static const unsigned char bip340_algo[13] = "BIP0340/nonce"; static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce";
static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC;
@ -36789,7 +36789,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
/* Tag the hash with algo which is important to avoid nonce reuse across /* Tag the hash with algo which is important to avoid nonce reuse across
* algorithms. If this nonce function is used in BIP-340 signing as defined * algorithms. If this nonce function is used in BIP-340 signing as defined
* in the spec, an optimized tagging implementation is used. */ * in the spec, an optimized tagging implementation is used. */
if (algolen == sizeof(bip340_algo) if (algolen == (sizeof(bip340_algo) - 1)
&& secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
secp256k1_nonce_function_bip340_sha256_tagged(&sha); secp256k1_nonce_function_bip340_sha256_tagged(&sha);
} else { } else {
@ -36869,7 +36869,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi
secp256k1_scalar_get_b32(seckey, &sk); secp256k1_scalar_get_b32(seckey, &sk);
secp256k1_fe_get_b32(pk_buf, &pk.x); secp256k1_fe_get_b32(pk_buf, &pk.x);
ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata);
secp256k1_scalar_set_b32(&k, buf, NULL); secp256k1_scalar_set_b32(&k, buf, NULL);
ret &= !secp256k1_scalar_is_zero(&k); ret &= !secp256k1_scalar_is_zero(&k);
secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret);

View File

@ -36981,7 +36981,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *
/* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
* by using the correct tagged hash function. */ * by using the correct tagged hash function. */
static const unsigned char bip340_algo[13] = "BIP0340/nonce"; static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce";
static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC;
@ -37017,7 +37017,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
/* Tag the hash with algo which is important to avoid nonce reuse across /* Tag the hash with algo which is important to avoid nonce reuse across
* algorithms. If this nonce function is used in BIP-340 signing as defined * algorithms. If this nonce function is used in BIP-340 signing as defined
* in the spec, an optimized tagging implementation is used. */ * in the spec, an optimized tagging implementation is used. */
if (algolen == sizeof(bip340_algo) if (algolen == (sizeof(bip340_algo) - 1)
&& secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
secp256k1_nonce_function_bip340_sha256_tagged(&sha); secp256k1_nonce_function_bip340_sha256_tagged(&sha);
} else { } else {
@ -37097,7 +37097,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi
secp256k1_scalar_get_b32(seckey, &sk); secp256k1_scalar_get_b32(seckey, &sk);
secp256k1_fe_get_b32(pk_buf, &pk.x); secp256k1_fe_get_b32(pk_buf, &pk.x);
ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata);
secp256k1_scalar_set_b32(&k, buf, NULL); secp256k1_scalar_set_b32(&k, buf, NULL);
ret &= !secp256k1_scalar_is_zero(&k); ret &= !secp256k1_scalar_is_zero(&k);
secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret);

View File

@ -36370,7 +36370,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *
/* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
* by using the correct tagged hash function. */ * by using the correct tagged hash function. */
static const unsigned char bip340_algo[13] = "BIP0340/nonce"; static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce";
static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC;
@ -36406,7 +36406,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
/* Tag the hash with algo which is important to avoid nonce reuse across /* Tag the hash with algo which is important to avoid nonce reuse across
* algorithms. If this nonce function is used in BIP-340 signing as defined * algorithms. If this nonce function is used in BIP-340 signing as defined
* in the spec, an optimized tagging implementation is used. */ * in the spec, an optimized tagging implementation is used. */
if (algolen == sizeof(bip340_algo) if (algolen == (sizeof(bip340_algo) - 1)
&& secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
secp256k1_nonce_function_bip340_sha256_tagged(&sha); secp256k1_nonce_function_bip340_sha256_tagged(&sha);
} else { } else {
@ -36486,7 +36486,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi
secp256k1_scalar_get_b32(seckey, &sk); secp256k1_scalar_get_b32(seckey, &sk);
secp256k1_fe_get_b32(pk_buf, &pk.x); secp256k1_fe_get_b32(pk_buf, &pk.x);
ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata);
secp256k1_scalar_set_b32(&k, buf, NULL); secp256k1_scalar_set_b32(&k, buf, NULL);
ret &= !secp256k1_scalar_is_zero(&k); ret &= !secp256k1_scalar_is_zero(&k);
secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret);

View File

@ -369,6 +369,16 @@ int main(int argc, char *argv[])
file.buffer = StringReplace(file.buffer, file.buffer = StringReplace(file.buffer,
STRING("secp256k1_sha256_write(&sha, data, 32)"), STRING("secp256k1_sha256_write(&sha, data, 32)"),
STRING("secp256k1_sha256_write(&sha, (const unsigned char *)data, 32)")); STRING("secp256k1_sha256_write(&sha, (const unsigned char *)data, 32)"));
// NOTE: C++ requires that a string array initialised by a literal has a space for the null-terminator
file.buffer = StringReplace(file.buffer,
STRING("unsigned char bip340_algo[13] ="),
STRING("unsigned char bip340_algo[13 + 1] ="));
// NOTE: Code that relied on sizeof(bip340_algo) must be adjusted to account for the null-terminator now
file.buffer = StringReplace(file.buffer,
STRING("sizeof(bip340_algo)"),
STRING("(sizeof(bip340_algo) - 1)"));
} }
} }
} }
@ -759,7 +769,7 @@ int main(int argc, char *argv[])
// NOTE: Misc patches to source code // NOTE: Misc patches to source code
{ {
// NOTE: Delete any Windows style new-lines if there were any // NOTE: Delete any Windows style new-lines if there were any
// buffer = StringReplace(buffer, STRING("\r"), STRING("")); buffer = StringReplace(buffer, STRING("\r"), STRING(""));
} }
// NOTE: Output file // NOTE: Output file