cryptopp icon indicating copy to clipboard operation
cryptopp copied to clipboard

Comparison of uninitialized SecByteBlock raises an assertion

Open hood-ok opened this issue 4 years ago • 2 comments

When the SecByteBlock is created but the memory is never initialized, the comparison works correctly but the assertion is raised

OS: Windows 10 x64 Home 21H2 Version: Crypto++ 8.6.0 Build: Visual Studio 2015 Configuration: Debug x64 Runtime library: Multi-threaded Debug (/MTd) Test application: Simply added the Win32 console project to the crypttest solution and add the cryptlib.lib library as a dependency. Then build and run.

Test case:

#include <iostream>

#include "secblock.h"

int main()
{
	CryptoPP::SecByteBlock b1, b2;
	std::cout << "EQ: " << ( b1 == b2 ) << std::endl;
	return 0;
}

Output:

Assertion failed: misc.cpp(220): CryptoPP::VerifyBufsEqual
Assertion failed: misc.cpp(221): CryptoPP::VerifyBufsEqual
EQ: 1
Press any key to continue . . .

hood-ok avatar Aug 05 '22 13:08 hood-ok

This is expected behavior. If you want initialized memory, then use one of the SecByteBlock overloads. See https://www.cryptopp.com/wiki/SecBlock .

noloader avatar Aug 05 '22 13:08 noloader

If you want initialized memory, then use one of the SecByteBlock overloads

I don't need any memory. I don't see assertions for the count parameter in VerifyBufsEqual but there is the requirement for memory to be allocated that is weird.

So, if my code allows the default-constructed SecByteBlock (or with New(0) function call), I need to use the following wrapper instead of a simple comparison operator.

equal( const CryptoPP::SecByteBlock & _left, const CryptoPP::SecByteBlock & _right )
{
  if ( _left.empty() && _right.empty() )
    return true;

  if ( _left.empty() || _right.empty() )
    return false;

  return _left == _right;
}

If this is expected behavior, it would be nice to be well-documented in the wiki, maybe with the code sample.

Also, Default comparisons (since C++20) is broken/unsupported from the box in current approach in case of I want to use SecByteBlock as struct member with == operator

hood-ok avatar Aug 05 '22 14:08 hood-ok