Add constants to default encryptor classes

pull/416/head
Jeffrey Walton 2017-05-06 09:07:24 -04:00
parent 6ad999ef2f
commit 3ea8e0655f
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
2 changed files with 70 additions and 35 deletions

View File

@ -180,6 +180,12 @@ template <class BC, class H, class MAC, class Info>
class DataEncryptorWithMAC : public ProxyFilter class DataEncryptorWithMAC : public ProxyFilter
{ {
public: public:
CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE)
CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH)
CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH)
CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE)
CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS)
//! \brief Constructs a DataEncryptorWithMAC //! \brief Constructs a DataEncryptorWithMAC
//! \param passphrase a C-String password //! \param passphrase a C-String password
//! \param attachment a BufferedTransformation to attach to this object //! \param attachment a BufferedTransformation to attach to this object
@ -220,6 +226,12 @@ template <class BC, class H, class MAC, class Info>
class DataDecryptorWithMAC : public ProxyFilter class DataDecryptorWithMAC : public ProxyFilter
{ {
public: public:
CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE)
CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH)
CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH)
CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE)
CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS)
//! \brief Constructs a DataDecryptor //! \brief Constructs a DataDecryptor
//! \param passphrase a C-String password //! \param passphrase a C-String password
//! \param attachment a BufferedTransformation to attach to this object //! \param attachment a BufferedTransformation to attach to this object

View File

@ -116,8 +116,9 @@ bool TestZinflate()
// Tamper // Tamper
try { try {
StringSource(dest.substr(0, len-2), true, new Inflator(new StringSink(rec))); StringSource(dest.substr(0, len-2), true, new Inflator(new StringSink(rec)));
throw Exception(Exception::OTHER_ERROR, "Deflate failed to detect a truncated stream"); std::cout << "Deflate failed to detect a truncated stream\n";
} catch(const Exception&) {} fail = true;
} catch(const Exception& ex) { }
} }
} }
catch(const Exception&) catch(const Exception&)
@ -236,26 +237,37 @@ bool TestDefaultEncryptorWithMAC()
if (src != rec) if (src != rec)
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed a self test"); throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed a self test");
// Tamper. Data format is [SALT][KEYCHECK][ENCRYPTED DATA]. // Tamper with the stream. Data format is [SALT][KEYCHECK][ENCRYPTED DATA].
try { try {
StringSource(dest.substr(0, len-2), true, new Inflator(new StringSink(rec))); StringSource(dest.substr(0, len-2), true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed to detect a truncated stream"); std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a truncated stream\n";
} catch(const Exception&) {} fail = true;
} catch(const Exception& ex) { }
try { try {
dest[4] ^= 0x01; // tamper salt
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[DefaultDecryptorWithMAC::SALTLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed to detect a tampered salt"); StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
} catch(const Exception&) {} std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered salt\n";
fail = true;
} catch(const Exception& ex) { }
try { try {
dest[4] ^= 0x01; dest[20] ^= 0x01; // undo previous tamper // undo previous tamper
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[DefaultDecryptorWithMAC::SALTLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed to detect a tampered keycheck"); // tamper keycheck
} catch(const Exception&) {} dest[DefaultDecryptorWithMAC::SALTLENGTH+DefaultDecryptorWithMAC::KEYLENGTH/2] ^= 0x01;
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered keycheck\n";
fail = true;
} catch(const Exception& ex) { }
try { try {
dest[20] ^= 0x01; dest[dest.length()-2] ^= 0x01; // undo previous tamper // undo previous tamper
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[DefaultDecryptorWithMAC::SALTLENGTH+DefaultDecryptorWithMAC::KEYLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "DefaultEncryptorWithMAC failed to detect a tampered data"); // tamper encrypted data
} catch(const Exception&) {} dest[dest.length()-2] ^= 0x01;
StringSource(dest, true, new DefaultDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: DefaultDecryptorWithMAC failed to detect a tampered data\n";
fail = true;
} catch(const Exception& ex) { }
} }
} }
catch(const Exception&) catch(const Exception&)
@ -335,26 +347,37 @@ bool TestLegacyEncryptorWithMAC()
if (src != rec) if (src != rec)
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed a self test"); throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed a self test");
// Tamper. Data format is [SALT][KEYCHECK][ENCRYPTED DATA]. // Tamper with the stream. Data format is [SALT][KEYCHECK][ENCRYPTED DATA].
try { try {
StringSource(dest.substr(0, len-2), true, new Inflator(new StringSink(rec))); StringSource(dest.substr(0, len-2), true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed to detect a truncated stream"); std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a truncated stream\n";
} catch(const Exception&) {} fail = true;
} catch(const Exception& ex) { }
try { try {
dest[4] ^= 0x01; // tamper salt
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[LegacyEncryptorWithMAC::SALTLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed to detect a tampered salt"); StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
} catch(const Exception&) {} std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered salt\n";
fail = true;
} catch(const Exception& ex) { }
try { try {
dest[4] ^= 0x01; dest[20] ^= 0x01; // undo previous tamper // undo previous tamper
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[LegacyEncryptorWithMAC::SALTLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed to detect a tampered keycheck"); // tamper keycheck
} catch(const Exception&) {} dest[LegacyEncryptorWithMAC::SALTLENGTH+LegacyEncryptorWithMAC::KEYLENGTH/2] ^= 0x01;
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered keycheck\n";
fail = true;
} catch(const Exception& ex) { }
try { try {
dest[20] ^= 0x01; dest[dest.length()-2] ^= 0x01; // undo previous tamper // undo previous tamper
StringSource(dest, true, new Inflator(new StringSink(rec))); dest[LegacyEncryptorWithMAC::SALTLENGTH+LegacyEncryptorWithMAC::KEYLENGTH/2] ^= 0x01;
throw Exception(Exception::OTHER_ERROR, "LegacyEncryptorWithMAC failed to detect a tampered data"); // tamper encrypted data
} catch(const Exception&) {} dest[dest.length()-2] ^= 0x01;
StringSource(dest, true, new LegacyDecryptorWithMAC(pwd.c_str(), new StringSink(rec)));
std::cout << "FAILED: LegacyEncryptorWithMAC failed to detect a tampered data\n";
fail = true;
} catch(const Exception& ex) { }
} }
} }
catch(const Exception&) catch(const Exception&)