From 7abed14d63a27012afe54df15abe7819b63f8cfb Mon Sep 17 00:00:00 2001 From: Mouse Date: Tue, 15 Aug 2017 22:00:46 -0400 Subject: [PATCH] Make compatible with the current Crypto++ master (#163) * Make compatible with the current Crypto++ master * Add auto-config and build script. Not important, just a time-saver. * Address compatibility with Crypto++ 6.0 release, while maintaining compatibility with the older Crypto++ releases. * Polish comments in cryptopp_byte.h. Forgot to include it to RandomGeneratorThread - fixed. * Late at night - forgot to fix the .cpp files that used ::byte... * Renamed auto-config-and-run script * Added comments/description, and commented out "make check" that fails anyway * Changed the include guard to match the rest of the .h files * Delete build script * Update ChangeLog.txt * Update ChangeLog.txt --- ChangeLog.txt | 3 +++ .../compressing/CompressedBlock.h | 4 +++- .../compressing/compressors/Gzip.cpp | 11 +++++---- .../encrypted/EncryptedBlock.h | 8 ++++--- src/cpp-utils/crypto/cryptopp_byte.h | 18 +++++++++++++++ src/cpp-utils/crypto/symmetric/CFB_Cipher.h | 19 +++++++-------- src/cpp-utils/crypto/symmetric/GCM_Cipher.h | 21 +++++++++-------- src/cpp-utils/random/OSRandomGenerator.h | 3 ++- .../random/RandomGeneratorThread.cpp | 3 ++- .../EncryptedBlockStoreTest_Specific.cpp | 3 ++- .../cpp-utils/crypto/symmetric/CipherTest.cpp | 23 ++++++++++--------- .../testutils/FakeAuthenticatedCipher.h | 17 +++++++------- test/cryfs/config/CompatibilityTest.cpp | 3 ++- 13 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 src/cpp-utils/crypto/cryptopp_byte.h diff --git a/ChangeLog.txt b/ChangeLog.txt index 192784aa..c82c84b9 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,5 +1,8 @@ Version 0.9.8 (unreleased) -------------- +Compatibility: +* Works with Crypto++ 6.0 + Fixed bugs: * `du` shows correct file system size diff --git a/src/blockstore/implementations/compressing/CompressedBlock.h b/src/blockstore/implementations/compressing/CompressedBlock.h index 35f07340..e2a8bffd 100644 --- a/src/blockstore/implementations/compressing/CompressedBlock.h +++ b/src/blockstore/implementations/compressing/CompressedBlock.h @@ -2,6 +2,8 @@ #ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_COMPRESSING_COMPRESSEDBLOCK_H_ #define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_COMPRESSING_COMPRESSEDBLOCK_H_ +#include "cpp-utils/crypto/cryptopp_byte.h" + #include "../../interface/Block.h" #include "../../interface/BlockStore.h" #include @@ -57,7 +59,7 @@ boost::optional>> CompressedBlo template cpputils::unique_ref> CompressedBlock::Decompress(cpputils::unique_ref baseBlock) { - cpputils::Data decompressed = Compressor::Decompress((byte*)baseBlock->data(), baseBlock->size()); + cpputils::Data decompressed = Compressor::Decompress((CryptoPP::byte*)baseBlock->data(), baseBlock->size()); return cpputils::make_unique_ref>(std::move(baseBlock), std::move(decompressed)); } diff --git a/src/blockstore/implementations/compressing/compressors/Gzip.cpp b/src/blockstore/implementations/compressing/compressors/Gzip.cpp index 3a8f5705..f2ade682 100644 --- a/src/blockstore/implementations/compressing/compressors/Gzip.cpp +++ b/src/blockstore/implementations/compressing/compressors/Gzip.cpp @@ -1,3 +1,4 @@ +#include "cpp-utils/crypto/cryptopp_byte.h" #include "Gzip.h" #include @@ -8,22 +9,22 @@ namespace blockstore { Data Gzip::Compress(const Data &data) { CryptoPP::Gzip zipper; - zipper.Put((byte *) data.data(), data.size()); + zipper.Put((CryptoPP::byte *) data.data(), data.size()); zipper.MessageEnd(); Data compressed(zipper.MaxRetrievable()); - zipper.Get((byte *) compressed.data(), compressed.size()); + zipper.Get((CryptoPP::byte *) compressed.data(), compressed.size()); return compressed; } Data Gzip::Decompress(const void *data, size_t size) { //TODO Change interface to taking cpputils::Data objects (needs changing blockstore so we can read their "class Data", because this is called from CompressedBlock::Decompress()). CryptoPP::Gunzip zipper; - zipper.Put((byte *) data, size); + zipper.Put((CryptoPP::byte *) data, size); zipper.MessageEnd(); Data decompressed(zipper.MaxRetrievable()); - zipper.Get((byte *) decompressed.data(), decompressed.size()); + zipper.Get((CryptoPP::byte *) decompressed.data(), decompressed.size()); return decompressed; } } -} \ No newline at end of file +} diff --git a/src/blockstore/implementations/encrypted/EncryptedBlock.h b/src/blockstore/implementations/encrypted/EncryptedBlock.h index b0c447d7..b364e051 100644 --- a/src/blockstore/implementations/encrypted/EncryptedBlock.h +++ b/src/blockstore/implementations/encrypted/EncryptedBlock.h @@ -2,6 +2,8 @@ #ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_ #define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_ +#include "cpp-utils/crypto/cryptopp_byte.h" + #include "../../interface/Block.h" #include #include "../../interface/BlockStore.h" @@ -79,7 +81,7 @@ template boost::optional>> EncryptedBlock::TryCreateNew(BlockStore *baseBlockStore, const Key &key, cpputils::Data data, const typename Cipher::EncryptionKey &encKey) { //TODO Is it possible to avoid copying the whole plaintext data into plaintextWithHeader? Maybe an encrypt() object that has an .addData() function and concatenates all data for encryption? Maybe Crypto++ offers this functionality already. cpputils::Data plaintextWithHeader = _prependKeyHeaderToData(key, std::move(data)); - cpputils::Data encrypted = Cipher::encrypt((byte*)plaintextWithHeader.data(), plaintextWithHeader.size(), encKey); + cpputils::Data encrypted = Cipher::encrypt((CryptoPP::byte*)plaintextWithHeader.data(), plaintextWithHeader.size(), encKey); //TODO Avoid copying the whole encrypted block into a encryptedWithFormatHeader by creating a Data object with full size and then giving it as an encryption target to Cipher::encrypt() cpputils::Data encryptedWithFormatHeader = _prependFormatHeader(std::move(encrypted)); auto baseBlock = baseBlockStore->tryCreate(key, std::move(encryptedWithFormatHeader)); @@ -102,7 +104,7 @@ cpputils::Data EncryptedBlock::_prependFormatHeader(const cpputils::Data template boost::optional>> EncryptedBlock::TryDecrypt(cpputils::unique_ref baseBlock, const typename Cipher::EncryptionKey &encKey) { _checkFormatHeader(baseBlock->data()); - boost::optional plaintextWithHeader = Cipher::decrypt((byte*)baseBlock->data() + sizeof(FORMAT_VERSION_HEADER), baseBlock->size() - sizeof(FORMAT_VERSION_HEADER), encKey); + boost::optional plaintextWithHeader = Cipher::decrypt((CryptoPP::byte*)baseBlock->data() + sizeof(FORMAT_VERSION_HEADER), baseBlock->size() - sizeof(FORMAT_VERSION_HEADER), encKey); if(plaintextWithHeader == boost::none) { //Decryption failed (e.g. an authenticated cipher detected modifications to the ciphertext) cpputils::logging::LOG(cpputils::logging::WARN, "Decrypting block {} failed. Was the block modified by an attacker?", baseBlock->key().ToString()); @@ -186,7 +188,7 @@ void EncryptedBlock::resize(size_t newSize) { template void EncryptedBlock::_encryptToBaseBlock() { if (_dataChanged) { - cpputils::Data encrypted = Cipher::encrypt((byte*)_plaintextWithHeader.data(), _plaintextWithHeader.size(), _encKey); + cpputils::Data encrypted = Cipher::encrypt((CryptoPP::byte*)_plaintextWithHeader.data(), _plaintextWithHeader.size(), _encKey); if (_baseBlock->size() != sizeof(FORMAT_VERSION_HEADER) + encrypted.size()) { _baseBlock->resize(sizeof(FORMAT_VERSION_HEADER) + encrypted.size()); } diff --git a/src/cpp-utils/crypto/cryptopp_byte.h b/src/cpp-utils/crypto/cryptopp_byte.h new file mode 100644 index 00000000..363990e9 --- /dev/null +++ b/src/cpp-utils/crypto/cryptopp_byte.h @@ -0,0 +1,18 @@ +#pragma once +#ifndef _CPPUTILS_CRYPTO_CRYPTOPP_BYTE_H +#define _CPPUTILS_CRYPTO_CRYPTOPP_BYTE_H + +#include + +// If we're running an older CryptoPP version, CryptoPP::byte isn't defined yet. +// Define it. Refer to "byte" type in the global namespace (placed by CryptoPP). +// Could also use CRYPTOPP_NO_GLOBAL_BYTE - but don't want to track when it was +// introduced. This way seems more reliable, as it is compatible with more of +// the Crypto++ versions. +#if CRYPTOPP_VERSION < 600 +namespace CryptoPP { + using byte = ::byte; +} +#endif /* CRYPTOPP_VERSION < 600 */ + +#endif /* _CPPUTILS_CRYPTO_CRYPTOPP_BYTE_H */ diff --git a/src/cpp-utils/crypto/symmetric/CFB_Cipher.h b/src/cpp-utils/crypto/symmetric/CFB_Cipher.h index c99b50c5..3d9d5e1d 100644 --- a/src/cpp-utils/crypto/symmetric/CFB_Cipher.h +++ b/src/cpp-utils/crypto/symmetric/CFB_Cipher.h @@ -2,6 +2,7 @@ #ifndef MESSMER_CPPUTILS_CRYPTO_SYMMETRIC_CFBCIPHER_H_ #define MESSMER_CPPUTILS_CRYPTO_SYMMETRIC_CFBCIPHER_H_ +#include "cpp-utils/crypto/cryptopp_byte.h" #include "../../data/FixedSizeData.h" #include "../../data/Data.h" #include "../../random/Random.h" @@ -28,34 +29,34 @@ public: return ciphertextBlockSize - IV_SIZE; } - static Data encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey); - static boost::optional decrypt(const byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey); + static Data encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey); + static boost::optional decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey); private: static constexpr unsigned int IV_SIZE = BlockCipher::BLOCKSIZE; }; template -Data CFB_Cipher::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { +Data CFB_Cipher::encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { FixedSizeData iv = Random::PseudoRandom().getFixedSize(); auto encryption = typename CryptoPP::CFB_Mode::Encryption(encKey.data(), encKey.BINARY_LENGTH, iv.data()); Data ciphertext(ciphertextSize(plaintextSize)); std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); - encryption.ProcessData((byte*)ciphertext.data() + IV_SIZE, plaintext, plaintextSize); + encryption.ProcessData((CryptoPP::byte*)ciphertext.data() + IV_SIZE, plaintext, plaintextSize); return ciphertext; } template -boost::optional CFB_Cipher::decrypt(const byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) { +boost::optional CFB_Cipher::decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) { if (ciphertextSize < IV_SIZE) { return boost::none; } - const byte *ciphertextIV = ciphertext; - const byte *ciphertextData = ciphertext + IV_SIZE; - auto decryption = typename CryptoPP::CFB_Mode::Decryption((byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV); + const CryptoPP::byte *ciphertextIV = ciphertext; + const CryptoPP::byte *ciphertextData = ciphertext + IV_SIZE; + auto decryption = typename CryptoPP::CFB_Mode::Decryption((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV); Data plaintext(plaintextSize(ciphertextSize)); - decryption.ProcessData((byte*)plaintext.data(), ciphertextData, plaintext.size()); + decryption.ProcessData((CryptoPP::byte*)plaintext.data(), ciphertextData, plaintext.size()); return std::move(plaintext); } diff --git a/src/cpp-utils/crypto/symmetric/GCM_Cipher.h b/src/cpp-utils/crypto/symmetric/GCM_Cipher.h index 023fa536..c7bbd7a5 100644 --- a/src/cpp-utils/crypto/symmetric/GCM_Cipher.h +++ b/src/cpp-utils/crypto/symmetric/GCM_Cipher.h @@ -2,6 +2,7 @@ #ifndef MESSMER_CPPUTILS_CRYPTO_SYMMETRIC_GCMCIPHER_H_ #define MESSMER_CPPUTILS_CRYPTO_SYMMETRIC_GCMCIPHER_H_ +#include "cpp-utils/crypto/cryptopp_byte.h" #include "../../data/FixedSizeData.h" #include "../../data/Data.h" #include "../../random/Random.h" @@ -27,8 +28,8 @@ public: return ciphertextBlockSize - IV_SIZE - TAG_SIZE; } - static Data encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey); - static boost::optional decrypt(const byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey); + static Data encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey); + static boost::optional decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey); private: static constexpr unsigned int IV_SIZE = BlockCipher::BLOCKSIZE; @@ -36,7 +37,7 @@ private: }; template -Data GCM_Cipher::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { +Data GCM_Cipher::encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { FixedSizeData iv = Random::PseudoRandom().getFixedSize(); typename CryptoPP::GCM::Encryption encryption; encryption.SetKeyWithIV(encKey.data(), encKey.BINARY_LENGTH, iv.data(), IV_SIZE); @@ -45,7 +46,7 @@ Data GCM_Cipher::encrypt(const byte *plaintext, unsigned i std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); CryptoPP::ArraySource(plaintext, plaintextSize, true, new CryptoPP::AuthenticatedEncryptionFilter(encryption, - new CryptoPP::ArraySink((byte*)ciphertext.data() + IV_SIZE, ciphertext.size() - IV_SIZE), + new CryptoPP::ArraySink((CryptoPP::byte*)ciphertext.data() + IV_SIZE, ciphertext.size() - IV_SIZE), false, TAG_SIZE ) ); @@ -53,21 +54,21 @@ Data GCM_Cipher::encrypt(const byte *plaintext, unsigned i } template -boost::optional GCM_Cipher::decrypt(const byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) { +boost::optional GCM_Cipher::decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) { if (ciphertextSize < IV_SIZE + TAG_SIZE) { return boost::none; } - const byte *ciphertextIV = ciphertext; - const byte *ciphertextData = ciphertext + IV_SIZE; + const CryptoPP::byte *ciphertextIV = ciphertext; + const CryptoPP::byte *ciphertextData = ciphertext + IV_SIZE; typename CryptoPP::GCM::Decryption decryption; - decryption.SetKeyWithIV((byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV, IV_SIZE); + decryption.SetKeyWithIV((CryptoPP::byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV, IV_SIZE); Data plaintext(plaintextSize(ciphertextSize)); try { - CryptoPP::ArraySource((byte*)ciphertextData, ciphertextSize - IV_SIZE, true, + CryptoPP::ArraySource((CryptoPP::byte*)ciphertextData, ciphertextSize - IV_SIZE, true, new CryptoPP::AuthenticatedDecryptionFilter(decryption, - new CryptoPP::ArraySink((byte*)plaintext.data(), plaintext.size()), + new CryptoPP::ArraySink((CryptoPP::byte*)plaintext.data(), plaintext.size()), CryptoPP::AuthenticatedDecryptionFilter::DEFAULT_FLAGS, TAG_SIZE ) ); diff --git a/src/cpp-utils/random/OSRandomGenerator.h b/src/cpp-utils/random/OSRandomGenerator.h index 0b4c9e73..b3268fce 100644 --- a/src/cpp-utils/random/OSRandomGenerator.h +++ b/src/cpp-utils/random/OSRandomGenerator.h @@ -2,6 +2,7 @@ #ifndef MESSMER_CPPUTILS_RANDOM_OSRANDOMGENERATOR_H #define MESSMER_CPPUTILS_RANDOM_OSRANDOMGENERATOR_H +#include "cpp-utils/crypto/cryptopp_byte.h" #include "RandomGenerator.h" #include @@ -20,7 +21,7 @@ namespace cpputils { inline OSRandomGenerator::OSRandomGenerator() {} inline void OSRandomGenerator::_get(void *target, size_t bytes) { - CryptoPP::OS_GenerateRandomBlock(true, (byte*)target, bytes); + CryptoPP::OS_GenerateRandomBlock(true, (CryptoPP::byte*)target, bytes); } } diff --git a/src/cpp-utils/random/RandomGeneratorThread.cpp b/src/cpp-utils/random/RandomGeneratorThread.cpp index 45fc74e4..3bbe4872 100644 --- a/src/cpp-utils/random/RandomGeneratorThread.cpp +++ b/src/cpp-utils/random/RandomGeneratorThread.cpp @@ -1,3 +1,4 @@ +#include "cpp-utils/crypto/cryptopp_byte.h" #include "RandomGeneratorThread.h" namespace cpputils { @@ -26,7 +27,7 @@ namespace cpputils { Data RandomGeneratorThread::_generateRandomData(size_t size) { Data newRandom(size); - _randomGenerator.GenerateBlock(static_cast(newRandom.data()), size); + _randomGenerator.GenerateBlock(static_cast(newRandom.data()), size); return newRandom; } diff --git a/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp b/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp index 9edf1488..70a8823a 100644 --- a/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp +++ b/test/blockstore/implementations/encrypted/EncryptedBlockStoreTest_Specific.cpp @@ -1,3 +1,4 @@ +#include "cpp-utils/crypto/cryptopp_byte.h" #include #include "../../../cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h" #include "blockstore/implementations/encrypted/EncryptedBlockStore.h" @@ -45,7 +46,7 @@ public: void ModifyBaseBlock(const blockstore::Key &key) { auto block = baseBlockStore->load(key).value(); - uint8_t middle_byte = ((byte*)block->data())[10]; + uint8_t middle_byte = ((CryptoPP::byte*)block->data())[10]; uint8_t new_middle_byte = middle_byte + 1; block->write(&new_middle_byte, 10, 1); } diff --git a/test/cpp-utils/crypto/symmetric/CipherTest.cpp b/test/cpp-utils/crypto/symmetric/CipherTest.cpp index 558e607f..d285b13f 100644 --- a/test/cpp-utils/crypto/symmetric/CipherTest.cpp +++ b/test/cpp-utils/crypto/symmetric/CipherTest.cpp @@ -1,3 +1,4 @@ +#include "cpp-utils/crypto/cryptopp_byte.h" #include #include "cpp-utils/crypto/symmetric/Cipher.h" #include "cpp-utils/crypto/symmetric/ciphers.h" @@ -39,16 +40,16 @@ public: } void ExpectDoesntDecrypt(const Data &ciphertext) { - auto decrypted = Cipher::decrypt((byte*)ciphertext.data(), ciphertext.size(), this->encKey); + auto decrypted = Cipher::decrypt((CryptoPP::byte*)ciphertext.data(), ciphertext.size(), this->encKey); EXPECT_FALSE(decrypted); } Data Encrypt(const Data &plaintext) { - return Cipher::encrypt((byte*)plaintext.data(), plaintext.size(), this->encKey); + return Cipher::encrypt((CryptoPP::byte*)plaintext.data(), plaintext.size(), this->encKey); } Data Decrypt(const Data &ciphertext) { - return Cipher::decrypt((byte*)ciphertext.data(), ciphertext.size(), this->encKey).value(); + return Cipher::decrypt((CryptoPP::byte*)ciphertext.data(), ciphertext.size(), this->encKey).value(); } static Data CreateZeroes(unsigned int size) { @@ -148,49 +149,49 @@ TYPED_TEST_CASE_P(AuthenticatedCipherTest); TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Zeroes_Size1) { Data ciphertext = this->Encrypt(this->zeroes1); - *(byte*)ciphertext.data() = *(byte*)ciphertext.data() + 1; + *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Data_Size1) { Data ciphertext = this->Encrypt(this->plaintext1); - *(byte*)ciphertext.data() = *(byte*)ciphertext.data() + 1; + *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - *(byte*)ciphertext.data() = *(byte*)ciphertext.data() + 1; + *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyFirstByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - *(byte*)ciphertext.data() = *(byte*)ciphertext.data() + 1; + *(CryptoPP::byte*)ciphertext.data() = *(CryptoPP::byte*)ciphertext.data() + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyLastByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - ((byte*)ciphertext.data())[ciphertext.size() - 1] = ((byte*)ciphertext.data())[ciphertext.size() - 1] + 1; + ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyLastByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - ((byte*)ciphertext.data())[ciphertext.size() - 1] = ((byte*)ciphertext.data())[ciphertext.size() - 1] + 1; + ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size() - 1] + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyMiddleByte_Zeroes) { Data ciphertext = this->Encrypt(this->zeroes2); - ((byte*)ciphertext.data())[ciphertext.size()/2] = ((byte*)ciphertext.data())[ciphertext.size()/2] + 1; + ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] + 1; this->ExpectDoesntDecrypt(ciphertext); } TYPED_TEST_P(AuthenticatedCipherTest, ModifyMiddleByte_Data) { Data ciphertext = this->Encrypt(this->plaintext2); - ((byte*)ciphertext.data())[ciphertext.size()/2] = ((byte*)ciphertext.data())[ciphertext.size()/2] + 1; + ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] = ((CryptoPP::byte*)ciphertext.data())[ciphertext.size()/2] + 1; this->ExpectDoesntDecrypt(ciphertext); } diff --git a/test/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h b/test/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h index cb8bf34a..7ec77e27 100644 --- a/test/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h +++ b/test/cpp-utils/crypto/symmetric/testutils/FakeAuthenticatedCipher.h @@ -2,6 +2,7 @@ #ifndef MESSMER_CPPUTILS_TEST_CRYPTO_SYMMETRIC_TESTUTILS_FAKEAUTHENTICATEDCIPHER_H_ #define MESSMER_CPPUTILS_TEST_CRYPTO_SYMMETRIC_TESTUTILS_FAKEAUTHENTICATEDCIPHER_H_ +#include "cpp-utils/crypto/cryptopp_byte.h" #include "cpp-utils/crypto/symmetric/Cipher.h" #include "cpp-utils/data/FixedSizeData.h" #include "cpp-utils/data/Data.h" @@ -47,7 +48,7 @@ namespace cpputils { return ciphertextBlockSize - 5; } - static Data encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { + static Data encrypt(const CryptoPP::byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { Data result(ciphertextSize(plaintextSize)); //Add a random IV @@ -55,16 +56,16 @@ namespace cpputils { std::memcpy(result.data(), &iv, 1); //Use caesar chiffre on plaintext - _caesar((byte *) result.data() + 1, plaintext, plaintextSize, encKey.value + iv); + _caesar((CryptoPP::byte *) result.data() + 1, plaintext, plaintextSize, encKey.value + iv); //Add parity information - int32_t parity = _parity((byte *) result.data(), plaintextSize + 1); - std::memcpy((byte *) result.data() + plaintextSize + 1, &parity, 4); + int32_t parity = _parity((CryptoPP::byte *) result.data(), plaintextSize + 1); + std::memcpy((CryptoPP::byte *) result.data() + plaintextSize + 1, &parity, 4); return result; } - static boost::optional decrypt(const byte *ciphertext, unsigned int ciphertextSize, + static boost::optional decrypt(const CryptoPP::byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) { //We need at least 5 bytes (iv + parity) if (ciphertextSize < 5) { @@ -81,14 +82,14 @@ namespace cpputils { //Decrypt caesar chiffre from ciphertext int32_t iv = *(int32_t *) ciphertext; Data result(plaintextSize(ciphertextSize)); - _caesar((byte *) result.data(), ciphertext + 1, plaintextSize(ciphertextSize), -(encKey.value + iv)); + _caesar((CryptoPP::byte *) result.data(), ciphertext + 1, plaintextSize(ciphertextSize), -(encKey.value + iv)); return std::move(result); } static constexpr const char *NAME = "FakeAuthenticatedCipher"; private: - static int32_t _parity(const byte *data, unsigned int size) { + static int32_t _parity(const CryptoPP::byte *data, unsigned int size) { int32_t parity = 34343435; // some init value const int32_t *intData = reinterpret_cast(data); unsigned int intSize = size / sizeof(int32_t); @@ -102,7 +103,7 @@ namespace cpputils { return parity; } - static void _caesar(byte *dst, const byte *src, unsigned int size, uint8_t key) { + static void _caesar(CryptoPP::byte *dst, const CryptoPP::byte *src, unsigned int size, uint8_t key) { for (unsigned int i = 0; i < size; ++i) { dst[i] = src[i] + key; } diff --git a/test/cryfs/config/CompatibilityTest.cpp b/test/cryfs/config/CompatibilityTest.cpp index ebaeaed6..b23f24de 100644 --- a/test/cryfs/config/CompatibilityTest.cpp +++ b/test/cryfs/config/CompatibilityTest.cpp @@ -1,3 +1,4 @@ +#include "cpp-utils/crypto/cryptopp_byte.h" #include #include #include @@ -38,7 +39,7 @@ private: Data result(hex.size()/2); CryptoPP::StringSource(hex, true, new CryptoPP::HexDecoder( - new CryptoPP::ArraySink((byte*)result.data(), result.size()) + new CryptoPP::ArraySink((CryptoPP::byte*)result.data(), result.size()) ) ); return result;