botan icon indicating copy to clipboard operation
botan copied to clipboard

Botan::AutoSeeded_RNG is not swappable

Open pfeatherstone opened this issue 2 years ago • 7 comments

The following doesn't compile:

using std::swap;
Botan::AutoSeeded_RNG a,b;
swap(a,b);

Please could we make most objects swappable.

pfeatherstone avatar Jul 13 '21 15:07 pfeatherstone

This could be implemented, but why would you need to do this?

randombit avatar Jul 13 '21 21:07 randombit

Because std::swap is great and it's nice when types have well defined swap functions as well as copy semantics and move semantics. Otherwise you need to use hacks like wrapping types in std::unique_ptr or std::shared_ptr which have all of the above.

pfeatherstone avatar Jul 13 '21 22:07 pfeatherstone

I can understand that as a general rule for types. But my point is more, in this specific instance - one AutoSeeded_RNG is literally (cryptographically) indistinguishable from any other AutoSeeded_RNG so not swapping should have identical behavior as swapping, because you should not be able to detect any difference between a and b.

randombit avatar Jul 13 '21 23:07 randombit

So AutoSeeded_RNG always produces the same sequence ?

pfeatherstone avatar Jul 14 '21 06:07 pfeatherstone

No, it always produces a cryptographically random sequence of bits.

If you could swap a and b and detect via any computational means any difference in their output distributions, then you would have broken HMAC-DRBG.

randombit avatar Jul 14 '21 13:07 randombit

Hmm. Not sure what the type system rules should be in this case for AutoSeeded_RNG. I guess it doesn't make sense to make it moveable either. Currently it's moveable. But if you're willing to make it moveable, then it makes sense to also make it swappable.

pfeatherstone avatar Jul 14 '21 13:07 pfeatherstone

You can either think of a swap as being 2 moves or a move as being a swap (with some default constructed empty state to reset the moved from object to something sensible). So move and swap go hand in hand in my mind.

pfeatherstone avatar Jul 14 '21 13:07 pfeatherstone