Return (move) CBC_Mode<AES>::Encryption object from a function

Skip to first unread message

Anton Schmidt

Aug 31, 2022, 11:24:47 AM8/31/22
to Crypto++ Users
I wanted to write a factory method to initialize CBC_Mode<AES>::Encryption object like that

> CBC_Mode<AES>::Encryption GetEncryption(const SecByteBlock &key) {
>   CBC_Mode<AES>::Encryption encryption;
>   auto iv = GetIV();  // this is constant IV for entire application
>   encryption.SetKeyWithIV(, key.size(),, iv.size());
>   return encryption;
> }

But when I use returned object in StreamTransformationFilter I get an address access violation in CryptoPP code
> CryptoPP::BlockOrientedCipherModeBase::IsForwardTransformation() modes.h:259

Is it a CryptoPP bug (move-ctors are not implemented correctly) or Encryption objects cannot be moved for some security reasons? Or that is kind of impossible to implement move-ctor for a template class?

Seems that I should rather implement
> template <class T>
> vector<byte> Encrypt(const T& text, const SecByteBlock& key) { ... }

but still curious about that 'move' issue.

CryptoPP library version 8.7.0
Test code is here

This is a stack trace when address access violation happened
> CryptoPP::BlockOrientedCipherModeBase::IsForwardTransformation() modes.h:259
> CryptoPP::StreamTransformationFilter::LastBlockSize(CryptoPP::StreamTransformation &, BlockPaddingScheme) filters.cpp:630
> CryptoPP::StreamTransformationFilter::InitializeDerivedAndReturnNewSizes(const CryptoPP::NameValuePairs &, unsigned int &, unsigned int &, unsigned int &) filters.cpp:658
> CryptoPP::FilterWithBufferedInput::IsolatedInitialize(const CryptoPP::NameValuePairs &) filters.cpp:332
> CryptoPP::StreamTransformationFilter::StreamTransformationFilter(CryptoPP::StreamTransformation &, CryptoPP::BufferedTransformation *, BlockPaddingScheme) filters.cpp:598
> Test2() main.cpp:67
> main() main.cpp:81
> invoke_main() 0x00000000007b3873
> __scrt_common_main_seh() 0x00000000007b36f7
> __scrt_common_main() 0x00000000007b358d
> mainCRTStartup(void *) 0x00000000007b38f8
> <unknown> 0x00000000774cfa29
> <unknown> 0x0000000077817a9e
> <unknown> 0x0000000077817a6e
Reply all
Reply to author
0 new messages