Skip to content

Commit 809f4ac

Browse files
committed
Dynafed blocks must have sighash byte
It is not included in the signature hash. The sighash byte MUST be SIGHASH_ALL to avoid malleability.
1 parent 690fed6 commit 809f4ac

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

src/block_proof.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ bool CheckChallenge(const CBlockHeader& block, const CBlockIndex& indexLast, con
2121

2222
static bool CheckProofGeneric(const CBlockHeader& block, const uint32_t max_block_signature_size, const CScript& challenge, const CScript& scriptSig, const CScriptWitness& witness)
2323
{
24-
// scriptSig or witness will be nonempty, but not both, so just compare both limits
24+
// Legacy blocks have empty witness, dynafed blocks have empty scriptSig
25+
bool is_dyna = !witness.stack.empty();
26+
27+
// Check signature limits for blocks
2528
if (scriptSig.size() > max_block_signature_size) {
29+
assert(!is_dyna);
2630
return false;
27-
}
28-
29-
if (witness.GetSerializedSize() > max_block_signature_size) {
31+
} else if (witness.GetSerializedSize() > max_block_signature_size) {
32+
assert(is_dyna);
3033
return false;
3134
}
3235

@@ -40,7 +43,7 @@ static bool CheckProofGeneric(const CBlockHeader& block, const uint32_t max_bloc
4043
| SCRIPT_VERIFY_SIGPUSHONLY // Witness is push-only
4144
| SCRIPT_VERIFY_LOW_S // Stop easiest signature fiddling
4245
| SCRIPT_VERIFY_WITNESS // Witness and to enforce cleanstack
43-
| SCRIPT_NO_SIGHASH_BYTE; // non-Check(Multi)Sig signatures will not have sighash byte
46+
| (is_dyna ? 0 : SCRIPT_NO_SIGHASH_BYTE); // Non-dynafed blocks do not have sighash byte
4447
return GenericVerifyScript(scriptSig, witness, challenge, proof_flags, block);
4548
}
4649

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class CHashVerifier : public CHashWriter
191191
}
192192
};
193193

