Skip to content

Commit c07b0af

Browse files
committed
Add signature checking flag for when no sighash byte is expected
1 parent 8f92b21 commit c07b0af

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/pow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ bool CheckProof(const CBlockHeader& block, const Consensus::Params& params)
8080
| SCRIPT_VERIFY_MINIMALDATA // Pushes are minimally-sized
8181
| SCRIPT_VERIFY_SIGPUSHONLY // Witness is push-only
8282
| SCRIPT_VERIFY_LOW_S // Stop easiest signature fiddling
83-
| SCRIPT_VERIFY_WITNESS; // Required for cleanstack eval in VerifyScript
83+
| SCRIPT_VERIFY_WITNESS // Required for cleanstack eval in VerifyScript
84+
| SCRIPT_NO_SIGHASH_BYTE; // non-Check(Multi)Sig signatures will not have sighash byte
8485
return GenericVerifyScript(block.proof.solution, block.proof.challenge, proof_flags, block);
8586
}
8687

src/script/interpreter.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,19 @@ bool CheckSignatureEncoding(const vector<unsigned char> &vchSig, unsigned int fl
223223
if (vchSig.size() == 0) {
224224
return true;
225225
}
226-
if ((flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC)) != 0 && !IsValidSignatureEncoding(vchSig)) {
226+
bool no_hash_byte = (flags & SCRIPT_NO_SIGHASH_BYTE) == SCRIPT_NO_SIGHASH_BYTE;
227+
std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size());
228+
// Push a dummy sighash byte to pass checks
229+
if (no_hash_byte) {
230+
vchSigCopy.push_back(SIGHASH_ALL);
231+
}
232+
233+
if ((flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC)) != 0 && !IsValidSignatureEncoding(vchSigCopy)) {
227234
return set_error(serror, SCRIPT_ERR_SIG_DER);
228-
} else if ((flags & SCRIPT_VERIFY_LOW_S) != 0 && !IsLowDERSignature(vchSig, serror)) {
235+
} else if ((flags & SCRIPT_VERIFY_LOW_S) != 0 && !IsLowDERSignature(vchSigCopy, serror)) {
229236
// serror is set
230237
return false;
231-
} else if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && !IsDefinedHashtypeSignature(vchSig)) {
238+
} else if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && !IsDefinedHashtypeSignature(vchSigCopy)) {
232239
return set_error(serror, SCRIPT_ERR_SIG_HASHTYPE);
233240
}
234241
return true;
@@ -1313,8 +1320,8 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
13131320
valtype& vchData = stacktop(-2);
13141321
valtype& vchPubKey = stacktop(-1);
13151322

1316-
// Sigs from stack have no hash type, so we disable strictenc check
1317-
if (!CheckSignatureEncoding(vchSig, (flags & ~SCRIPT_VERIFY_STRICTENC), serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
1323+
// Sigs from stack have no hash byte ever
1324+
if (!CheckSignatureEncoding(vchSig, (flags | SCRIPT_NO_SIGHASH_BYTE), serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
13181325
//serror is set
13191326
return false;
13201327
}

src/script/interpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ enum
107107
// Public keys in segregated witness scripts must be compressed
108108
//
109109
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE = (1U << 15),
110+
111+
// Signature checking assumes no sighash byte after the DER signature
112+
//
113+
SCRIPT_NO_SIGHASH_BYTE = (1U << 16),
110114

111115
};
112116

0 commit comments

Comments
 (0)