Jeff Garzik
12 years ago
12 changed files with 245 additions and 62 deletions
@ -0,0 +1,87 @@
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// Unit tests for canonical signatures
|
||||
|
||||
#include "json/json_spirit_writer_template.h" |
||||
#include <boost/test/unit_test.hpp> |
||||
#include <openssl/ecdsa.h> |
||||
|
||||
#include "key.h" |
||||
#include "script.h" |
||||
#include "util.h" |
||||
|
||||
using namespace std; |
||||
using namespace json_spirit; |
||||
|
||||
|
||||
// In script_tests.cpp
|
||||
extern Array read_json(const std::string& filename); |
||||
|
||||
BOOST_AUTO_TEST_SUITE(canonical_tests) |
||||
|
||||
// OpenSSL-based test for canonical signature (without test for hashtype byte)
|
||||
bool static IsCanonicalSignature_OpenSSL_inner(const std::vector<unsigned char>& vchSig) |
||||
{ |
||||
if (vchSig.size() == 0) |
||||
return false; |
||||
const unsigned char *input = &vchSig[0]; |
||||
ECDSA_SIG *psig = NULL; |
||||
d2i_ECDSA_SIG(&psig, &input, vchSig.size()); |
||||
if (psig == NULL) |
||||
return false; |
||||
unsigned char buf[256]; |
||||
unsigned char *pbuf = buf; |
||||
unsigned int nLen = i2d_ECDSA_SIG(psig, NULL); |
||||
if (nLen != vchSig.size()) { |
||||
ECDSA_SIG_free(psig); |
||||
return false; |
||||
} |
||||
nLen = i2d_ECDSA_SIG(psig, &pbuf); |
||||
ECDSA_SIG_free(psig); |
||||
return (memcmp(&vchSig[0], &buf[0], nLen) == 0); |
||||
} |
||||
|
||||
// OpenSSL-based test for canonical signature
|
||||
bool static IsCanonicalSignature_OpenSSL(const std::vector<unsigned char> &vchSignature) { |
||||
if (vchSignature.size() < 1) |
||||
return false; |
||||
if (vchSignature.size() > 127) |
||||
return false; |
||||
if (vchSignature[vchSignature.size() - 1] & 0x7C) |
||||
return false; |
||||
|
||||
std::vector<unsigned char> vchSig(vchSignature); |
||||
vchSig.pop_back(); |
||||
if (!IsCanonicalSignature_OpenSSL_inner(vchSig)) |
||||
return false; |
||||
return true; |
||||
} |
||||
|
||||
BOOST_AUTO_TEST_CASE(script_canon) |
||||
{ |
||||
Array tests = read_json("sig_canonical.json"); |
||||
|
||||
BOOST_FOREACH(Value &tv, tests) { |
||||
string test = tv.get_str(); |
||||
if (IsHex(test)) { |
||||
std::vector<unsigned char> sig = ParseHex(test); |
||||
BOOST_CHECK_MESSAGE(IsCanonicalSignature(sig), test); |
||||
BOOST_CHECK_MESSAGE(IsCanonicalSignature_OpenSSL(sig), test); |
||||
} |
||||
} |
||||
} |
||||
|
||||
BOOST_AUTO_TEST_CASE(script_noncanon) |
||||
{ |
||||
Array tests = read_json("sig_noncanonical.json"); |
||||
|
||||
BOOST_FOREACH(Value &tv, tests) { |
||||
string test = tv.get_str(); |
||||
if (IsHex(test)) { |
||||
std::vector<unsigned char> sig = ParseHex(test); |
||||
BOOST_CHECK_MESSAGE(!IsCanonicalSignature(sig), test); |
||||
BOOST_CHECK_MESSAGE(!IsCanonicalSignature_OpenSSL(sig), test); |
||||
} |
||||
} |
||||
} |
||||
|
||||
BOOST_AUTO_TEST_SUITE_END() |
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
[ |
||||
"300602010002010001", |
||||
"3008020200ff020200ff01", |
||||
"304402203932c892e2e550f3af8ee4ce9c215a87f9bb831dcac87b2838e2c2eaa891df0c022030b61dd36543125d56b9f9f3a1f9353189e5af33cdda8d77a5209aec03978fa001", |
||||
"30450220076045be6f9eca28ff1ec606b833d0b87e70b2a630f5e3a496b110967a40f90a0221008fffd599910eefe00bc803c688c2eca1d2ba7f6b180620eaa03488e6585db6ba01", |
||||
"3046022100876045be6f9eca28ff1ec606b833d0b87e70b2a630f5e3a496b110967a40f90a0221008fffd599910eefe00bc803c688c2eca1d2ba7f6b180620eaa03488e6585db6ba01" |
||||
] |
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
[ |
||||
"non-hex strings are ignored", |
||||
|
||||
"too short:", "30050201FF020001", |
||||
"too long:", "30470221005990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba6105022200002d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"hashtype:", "304402205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed11", |
||||
"type:", "314402205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"total length:", "304502205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"S len oob:", "301F01205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb101", |
||||
"R+S:", "304502205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed0001", |
||||
|
||||
"R type:", "304401205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"R len = 0:", "3024020002202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"R<0:", "304402208990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"R padded:", "30450221005990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610502202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
|
||||
|
||||
"S type:", "304402205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba610501202d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"S len = 0:", "302402205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba6105020001", |
||||
"S<0:", "304402205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba61050220fd5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01", |
||||
"S padded:", "304502205990e0584b2b238e1dfaad8d6ed69ecc1a4a13ac85fc0b31d0df395eb1ba61050221002d5876262c288beb511d061691bf26777344b702b00f8fe28621fe4e566695ed01" |
||||
] |
Loading…
Reference in new issue