194-
/** Compute the 256-bit hash of an object's serialization. */
194+
/** Compute the 256-bit hash of an object's serialization, with optional sighash byte. */
195195
template<typename T>
196196
uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
197197
{

src/rpc/mining.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,11 +1134,13 @@ UniValue combineblocksigs(const JSONRPCRequest& request)
11341134
if (!DecodeHexBlk(block, request.params[0].get_str()))
11351135
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
11361136

1137+
bool is_dynafed = !block.m_dynafed_params.IsNull();
1138+
11371139
const Consensus::Params& params = Params().GetConsensus();
11381140
const UniValue& sigs = request.params[1].get_array();
11391141
CBasicKeyStore keystore;
11401142
SignatureData sig_data;
1141-
SimpleSignatureCreator signature_creator(block.GetHash());
1143+
SimpleSignatureCreator signature_creator(block.GetHash(), is_dynafed ? SIGHASH_ALL : 0);
11421144
for (unsigned int i = 0; i < sigs.size(); i++) {
11431145
UniValue pubkey_sig = sigs[i];
11441146
const std::string& pubkey_str = pubkey_sig["pubkey"].get_str();
@@ -1155,7 +1157,7 @@ UniValue combineblocksigs(const JSONRPCRequest& request)
11551157
sig_data.signatures[pubkey.GetID()] = std::make_pair(pubkey, sig_bytes);
11561158
}
11571159

1158-
if (!block.m_dynafed_params.IsNull()) {
1160+
if (is_dynafed) {
11591161
if (request.params[2].isNull()) {
11601162
throw JSONRPCError(RPC_INVALID_PARAMETER, "Signing dynamic blocks requires the witnessScript argument");
11611163
}
@@ -1164,7 +1166,7 @@ UniValue combineblocksigs(const JSONRPCRequest& request)
11641166
keystore.AddCScript(CScript(witness_bytes.begin(), witness_bytes.end()));
11651167
}
11661168
// Finalizes the signatures, has no access to keys
1167-
ProduceSignature(keystore, signature_creator, block.m_dynafed_params.m_current.m_signblockscript, sig_data, SCRIPT_NO_SIGHASH_BYTE);
1169+
ProduceSignature(keystore, signature_creator, block.m_dynafed_params.m_current.m_signblockscript, sig_data, SCRIPT_VERIFY_NONE);
11681170
block.m_signblock_witness = sig_data.scriptWitness;
11691171
} else {
11701172
// Finalizes the signatures, has no access to keys

src/script/generic.hpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,68 @@ class SimpleSignatureChecker : public BaseSignatureChecker
1515
{
1616
public:
1717
uint256 hash;
18+
bool sighash_byte;
1819

19-
SimpleSignatureChecker(const uint256& hashIn) : hash(hashIn) {};
20+
SimpleSignatureChecker(const uint256& hashIn, bool sighash_byte_in) : hash(hashIn), sighash_byte(sighash_byte_in) {};
2021
bool CheckSig(const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
2122
{
23+
std::vector<unsigned char> vchSigCopy(vchSig);
2224
CPubKey pubkey(vchPubKey);
2325
if (!pubkey.IsValid())
2426
return false;
2527
if (vchSig.empty())
2628
return false;
29+
// Get rid of sighash byte before verifying hash
30+
// Note: We only accept SIGHASH_ALL!
31+
if (sighash_byte) {
32+
const unsigned char popped_byte = vchSigCopy.back();
33+
vchSigCopy.pop_back();
34+
if (popped_byte != SIGHASH_ALL) {
35+
return false;
36+
}
37+
}
2738
return pubkey.Verify(hash, vchSig);
2839
}
2940
};
3041

3142
class SimpleSignatureCreator : public BaseSignatureCreator
3243
{
3344
SimpleSignatureChecker checker;
45+
bool sighash_byte;
3446

3547
public:
36-
SimpleSignatureCreator(const uint256& hashIn) : checker(hashIn) {};
48+
SimpleSignatureCreator(const uint256& hashIn, bool sighash_byte_in) : checker(hashIn, sighash_byte_in), sighash_byte(sighash_byte_in) {};
3749
const BaseSignatureChecker& Checker() const { return checker; }
3850
bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const
3951
{
4052
CKey key;
4153
if (!provider.GetKey(keyid, key))
4254
return false;
43-
return key.Sign(checker.hash, vchSig);
55+
if (!key.Sign(checker.hash, vchSig)) {
56+
return false;
57+
}
58+
// We only support sighash all for malleability reasons(it is not committed in sighash)
59+
if (sighash_byte) {
60+
vchSig.push_back(SIGHASH_ALL);
61+
}
62+
return true;
4463
}
4564
};
4665

4766
template<typename T>
4867
bool GenericVerifyScript(const CScript& scriptSig, const CScriptWitness& witness, const CScript& scriptPubKey, unsigned int flags, const T& data)
4968
{
50-
return VerifyScript(scriptSig, scriptPubKey, &witness, flags, SimpleSignatureChecker(SerializeHash(data)));
69+
bool sighash_byte = (flags & SCRIPT_NO_SIGHASH_BYTE) ? false : true;
70+
// Note: Our hash doesn't commit to the sighash byte
71+
return VerifyScript(scriptSig, scriptPubKey, &witness, flags, SimpleSignatureChecker(SerializeHash(data), sighash_byte));
5172
}
5273

5374
template<typename T>
54-
bool GenericSignScript(const CKeyStore& keystore, const T& data, const CScript& fromPubKey, SignatureData& scriptSig)
75+
bool GenericSignScript(const CKeyStore& keystore, const T& data, const CScript& fromPubKey, SignatureData& scriptSig, unsigned int additional_flags)
5576
{
56-
return ProduceSignature(keystore, SimpleSignatureCreator(SerializeHash(data)), fromPubKey, scriptSig, SCRIPT_NO_SIGHASH_BYTE);
77+
bool sighash_byte = (additional_flags & SCRIPT_NO_SIGHASH_BYTE) ? false : true;
78+
// Note: Our hash doesn't commit to the sighash byte
79+
return ProduceSignature(keystore, SimpleSignatureCreator(SerializeHash(data), sighash_byte), fromPubKey, scriptSig, additional_flags);
5780
}
5881

5982
#endif // H_BITCOIN_SCRIPT_GENERIC

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,7 +4783,7 @@ UniValue signblock(const JSONRPCRequest& request)
47834783
// Expose SignatureData internals in return value in lieu of "Partially Signed Bitcoin Blocks"
47844784
SignatureData block_sigs;
47854785
if (block.m_dynafed_params.IsNull()) {
4786-
GenericSignScript(*pwallet, block.GetBlockHeader(), block.proof.challenge, block_sigs);
4786+
GenericSignScript(*pwallet, block.GetBlockHeader(), block.proof.challenge, block_sigs, SCRIPT_NO_SIGHASH_BYTE /* additional_flags */);
47874787
} else {
47884788
if (request.params[1].isNull()) {
47894789
throw JSONRPCError(RPC_INVALID_PARAMETER, "Signing dynamic blocks requires the witnessScript argument");
@@ -4794,7 +4794,7 @@ UniValue signblock(const JSONRPCRequest& request)
47944794
if (!witness_bytes.empty()) {
47954795
pwallet->AddCScript(CScript(witness_bytes.begin(), witness_bytes.end()));
47964796
}
4797-
GenericSignScript(*pwallet, block.GetBlockHeader(), block.m_dynafed_params.m_current.m_signblockscript, block_sigs);
4797+
GenericSignScript(*pwallet, block.GetBlockHeader(), block.m_dynafed_params.m_current.m_signblockscript, block_sigs, SCRIPT_VERIFY_NONE /* additional_flags */);
47984798
}
47994799

48004800
// Error if sig data didn't "grow"

0 commit comments

Comments
 (0)